Rep-string: Difference between revisions

4,175 bytes added ,  14 days ago
Add ed example
(Add SETL)
(Add ed example)
 
(7 intermediate revisions by 5 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 1,365 ⟶ 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 1,410 ⟶ 1,543:
"1" too-short-no-rep
</syntaxhighlight>
 
=={{header|Ed}}==
 
This is not a comprehensive solution, as it only handles sequences repeated twice. But it's good enough for a tool not intended for general-purpose programming.
 
<syntaxhighlight>
H
g/([01]*)\1(.*)/s//\1/
,p
Q
</syntaxhighlight>
 
{{out}}
<pre>
10011
1110
001
1010
11111
010
010
 
1
0
 
</pre>
 
=={{header|Elixir}}==
Line 3,847 ⟶ 4,006:
}</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 4,577 ⟶ 4,783:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<syntaxhighlight lang="ecmascriptwren">import "./fmt" for Fmt
 
var repString = Fn.new { |s|
90

edits