Rep-string: Difference between revisions
Content added Content deleted
m (Minor improvement to code.) |
(New post in addition to an existing post which uses labels and 'goto' which many people consider inappropriate in modern programming.) |
||
Line 2,119: | Line 2,119: | ||
1 : none |
1 : none |
||
0100101 : none</pre> |
0100101 : none</pre> |
||
Alternative version avoiding the use of 'goto' |
|||
<syntaxhighlight lang="java"> |
|||
import java.util.ArrayList; |
|||
import java.util.List; |
|||
public final class RepStrings { |
|||
public static void main(String[] aArgs) { |
|||
List<String> tests = List.of( "1001110011", "1110111011", "0010010010", |
|||
"1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1" ); |
|||
System.out.println("The longest rep-strings are:"); |
|||
for ( String test : tests ) { |
|||
List<String> repeats = repString(test); |
|||
String result = repeats.isEmpty() ? "Not a rep-string" : repeats.get(repeats.size() - 1); |
|||
System.out.println(String.format("%10s%s%s", test, " -> ", result)); |
|||
} |
|||
} |
|||
private static List<String> repString(String aText) { |
|||
List<String> repetitions = new ArrayList<String>(); |
|||
for ( int length = 1; length <= aText.length() / 2; length++ ) { |
|||
String possible = aText.substring(0, length); |
|||
int quotient = aText.length() / length; |
|||
int remainder = aText.length() % length; |
|||
String candidate = possible.repeat(quotient) + possible.substring(0, remainder); |
|||
if ( candidate.equals(aText) ) { |
|||
repetitions.add(possible); |
|||
} |
|||
} |
|||
return repetitions; |
|||
} |
|||
} |
|||
</syntaxhighlight> |
|||
{{ out }} |
|||
<pre> |
|||
The longest rep-strings are: |
|||
1001110011 -> 10011 |
|||
1110111011 -> 1110 |
|||
0010010010 -> 001 |
|||
1010101010 -> 1010 |
|||
1111111111 -> 11111 |
|||
0100101101 -> Not a rep-string |
|||
0100100 -> 010 |
|||
101 -> Not a rep-string |
|||
11 -> 1 |
|||
00 -> 0 |
|||
1 -> Not a rep-string |
|||
</pre> |
|||
=={{header|JavaScript}}== |
=={{header|JavaScript}}== |