Rep-string: Difference between revisions

9,290 bytes added ,  2 months ago
m
→‎{{header|ABC}}: Added main program
m (syntax highlighting fixup automation)
m (→‎{{header|ABC}}: Added main program)
 
(15 intermediate revisions by 8 users not shown)
Line 68:
'1' has reps []
</pre>
 
=={{header|ABC}}==
<syntaxhighlight lang="abc">HOW TO RETURN repstrings str:
PUT {} IN reps
FOR len IN {0..floor(#str/2-1)}:
PUT str|floor(#str/2-len) IN rep
PUT rep IN rpt
WHILE #rpt < #str: PUT rpt^rep IN rpt
IF rpt|#str = str: INSERT rep IN reps
RETURN reps
 
PUT {} IN tests
PUT "1001110011" IN tests[1]
PUT "1110111011" IN tests[2]
PUT "0010010010" IN tests[3]
PUT "1010101010" IN tests[4]
PUT "1111111111" IN tests[5]
PUT "0100101101" IN tests[6]
PUT "0100100" IN tests[7]
PUT "101" IN tests[8]
PUT "11" IN tests[9]
PUT "00" IN tests[10]
PUT "1" IN tests[11]
 
FOR t IN tests:
WRITE t, repstrings t /
</syntaxhighlight>
{{out}}
<pre>1001110011: {"10011"}
1110111011: {"1110"}
0010010010: {"001"}
1010101010: {"10"; "1010"}
1111111111: {"1"; "11"; "111"; "1111"; "11111"}
0100101101: {}
0100100: {"010"}
101: {}
11: {"1"}
00: {"0"}
1: {}</pre>
 
=={{header|Action!}}==
Line 913 ⟶ 952:
}
</syntaxhighlight>
 
=={{header|C#}}==
{{trans|Java}}
<syntaxhighlight lang="C#">
using System;
 
public class RepString
{
static readonly string[] input = {"1001110011", "1110111011", "0010010010",
"1010101010", "1111111111", "0100101101", "0100100", "101", "11",
"00", "1", "0100101"};
 
public static void Main(string[] args)
{
foreach (string s in input)
Console.WriteLine($"{s} : {repString(s)}");
}
 
static string repString(string s)
{
int len = s.Length;
for (int part = len / 2; part > 0; part--)
{
int tail = len % part;
if (tail > 0 && !s.Substring(0, tail).Equals(s.Substring(len - tail)))
continue;
bool isRepeated = true;
for (int j = 0; j < len / part - 1; j++)
{
int a = j * part;
int b = (j + 1) * part;
int c = (j + 2) * part;
if (!s.Substring(a, part).Equals(s.Substring(b, part)))
{
isRepeated = false;
break;
}
}
if (isRepeated)
return s.Substring(0, part);
}
return "none";
}
}
</syntaxhighlight>
{{out}}
<pre>
1001110011 : 10011
1110111011 : 1110
0010010010 : 001
1010101010 : 1010
1111111111 : 11111
0100101101 : none
0100100 : 010
101 : none
11 : 1
00 : 0
1 : none
0100101 : none
 
</pre>
 
=={{header|C++}}==
Line 971 ⟶ 1,071:
0
1 is no rep string!</pre>
 
===Without external libraries===
<syntaxhighlight lang="c++">
#include <cstdint>
#include <iomanip>
#include <iostream>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>
 
std::string repeat(const std::string& text, const int32_t& repetitions) {
std::stringstream stream;
std::fill_n(std::ostream_iterator<std::string>(stream), repetitions, text);
return stream.str();
}
 
std::vector<std::string> rep_string(const std::string& text) {
std::vector<std::string> repetitions;
 
for ( uint64_t len = 1; len <= text.length() / 2; ++len ) {
std::string possible = text.substr(0, len);
uint64_t quotient = text.length() / len;
uint64_t remainder = text.length() % len;
std::string candidate = repeat(possible, quotient) + possible.substr(0, remainder);
if ( candidate == text ) {
repetitions.emplace_back(possible);
}
}
return repetitions;
}
 
int main() {
const std::vector<std::string> tests = { "1001110011", "1110111011", "0010010010",
"1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1" };
 
std::cout << "The longest rep-strings are:" << std::endl;
for ( const std::string& test : tests ) {
std::vector<std::string> repeats = rep_string(test);
std::string result = repeats.empty() ? "Not a rep-string" : repeats.back();
std::cout << std::setw(10) << test << " -> " << result << std::endl;
}
}
</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|Clojure}}==
Line 977 ⟶ 1,136:
first-half (subs s 0 (/ len 2))
test-group (take-while seq (iterate butlast first-half))
test-reptd (map (comp #(take len (cycle %) cycle) test-group)]
(some #(= (seq s) %) test-reptd)))</syntaxhighlight>
{{out}}
Line 1,306 ⟶ 1,465:
00 1 rep-string 0
1 not a rep-string</pre>
 
=={{header|EasyLang}}==
{{trans|C}}
<syntaxhighlight>
func$ repstr s$ .
sl = len s$ div 2 + 1
while sl > 1
r$ = substr s$ sl 999
if r$ = substr s$ 1 len r$
return substr r$ 1 (sl - 1)
.
sl -= 1
.
return ""
.
repeat
s$ = input
until s$ = ""
print s$ & " -> " & repstr s$
.
input_data
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
11
00
1
</syntaxhighlight>
 
=={{header|EchoLisp}}==
Line 2,060 ⟶ 2,252:
1 : none
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}}==
Line 2,490 ⟶ 2,734:
It outputs all the possibilities for a rep-string,
if there is no rep-string it will show an empty list {}.
 
=={{header|Miranda}}==
<syntaxhighlight lang="miranda">main :: [sys_message]
main = [Stdout (lay (map test tests))]
 
test :: [char]->[char]
test s = s ++ ": " ++ show (repstrings s)
 
tests :: [[char]]
tests = ["1001110011", "1110111011", "0010010010", "1010101010",
"1111111111", "0100101101", "0100100", "101", "11", "00",
"1"]
 
repstrings :: [*]->[[*]]
repstrings s = filter matching bases
where bases = [take n s | n<-[1..#s div 2]]
matching r = s = take (#s) (concat (repeat r))</syntaxhighlight>
{{out}}
<pre>1001110011: ["10011"]
1110111011: ["1110"]
0010010010: ["001"]
1010101010: ["10","1010"]
1111111111: ["1","11","111","1111","11111"]
0100101101: []
0100100: ["010"]
101: []
11: ["1"]
00: ["0"]
1: []</pre>
 
=={{header|Modula-2}}==
Line 3,707 ⟶ 3,980:
}</syntaxhighlight>
 
=={{header|Refal}}==
<syntaxhighlight lang="refal">$ENTRY Go {
, ('1001110011') ('1110111011') ('0010010010')
('1010101010') ('1111111111') ('0100101101')
('0100100') ('101') ('11') ('00') ('1'): e.Tests
= <Each Show e.Tests>;
};
 
Each {
s.F = ;
s.F t.I e.R = <Mu s.F t.I> <Each s.F e.R>;
};
 
Show {
(e.S), <RepString e.S>: e.R = <Prout e.S ' => ' e.R>;
};
 
RepString {
() e.S = ;
(e.R) e.S, <Lengthen (e.R) e.S>: e.S e.X = e.R;
(e.R s.C) e.S = <RepString (e.R) e.S>;
e.S, <Lenw e.S>: s.L e.S,
<First <Div s.L 2> e.S>: (e.F) e.X
= <RepString (e.F) e.S>;
};
 
Lengthen {
(e.A) e.B, <Lenw e.A>: s.LA e.A,
<Lenw e.B>: s.LB e.B,
<Compare s.LA s.LB>: '-'
= <Lengthen (e.A e.A) e.B>;
(e.A) e.B, <Lenw e.B>: s.LB e.B,
<First s.LB e.A>: (e.FA) e.RA
= e.FA;
};</syntaxhighlight>
{{out}}
<pre>1001110011 => 10011
1110111011 => 1110
0010010010 => 001
1010101010 => 1010
1111111111 => 11111
0100101101 =>
0100100 => 010
101 =>
11 => 1
00 => 0
1 =></pre>
=={{header|REXX}}==
===version 1===
Line 3,859 ⟶ 4,179:
00 -> 0
1 -> (none)
</pre>
 
=={{header|RPL}}==
≪ DUP SIZE → in lin
≪ ""
'''IF''' lin 1 > '''THEN'''
lin 2 / FLOOR 1 '''FOR''' lrep
in 1 lrep SUB DUP
'''DO''' OVER + '''UNTIL''' DUP SIZE lin ≥ '''END'''
'''IF''' 1 lin SUB in == '''THEN''' SWAP 0 ‘lrep’ STO '''END'''
DROP
-1 '''STEP'''
'''END'''
≫ ≫ '<span style="color:blue">REPSTR</span>' STO
≪ { "1001110011" "1110111011" "0010010010" "1010101010" "1111111111" "0100101101" "0100100" "101" "11" "00" "1" }
{ } 1 3 PICK SIZE '''FOR''' j
OVER j GET <span style="color:blue">REPSTR</span> +
'''NEXT''' SWAP DROP
≫ 'TASK' STO
{{out}}
<pre>
1: { "10011" "1110" "001" "1010" "11111" "" "010" "" "1" "0" "" }
</pre>
 
Line 4,082 ⟶ 4,425:
No rep-string for "1"
</pre>
 
=={{header|SETL}}==
<syntaxhighlight lang="setl">program repstring;
tests := [
"1001110011", "1110111011", "0010010010", "1010101010",
"1111111111", "0100101101", "0100100", "101", "11", "00", "1"
];
 
loop for test in tests do
print(test + ": " + str repstrings(test));
end loop;
 
proc repstrings(s);
return {
s(..l) : l in [1..#s div 2]
| (s(..l)*(#s div l+1))(..#s) = s
};
end proc;
end program;</syntaxhighlight>
{{out}}
<pre>1001110011: {'10011'}
1110111011: {'1110'}
0010010010: {'001'}
1010101010: {'10' '1010'}
1111111111: {'1' '11' '111' '1111' '11111'}
0100101101: {}
0100100: {'010'}
101: {}
11: {'1'}
00: {'0'}
1: {}</pre>
 
=={{header|Sidef}}==
Line 4,330 ⟶ 4,704:
</pre>
 
=={{header|V (Vlang)}}==
{{trans|Go}}
<syntaxhighlight lang="v (vlang)">fn rep(s string) int {
for x := s.len / 2; x > 0; x-- {
if s.starts_with(s[x..]) {
Line 4,383 ⟶ 4,757:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var repString = Fn.new { |s|
3,043

edits