Rep-string: Difference between revisions

m
syntax highlighting fixup automation
No edit summary
m (syntax highlighting fixup automation)
Line 35:
{{trans|Python: Functional}}
 
<langsyntaxhighlight lang="11l">F reps(text)
R (1 .< 1 + text.len I/ 2).filter(x -> @text.starts_with(@text[x..])).map(x -> @text[0 .< x])
 
Line 52:
 
L(line) matchstr.split("\n")
print(‘'#.' has reps #.’.format(line, reps(line)))</langsyntaxhighlight>
 
{{out}}
Line 70:
 
=={{header|Action!}}==
<langsyntaxhighlight Actionlang="action!">BYTE FUNC IsCycle(CHAR ARRAY s,sub)
BYTE i,j,count
 
Line 128:
Test("00")
Test("1")
RETURN</langsyntaxhighlight>
{{out}}
[https://gitlab.com/amarok8bit/action-rosetta-code/-/raw/master/images/Rep-string.png Screenshot from Atari 8-bit computer]
Line 147:
=={{header|Ada}}==
 
<langsyntaxhighlight Adalang="ada">with Ada.Command_Line, Ada.Text_IO, Ada.Strings.Fixed;
 
procedure Rep_String is
Line 177:
Ada.Text_IO.Put_Line("Longest rep-string for """& X &""": """& Y &"""");
end if;
end Rep_String;</langsyntaxhighlight>
 
{{out}}
Line 206:
=={{header|ALGOL 68}}==
{{works with|ALGOL 68G|Any - tested with release 2.8.win32}}
<langsyntaxhighlight 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 #
PROC longest rep string = ( STRING input )STRING:
Line 273:
)
OD
)</langsyntaxhighlight>
{{out}}
<pre>1001110011: longest rep string: "10011"
Line 291:
This function returns a list of all possible repeated substrings.
It returns the empty list if there are none.
<langsyntaxhighlight APLlang="apl">rep ← ⊢ (⊢(/⍨)(⊂⊣)≡¨(≢⊣)⍴¨⊢) ⍳∘(⌊0.5×≢)↑¨⊂</langsyntaxhighlight>
{{out}}
<pre> rep '1001110011'
Line 319:
 
=={{header|AppleScript}}==
<langsyntaxhighlight AppleScriptlang="applescript">------------------------REP-CYCLES-------------------------
 
-- repCycles :: String -> [String]
Line 540:
on unlines(xs)
|λ|(xs) of intercalate(linefeed)
end unlines</langsyntaxhighlight>
{{Out}}
<pre>Longest cycle:
Line 558:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">repeated?: function [text][
loop ((size text)/2)..0 'x [
if prefix? text slice text x (size text)-1 [
Line 588:
else ->
print [str "->" rep "( length:" size rep ")"]
]</langsyntaxhighlight>
 
{{out}}
Line 606:
=={{header|AutoHotkey}}==
{{works with|AutoHotkey 1.1}}
<langsyntaxhighlight AutoHotkeylang="autohotkey">In := ["1001110011", "1110111011", "0010010010", "1010101010"
, "1111111111", "0100101101", "0100100", "101", "11", "00", "1"]
for k, v in In
Line 624:
}
return "N/A"
}</langsyntaxhighlight>
{{Out}}
<pre>10011 1001110011
Line 639:
 
=={{header|BaCon}}==
<langsyntaxhighlight lang="freebasic">all$ = "1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1"
 
FOR word$ IN all$
Line 655:
 
PRINT "Not a repeating string: ", word$
NEXT</langsyntaxhighlight>
{{out}}
<pre>
Line 672:
 
=={{header|BASIC}}==
<langsyntaxhighlight BASIClang="basic">10 DEFINT I: DEFSTR S,T
20 READ S: IF S="" THEN END
30 IF LEN(S)<2 THEN 80 ELSE FOR I = LEN(S)\2 TO 1 STEP -1
Line 686:
130 DATA "0100100","101"
140 DATA "11","00"
150 DATA "1",""</langsyntaxhighlight>
{{out}}
<pre>1001110011: 10011
Line 701:
 
=={{header|BCPL}}==
<langsyntaxhighlight lang="bcpl">get "libhdr"
 
// Returns the length of the longest rep-string
Line 750:
rep("00")
rep("1")
$)</langsyntaxhighlight>
{{out}}
<pre>1001110011: 10011
Line 765:
 
=={{header|Bracmat}}==
<langsyntaxhighlight lang="bracmat">( ( rep-string
= reps L x y
. ( reps
Line 800:
& rep-string$00
& rep-string$1
);</langsyntaxhighlight>
{{Out}}
<pre>1001110011 : 10011
Line 815:
 
=={{header|BQN}}==
<langsyntaxhighlight lang="bqn"># Returns a list of all rep-strings
Reps←(⌊≠÷2˙)((⊣≥≠¨∘⊢)/⊢)(<≡¨≠⥊¨1↓↑)/1↓↑
 
Line 825:
 
∾´{ 𝕩∾':'∾(•Fmt Reps 𝕩)∾@+10 }¨tests</langsyntaxhighlight>
{{out}}
<pre>"1001110011:⟨ ""10011"" ⟩
Line 843:
===Longest substring ===
 
<syntaxhighlight lang="c">
<lang c>
#include <stdio.h>
#include <string.h>
Line 878:
return 0;
}
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 893:
</pre>
===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.
// size_t is an unsigned integer typ
Line 912:
return 0;
}
</syntaxhighlight>
</lang>
 
=={{header|C++}}==
<langsyntaxhighlight lang="cpp">#include <string>
#include <vector>
#include <boost/regex.hpp>
Line 950:
}
return 0 ;
}</langsyntaxhighlight>
{{out}}
<pre>1001110011 is a rep string! Here is a repeating string:
Line 973:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="lisp">(defn rep-string [s]
(let [len (count s)
first-half (subs s 0 (/ len 2))
test-group (take-while seq (iterate butlast first-half))
test-reptd (map (comp #(take len %) cycle) test-group)]
(some #(= (seq s) %) test-reptd)))</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="lisp">
(def test-strings ["1001110011"
"1110111011"
Line 994:
 
(map (juxt identity rep-string) test-strings)
</syntaxhighlight>
</lang>
<pre>
(["1001110011" true]
Line 1,010:
 
=={{header|CLU}}==
<langsyntaxhighlight lang="clu">rep_strings = iter (s: string) yields (string)
for len: int in int$from_to_by(string$size(s)/2, 1, -1) do
repstr: string := string$substr(s, 1, len)
Line 1,037:
stream$putc(po, '\n')
end
end start_up</langsyntaxhighlight>
{{out}}
<pre>1001110011: <10011>
Line 1,052:
 
=={{header|Common Lisp}}==
<langsyntaxhighlight lang="lisp">
(ql:quickload :alexandria)
(defun rep-stringv (a-str &optional (max-rotation (floor (/ (length a-str) 2))))
Line 1,071:
;; Recurse function reducing length of rotation.
(t (rep-stringv a-str (1- max-rotation)))))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="lisp">
(setf test-strings '("1001110011"
"1110111011"
Line 1,089:
(loop for item in test-strings
collecting (cons item (rep-stringv item)))
</syntaxhighlight>
</lang>
<pre>
(("1001110011" . "10011") ("1110111011" . "1110") ("0010010010" . "001")
Line 1,100:
=={{header|Crystal}}==
{{trans|Go}}
<langsyntaxhighlight lang="ruby">def rep(s : String) : Int32
x = s.size // 2
 
Line 1,131:
 
main
</syntaxhighlight>
</lang>
 
{{out}}
Line 1,150:
=={{header|D}}==
Two different algorithms. The second is from the Raku entry.
<langsyntaxhighlight lang="d">import std.stdio, std.string, std.conv, std.range, std.algorithm,
std.ascii, std.typecons;
 
Line 1,193:
writefln("%(%s %)", w.chunks(r1));
}
}</langsyntaxhighlight>
{{out}}
<pre>10011 10011
Line 1,210:
{{libheader| System.SysUtils}}
{{Trans|Go}}
<syntaxhighlight lang="delphi">
<lang Delphi>
program Rep_string;
 
Line 1,256:
end;
{$IFNDEF UNIX}readln;{$ENDIF}
end.</langsyntaxhighlight>
 
=={{header|Dyalect}}==
{{trans|Go}}
<langsyntaxhighlight lang="dyalect">func rep(s) {
var x = s.Length() / 2
while x > 0 {
Line 1,291:
print("\(s) not a rep-string")
}
}</langsyntaxhighlight>
 
{{out}}
Line 1,308:
 
=={{header|EchoLisp}}==
<langsyntaxhighlight lang="scheme">
(lib 'list) ;; list-rotate
 
Line 1,327:
'too-short-no-rep))
</syntaxhighlight>
</lang>
{{out}}
<langsyntaxhighlight lang="scheme">
(define strings '["1001110011" "1110111011" "0010010010" "1010101010"
"1111111111" "0100101101" "0100100" "101" "11" "00" "1"])
Line 1,350:
"00" "0"
"1" too-short-no-rep
</syntaxhighlight>
</lang>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">defmodule Rep_string do
def find(""), do: IO.puts "String was empty (no repetition)"
def find(str) do
Line 1,381:
1)
 
Enum.each(strs, fn str -> Rep_string.find(str) end)</langsyntaxhighlight>
 
{{out}}
Line 1,427:
 
{{Works with|Office 365 betas 2021}}
<langsyntaxhighlight lang="lisp">REPCYCLES
=LAMBDA(s,
LET(
Line 1,463:
)
)
)</langsyntaxhighlight>
 
And also assuming the following generic bindings in the Name Manager:
 
<langsyntaxhighlight lang="lisp">FILTERP
=LAMBDA(p,
LAMBDA(xs,
Line 1,506:
NA()
)
)</langsyntaxhighlight>
 
{{Out}}
Line 1,568:
 
=={{header|F_Sharp|F#}}==
<langsyntaxhighlight lang="fsharp">let isPrefix p (s : string) = s.StartsWith(p)
let getPrefix n (s : string) = s.Substring(0,n)
 
Line 1,602:
match repPrefixOf s with | None -> s + ": NO" | Some(p) -> s + ": YES ("+ p + ")")
|> List.iter (printfn "%s")
0</langsyntaxhighlight>
{{out}}
<pre>Testing for rep-string (and showing the longest repeated prefix in case):
Line 1,618:
 
=={{header|Factor}}==
<langsyntaxhighlight lang="factor">USING: formatting grouping kernel math math.ranges qw sequences ;
IN: rosetta-code.rep-string
Line 1,632:
0100101101 0100100 101 11 00 1 }
"Shortest cycle:\n\n" printf
[ dup find-rep-string "%-10s -> %s\n" printf ] each</langsyntaxhighlight>
{{out}}
<pre>
Line 1,653:
{{trans|Python}}
{{works with|GNU Forth}}
<langsyntaxhighlight lang="forth">: rep-string ( caddr1 u1 -- caddr2 u2 ) \ u2=0: not a rep-string
2dup dup >r r@ 2/ /string
begin 2over 2over string-prefix? 0= over r@ < and while -1 /string repeat
Line 1,673:
s" 11" test
s" 00" test
s" 1" test ;</langsyntaxhighlight>
{{out}}
<langsyntaxhighlight lang="forth">cr tests
1001110011 has 10011 as repeating substring
1110111011 has 1110 as repeating substring
Line 1,687:
00 has 0 as repeating substring
1 has no repeating substring
ok</langsyntaxhighlight>
 
 
=={{header|FreeBASIC}}==
{{trans|Yabasic}}
<langsyntaxhighlight lang="freebasic">
Data "1001110011", "1110111011", "0010010010", "1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1", ""
 
Line 1,722:
Loop
Sleep
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 1,741:
=={{header|Go}}==
{{trans|Python}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 1,778:
}
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 1,795:
 
=={{header|Haskell}}==
<langsyntaxhighlight lang="haskell">import Data.List (inits, maximumBy)
import Data.Maybe (fromMaybe)
 
Line 1,850:
processIO xs = do
putStr (xs <> ": ")
putStrLn $ process xs</langsyntaxhighlight>
{{Out}}
<pre>1001110011: 10011
Line 1,865:
 
Or, alternatively:
<langsyntaxhighlight lang="haskell">import Data.Bool (bool)
import Data.List (inits, intercalate, transpose)
 
Line 1,917:
<*> ((" -> " <>) . fxShow . f)
)
xs</langsyntaxhighlight>
{{Out}}
<pre>Longest cycles:
Line 1,937:
The following works in both languages.
 
<langsyntaxhighlight lang="unicon">procedure main(A)
every write(s := !A,": ",(repString(s) | "Not a rep string!")\1)
end
Line 1,950:
while *s1 < n do s1 ||:= s2
return s1[1+:n]
end</langsyntaxhighlight>
 
{{Out}}
Line 1,970:
Here's a test:
 
<langsyntaxhighlight lang="j">replengths=: >:@i.@<.@-:@#
rep=: $@] $ $
 
isRepStr=: +./@((] -: rep)"0 1~ replengths)</langsyntaxhighlight>
 
Example use:
 
<langsyntaxhighlight lang="j"> isRepStr '1001110011'
1
Tests=: noun define
Line 1,993:
)
isRepStr;._2 Tests NB. run all tests
1 1 1 1 1 0 1 0 1 1 0</langsyntaxhighlight>
 
We could also report the lengths of the repeated prefix, though this seems more arbitrary:
 
<langsyntaxhighlight lang="j">nRepStr=: 0 -.~ (([ * ] -: rep)"0 1~ replengths)</langsyntaxhighlight>
 
With the above examples:
 
<langsyntaxhighlight lang="j"> ":@nRepStr;._2 Tests
5
4
Line 2,011:
1
1</langsyntaxhighlight>
 
Here, the "non-str-rep" cases are indicated by an empty list of prefix lengths.
 
=={{header|Java}}==
<langsyntaxhighlight lang="java">public class RepString {
 
static final String[] input = {"1001110011", "1110111011", "0010010010",
Line 2,045:
return "none";
}
}</langsyntaxhighlight>
 
{{Out}}
Line 2,063:
=={{header|JavaScript}}==
===ES6===
<langsyntaxhighlight lang="javascript">(() => {
'use strict';
 
Line 2,207:
// MAIN ---
return main();
})();</langsyntaxhighlight>
{{Out}}
<pre>Longest cycles:
Line 2,225:
=={{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.
<langsyntaxhighlight lang="jq">def is_rep_string:
# if self is a rep-string then return [n, prefix]
# where n is the number of full occurrences of prefix
Line 2,245:
| select( .[0] > 1 ) ]
;
</syntaxhighlight>
</lang>
'''Example''':
<langsyntaxhighlight lang="jq">def test:
(
"1001110011",
Line 2,263:
;
 
test</langsyntaxhighlight>
{{Out}}
<langsyntaxhighlight lang="sh"> $ jq -n -c -f rep-string.jq
{"1001110011":[[2,"10011"]]}
{"1110111011":[[2,"1110"]]}
Line 2,276:
{"11":[[2,"1"]]}
{"00":[[2,"0"]]}
{"1":[]}</langsyntaxhighlight>
 
=={{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.
 
<langsyntaxhighlight lang="julia">function repstring(r::AbstractString)
n = length(r)
replst = String[]
Line 2,303:
println("$r is a rep-string of ", join(replst, ", "), ".")
end
end</langsyntaxhighlight>
 
{{out}}
Line 2,320:
 
=={{header|Kotlin}}==
<langsyntaxhighlight lang="scala">// version 1.0.6
 
fun repString(s: String): MutableList<String> {
Line 2,356:
println("${s.padStart(10)} -> ${if (size > 0) reps[size - 1] else "Not a rep-string"}")
}
}</langsyntaxhighlight>
 
{{out}}
Line 2,379:
The heavy lifting:
 
<langsyntaxhighlight lang="lisp">
(defun get-reps (text)
(lists:filtermap
Line 2,393:
(1 head)
(_ '()))))
</syntaxhighlight>
</lang>
 
Displaying the results:
 
<langsyntaxhighlight lang="lisp">
(defun report
((`#(,text ()))
Line 2,408:
(lists:zip data (lists:map #'get-reps/1 data)))
'ok))
</syntaxhighlight>
</lang>
 
Running the code:
Line 2,441:
=={{header|Maple}}==
The built-in <code>Period</code> command in the <code>StringTools</code> package computes the length of the longest repeated prefix.
<langsyntaxhighlight Maplelang="maple">repstr? := proc( s :: string )
local per := StringTools:-Period( s );
if 2 * per <= length( s ) then
Line 2,448:
false, ""
end if
end proc:</langsyntaxhighlight>
For the given set of test strings, we can generate the following output.
<syntaxhighlight lang="maple">
<lang Maple>
> Test := ["1001110011", "1110111011", "0010010010", "1010101010", "1111111111", \
"0100101101", "0100100", "101", "11", "00", "1"]:
Line 2,468:
1 false
 
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
Mathematica is based on pattern-based matching, so this is very easily implemented:
<langsyntaxhighlight Mathematicalang="mathematica">RepStringQ[strin_String]:=StringCases[strin,StartOfString~~Repeated[x__,{2,\[Infinity]}]~~y___~~EndOfString/;StringMatchQ[x,StartOfString~~y~~___]:>x, Overlaps -> All]</langsyntaxhighlight>
Trying it out for the test-strings:
<syntaxhighlight lang="text">str={"1001110011","1110111011","0010010010","1010101010","1111111111","0100101101","0100100","101","11","00","1"};
{#,RepStringQ[#]}&/@str//Grid</langsyntaxhighlight>
{{out}}
<pre>1001110011 {10011}
Line 2,492:
 
=={{header|Modula-2}}==
<langsyntaxhighlight lang="modula2">MODULE RepStrings;
FROM InOut IMPORT Write, WriteString, WriteLn, WriteCard;
FROM Strings IMPORT Copy, Length;
Line 2,552:
rep("00");
rep("1");
END RepStrings.</langsyntaxhighlight>
{{out}}
<pre>1001110011: 10011
Line 2,568:
=={{header|NetRexx}}==
{{trans|REXX}}
<langsyntaxhighlight NetRexxlang="netrexx">/* NetRexx */
options replace format comments java crossref symbols nobinary
 
Line 2,629:
end w_
return
</syntaxhighlight>
</lang>
{{out}}
<pre>
Line 2,647:
=={{header|NGS}}==
{{trans|Python}}
<langsyntaxhighlight NGSlang="ngs">tests = [
'1001110011'
'1110111011'
Line 2,668:
echo("${test} ${if r "has repetition of length ${r} (i.e. ${test[0..r]})" "is not a rep-string"}")
})
}</langsyntaxhighlight>
{{out}}<pre>1001110011 has repetition of length 5 (i.e. 10011)
1110111011 has repetition of length 4 (i.e. 1110)
Line 2,683:
=={{header|Nim}}==
{{trans|Python}}
<langsyntaxhighlight lang="nim">import strutils
 
proc isRepeated(text: string): int =
Line 2,704:
let ln = isRepeated(line)
echo "'", line, "' has a repetition length of ", ln, " i.e ",
(if ln > 0: "'" & line[0 ..< ln] & "'" else: "*not* a rep-string")</langsyntaxhighlight>
{{Out}}
<pre>'1001110011' has a repetition length of 5 i.e '10011'
Line 2,719:
 
=={{header|Objeck}}==
<langsyntaxhighlight lang="objeck">class RepString {
function : Main(args : String[]) ~ Nil {
strings := ["1001110011", "1110111011", "0010010010", "1111111111",
Line 2,780:
return true;
}
}</langsyntaxhighlight>
 
Output:
Line 2,800:
Returns null if no rep string.
 
<langsyntaxhighlight lang="oforth">: repString(s)
| sz i |
s size dup ->sz 2 / 1 -1 step: i [
s left(sz i - ) s right(sz i -) == ifTrue: [ s left(i) return ]
]
null ;</langsyntaxhighlight>
 
{{Out}}
Line 2,815:
 
=={{header|PARI/GP}}==
<langsyntaxhighlight 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"];
for(i=1,#v,print(v[i]" "rep(Vec(v[i]))))</langsyntaxhighlight>
{{out}}
<pre>1001110011 5
Line 2,832:
 
=={{header|Perl}}==
<langsyntaxhighlight lang="perl">foreach (qw(1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1)) {
print "$_\n";
if (/^(.+)\1+(.*$)(?(?{ substr($1, 0, length $2) eq $2 })|(?!))/) {
Line 2,839:
print " (no repeat)\n\n";
}
}</langsyntaxhighlight>
{{out}}
<pre>1001110011
Line 2,877:
{{trans|Julia}}
Shows all possible repeated sub-strings, as Julia, but in the output style of Perl/Elixir
<!--<langsyntaxhighlight Phixlang="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: #004080;">sequence</span> <span style="color: #000000;">replist</span> <span style="color: #0000FF;">=</span> <span style="color: #0000FF;">{}</span>
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: #008080;">end</span> <span style="color: #008080;">for</span>
<!--</langsyntaxhighlight>-->
{{out}}
<pre>
Line 2,959:
 
=={{header|Phixmonti}}==
<langsyntaxhighlight Phixmontilang="phixmonti">include ..\Utilitys.pmt
 
def repstr /# s n -- s #/
Line 2,986:
len for
get repString print nl
endfor</langsyntaxhighlight>
{{out}}
<pre>1001110011: 10011
Line 3,004:
 
=={{header|Picat}}==
<langsyntaxhighlight Picatlang="picat">go =>
Strings = [
"1001110011", % 10011
Line 3,049:
% the last (truncated) slice (or []) must be a substring of Substr
Rest = slice(S,Len2+1,Len),
find(Substr,Rest,1,_). </langsyntaxhighlight>
 
{{out}}
Line 3,072:
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(de repString (Str)
(let Lst (chop Str)
(for (N (/ (length Lst) 2) (gt0 N) (dec N))
Line 3,082:
(NIL (head X H))
(NIL Lst T) ) ) )
N ) ) ) )</langsyntaxhighlight>
Test:
<langsyntaxhighlight PicoLisplang="picolisp">(test 5 (repString "1001110011"))
(test 4 (repString "1110111011"))
(test 3 (repString "0010010010"))
Line 3,095:
(test 1 (repString "00"))
(test NIL (repString "1"))
(test NIL (repString "0100101"))</langsyntaxhighlight>
 
=={{header|PL/I}}==
{{Incorrect|PL/I|"0100101101" is not a rep-string.}}
<langsyntaxhighlight PLlang="pl/Ii">rep: procedure options (main); /* 5 May 2015 */
declare s bit (10) varying;
declare (i, k) fixed binary;
Line 3,117:
end;
 
end rep;</langsyntaxhighlight>
{{Out}}
<pre>
Line 3,134:
 
=={{header|PL/M}}==
<langsyntaxhighlight lang="plmi">100H:
 
DECLARE MAX$REP LITERALLY '32';
Line 3,240:
CALL PRINT$STRING( .( CR, LF, '$' ) );
END;
EOF</langsyntaxhighlight>
{{out}}
<pre>
Line 3,260:
Using SWI-Prolog 7 library(func), for some functional syntax.
 
<langsyntaxhighlight Prologlang="prolog">:- use_module(library(func)).
 
%% Implementation logic:
Line 3,291:
report_repstrings :-
Results = maplist(test_for_repstring) $ test_strings(~),
maplist(report_repstring, Results).</langsyntaxhighlight>
 
Output
Line 3,310:
 
=={{header|PureBasic}}==
<langsyntaxhighlight lang="purebasic">a$="1001110011"+#CRLF$+"1110111011"+#CRLF$+"0010010010"+#CRLF$+"1010101010"+#CRLF$+"1111111111"+#CRLF$+
"0100101101"+#CRLF$+"0100100" +#CRLF$+"101" +#CRLF$+"11" +#CRLF$+"00" +#CRLF$+
"1" +#CRLF$
Line 3,331:
If Not Len(s2$) : PrintN(LSet(s1$,15,Chr(32))+#TAB$+"found nothing.") : EndIf
Next
Input()</langsyntaxhighlight>
{{out}}
<pre>1001110011 longest sequence: 10011
Line 3,348:
 
===Python: Procedural===
<langsyntaxhighlight lang="python">def is_repeated(text):
'check if the first part of the string is repeated throughout the string'
for x in range(len(text)//2, 0, -1):
Line 3,370:
ln = is_repeated(line)
print('%r has a repetition length of %i i.e. %s'
% (line, ln, repr(line[:ln]) if ln else '*not* a rep-string'))</langsyntaxhighlight>
 
{{out}}
Line 3,387:
===Python: Functional===
This returns all the possible repeated substrings
<langsyntaxhighlight lang="python">>>> def reps(text):
return [text[:x] for x in range(1, 1 + len(text) // 2)
if text.startswith(text[x:])]
Line 3,416:
'00' has reps ['0']
'1' has reps []
>>> </langsyntaxhighlight>
 
 
Line 3,422:
{{Trans|Haskell}}
{{Works with|Python|3.7}}
<langsyntaxhighlight lang="python">'''Rep-strings'''
 
from itertools import (accumulate, chain, cycle, islice)
Line 3,518:
# MAIN ---
if __name__ == '__main__':
main()</langsyntaxhighlight>
{{Out}}
<pre>Longest cycles:
Line 3,536:
===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.
<langsyntaxhighlight lang="python">import re
 
matchstr = """\
Line 3,560:
print(re.sub(r'(.+)(\1+)(.*)|(.*)', _checker, txt))
 
checkit(matchstr)</langsyntaxhighlight>
 
{{out}}
Line 3,581:
<code>factors</code> is defined at [http://rosettacode.org/wiki/Factors_of_an_integer#Quackery Factors of an integer].
 
<langsyntaxhighlight Quackerylang="quackery"> [ false swap
dup size 1 > if
[ [] temp put
Line 3,604:
[ say 'There is no rep-string for "'
nip echo$ say '".' ]
cr ] is task ( $ --> )</langsyntaxhighlight>
 
{{out}}
Line 3,621:
 
=={{header|Racket}}==
<langsyntaxhighlight Racketlang="racket">#lang racket
(define (rep-string str)
Line 3,646:
"00"
"1")])
(printf "~a => ~a\n" str (or (rep-string str) "not a rep-string")))</langsyntaxhighlight>
 
{{Out}}
Line 3,663:
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>for <1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1> {
if /^ (.+) $0+: (.*$) <?{ $0.substr(0,$1.chars) eq $1 }> / {
my $rep = $0.chars;
Line 3,671:
say "$_ (no repeat)";
}
}</langsyntaxhighlight>
{{out}}
<pre>10011𝟙𝟘𝟘𝟙𝟙
Line 3,689:
throw away the bits on the right that you want thrown away.)
This produces the same output as above.
<syntaxhighlight lang="raku" perl6line>sub repstr(Str $s) {
my $bits = :2($s);
for reverse 1 .. $s.chars div 2 -> $left {
Line 3,705:
say "$_ (no repeat)";
}
}</langsyntaxhighlight>
 
=={{header|REXX}}==
===version 1===
<langsyntaxhighlight lang="rexx">/* REXX ***************************************************************
* 11.05.2013 Walter Pachl
* 14.05.2013 Walter Pachl extend to show additional rep-strings
Line 3,760:
show_norep:
Say right(sq,12) 'is not a repeated string'
Return</langsyntaxhighlight>
{{Out}}
<pre>'1001110011' has a repetition length of 5 i.e. '10011'
Line 3,781:
===version 2===
A check was added to validate if the strings are binary strings. &nbsp; The binary strings can be of any length.
<langsyntaxhighlight 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. */
if s='' then s=1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1 45
Line 3,796:
if left($$,L)==x then return @rep left($,15) "[length" j']'
end /*j*/ /* [↑] we have found a good repString.*/
return ' (no repetitions)' /*failure to find repString.*/</langsyntaxhighlight>
'''output''' &nbsp; when using the default binary strings for input:
<pre>
Line 3,814:
 
=={{header|Ring}}==
<langsyntaxhighlight lang="ring">
# Project : Rep-string
 
Line 3,845:
ok
next
</syntaxhighlight>
</lang>
Output:
<pre>
Line 3,862:
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">ar = %w(1001110011
1110111011
0010010010
Line 3,877:
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)", ""
end</langsyntaxhighlight>
{{Out|Output (as Perl)}}
<pre>1001110011
Line 3,913:
 
=={{header|Rust}}==
<langsyntaxhighlight Rustlang="rust">fn main() {
let strings = vec![
String::from("1001110011"),
Line 3,993:
}
}
}</langsyntaxhighlight>
 
=={{header|Scala}}==
<langsyntaxhighlight Scalalang="scala">object RepString extends App {
def repsOf(s: String) = s.trim match {
case s if s.length < 2 => Nil
Line 4,022:
val todo = if (args.length > 0) args else tests
todo.map(printReps).foreach(println)
}</langsyntaxhighlight>
{{out}}
<pre>1001110011: YES (10011)
Line 4,037:
 
=={{header|Seed7}}==
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
 
const func integer: repeatLength (in string: text) is func
Line 4,066:
end if;
end for;
end func;</langsyntaxhighlight>
 
{{out}}
Line 4,084:
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">var arr = <1001110011 1110111011
0010010010 1010101010
1111111111 0100101101
Line 4,098:
say "#{n} (no repeat)";
}
}</langsyntaxhighlight>
{{out}}
<pre>10011𝟙𝟘𝟘𝟙𝟙
Line 4,113:
 
=={{header|Snobol4}}==
<langsyntaxhighlight Snobol4lang="snobol4">* Rep-string
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)))
Line 4,124:
:(getstring)
end
</syntaxhighlight>
</lang>
{{out}}
<pre>1001110011 has shortest rep-string value of -> 10011
Line 4,141:
=={{header|Swift}}==
 
<langsyntaxhighlight lang="swift">import Foundation
 
func repString(_ input: String) -> [String] {
Line 4,166:
for testCase in testCases {
print("\(testCase) has reps: \(repString(testCase))")
}</langsyntaxhighlight>
 
{{out}}
Line 4,183:
 
=={{header|Tcl}}==
<langsyntaxhighlight lang="tcl">proc repstring {text} {
set len [string length $text]
for {set i [expr {$len/2}]} {$i > 0} {incr i -1} {
Line 4,193:
}
error "no repetition"
}</langsyntaxhighlight>
Demonstrating:
<langsyntaxhighlight lang="tcl">foreach sample {
"1001110011" "1110111011" "0010010010" "1010101010" "1111111111"
"0100101101" "0100100" "101" "11" "00" "1"
Line 4,206:
puts [format "\"%s\" is not a repeated string" $sample]
}
}</langsyntaxhighlight>
{{out}}
<pre>
Line 4,225:
{{trans|Tcl}}
{{works with|bash}}
<langsyntaxhighlight lang="bash">is_repeated() {
local str=$1 len rep part
for (( len = ${#str} / 2; len > 0; len-- )); do
Line 4,259:
00
1
END_TESTS</langsyntaxhighlight>
 
{{out}}
Line 4,275:
 
=={{header|VBScript}}==
<syntaxhighlight lang="vb">
<lang vb>
Function rep_string(s)
max_len = Int(Len(s)/2)
Line 4,313:
WScript.StdOut.WriteLine
Next
</syntaxhighlight>
</lang>
 
{{Out}}
Line 4,332:
=={{header|Vlang}}==
{{trans|Go}}
<langsyntaxhighlight lang="vlang">fn rep(s string) int {
for x := s.len / 2; x > 0; x-- {
if s.starts_with(s[x..]) {
Line 4,363:
}
}
}</langsyntaxhighlight>
 
{{out}}
Line 4,383:
{{trans|Kotlin}}
{{libheader|Wren-fmt}}
<langsyntaxhighlight lang="ecmascript">import "/fmt" for Fmt
 
var repString = Fn.new { |s|
Line 4,418:
var t = (reps.count > 0) ? reps[-1] : "Not a rep-string"
Fmt.print("$10s -> $s", s, t)
}</langsyntaxhighlight>
 
{{out}}
Line 4,438:
 
=={{header|Yabasic}}==
<langsyntaxhighlight Yabasiclang="yabasic">data "1001110011", "1110111011", "0010010010", "1010101010", "1111111111", "0100101101", "0100100", "101", "11", "00", "1", ""
 
sub rep$(c$, n)
Line 4,464:
print p$, " is composed of ", b$, " repeated"
end if
loop</langsyntaxhighlight>
 
=={{header|zkl}}==
{{trans|D}}
<langsyntaxhighlight lang="zkl">fcn repString(s){
foreach n in ([s.len()/2+1..1,-1]){
Walker.cycle(s[0,n]).pump(s.len(),String) :
Line 4,474:
}
return(False)
}</langsyntaxhighlight>
{{trans|Python}}
<langsyntaxhighlight lang="zkl">fcn repString(s){
foreach n in ([s.len()/2..0,-1]){
if(s.matches(s[n,*] + "*") and n*2<=s.len()) return(n);
}
return(False)
}</langsyntaxhighlight>
<langsyntaxhighlight lang="zkl">words:=("1001110011 1110111011 0010010010 1010101010 "
"1111111111 0100101101 0100100 101 11 00 1").split(" ");
foreach w in (words){
Line 4,488:
else [0..*,n].tweak('wrap(z){ if(s:=w[z,n]) s else Void.Stop })
.walk().concat(" ").println();
}</langsyntaxhighlight>
{{out}}
<pre>
10,333

edits