Rep-string: Difference between revisions

Content added Content deleted
No edit summary
m (syntax highlighting fixup automation)
Line 35: Line 35:
{{trans|Python: Functional}}
{{trans|Python: Functional}}


<lang 11l>F reps(text)
<syntaxhighlight lang="11l">F reps(text)
R (1 .< 1 + text.len I/ 2).filter(x -> @text.starts_with(@text[x..])).map(x -> @text[0 .< x])
R (1 .< 1 + text.len I/ 2).filter(x -> @text.starts_with(@text[x..])).map(x -> @text[0 .< x])


Line 52: Line 52:


L(line) matchstr.split("\n")
L(line) matchstr.split("\n")
print(‘'#.' has reps #.’.format(line, reps(line)))</lang>
print(‘'#.' has reps #.’.format(line, reps(line)))</syntaxhighlight>


{{out}}
{{out}}
Line 70: Line 70:


=={{header|Action!}}==
=={{header|Action!}}==
<lang Action!>BYTE FUNC IsCycle(CHAR ARRAY s,sub)
<syntaxhighlight lang="action!">BYTE FUNC IsCycle(CHAR ARRAY s,sub)
BYTE i,j,count
BYTE i,j,count


Line 128: Line 128:
Test("00")
Test("00")
Test("1")
Test("1")
RETURN</lang>
RETURN</syntaxhighlight>
{{out}}
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Rep-string.png Screenshot from Atari 8-bit computer]
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Rep-string.png Screenshot from Atari 8-bit computer]
Line 147: Line 147:
=={{header|Ada}}==
=={{header|Ada}}==


<lang Ada>with Ada.Command_Line, Ada.Text_IO, Ada.Strings.Fixed;
<syntaxhighlight lang="ada">with Ada.Command_Line, Ada.Text_IO, Ada.Strings.Fixed;


procedure Rep_String is
procedure Rep_String is
Line 177: Line 177:
Ada.Text_IO.Put_Line("Longest rep-string for """& X &""": """& Y &"""");
Ada.Text_IO.Put_Line("Longest rep-string for """& X &""": """& Y &"""");
end if;
end if;
end Rep_String;</lang>
end Rep_String;</syntaxhighlight>


{{out}}
{{out}}
Line 206: Line 206:
=={{header|ALGOL 68}}==
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<lang algol68># procedure to find the longest rep-string in a given string #
<syntaxhighlight lang="algol68"># procedure to find the longest rep-string in a given string #
# the input string is not validated to contain only "0" and "1" characters #
# the input string is not validated to contain only "0" and "1" characters #
PROC longest rep string = ( STRING input )STRING:
PROC longest rep string = ( STRING input )STRING:
Line 273: Line 273:
)
)
OD
OD
)</lang>
)</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: longest rep string: "10011"
<pre>1001110011: longest rep string: "10011"
Line 291: Line 291:
This function returns a list of all possible repeated substrings.
This function returns a list of all possible repeated substrings.
It returns the empty list if there are none.
It returns the empty list if there are none.
<lang APL>rep ← ⊢ (⊢(/⍨)(⊂⊣)≡¨(≢⊣)⍴¨⊢) ⍳∘(⌊0.5×≢)↑¨⊂</lang>
<syntaxhighlight lang="apl">rep ← ⊢ (⊢(/⍨)(⊂⊣)≡¨(≢⊣)⍴¨⊢) ⍳∘(⌊0.5×≢)↑¨⊂</syntaxhighlight>
{{out}}
{{out}}
<pre> rep '1001110011'
<pre> rep '1001110011'
Line 319: Line 319:


=={{header|AppleScript}}==
=={{header|AppleScript}}==
<lang AppleScript>------------------------REP-CYCLES-------------------------
<syntaxhighlight lang="applescript">------------------------REP-CYCLES-------------------------


-- repCycles :: String -> [String]
-- repCycles :: String -> [String]
Line 540: Line 540:
on unlines(xs)
on unlines(xs)
|λ|(xs) of intercalate(linefeed)
|λ|(xs) of intercalate(linefeed)
end unlines</lang>
end unlines</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Longest cycle:
<pre>Longest cycle:
Line 558: Line 558:
=={{header|Arturo}}==
=={{header|Arturo}}==


<lang rebol>repeated?: function [text][
<syntaxhighlight lang="rebol">repeated?: function [text][
loop ((size text)/2)..0 'x [
loop ((size text)/2)..0 'x [
if prefix? text slice text x (size text)-1 [
if prefix? text slice text x (size text)-1 [
Line 588: Line 588:
else ->
else ->
print [str "->" rep "( length:" size rep ")"]
print [str "->" rep "( length:" size rep ")"]
]</lang>
]</syntaxhighlight>


{{out}}
{{out}}
Line 606: Line 606:
=={{header|AutoHotkey}}==
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
{{works with|AutoHotkey 1.1}}
<lang AutoHotkey>In := ["1001110011", "1110111011", "0010010010", "1010101010"
<syntaxhighlight lang="autohotkey">In := ["1001110011", "1110111011", "0010010010", "1010101010"
, "1111111111", "0100101101", "0100100", "101", "11", "00", "1"]
, "1111111111", "0100101101", "0100100", "101", "11", "00", "1"]
for k, v in In
for k, v in In
Line 624: Line 624:
}
}
return "N/A"
return "N/A"
}</lang>
}</syntaxhighlight>
{{Out}}
{{Out}}
<pre>10011 1001110011
<pre>10011 1001110011
Line 639: Line 639:


=={{header|BaCon}}==
=={{header|BaCon}}==
<lang freebasic>all$ = "1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1"
<syntaxhighlight lang="freebasic">all$ = "1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1"


FOR word$ IN all$
FOR word$ IN all$
Line 655: Line 655:


PRINT "Not a repeating string: ", word$
PRINT "Not a repeating string: ", word$
NEXT</lang>
NEXT</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 672: Line 672:


=={{header|BASIC}}==
=={{header|BASIC}}==
<lang BASIC>10 DEFINT I: DEFSTR S,T
<syntaxhighlight lang="basic">10 DEFINT I: DEFSTR S,T
20 READ S: IF S="" THEN END
20 READ S: IF S="" THEN END
30 IF LEN(S)<2 THEN 80 ELSE FOR I = LEN(S)\2 TO 1 STEP -1
30 IF LEN(S)<2 THEN 80 ELSE FOR I = LEN(S)\2 TO 1 STEP -1
Line 686: Line 686:
130 DATA "0100100","101"
130 DATA "0100100","101"
140 DATA "11","00"
140 DATA "11","00"
150 DATA "1",""</lang>
150 DATA "1",""</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: 10011
<pre>1001110011: 10011
Line 701: Line 701:


=={{header|BCPL}}==
=={{header|BCPL}}==
<lang bcpl>get "libhdr"
<syntaxhighlight lang="bcpl">get "libhdr"


// Returns the length of the longest rep-string
// Returns the length of the longest rep-string
Line 750: Line 750:
rep("00")
rep("00")
rep("1")
rep("1")
$)</lang>
$)</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: 10011
<pre>1001110011: 10011
Line 765: Line 765:


=={{header|Bracmat}}==
=={{header|Bracmat}}==
<lang bracmat>( ( rep-string
<syntaxhighlight lang="bracmat">( ( rep-string
= reps L x y
= reps L x y
. ( reps
. ( reps
Line 800: Line 800:
& rep-string$00
& rep-string$00
& rep-string$1
& rep-string$1
);</lang>
);</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1001110011 : 10011
<pre>1001110011 : 10011
Line 815: Line 815:


=={{header|BQN}}==
=={{header|BQN}}==
<lang bqn># Returns a list of all rep-strings
<syntaxhighlight lang="bqn"># Returns a list of all rep-strings
Reps←(⌊≠÷2˙)((⊣≥≠¨∘⊢)/⊢)(<≡¨≠⥊¨1↓↑)/1↓↑
Reps←(⌊≠÷2˙)((⊣≥≠¨∘⊢)/⊢)(<≡¨≠⥊¨1↓↑)/1↓↑


Line 825: Line 825:


∾´{ 𝕩∾':'∾(•Fmt Reps 𝕩)∾@+10 }¨tests</lang>
∾´{ 𝕩∾':'∾(•Fmt Reps 𝕩)∾@+10 }¨tests</syntaxhighlight>
{{out}}
{{out}}
<pre>"1001110011:⟨ ""10011"" ⟩
<pre>"1001110011:⟨ ""10011"" ⟩
Line 843: Line 843:
===Longest substring ===
===Longest substring ===


<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <stdio.h>
#include <string.h>
#include <string.h>
Line 878: Line 878:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 893: Line 893:
</pre>
</pre>
===shortest substring===
===shortest substring===
<syntaxhighlight lang="c">
<lang c>
// strstr : Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
// strstr : Returns a pointer to the first occurrence of str2 in str1, or a null pointer if str2 is not part of str1.
// size_t is an unsigned integer typ
// size_t is an unsigned integer typ
Line 912: Line 912:
return 0;
return 0;
}
}
</syntaxhighlight>
</lang>


=={{header|C++}}==
=={{header|C++}}==
<lang cpp>#include <string>
<syntaxhighlight lang="cpp">#include <string>
#include <vector>
#include <vector>
#include <boost/regex.hpp>
#include <boost/regex.hpp>
Line 950: Line 950:
}
}
return 0 ;
return 0 ;
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011 is a rep string! Here is a repeating string:
<pre>1001110011 is a rep string! Here is a repeating string:
Line 973: Line 973:


=={{header|Clojure}}==
=={{header|Clojure}}==
<lang lisp>(defn rep-string [s]
<syntaxhighlight lang="lisp">(defn rep-string [s]
(let [len (count s)
(let [len (count s)
first-half (subs s 0 (/ len 2))
first-half (subs s 0 (/ len 2))
test-group (take-while seq (iterate butlast first-half))
test-group (take-while seq (iterate butlast first-half))
test-reptd (map (comp #(take len %) cycle) test-group)]
test-reptd (map (comp #(take len %) cycle) test-group)]
(some #(= (seq s) %) test-reptd)))</lang>
(some #(= (seq s) %) test-reptd)))</syntaxhighlight>
{{out}}
{{out}}
<lang lisp>
<syntaxhighlight lang="lisp">
(def test-strings ["1001110011"
(def test-strings ["1001110011"
"1110111011"
"1110111011"
Line 994: Line 994:


(map (juxt identity rep-string) test-strings)
(map (juxt identity rep-string) test-strings)
</syntaxhighlight>
</lang>
<pre>
<pre>
(["1001110011" true]
(["1001110011" true]
Line 1,010: Line 1,010:


=={{header|CLU}}==
=={{header|CLU}}==
<lang clu>rep_strings = iter (s: string) yields (string)
<syntaxhighlight lang="clu">rep_strings = iter (s: string) yields (string)
for len: int in int$from_to_by(string$size(s)/2, 1, -1) do
for len: int in int$from_to_by(string$size(s)/2, 1, -1) do
repstr: string := string$substr(s, 1, len)
repstr: string := string$substr(s, 1, len)
Line 1,037: Line 1,037:
stream$putc(po, '\n')
stream$putc(po, '\n')
end
end
end start_up</lang>
end start_up</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: <10011>
<pre>1001110011: <10011>
Line 1,052: Line 1,052:


=={{header|Common Lisp}}==
=={{header|Common Lisp}}==
<lang lisp>
<syntaxhighlight lang="lisp">
(ql:quickload :alexandria)
(ql:quickload :alexandria)
(defun rep-stringv (a-str &optional (max-rotation (floor (/ (length a-str) 2))))
(defun rep-stringv (a-str &optional (max-rotation (floor (/ (length a-str) 2))))
Line 1,071: Line 1,071:
;; Recurse function reducing length of rotation.
;; Recurse function reducing length of rotation.
(t (rep-stringv a-str (1- max-rotation)))))
(t (rep-stringv a-str (1- max-rotation)))))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<lang lisp>
<syntaxhighlight lang="lisp">
(setf test-strings '("1001110011"
(setf test-strings '("1001110011"
"1110111011"
"1110111011"
Line 1,089: Line 1,089:
(loop for item in test-strings
(loop for item in test-strings
collecting (cons item (rep-stringv item)))
collecting (cons item (rep-stringv item)))
</syntaxhighlight>
</lang>
<pre>
<pre>
(("1001110011" . "10011") ("1110111011" . "1110") ("0010010010" . "001")
(("1001110011" . "10011") ("1110111011" . "1110") ("0010010010" . "001")
Line 1,100: Line 1,100:
=={{header|Crystal}}==
=={{header|Crystal}}==
{{trans|Go}}
{{trans|Go}}
<lang ruby>def rep(s : String) : Int32
<syntaxhighlight lang="ruby">def rep(s : String) : Int32
x = s.size // 2
x = s.size // 2


Line 1,131: Line 1,131:


main
main
</syntaxhighlight>
</lang>


{{out}}
{{out}}
Line 1,150: Line 1,150:
=={{header|D}}==
=={{header|D}}==
Two different algorithms. The second is from the Raku entry.
Two different algorithms. The second is from the Raku entry.
<lang d>import std.stdio, std.string, std.conv, std.range, std.algorithm,
<syntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.range, std.algorithm,
std.ascii, std.typecons;
std.ascii, std.typecons;


Line 1,193: Line 1,193:
writefln("%(%s %)", w.chunks(r1));
writefln("%(%s %)", w.chunks(r1));
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>10011 10011
<pre>10011 10011
Line 1,210: Line 1,210:
{{libheader| System.SysUtils}}
{{libheader| System.SysUtils}}
{{Trans|Go}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Rep_string;
program Rep_string;


Line 1,256: Line 1,256:
end;
end;
{$IFNDEF UNIX}readln;{$ENDIF}
{$IFNDEF UNIX}readln;{$ENDIF}
end.</lang>
end.</syntaxhighlight>


=={{header|Dyalect}}==
=={{header|Dyalect}}==
{{trans|Go}}
{{trans|Go}}
<lang dyalect>func rep(s) {
<syntaxhighlight lang="dyalect">func rep(s) {
var x = s.Length() / 2
var x = s.Length() / 2
while x > 0 {
while x > 0 {
Line 1,291: Line 1,291:
print("\(s) not a rep-string")
print("\(s) not a rep-string")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 1,308: Line 1,308:


=={{header|EchoLisp}}==
=={{header|EchoLisp}}==
<lang scheme>
<syntaxhighlight lang="scheme">
(lib 'list) ;; list-rotate
(lib 'list) ;; list-rotate


Line 1,327: Line 1,327:
'too-short-no-rep))
'too-short-no-rep))
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<lang scheme>
<syntaxhighlight lang="scheme">
(define strings '["1001110011" "1110111011" "0010010010" "1010101010"
(define strings '["1001110011" "1110111011" "0010010010" "1010101010"
"1111111111" "0100101101" "0100100" "101" "11" "00" "1"])
"1111111111" "0100101101" "0100100" "101" "11" "00" "1"])
Line 1,350: Line 1,350:
"00" "0"
"00" "0"
"1" too-short-no-rep
"1" too-short-no-rep
</syntaxhighlight>
</lang>


=={{header|Elixir}}==
=={{header|Elixir}}==
<lang elixir>defmodule Rep_string do
<syntaxhighlight lang="elixir">defmodule Rep_string do
def find(""), do: IO.puts "String was empty (no repetition)"
def find(""), do: IO.puts "String was empty (no repetition)"
def find(str) do
def find(str) do
Line 1,381: Line 1,381:
1)
1)


Enum.each(strs, fn str -> Rep_string.find(str) end)</lang>
Enum.each(strs, fn str -> Rep_string.find(str) end)</syntaxhighlight>


{{out}}
{{out}}
Line 1,427: Line 1,427:


{{Works with|Office 365 betas 2021}}
{{Works with|Office 365 betas 2021}}
<lang lisp>REPCYCLES
<syntaxhighlight lang="lisp">REPCYCLES
=LAMBDA(s,
=LAMBDA(s,
LET(
LET(
Line 1,463: Line 1,463:
)
)
)
)
)</lang>
)</syntaxhighlight>


And also assuming the following generic bindings in the Name Manager:
And also assuming the following generic bindings in the Name Manager:


<lang lisp>FILTERP
<syntaxhighlight lang="lisp">FILTERP
=LAMBDA(p,
=LAMBDA(p,
LAMBDA(xs,
LAMBDA(xs,
Line 1,506: Line 1,506:
NA()
NA()
)
)
)</lang>
)</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,568: Line 1,568:


=={{header|F_Sharp|F#}}==
=={{header|F_Sharp|F#}}==
<lang fsharp>let isPrefix p (s : string) = s.StartsWith(p)
<syntaxhighlight lang="fsharp">let isPrefix p (s : string) = s.StartsWith(p)
let getPrefix n (s : string) = s.Substring(0,n)
let getPrefix n (s : string) = s.Substring(0,n)


Line 1,602: Line 1,602:
match repPrefixOf s with | None -> s + ": NO" | Some(p) -> s + ": YES ("+ p + ")")
match repPrefixOf s with | None -> s + ": NO" | Some(p) -> s + ": YES ("+ p + ")")
|> List.iter (printfn "%s")
|> List.iter (printfn "%s")
0</lang>
0</syntaxhighlight>
{{out}}
{{out}}
<pre>Testing for rep-string (and showing the longest repeated prefix in case):
<pre>Testing for rep-string (and showing the longest repeated prefix in case):
Line 1,618: Line 1,618:


=={{header|Factor}}==
=={{header|Factor}}==
<lang factor>USING: formatting grouping kernel math math.ranges qw sequences ;
<syntaxhighlight lang="factor">USING: formatting grouping kernel math math.ranges qw sequences ;
IN: rosetta-code.rep-string
IN: rosetta-code.rep-string
Line 1,632: Line 1,632:
0100101101 0100100 101 11 00 1 }
0100101101 0100100 101 11 00 1 }
"Shortest cycle:\n\n" printf
"Shortest cycle:\n\n" printf
[ dup find-rep-string "%-10s -> %s\n" printf ] each</lang>
[ dup find-rep-string "%-10s -> %s\n" printf ] each</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,653: Line 1,653:
{{trans|Python}}
{{trans|Python}}
{{works with|GNU Forth}}
{{works with|GNU Forth}}
<lang forth>: rep-string ( caddr1 u1 -- caddr2 u2 ) \ u2=0: not a rep-string
<syntaxhighlight lang="forth">: rep-string ( caddr1 u1 -- caddr2 u2 ) \ u2=0: not a rep-string
2dup dup >r r@ 2/ /string
2dup dup >r r@ 2/ /string
begin 2over 2over string-prefix? 0= over r@ < and while -1 /string repeat
begin 2over 2over string-prefix? 0= over r@ < and while -1 /string repeat
Line 1,673: Line 1,673:
s" 11" test
s" 11" test
s" 00" test
s" 00" test
s" 1" test ;</lang>
s" 1" test ;</syntaxhighlight>
{{out}}
{{out}}
<lang forth>cr tests
<syntaxhighlight lang="forth">cr tests
1001110011 has 10011 as repeating substring
1001110011 has 10011 as repeating substring
1110111011 has 1110 as repeating substring
1110111011 has 1110 as repeating substring
Line 1,687: Line 1,687:
00 has 0 as repeating substring
00 has 0 as repeating substring
1 has no repeating substring
1 has no repeating substring
ok</lang>
ok</syntaxhighlight>




=={{header|FreeBASIC}}==
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
{{trans|Yabasic}}
<lang freebasic>
<syntaxhighlight lang="freebasic">
Data "1001110011", "1110111011", "0010010010", "1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1", ""
Data "1001110011", "1110111011", "0010010010", "1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1", ""


Line 1,722: Line 1,722:
Loop
Loop
Sleep
Sleep
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 1,741: Line 1,741:
=={{header|Go}}==
=={{header|Go}}==
{{trans|Python}}
{{trans|Python}}
<lang go>package main
<syntaxhighlight lang="go">package main


import (
import (
Line 1,778: Line 1,778:
}
}
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 1,795: Line 1,795:


=={{header|Haskell}}==
=={{header|Haskell}}==
<lang haskell>import Data.List (inits, maximumBy)
<syntaxhighlight lang="haskell">import Data.List (inits, maximumBy)
import Data.Maybe (fromMaybe)
import Data.Maybe (fromMaybe)


Line 1,850: Line 1,850:
processIO xs = do
processIO xs = do
putStr (xs <> ": ")
putStr (xs <> ": ")
putStrLn $ process xs</lang>
putStrLn $ process xs</syntaxhighlight>
{{Out}}
{{Out}}
<pre>1001110011: 10011
<pre>1001110011: 10011
Line 1,865: Line 1,865:


Or, alternatively:
Or, alternatively:
<lang haskell>import Data.Bool (bool)
<syntaxhighlight lang="haskell">import Data.Bool (bool)
import Data.List (inits, intercalate, transpose)
import Data.List (inits, intercalate, transpose)


Line 1,917: Line 1,917:
<*> ((" -> " <>) . fxShow . f)
<*> ((" -> " <>) . fxShow . f)
)
)
xs</lang>
xs</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Longest cycles:
<pre>Longest cycles:
Line 1,937: Line 1,937:
The following works in both languages.
The following works in both languages.


<lang unicon>procedure main(A)
<syntaxhighlight lang="unicon">procedure main(A)
every write(s := !A,": ",(repString(s) | "Not a rep string!")\1)
every write(s := !A,": ",(repString(s) | "Not a rep string!")\1)
end
end
Line 1,950: Line 1,950:
while *s1 < n do s1 ||:= s2
while *s1 < n do s1 ||:= s2
return s1[1+:n]
return s1[1+:n]
end</lang>
end</syntaxhighlight>


{{Out}}
{{Out}}
Line 1,970: Line 1,970:
Here's a test:
Here's a test:


<lang j>replengths=: >:@i.@<.@-:@#
<syntaxhighlight lang="j">replengths=: >:@i.@<.@-:@#
rep=: $@] $ $
rep=: $@] $ $


isRepStr=: +./@((] -: rep)"0 1~ replengths)</lang>
isRepStr=: +./@((] -: rep)"0 1~ replengths)</syntaxhighlight>


Example use:
Example use:


<lang j> isRepStr '1001110011'
<syntaxhighlight lang="j"> isRepStr '1001110011'
1
1
Tests=: noun define
Tests=: noun define
Line 1,993: Line 1,993:
)
)
isRepStr;._2 Tests NB. run all tests
isRepStr;._2 Tests NB. run all tests
1 1 1 1 1 0 1 0 1 1 0</lang>
1 1 1 1 1 0 1 0 1 1 0</syntaxhighlight>


We could also report the lengths of the repeated prefix, though this seems more arbitrary:
We could also report the lengths of the repeated prefix, though this seems more arbitrary:


<lang j>nRepStr=: 0 -.~ (([ * ] -: rep)"0 1~ replengths)</lang>
<syntaxhighlight lang="j">nRepStr=: 0 -.~ (([ * ] -: rep)"0 1~ replengths)</syntaxhighlight>


With the above examples:
With the above examples:


<lang j> ":@nRepStr;._2 Tests
<syntaxhighlight lang="j"> ":@nRepStr;._2 Tests
5
5
4
4
Line 2,011: Line 2,011:
1
1
1</lang>
1</syntaxhighlight>


Here, the "non-str-rep" cases are indicated by an empty list of prefix lengths.
Here, the "non-str-rep" cases are indicated by an empty list of prefix lengths.


=={{header|Java}}==
=={{header|Java}}==
<lang java>public class RepString {
<syntaxhighlight lang="java">public class RepString {


static final String[] input = {"1001110011", "1110111011", "0010010010",
static final String[] input = {"1001110011", "1110111011", "0010010010",
Line 2,045: Line 2,045:
return "none";
return "none";
}
}
}</lang>
}</syntaxhighlight>


{{Out}}
{{Out}}
Line 2,063: Line 2,063:
=={{header|JavaScript}}==
=={{header|JavaScript}}==
===ES6===
===ES6===
<lang javascript>(() => {
<syntaxhighlight lang="javascript">(() => {
'use strict';
'use strict';


Line 2,207: Line 2,207:
// MAIN ---
// MAIN ---
return main();
return main();
})();</lang>
})();</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Longest cycles:
<pre>Longest cycles:
Line 2,225: Line 2,225:
=={{header|jq}}==
=={{header|jq}}==
For each test string, a JSON object giving details about the prefixes that satisfy the requirement is presented; if the string is not a rep-string, the empty array ([]) is shown.
For each test string, a JSON object giving details about the prefixes that satisfy the requirement is presented; if the string is not a rep-string, the empty array ([]) is shown.
<lang jq>def is_rep_string:
<syntaxhighlight lang="jq">def is_rep_string:
# if self is a rep-string then return [n, prefix]
# if self is a rep-string then return [n, prefix]
# where n is the number of full occurrences of prefix
# where n is the number of full occurrences of prefix
Line 2,245: Line 2,245:
| select( .[0] > 1 ) ]
| select( .[0] > 1 ) ]
;
;
</syntaxhighlight>
</lang>
'''Example''':
'''Example''':
<lang jq>def test:
<syntaxhighlight lang="jq">def test:
(
(
"1001110011",
"1001110011",
Line 2,263: Line 2,263:
;
;


test</lang>
test</syntaxhighlight>
{{Out}}
{{Out}}
<lang sh> $ jq -n -c -f rep-string.jq
<syntaxhighlight lang="sh"> $ jq -n -c -f rep-string.jq
{"1001110011":[[2,"10011"]]}
{"1001110011":[[2,"10011"]]}
{"1110111011":[[2,"1110"]]}
{"1110111011":[[2,"1110"]]}
Line 2,276: Line 2,276:
{"11":[[2,"1"]]}
{"11":[[2,"1"]]}
{"00":[[2,"0"]]}
{"00":[[2,"0"]]}
{"1":[]}</lang>
{"1":[]}</syntaxhighlight>


=={{header|Julia}}==
=={{header|Julia}}==
<tt>repstring</tt> returns a list of all of the substrings of its input that are the repeating units of a rep-string. If the input is not a valid rep-string, it returns an empty list. Julia indexes strings, including those that contain multi-byte characters, at the byte level. Because of this characteristic, <tt>repstring</tt> indexes its input using the <tt>chr2ind</tt> built-in.
<tt>repstring</tt> returns a list of all of the substrings of its input that are the repeating units of a rep-string. If the input is not a valid rep-string, it returns an empty list. Julia indexes strings, including those that contain multi-byte characters, at the byte level. Because of this characteristic, <tt>repstring</tt> indexes its input using the <tt>chr2ind</tt> built-in.


<lang julia>function repstring(r::AbstractString)
<syntaxhighlight lang="julia">function repstring(r::AbstractString)
n = length(r)
n = length(r)
replst = String[]
replst = String[]
Line 2,303: Line 2,303:
println("$r is a rep-string of ", join(replst, ", "), ".")
println("$r is a rep-string of ", join(replst, ", "), ".")
end
end
end</lang>
end</syntaxhighlight>


{{out}}
{{out}}
Line 2,320: Line 2,320:


=={{header|Kotlin}}==
=={{header|Kotlin}}==
<lang scala>// version 1.0.6
<syntaxhighlight lang="scala">// version 1.0.6


fun repString(s: String): MutableList<String> {
fun repString(s: String): MutableList<String> {
Line 2,356: Line 2,356:
println("${s.padStart(10)} -> ${if (size > 0) reps[size - 1] else "Not a rep-string"}")
println("${s.padStart(10)} -> ${if (size > 0) reps[size - 1] else "Not a rep-string"}")
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 2,379: Line 2,379:
The heavy lifting:
The heavy lifting:


<lang lisp>
<syntaxhighlight lang="lisp">
(defun get-reps (text)
(defun get-reps (text)
(lists:filtermap
(lists:filtermap
Line 2,393: Line 2,393:
(1 head)
(1 head)
(_ '()))))
(_ '()))))
</syntaxhighlight>
</lang>


Displaying the results:
Displaying the results:


<lang lisp>
<syntaxhighlight lang="lisp">
(defun report
(defun report
((`#(,text ()))
((`#(,text ()))
Line 2,408: Line 2,408:
(lists:zip data (lists:map #'get-reps/1 data)))
(lists:zip data (lists:map #'get-reps/1 data)))
'ok))
'ok))
</syntaxhighlight>
</lang>


Running the code:
Running the code:
Line 2,441: Line 2,441:
=={{header|Maple}}==
=={{header|Maple}}==
The built-in <code>Period</code> command in the <code>StringTools</code> package computes the length of the longest repeated prefix.
The built-in <code>Period</code> command in the <code>StringTools</code> package computes the length of the longest repeated prefix.
<lang Maple>repstr? := proc( s :: string )
<syntaxhighlight lang="maple">repstr? := proc( s :: string )
local per := StringTools:-Period( s );
local per := StringTools:-Period( s );
if 2 * per <= length( s ) then
if 2 * per <= length( s ) then
Line 2,448: Line 2,448:
false, ""
false, ""
end if
end if
end proc:</lang>
end proc:</syntaxhighlight>
For the given set of test strings, we can generate the following output.
For the given set of test strings, we can generate the following output.
<syntaxhighlight lang="maple">
<lang Maple>
> Test := ["1001110011", "1110111011", "0010010010", "1010101010", "1111111111", \
> Test := ["1001110011", "1110111011", "0010010010", "1010101010", "1111111111", \
"0100101101", "0100100", "101", "11", "00", "1"]:
"0100101101", "0100100", "101", "11", "00", "1"]:
Line 2,468: Line 2,468:
1 false
1 false


</syntaxhighlight>
</lang>


=={{header|Mathematica}}/{{header|Wolfram Language}}==
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica is based on pattern-based matching, so this is very easily implemented:
Mathematica is based on pattern-based matching, so this is very easily implemented:
<lang Mathematica>RepStringQ[strin_String]:=StringCases[strin,StartOfString~~Repeated[x__,{2,\[Infinity]}]~~y___~~EndOfString/;StringMatchQ[x,StartOfString~~y~~___]:>x, Overlaps -> All]</lang>
<syntaxhighlight lang="mathematica">RepStringQ[strin_String]:=StringCases[strin,StartOfString~~Repeated[x__,{2,\[Infinity]}]~~y___~~EndOfString/;StringMatchQ[x,StartOfString~~y~~___]:>x, Overlaps -> All]</syntaxhighlight>
Trying it out for the test-strings:
Trying it out for the test-strings:
<lang>str={"1001110011","1110111011","0010010010","1010101010","1111111111","0100101101","0100100","101","11","00","1"};
<syntaxhighlight lang="text">str={"1001110011","1110111011","0010010010","1010101010","1111111111","0100101101","0100100","101","11","00","1"};
{#,RepStringQ[#]}&/@str//Grid</lang>
{#,RepStringQ[#]}&/@str//Grid</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011 {10011}
<pre>1001110011 {10011}
Line 2,492: Line 2,492:


=={{header|Modula-2}}==
=={{header|Modula-2}}==
<lang modula2>MODULE RepStrings;
<syntaxhighlight lang="modula2">MODULE RepStrings;
FROM InOut IMPORT Write, WriteString, WriteLn, WriteCard;
FROM InOut IMPORT Write, WriteString, WriteLn, WriteCard;
FROM Strings IMPORT Copy, Length;
FROM Strings IMPORT Copy, Length;
Line 2,552: Line 2,552:
rep("00");
rep("00");
rep("1");
rep("1");
END RepStrings.</lang>
END RepStrings.</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: 10011
<pre>1001110011: 10011
Line 2,568: Line 2,568:
=={{header|NetRexx}}==
=={{header|NetRexx}}==
{{trans|REXX}}
{{trans|REXX}}
<lang NetRexx>/* NetRexx */
<syntaxhighlight lang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
options replace format comments java crossref symbols nobinary


Line 2,629: Line 2,629:
end w_
end w_
return
return
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>
<pre>
Line 2,647: Line 2,647:
=={{header|NGS}}==
=={{header|NGS}}==
{{trans|Python}}
{{trans|Python}}
<lang NGS>tests = [
<syntaxhighlight lang="ngs">tests = [
'1001110011'
'1001110011'
'1110111011'
'1110111011'
Line 2,668: Line 2,668:
echo("${test} ${if r "has repetition of length ${r} (i.e. ${test[0..r]})" "is not a rep-string"}")
echo("${test} ${if r "has repetition of length ${r} (i.e. ${test[0..r]})" "is not a rep-string"}")
})
})
}</lang>
}</syntaxhighlight>
{{out}}<pre>1001110011 has repetition of length 5 (i.e. 10011)
{{out}}<pre>1001110011 has repetition of length 5 (i.e. 10011)
1110111011 has repetition of length 4 (i.e. 1110)
1110111011 has repetition of length 4 (i.e. 1110)
Line 2,683: Line 2,683:
=={{header|Nim}}==
=={{header|Nim}}==
{{trans|Python}}
{{trans|Python}}
<lang nim>import strutils
<syntaxhighlight lang="nim">import strutils


proc isRepeated(text: string): int =
proc isRepeated(text: string): int =
Line 2,704: Line 2,704:
let ln = isRepeated(line)
let ln = isRepeated(line)
echo "'", line, "' has a repetition length of ", ln, " i.e ",
echo "'", line, "' has a repetition length of ", ln, " i.e ",
(if ln > 0: "'" & line[0 ..< ln] & "'" else: "*not* a rep-string")</lang>
(if ln > 0: "'" & line[0 ..< ln] & "'" else: "*not* a rep-string")</syntaxhighlight>
{{Out}}
{{Out}}
<pre>'1001110011' has a repetition length of 5 i.e '10011'
<pre>'1001110011' has a repetition length of 5 i.e '10011'
Line 2,719: Line 2,719:


=={{header|Objeck}}==
=={{header|Objeck}}==
<lang objeck>class RepString {
<syntaxhighlight lang="objeck">class RepString {
function : Main(args : String[]) ~ Nil {
function : Main(args : String[]) ~ Nil {
strings := ["1001110011", "1110111011", "0010010010", "1111111111",
strings := ["1001110011", "1110111011", "0010010010", "1111111111",
Line 2,780: Line 2,780:
return true;
return true;
}
}
}</lang>
}</syntaxhighlight>


Output:
Output:
Line 2,800: Line 2,800:
Returns null if no rep string.
Returns null if no rep string.


<lang oforth>: repString(s)
<syntaxhighlight lang="oforth">: repString(s)
| sz i |
| sz i |
s size dup ->sz 2 / 1 -1 step: i [
s size dup ->sz 2 / 1 -1 step: i [
s left(sz i - ) s right(sz i -) == ifTrue: [ s left(i) return ]
s left(sz i - ) s right(sz i -) == ifTrue: [ s left(i) return ]
]
]
null ;</lang>
null ;</syntaxhighlight>


{{Out}}
{{Out}}
Line 2,815: Line 2,815:


=={{header|PARI/GP}}==
=={{header|PARI/GP}}==
<lang parigp>rep(v)=for(i=1,#v\2,for(j=i+1,#v,if(v[j]!=v[j-i],next(2)));return(i));0;
<syntaxhighlight lang="parigp">rep(v)=for(i=1,#v\2,for(j=i+1,#v,if(v[j]!=v[j-i],next(2)));return(i));0;
v=["1001110011","1110111011","0010010010","1010101010","1111111111","0100101101","0100100","101","11","00","1"];
v=["1001110011","1110111011","0010010010","1010101010","1111111111","0100101101","0100100","101","11","00","1"];
for(i=1,#v,print(v[i]" "rep(Vec(v[i]))))</lang>
for(i=1,#v,print(v[i]" "rep(Vec(v[i]))))</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011 5
<pre>1001110011 5
Line 2,832: Line 2,832:


=={{header|Perl}}==
=={{header|Perl}}==
<lang perl>foreach (qw(1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1)) {
<syntaxhighlight lang="perl">foreach (qw(1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1)) {
print "$_\n";
print "$_\n";
if (/^(.+)\1+(.*$)(?(?{ substr($1, 0, length $2) eq $2 })|(?!))/) {
if (/^(.+)\1+(.*$)(?(?{ substr($1, 0, length $2) eq $2 })|(?!))/) {
Line 2,839: Line 2,839:
print " (no repeat)\n\n";
print " (no repeat)\n\n";
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011
<pre>1001110011
Line 2,877: Line 2,877:
{{trans|Julia}}
{{trans|Julia}}
Shows all possible repeated sub-strings, as Julia, but in the output style of Perl/Elixir
Shows all possible repeated sub-strings, as Julia, but in the output style of Perl/Elixir
<!--<lang Phix>(phixonline)-->
<!--<syntaxhighlight lang="phix">(phixonline)-->
<span style="color: #008080;">function</span> <span style="color: #000000;">list_reps</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">function</span> <span style="color: #000000;">list_reps</span><span style="color: #0000FF;">(</span><span style="color: #004080;">string</span> <span style="color: #000000;">r</span><span style="color: #0000FF;">)</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">replist</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
<span style="color: #004080;">sequence</span> <span style="color: #000000;">replist</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
Line 2,916: Line 2,916:
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #7060A8;">printf</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</lang>-->
<!--</syntaxhighlight>-->
{{out}}
{{out}}
<pre>
<pre>
Line 2,959: Line 2,959:


=={{header|Phixmonti}}==
=={{header|Phixmonti}}==
<lang Phixmonti>include ..\Utilitys.pmt
<syntaxhighlight lang="phixmonti">include ..\Utilitys.pmt


def repstr /# s n -- s #/
def repstr /# s n -- s #/
Line 2,986: Line 2,986:
len for
len for
get repString print nl
get repString print nl
endfor</lang>
endfor</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: 10011
<pre>1001110011: 10011
Line 3,004: Line 3,004:


=={{header|Picat}}==
=={{header|Picat}}==
<lang Picat>go =>
<syntaxhighlight lang="picat">go =>
Strings = [
Strings = [
"1001110011", % 10011
"1001110011", % 10011
Line 3,049: Line 3,049:
% the last (truncated) slice (or []) must be a substring of Substr
% the last (truncated) slice (or []) must be a substring of Substr
Rest = slice(S,Len2+1,Len),
Rest = slice(S,Len2+1,Len),
find(Substr,Rest,1,_). </lang>
find(Substr,Rest,1,_). </syntaxhighlight>


{{out}}
{{out}}
Line 3,072: Line 3,072:


=={{header|PicoLisp}}==
=={{header|PicoLisp}}==
<lang PicoLisp>(de repString (Str)
<syntaxhighlight lang="picolisp">(de repString (Str)
(let Lst (chop Str)
(let Lst (chop Str)
(for (N (/ (length Lst) 2) (gt0 N) (dec N))
(for (N (/ (length Lst) 2) (gt0 N) (dec N))
Line 3,082: Line 3,082:
(NIL (head X H))
(NIL (head X H))
(NIL Lst T) ) ) )
(NIL Lst T) ) ) )
N ) ) ) )</lang>
N ) ) ) )</syntaxhighlight>
Test:
Test:
<lang PicoLisp>(test 5 (repString "1001110011"))
<syntaxhighlight lang="picolisp">(test 5 (repString "1001110011"))
(test 4 (repString "1110111011"))
(test 4 (repString "1110111011"))
(test 3 (repString "0010010010"))
(test 3 (repString "0010010010"))
Line 3,095: Line 3,095:
(test 1 (repString "00"))
(test 1 (repString "00"))
(test NIL (repString "1"))
(test NIL (repString "1"))
(test NIL (repString "0100101"))</lang>
(test NIL (repString "0100101"))</syntaxhighlight>


=={{header|PL/I}}==
=={{header|PL/I}}==
{{Incorrect|PL/I|"0100101101" is not a rep-string.}}
{{Incorrect|PL/I|"0100101101" is not a rep-string.}}
<lang PL/I>rep: procedure options (main); /* 5 May 2015 */
<syntaxhighlight lang="pl/i">rep: procedure options (main); /* 5 May 2015 */
declare s bit (10) varying;
declare s bit (10) varying;
declare (i, k) fixed binary;
declare (i, k) fixed binary;
Line 3,117: Line 3,117:
end;
end;


end rep;</lang>
end rep;</syntaxhighlight>
{{Out}}
{{Out}}
<pre>
<pre>
Line 3,134: Line 3,134:


=={{header|PL/M}}==
=={{header|PL/M}}==
<lang plmi>100H:
<syntaxhighlight lang="plmi">100H:


DECLARE MAX$REP LITERALLY '32';
DECLARE MAX$REP LITERALLY '32';
Line 3,240: Line 3,240:
CALL PRINT$STRING( .( CR, LF, '$' ) );
CALL PRINT$STRING( .( CR, LF, '$' ) );
END;
END;
EOF</lang>
EOF</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 3,260: Line 3,260:
Using SWI-Prolog 7 library(func), for some functional syntax.
Using SWI-Prolog 7 library(func), for some functional syntax.


<lang Prolog>:- use_module(library(func)).
<syntaxhighlight lang="prolog">:- use_module(library(func)).


%% Implementation logic:
%% Implementation logic:
Line 3,291: Line 3,291:
report_repstrings :-
report_repstrings :-
Results = maplist(test_for_repstring) $ test_strings(~),
Results = maplist(test_for_repstring) $ test_strings(~),
maplist(report_repstring, Results).</lang>
maplist(report_repstring, Results).</syntaxhighlight>


Output
Output
Line 3,310: Line 3,310:


=={{header|PureBasic}}==
=={{header|PureBasic}}==
<lang purebasic>a$="1001110011"+#CRLF$+"1110111011"+#CRLF$+"0010010010"+#CRLF$+"1010101010"+#CRLF$+"1111111111"+#CRLF$+
<syntaxhighlight lang="purebasic">a$="1001110011"+#CRLF$+"1110111011"+#CRLF$+"0010010010"+#CRLF$+"1010101010"+#CRLF$+"1111111111"+#CRLF$+
"0100101101"+#CRLF$+"0100100" +#CRLF$+"101" +#CRLF$+"11" +#CRLF$+"00" +#CRLF$+
"0100101101"+#CRLF$+"0100100" +#CRLF$+"101" +#CRLF$+"11" +#CRLF$+"00" +#CRLF$+
"1" +#CRLF$
"1" +#CRLF$
Line 3,331: Line 3,331:
If Not Len(s2$) : PrintN(LSet(s1$,15,Chr(32))+#TAB$+"found nothing.") : EndIf
If Not Len(s2$) : PrintN(LSet(s1$,15,Chr(32))+#TAB$+"found nothing.") : EndIf
Next
Next
Input()</lang>
Input()</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011 longest sequence: 10011
<pre>1001110011 longest sequence: 10011
Line 3,348: Line 3,348:


===Python: Procedural===
===Python: Procedural===
<lang python>def is_repeated(text):
<syntaxhighlight lang="python">def is_repeated(text):
'check if the first part of the string is repeated throughout the string'
'check if the first part of the string is repeated throughout the string'
for x in range(len(text)//2, 0, -1):
for x in range(len(text)//2, 0, -1):
Line 3,370: Line 3,370:
ln = is_repeated(line)
ln = is_repeated(line)
print('%r has a repetition length of %i i.e. %s'
print('%r has a repetition length of %i i.e. %s'
% (line, ln, repr(line[:ln]) if ln else '*not* a rep-string'))</lang>
% (line, ln, repr(line[:ln]) if ln else '*not* a rep-string'))</syntaxhighlight>


{{out}}
{{out}}
Line 3,387: Line 3,387:
===Python: Functional===
===Python: Functional===
This returns all the possible repeated substrings
This returns all the possible repeated substrings
<lang python>>>> def reps(text):
<syntaxhighlight lang="python">>>> def reps(text):
return [text[:x] for x in range(1, 1 + len(text) // 2)
return [text[:x] for x in range(1, 1 + len(text) // 2)
if text.startswith(text[x:])]
if text.startswith(text[x:])]
Line 3,416: Line 3,416:
'00' has reps ['0']
'00' has reps ['0']
'1' has reps []
'1' has reps []
>>> </lang>
>>> </syntaxhighlight>




Line 3,422: Line 3,422:
{{Trans|Haskell}}
{{Trans|Haskell}}
{{Works with|Python|3.7}}
{{Works with|Python|3.7}}
<lang python>'''Rep-strings'''
<syntaxhighlight lang="python">'''Rep-strings'''


from itertools import (accumulate, chain, cycle, islice)
from itertools import (accumulate, chain, cycle, islice)
Line 3,518: Line 3,518:
# MAIN ---
# MAIN ---
if __name__ == '__main__':
if __name__ == '__main__':
main()</lang>
main()</syntaxhighlight>
{{Out}}
{{Out}}
<pre>Longest cycles:
<pre>Longest cycles:
Line 3,536: Line 3,536:
===Python: Regexp===
===Python: Regexp===
This version, inspired by the Raku entry uses the regexp substitute where what the match is substituted with is returned by a function.
This version, inspired by the Raku entry uses the regexp substitute where what the match is substituted with is returned by a function.
<lang python>import re
<syntaxhighlight lang="python">import re


matchstr = """\
matchstr = """\
Line 3,560: Line 3,560:
print(re.sub(r'(.+)(\1+)(.*)|(.*)', _checker, txt))
print(re.sub(r'(.+)(\1+)(.*)|(.*)', _checker, txt))


checkit(matchstr)</lang>
checkit(matchstr)</syntaxhighlight>


{{out}}
{{out}}
Line 3,581: Line 3,581:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].


<lang Quackery> [ false swap
<syntaxhighlight lang="quackery"> [ false swap
dup size 1 > if
dup size 1 > if
[ [] temp put
[ [] temp put
Line 3,604: Line 3,604:
[ say 'There is no rep-string for "'
[ say 'There is no rep-string for "'
nip echo$ say '".' ]
nip echo$ say '".' ]
cr ] is task ( $ --> )</lang>
cr ] is task ( $ --> )</syntaxhighlight>


{{out}}
{{out}}
Line 3,621: Line 3,621:


=={{header|Racket}}==
=={{header|Racket}}==
<lang Racket>#lang racket
<syntaxhighlight lang="racket">#lang racket
(define (rep-string str)
(define (rep-string str)
Line 3,646: Line 3,646:
"00"
"00"
"1")])
"1")])
(printf "~a => ~a\n" str (or (rep-string str) "not a rep-string")))</lang>
(printf "~a => ~a\n" str (or (rep-string str) "not a rep-string")))</syntaxhighlight>


{{Out}}
{{Out}}
Line 3,663: Line 3,663:
=={{header|Raku}}==
=={{header|Raku}}==
(formerly Perl 6)
(formerly Perl 6)
<lang perl6>for <1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1> {
<syntaxhighlight lang="raku" line>for <1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1> {
if /^ (.+) $0+: (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
if /^ (.+) $0+: (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
my $rep = $0.chars;
my $rep = $0.chars;
Line 3,671: Line 3,671:
say "$_ (no repeat)";
say "$_ (no repeat)";
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>10011𝟙𝟘𝟘𝟙𝟙
<pre>10011𝟙𝟘𝟘𝟙𝟙
Line 3,689: Line 3,689:
throw away the bits on the right that you want thrown away.)
throw away the bits on the right that you want thrown away.)
This produces the same output as above.
This produces the same output as above.
<lang perl6>sub repstr(Str $s) {
<syntaxhighlight lang="raku" line>sub repstr(Str $s) {
my $bits = :2($s);
my $bits = :2($s);
for reverse 1 .. $s.chars div 2 -> $left {
for reverse 1 .. $s.chars div 2 -> $left {
Line 3,705: Line 3,705:
say "$_ (no repeat)";
say "$_ (no repeat)";
}
}
}</lang>
}</syntaxhighlight>


=={{header|REXX}}==
=={{header|REXX}}==
===version 1===
===version 1===
<lang rexx>/* REXX ***************************************************************
<syntaxhighlight lang="rexx">/* REXX ***************************************************************
* 11.05.2013 Walter Pachl
* 11.05.2013 Walter Pachl
* 14.05.2013 Walter Pachl extend to show additional rep-strings
* 14.05.2013 Walter Pachl extend to show additional rep-strings
Line 3,760: Line 3,760:
show_norep:
show_norep:
Say right(sq,12) 'is not a repeated string'
Say right(sq,12) 'is not a repeated string'
Return</lang>
Return</syntaxhighlight>
{{Out}}
{{Out}}
<pre>'1001110011' has a repetition length of 5 i.e. '10011'
<pre>'1001110011' has a repetition length of 5 i.e. '10011'
Line 3,781: Line 3,781:
===version 2===
===version 2===
A check was added to validate if the strings are binary strings. &nbsp; The binary strings can be of any length.
A check was added to validate if the strings are binary strings. &nbsp; The binary strings can be of any length.
<lang rexx>/*REXX pgm determines if a string is a repString, it returns minimum length repString.*/
<syntaxhighlight lang="rexx">/*REXX pgm determines if a string is a repString, it returns minimum length repString.*/
parse arg s /*get optional strings from the C.L. */
parse arg s /*get optional strings from the C.L. */
if s='' then s=1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1 45
if s='' then s=1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1 45
Line 3,796: Line 3,796:
if left($$,L)==x then return @rep left($,15) "[length" j']'
if left($$,L)==x then return @rep left($,15) "[length" j']'
end /*j*/ /* [↑] we have found a good repString.*/
end /*j*/ /* [↑] we have found a good repString.*/
return ' (no repetitions)' /*failure to find repString.*/</lang>
return ' (no repetitions)' /*failure to find repString.*/</syntaxhighlight>
'''output''' &nbsp; when using the default binary strings for input:
'''output''' &nbsp; when using the default binary strings for input:
<pre>
<pre>
Line 3,814: Line 3,814:


=={{header|Ring}}==
=={{header|Ring}}==
<lang ring>
<syntaxhighlight lang="ring">
# Project : Rep-string
# Project : Rep-string


Line 3,845: Line 3,845:
ok
ok
next
next
</syntaxhighlight>
</lang>
Output:
Output:
<pre>
<pre>
Line 3,862: Line 3,862:


=={{header|Ruby}}==
=={{header|Ruby}}==
<lang ruby>ar = %w(1001110011
<syntaxhighlight lang="ruby">ar = %w(1001110011
1110111011
1110111011
0010010010
0010010010
Line 3,877: Line 3,877:
rep_pos = (str.size/2).downto(1).find{|pos| str.start_with? str[pos..-1]}
rep_pos = (str.size/2).downto(1).find{|pos| str.start_with? str[pos..-1]}
puts str, rep_pos ? " "*rep_pos + str[0, rep_pos] : "(no repetition)", ""
puts str, rep_pos ? " "*rep_pos + str[0, rep_pos] : "(no repetition)", ""
end</lang>
end</syntaxhighlight>
{{Out|Output (as Perl)}}
{{Out|Output (as Perl)}}
<pre>1001110011
<pre>1001110011
Line 3,913: Line 3,913:


=={{header|Rust}}==
=={{header|Rust}}==
<lang Rust>fn main() {
<syntaxhighlight lang="rust">fn main() {
let strings = vec![
let strings = vec![
String::from("1001110011"),
String::from("1001110011"),
Line 3,993: Line 3,993:
}
}
}
}
}</lang>
}</syntaxhighlight>


=={{header|Scala}}==
=={{header|Scala}}==
<lang Scala>object RepString extends App {
<syntaxhighlight lang="scala">object RepString extends App {
def repsOf(s: String) = s.trim match {
def repsOf(s: String) = s.trim match {
case s if s.length < 2 => Nil
case s if s.length < 2 => Nil
Line 4,022: Line 4,022:
val todo = if (args.length > 0) args else tests
val todo = if (args.length > 0) args else tests
todo.map(printReps).foreach(println)
todo.map(printReps).foreach(println)
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>1001110011: YES (10011)
<pre>1001110011: YES (10011)
Line 4,037: Line 4,037:


=={{header|Seed7}}==
=={{header|Seed7}}==
<lang seed7>$ include "seed7_05.s7i";
<syntaxhighlight lang="seed7">$ include "seed7_05.s7i";


const func integer: repeatLength (in string: text) is func
const func integer: repeatLength (in string: text) is func
Line 4,066: Line 4,066:
end if;
end if;
end for;
end for;
end func;</lang>
end func;</syntaxhighlight>


{{out}}
{{out}}
Line 4,084: Line 4,084:


=={{header|Sidef}}==
=={{header|Sidef}}==
<lang ruby>var arr = <1001110011 1110111011
<syntaxhighlight lang="ruby">var arr = <1001110011 1110111011
0010010010 1010101010
0010010010 1010101010
1111111111 0100101101
1111111111 0100101101
Line 4,098: Line 4,098:
say "#{n} (no repeat)";
say "#{n} (no repeat)";
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>10011𝟙𝟘𝟘𝟙𝟙
<pre>10011𝟙𝟘𝟘𝟙𝟙
Line 4,113: Line 4,113:


=={{header|Snobol4}}==
=={{header|Snobol4}}==
<lang Snobol4>* Rep-string
<syntaxhighlight lang="snobol4">* Rep-string
strings = "1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1 "
strings = "1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1 "
pat1 = (len(1) $ fc breakx(*fc)) $ x *x (arbno(*x) (rpos(0) | rem $ y *?(x ? y)))
pat1 = (len(1) $ fc breakx(*fc)) $ x *x (arbno(*x) (rpos(0) | rem $ y *?(x ? y)))
Line 4,124: Line 4,124:
:(getstring)
:(getstring)
end
end
</syntaxhighlight>
</lang>
{{out}}
{{out}}
<pre>1001110011 has shortest rep-string value of -> 10011
<pre>1001110011 has shortest rep-string value of -> 10011
Line 4,141: Line 4,141:
=={{header|Swift}}==
=={{header|Swift}}==


<lang swift>import Foundation
<syntaxhighlight lang="swift">import Foundation


func repString(_ input: String) -> [String] {
func repString(_ input: String) -> [String] {
Line 4,166: Line 4,166:
for testCase in testCases {
for testCase in testCases {
print("\(testCase) has reps: \(repString(testCase))")
print("\(testCase) has reps: \(repString(testCase))")
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,183: Line 4,183:


=={{header|Tcl}}==
=={{header|Tcl}}==
<lang tcl>proc repstring {text} {
<syntaxhighlight lang="tcl">proc repstring {text} {
set len [string length $text]
set len [string length $text]
for {set i [expr {$len/2}]} {$i > 0} {incr i -1} {
for {set i [expr {$len/2}]} {$i > 0} {incr i -1} {
Line 4,193: Line 4,193:
}
}
error "no repetition"
error "no repetition"
}</lang>
}</syntaxhighlight>
Demonstrating:
Demonstrating:
<lang tcl>foreach sample {
<syntaxhighlight lang="tcl">foreach sample {
"1001110011" "1110111011" "0010010010" "1010101010" "1111111111"
"1001110011" "1110111011" "0010010010" "1010101010" "1111111111"
"0100101101" "0100100" "101" "11" "00" "1"
"0100101101" "0100100" "101" "11" "00" "1"
Line 4,206: Line 4,206:
puts [format "\"%s\" is not a repeated string" $sample]
puts [format "\"%s\" is not a repeated string" $sample]
}
}
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>
Line 4,225: Line 4,225:
{{trans|Tcl}}
{{trans|Tcl}}
{{works with|bash}}
{{works with|bash}}
<lang bash>is_repeated() {
<syntaxhighlight lang="bash">is_repeated() {
local str=$1 len rep part
local str=$1 len rep part
for (( len = ${#str} / 2; len > 0; len-- )); do
for (( len = ${#str} / 2; len > 0; len-- )); do
Line 4,259: Line 4,259:
00
00
1
1
END_TESTS</lang>
END_TESTS</syntaxhighlight>


{{out}}
{{out}}
Line 4,275: Line 4,275:


=={{header|VBScript}}==
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function rep_string(s)
Function rep_string(s)
max_len = Int(Len(s)/2)
max_len = Int(Len(s)/2)
Line 4,313: Line 4,313:
WScript.StdOut.WriteLine
WScript.StdOut.WriteLine
Next
Next
</syntaxhighlight>
</lang>


{{Out}}
{{Out}}
Line 4,332: Line 4,332:
=={{header|Vlang}}==
=={{header|Vlang}}==
{{trans|Go}}
{{trans|Go}}
<lang vlang>fn rep(s string) int {
<syntaxhighlight lang="vlang">fn rep(s string) int {
for x := s.len / 2; x > 0; x-- {
for x := s.len / 2; x > 0; x-- {
if s.starts_with(s[x..]) {
if s.starts_with(s[x..]) {
Line 4,363: Line 4,363:
}
}
}
}
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,383: Line 4,383:
{{trans|Kotlin}}
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
{{libheader|Wren-fmt}}
<lang ecmascript>import "/fmt" for Fmt
<syntaxhighlight lang="ecmascript">import "/fmt" for Fmt


var repString = Fn.new { |s|
var repString = Fn.new { |s|
Line 4,418: Line 4,418:
var t = (reps.count > 0) ? reps[-1] : "Not a rep-string"
var t = (reps.count > 0) ? reps[-1] : "Not a rep-string"
Fmt.print("$10s -> $s", s, t)
Fmt.print("$10s -> $s", s, t)
}</lang>
}</syntaxhighlight>


{{out}}
{{out}}
Line 4,438: Line 4,438:


=={{header|Yabasic}}==
=={{header|Yabasic}}==
<lang Yabasic>data "1001110011", "1110111011", "0010010010", "1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1", ""
<syntaxhighlight lang="yabasic">data "1001110011", "1110111011", "0010010010", "1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1", ""


sub rep$(c$, n)
sub rep$(c$, n)
Line 4,464: Line 4,464:
print p$, " is composed of ", b$, " repeated"
print p$, " is composed of ", b$, " repeated"
end if
end if
loop</lang>
loop</syntaxhighlight>


=={{header|zkl}}==
=={{header|zkl}}==
{{trans|D}}
{{trans|D}}
<lang zkl>fcn repString(s){
<syntaxhighlight lang="zkl">fcn repString(s){
foreach n in ([s.len()/2+1..1,-1]){
foreach n in ([s.len()/2+1..1,-1]){
Walker.cycle(s[0,n]).pump(s.len(),String) :
Walker.cycle(s[0,n]).pump(s.len(),String) :
Line 4,474: Line 4,474:
}
}
return(False)
return(False)
}</lang>
}</syntaxhighlight>
{{trans|Python}}
{{trans|Python}}
<lang zkl>fcn repString(s){
<syntaxhighlight lang="zkl">fcn repString(s){
foreach n in ([s.len()/2..0,-1]){
foreach n in ([s.len()/2..0,-1]){
if(s.matches(s[n,*] + "*") and n*2<=s.len()) return(n);
if(s.matches(s[n,*] + "*") and n*2<=s.len()) return(n);
}
}
return(False)
return(False)
}</lang>
}</syntaxhighlight>
<lang zkl>words:=("1001110011 1110111011 0010010010 1010101010 "
<syntaxhighlight lang="zkl">words:=("1001110011 1110111011 0010010010 1010101010 "
"1111111111 0100101101 0100100 101 11 00 1").split(" ");
"1111111111 0100101101 0100100 101 11 00 1").split(" ");
foreach w in (words){
foreach w in (words){
Line 4,488: Line 4,488:
else [0..*,n].tweak('wrap(z){ if(s:=w[z,n]) s else Void.Stop })
else [0..*,n].tweak('wrap(z){ if(s:=w[z,n]) s else Void.Stop })
.walk().concat(" ").println();
.walk().concat(" ").println();
}</lang>
}</syntaxhighlight>
{{out}}
{{out}}
<pre>
<pre>