Update all new Tasks
This commit is contained in:
parent
00a190b0a6
commit
91df62d461
5697 changed files with 93386 additions and 804 deletions
22
Task/Rep-string/00DESCRIPTION
Normal file
22
Task/Rep-string/00DESCRIPTION
Normal file
|
|
@ -0,0 +1,22 @@
|
|||
Given a series of ones and zeroes in a string, define a repeated string or ''rep-string'' as a string which is created by repeating a substring of the ''first'' N characters of the string ''truncated on the right to the length of the input string, and in which the substring appears repeated at least twice in the original''.
|
||||
|
||||
For example, the string <code>'10011001100'</code> is a rep-string as the leftmost four characters of <code>'1001'</code> are repeated three times and truncated on the right to give the original string.
|
||||
|
||||
Note that the requirement for having the repeat occur two or more times means that the repeating unit is ''never'' longer than half the length of the input string.
|
||||
|
||||
The task is to:
|
||||
* Write a function/subroutine/method/... that takes a string and returns an indication of if it is a rep-string and the repeated string. (Either the string that is repeated, or the number of repeated characters would suffice).
|
||||
* There may be multiple sub-strings that make a string a rep-string - in that case an indication of all, or the longest, or the shortest would suffice.
|
||||
* Use the function to indicate the repeating substring if any, in the following:
|
||||
<dl><dd><pre>'1001110011'
|
||||
'1110111011'
|
||||
'0010010010'
|
||||
'1010101010'
|
||||
'1111111111'
|
||||
'0100101101'
|
||||
'0100100'
|
||||
'101'
|
||||
'11'
|
||||
'00'
|
||||
'1'</pre></dl>
|
||||
* Show your output on this page.
|
||||
68
Task/Rep-string/ALGOL-68/rep-string.alg
Normal file
68
Task/Rep-string/ALGOL-68/rep-string.alg
Normal file
|
|
@ -0,0 +1,68 @@
|
|||
# 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:
|
||||
BEGIN
|
||||
|
||||
STRING result := "";
|
||||
|
||||
# ensure the string we are working on has a lower-bound of 1 #
|
||||
STRING str = input[ AT 1 ];
|
||||
|
||||
# work backwards from half the input string looking for a rep-string #
|
||||
FOR string length FROM UPB str OVER 2 BY -1 TO 1
|
||||
WHILE
|
||||
STRING left substring = str[ 1 : string length ];
|
||||
# if the left substgring repeated a sufficient number of times #
|
||||
# (truncated on the right) is equal to the original string, then #
|
||||
# we have found the longest rep-string #
|
||||
STRING repeated string = ( left substring
|
||||
* ( ( UPB str OVER string length ) + 1 )
|
||||
)[ 1 : UPB str ];
|
||||
IF str = repeated string
|
||||
THEN
|
||||
# found a rep-string #
|
||||
result := left substring;
|
||||
FALSE
|
||||
ELSE
|
||||
# not a rep-string, keep looking #
|
||||
TRUE
|
||||
FI
|
||||
DO
|
||||
SKIP
|
||||
OD;
|
||||
|
||||
result
|
||||
END; # longest rep string #
|
||||
|
||||
|
||||
# test the longest rep string procedure #
|
||||
main:
|
||||
(
|
||||
|
||||
[]STRING tests = ( "1001110011"
|
||||
, "1110111011"
|
||||
, "0010010010"
|
||||
, "1010101010"
|
||||
, "1111111111"
|
||||
, "0100101101"
|
||||
, "0100100"
|
||||
, "101"
|
||||
, "11"
|
||||
, "00"
|
||||
, "1"
|
||||
);
|
||||
|
||||
FOR test number FROM LWB tests TO UPB tests
|
||||
DO
|
||||
STRING rep string = longest rep string( tests[ test number ] );
|
||||
print( ( tests[ test number ]
|
||||
, ": "
|
||||
, IF rep string = ""
|
||||
THEN "no rep string"
|
||||
ELSE "longest rep string: """ + rep string + """"
|
||||
FI
|
||||
, newline
|
||||
)
|
||||
)
|
||||
OD
|
||||
)
|
||||
31
Task/Rep-string/Ada/rep-string.ada
Normal file
31
Task/Rep-string/Ada/rep-string.ada
Normal file
|
|
@ -0,0 +1,31 @@
|
|||
with Ada.Command_Line, Ada.Text_IO, Ada.Strings.Fixed;
|
||||
|
||||
procedure Rep_String is
|
||||
|
||||
function Find_Largest_Rep_String(S:String) return String is
|
||||
L: Natural := S'Length;
|
||||
begin
|
||||
for I in reverse 1 .. L/2 loop
|
||||
declare
|
||||
use Ada.Strings.Fixed;
|
||||
T: String := S(S'First .. S'First + I-1); -- the first I characters of S
|
||||
U: String := (1+(L/I)) * T; -- repeat T so often that U'Length >= L
|
||||
begin -- compare first L characers of U with S
|
||||
if U(U'First .. U'First + S'Length -1) = S then
|
||||
return T; -- T is a rep-string
|
||||
end if;
|
||||
end;
|
||||
end loop;
|
||||
return ""; -- no rep string;
|
||||
end Find_Largest_Rep_String;
|
||||
|
||||
X: String := Ada.Command_Line.Argument(1);
|
||||
Y: String := Find_Largest_Rep_String(X);
|
||||
|
||||
begin
|
||||
if Y="" then
|
||||
Ada.Text_IO.Put_Line("No rep-string for """ & X & """");
|
||||
else
|
||||
Ada.Text_IO.Put_Line("Longest rep-string for """& X &""": """& Y &"""");
|
||||
end if;
|
||||
end Rep_String;
|
||||
19
Task/Rep-string/AutoHotkey/rep-string.ahk
Normal file
19
Task/Rep-string/AutoHotkey/rep-string.ahk
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
In := ["1001110011", "1110111011", "0010010010", "1010101010"
|
||||
, "1111111111", "0100101101", "0100100", "101", "11", "00", "1"]
|
||||
for k, v in In
|
||||
Out .= RepString(v) "`t" v "`n"
|
||||
MsgBox, % Out
|
||||
|
||||
RepString(s) {
|
||||
Loop, % StrLen(s) // 2 {
|
||||
i := A_Index
|
||||
Loop, Parse, s
|
||||
{
|
||||
pos := Mod(A_Index, i)
|
||||
if (A_LoopField != SubStr(s, !pos ? i : pos, 1))
|
||||
continue, 2
|
||||
}
|
||||
return SubStr(s, 1, i)
|
||||
}
|
||||
return "N/A"
|
||||
}
|
||||
36
Task/Rep-string/Bracmat/rep-string.bracmat
Normal file
36
Task/Rep-string/Bracmat/rep-string.bracmat
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
( ( rep-string
|
||||
= reps L x y
|
||||
. ( reps
|
||||
= x y z
|
||||
. !arg:(?x.?y)
|
||||
& ( @(!y:!x ?z)&reps$(!x.!z)
|
||||
| @(!x:!y ?)
|
||||
)
|
||||
)
|
||||
& ( :?L
|
||||
& @( !arg
|
||||
: %?x
|
||||
!x
|
||||
( ?y
|
||||
& reps$(!x.!y)
|
||||
& !x !L:?L
|
||||
& ~
|
||||
)
|
||||
)
|
||||
| !L:
|
||||
& out$(str$(!arg " is not a rep-string"))
|
||||
| out$(!arg ":" !L)
|
||||
)
|
||||
)
|
||||
& rep-string$1001110011
|
||||
& rep-string$1110111011
|
||||
& rep-string$0010010010
|
||||
& rep-string$1010101010
|
||||
& rep-string$1111111111
|
||||
& rep-string$0100101101
|
||||
& rep-string$0100100
|
||||
& rep-string$101
|
||||
& rep-string$11
|
||||
& rep-string$00
|
||||
& rep-string$1
|
||||
);
|
||||
36
Task/Rep-string/C++/rep-string.cpp
Normal file
36
Task/Rep-string/C++/rep-string.cpp
Normal file
|
|
@ -0,0 +1,36 @@
|
|||
#include <string>
|
||||
#include <vector>
|
||||
#include <boost/regex.hpp>
|
||||
|
||||
bool is_repstring( const std::string & teststring , std::string & repunit ) {
|
||||
std::string regex( "^(.+)\\1+(.*)$" ) ;
|
||||
boost::regex e ( regex ) ;
|
||||
boost::smatch what ;
|
||||
if ( boost::regex_match( teststring , what , e , boost::match_extra ) ) {
|
||||
std::string firstbracket( what[1 ] ) ;
|
||||
std::string secondbracket( what[ 2 ] ) ;
|
||||
if ( firstbracket.length( ) >= secondbracket.length( ) &&
|
||||
firstbracket.find( secondbracket ) != std::string::npos ) {
|
||||
repunit = firstbracket ;
|
||||
}
|
||||
}
|
||||
return !repunit.empty( ) ;
|
||||
}
|
||||
|
||||
int main( ) {
|
||||
std::vector<std::string> teststrings { "1001110011" , "1110111011" , "0010010010" ,
|
||||
"1010101010" , "1111111111" , "0100101101" , "0100100" , "101" , "11" , "00" , "1" } ;
|
||||
std::string theRep ;
|
||||
for ( std::string myString : teststrings ) {
|
||||
if ( is_repstring( myString , theRep ) ) {
|
||||
std::cout << myString << " is a rep string! Here is a repeating string:\n" ;
|
||||
std::cout << theRep << " " ;
|
||||
}
|
||||
else {
|
||||
std::cout << myString << " is no rep string!" ;
|
||||
}
|
||||
theRep.clear( ) ;
|
||||
std::cout << std::endl ;
|
||||
}
|
||||
return 0 ;
|
||||
}
|
||||
34
Task/Rep-string/C/rep-string.c
Normal file
34
Task/Rep-string/C/rep-string.c
Normal file
|
|
@ -0,0 +1,34 @@
|
|||
#include <stdio.h>
|
||||
#include <string.h>
|
||||
|
||||
int repstr(char *str)
|
||||
{
|
||||
if (!str) return 0;
|
||||
|
||||
size_t sl = strlen(str) / 2;
|
||||
while (sl > 0) {
|
||||
if (strstr(str, str + sl) == str)
|
||||
return sl;
|
||||
--sl;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
char *strs[] = { "1001110011", "1110111011", "0010010010", "1111111111",
|
||||
"0100101101", "0100100", "101", "11", "00", "1" };
|
||||
|
||||
size_t strslen = sizeof(strs) / sizeof(strs[0]);
|
||||
size_t i;
|
||||
for (i = 0; i < strslen; ++i) {
|
||||
int n = repstr(strs[i]);
|
||||
if (n)
|
||||
printf("\"%s\" = rep-string \"%.*s\"\n", strs[i], n, strs[i]);
|
||||
else
|
||||
printf("\"%s\" = not a rep-string\n", strs[i]);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
44
Task/Rep-string/D/rep-string.d
Normal file
44
Task/Rep-string/D/rep-string.d
Normal file
|
|
@ -0,0 +1,44 @@
|
|||
import std.stdio, std.string, std.conv, std.range, std.algorithm,
|
||||
std.ascii, std.typecons;
|
||||
|
||||
Nullable!(size_t, 0) repString1(in string s) pure nothrow @safe @nogc
|
||||
in {
|
||||
//assert(s.all!isASCII);
|
||||
assert(s.representation.all!isASCII);
|
||||
} body {
|
||||
immutable sr = s.representation;
|
||||
foreach_reverse (immutable n; 1 .. sr.length / 2 + 1)
|
||||
if (sr.take(n).cycle.take(sr.length).equal(sr))
|
||||
return typeof(return)(n);
|
||||
return typeof(return)();
|
||||
}
|
||||
|
||||
Nullable!(size_t, 0) repString2(in string s) pure @safe /*@nogc*/
|
||||
in {
|
||||
assert(s.countchars("01") == s.length);
|
||||
} body {
|
||||
immutable bits = s.to!ulong(2);
|
||||
|
||||
foreach_reverse (immutable left; 1 .. s.length / 2 + 1) {
|
||||
immutable right = s.length - left;
|
||||
if ((bits ^ (bits >> left)) == ((bits >> right) << right))
|
||||
return typeof(return)(left);
|
||||
}
|
||||
return typeof(return)();
|
||||
}
|
||||
|
||||
void main() {
|
||||
immutable words = "1001110011 1110111011 0010010010 1010101010
|
||||
1111111111 0100101101 0100100 101 11 00 1".split;
|
||||
|
||||
foreach (immutable w; words) {
|
||||
immutable r1 = w.repString1;
|
||||
//assert(r1 == w.repString2);
|
||||
immutable r2 = w.repString2;
|
||||
assert((r1.isNull && r2.isNull) || r1 == r2);
|
||||
if (r1.isNull)
|
||||
writeln(w, " (no repeat)");
|
||||
else
|
||||
writefln("%(%s %)", w.chunks(r1));
|
||||
}
|
||||
}
|
||||
38
Task/Rep-string/Go/rep-string.go
Normal file
38
Task/Rep-string/Go/rep-string.go
Normal file
|
|
@ -0,0 +1,38 @@
|
|||
package main
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"strings"
|
||||
)
|
||||
|
||||
func rep(s string) int {
|
||||
for x := len(s) / 2; x > 0; x-- {
|
||||
if strings.HasPrefix(s, s[x:]) {
|
||||
return x
|
||||
}
|
||||
}
|
||||
return 0
|
||||
}
|
||||
|
||||
const m = `
|
||||
1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1`
|
||||
|
||||
func main() {
|
||||
for _, s := range strings.Fields(m) {
|
||||
if n := rep(s); n > 0 {
|
||||
fmt.Printf("%q %d rep-string %q\n", s, n, s[:n])
|
||||
} else {
|
||||
fmt.Printf("%q not a rep-string\n", s)
|
||||
}
|
||||
}
|
||||
}
|
||||
45
Task/Rep-string/Haskell/rep-string.hs
Normal file
45
Task/Rep-string/Haskell/rep-string.hs
Normal file
|
|
@ -0,0 +1,45 @@
|
|||
import Data.List (maximumBy, inits)
|
||||
|
||||
repstring :: String -> Maybe String
|
||||
-- empty strings are not rep strings
|
||||
repstring [] = Nothing
|
||||
-- strings with only one character are not rep strings
|
||||
repstring (_:[]) = Nothing
|
||||
repstring xs
|
||||
| any (`notElem` "01") xs = Nothing
|
||||
| otherwise = longest xs
|
||||
where
|
||||
-- length of the original string
|
||||
lxs = length xs
|
||||
-- half that length
|
||||
lq2 = lxs `quot` 2
|
||||
-- make a string of same length using repetitions of a part
|
||||
-- of the original string, and also return the substring used
|
||||
subrepeat x = (x, take lxs $ concat $ repeat x)
|
||||
-- check if a repeated string matches the original string
|
||||
sndValid (_, ys) = ys == xs
|
||||
-- make all possible strings out of repetitions of parts of
|
||||
-- the original string, which have max. length lq2
|
||||
possible = map subrepeat . take lq2 . tail . inits
|
||||
-- filter only valid possibilities, and return the substrings
|
||||
-- used for building them
|
||||
valid = map fst . filter sndValid . possible
|
||||
-- see which string is longer
|
||||
compLength a b = compare (length a) (length b)
|
||||
-- get the longest substring that, repeated, builds a string
|
||||
-- that matches the original string
|
||||
longest ys = case valid ys of
|
||||
[] -> Nothing
|
||||
zs -> Just $ maximumBy compLength zs
|
||||
|
||||
main :: IO ()
|
||||
main = do
|
||||
mapM_ processIO examples
|
||||
where
|
||||
examples = ["1001110011", "1110111011", "0010010010",
|
||||
"1010101010", "1111111111", "0100101101", "0100100",
|
||||
"101", "11", "00", "1"]
|
||||
process = maybe "Not a rep string" id . repstring
|
||||
processIO xs = do
|
||||
putStr (xs ++ ": ")
|
||||
putStrLn $ process xs
|
||||
14
Task/Rep-string/Icon/rep-string.icon
Normal file
14
Task/Rep-string/Icon/rep-string.icon
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
procedure main(A)
|
||||
every write(s := !A,": ",(repString(s) | "Not a rep string!")\1)
|
||||
end
|
||||
|
||||
procedure repString(s)
|
||||
rs := s[1+:*s/2]
|
||||
while (*rs > 0) & (s ~== lrepl(rs,*s,rs)) do rs := rs[1:-1]
|
||||
return (*rs > 0, rs)
|
||||
end
|
||||
|
||||
procedure lrepl(s1,n,s2) # The standard left() procedure won't work.
|
||||
while *s1 < n do s1 ||:= s2
|
||||
return s1[1+:n]
|
||||
end
|
||||
4
Task/Rep-string/J/rep-string-1.j
Normal file
4
Task/Rep-string/J/rep-string-1.j
Normal file
|
|
@ -0,0 +1,4 @@
|
|||
replengths=: >:@i.@<.@-:@#
|
||||
rep=: $@] $ $
|
||||
|
||||
isRepStr=: +./@((] -: rep)"0 1~ replengths)
|
||||
17
Task/Rep-string/J/rep-string-2.j
Normal file
17
Task/Rep-string/J/rep-string-2.j
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
isRepStr '1001110011'
|
||||
1
|
||||
Tests=: noun define
|
||||
1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1
|
||||
)
|
||||
isRepStr;._2 Tests NB. run all tests
|
||||
1 1 1 1 1 0 1 0 1 1 0
|
||||
1
Task/Rep-string/J/rep-string-3.j
Normal file
1
Task/Rep-string/J/rep-string-3.j
Normal file
|
|
@ -0,0 +1 @@
|
|||
nRepStr=: 0 -.~ (([ * ] -: rep)"0 1~ replengths)
|
||||
11
Task/Rep-string/J/rep-string-4.j
Normal file
11
Task/Rep-string/J/rep-string-4.j
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
":@nRepStr;._2 Tests
|
||||
5
|
||||
4
|
||||
3
|
||||
2 4
|
||||
1 2 3 4 5
|
||||
|
||||
3
|
||||
|
||||
1
|
||||
1
|
||||
30
Task/Rep-string/Java/rep-string.java
Normal file
30
Task/Rep-string/Java/rep-string.java
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
public class RepString {
|
||||
|
||||
static final String[] input = {"1001110011", "1110111011", "0010010010",
|
||||
"1010101010", "1111111111", "0100101101", "0100100", "101", "11",
|
||||
"00", "1", "0100101"};
|
||||
|
||||
public static void main(String[] args) {
|
||||
for (String s : input)
|
||||
System.out.printf("%s : %s%n", s, repString(s));
|
||||
}
|
||||
|
||||
static String repString(String s) {
|
||||
int len = s.length();
|
||||
outer:
|
||||
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;
|
||||
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, b).equals(s.substring(b, c)))
|
||||
continue outer;
|
||||
}
|
||||
return s.substring(0, part);
|
||||
}
|
||||
return "none";
|
||||
}
|
||||
}
|
||||
8
Task/Rep-string/Maple/rep-string-1.maple
Normal file
8
Task/Rep-string/Maple/rep-string-1.maple
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
repstr? := proc( s :: string )
|
||||
local per := StringTools:-Period( s );
|
||||
if 2 * per <= length( s ) then
|
||||
true, s[ 1 .. per ]
|
||||
else
|
||||
false, ""
|
||||
end if
|
||||
end proc:
|
||||
16
Task/Rep-string/Maple/rep-string-2.maple
Normal file
16
Task/Rep-string/Maple/rep-string-2.maple
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
> Test := ["1001110011", "1110111011", "0010010010", "1010101010", "1111111111", \
|
||||
"0100101101", "0100100", "101", "11", "00", "1"]:
|
||||
> for s in Test do
|
||||
> printf( "%*s\t%5s %s\n", 3 + max(map(length,Test)), s, repstr?( s ) )
|
||||
> end do:
|
||||
1001110011 true 10011
|
||||
1110111011 true 1110
|
||||
0010010010 true 001
|
||||
1010101010 true 10
|
||||
1111111111 true 1
|
||||
0100101101 false
|
||||
0100100 true 010
|
||||
101 false
|
||||
11 true 1
|
||||
00 true 0
|
||||
1 false
|
||||
1
Task/Rep-string/Mathematica/rep-string-1.math
Normal file
1
Task/Rep-string/Mathematica/rep-string-1.math
Normal file
|
|
@ -0,0 +1 @@
|
|||
RepStringQ[strin_String]:=StringCases[strin,StartOfString~~Repeated[x__,{2,\[Infinity]}]~~y___~~EndOfString/;StringMatchQ[x,StartOfString~~y~~___]:>x, Overlaps -> All]
|
||||
2
Task/Rep-string/Mathematica/rep-string-2.math
Normal file
2
Task/Rep-string/Mathematica/rep-string-2.math
Normal file
|
|
@ -0,0 +1,2 @@
|
|||
str={"1001110011","1110111011","0010010010","1010101010","1111111111","0100101101","0100100","101","11","00","1"};
|
||||
{#,RepStringQ[#]}&/@str//Grid
|
||||
61
Task/Rep-string/NetRexx/rep-string.netrexx
Normal file
61
Task/Rep-string/NetRexx/rep-string.netrexx
Normal file
|
|
@ -0,0 +1,61 @@
|
|||
/* NetRexx */
|
||||
options replace format comments java crossref symbols nobinary
|
||||
|
||||
/* REXX ***************************************************************
|
||||
* 11.05.2013 Walter Pachl
|
||||
**********************************************************************/
|
||||
runSample(arg)
|
||||
return
|
||||
|
||||
/**
|
||||
* Test for rep-strings
|
||||
* @param s_str a string to check for rep-strings
|
||||
* @return Rexx string: boolean indication of reps, length, repeated value
|
||||
*/
|
||||
method repstring(s_str) public static
|
||||
s_str_n = s_str.length()
|
||||
rep_str = ''
|
||||
Loop lx = s_str.length() % 2 to 1 By -1
|
||||
If s_str.substr(lx + 1, lx) = s_str.left(lx) Then Leave lx
|
||||
End lx
|
||||
If lx > 0 Then Do label reps
|
||||
rep_str = s_str.left(lx)
|
||||
Loop ix = 1 By 1
|
||||
If s_str.substr(ix * lx + 1, lx) <> rep_str Then
|
||||
Leave ix
|
||||
End ix
|
||||
If rep_str.copies(s_str_n).left(s_str.length()) <> s_str Then
|
||||
rep_str = ''
|
||||
End reps
|
||||
Return (rep_str.length() > 0) rep_str.length() rep_str
|
||||
|
||||
-- ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
|
||||
method runSample(arg) public static
|
||||
parse arg samples
|
||||
if samples = '' then -
|
||||
samples = -
|
||||
'1001110011' -
|
||||
'1110111011' -
|
||||
'0010010010' -
|
||||
'1010101010' -
|
||||
'1111111111' -
|
||||
'0100101101' -
|
||||
'0100100' -
|
||||
'101' -
|
||||
'11' -
|
||||
'00' -
|
||||
'1'
|
||||
|
||||
loop w_ = 1 to samples.words()
|
||||
in_str = samples.word(w_)
|
||||
parse repstring(in_str) is_rep_str rep_str_len rep_str
|
||||
|
||||
sq = ''''in_str''''
|
||||
tstrlen = sq.length().max(20)
|
||||
sq=sq.right(tstrlen)
|
||||
if is_rep_str then
|
||||
Say sq 'has a repetition length of' rep_str_len "i.e. '"rep_str"'"
|
||||
else
|
||||
Say sq 'is not a repeated string'
|
||||
end w_
|
||||
return
|
||||
3
Task/Rep-string/PARI-GP/rep-string.pari
Normal file
3
Task/Rep-string/PARI-GP/rep-string.pari
Normal file
|
|
@ -0,0 +1,3 @@
|
|||
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]))))
|
||||
19
Task/Rep-string/PL-I/rep-string.pli
Normal file
19
Task/Rep-string/PL-I/rep-string.pli
Normal file
|
|
@ -0,0 +1,19 @@
|
|||
rep: procedure options (main); /* 5 May 2015 */
|
||||
declare s bit (10) varying;
|
||||
declare (i, k) fixed binary;
|
||||
|
||||
main_loop:
|
||||
do s = '1001110011'b, '1110111011'b, '0010010010'b, '1010101010'b,
|
||||
'1111111111'b, '0100101101'b, '0100100'b, '101'b, '11'b, '00'b, '1'b;
|
||||
k = length(s);
|
||||
do i = k/2 to 1 by -1;
|
||||
if substr(s, 1, i) = substr(s, i+1, i) then
|
||||
do;
|
||||
put skip edit (s, ' is a rep-string containing ', substr(s, 1, i) ) (a);
|
||||
iterate main_loop;
|
||||
end;
|
||||
end;
|
||||
put skip edit (s, ' is not a rep-string') (a);
|
||||
end;
|
||||
|
||||
end rep;
|
||||
9
Task/Rep-string/Perl-6/rep-string-1.pl6
Normal file
9
Task/Rep-string/Perl-6/rep-string-1.pl6
Normal file
|
|
@ -0,0 +1,9 @@
|
|||
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;
|
||||
say .substr(0,$rep), .substr($rep,$rep).trans('01' => '𝟘𝟙'), .substr($rep*2);
|
||||
}
|
||||
else {
|
||||
say "$_ (no repeat)";
|
||||
}
|
||||
}
|
||||
17
Task/Rep-string/Perl-6/rep-string-2.pl6
Normal file
17
Task/Rep-string/Perl-6/rep-string-2.pl6
Normal file
|
|
@ -0,0 +1,17 @@
|
|||
sub repstr(Str $s) {
|
||||
my $bits = :2($s);
|
||||
for reverse 1 .. $s.chars div 2 -> $left {
|
||||
my $right = $s.chars - $left;
|
||||
return $left if $bits +^ ($bits +> $left) == $bits +> $right +< $right;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
for '1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1'.words {
|
||||
if repstr $_ -> $rep {
|
||||
say .substr(0,$rep), .substr($rep,$rep).trans('01' => '𝟘𝟙'), .substr($rep*2);
|
||||
}
|
||||
else {
|
||||
say "$_ (no repeat)";
|
||||
}
|
||||
}
|
||||
8
Task/Rep-string/Perl/rep-string.pl
Normal file
8
Task/Rep-string/Perl/rep-string.pl
Normal file
|
|
@ -0,0 +1,8 @@
|
|||
foreach (qw(1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1)) {
|
||||
print "$_\n";
|
||||
if (/^(.+)\1+(.*$)(?(?{ substr($1, 0, length $2) eq $2 })|(?!))/) {
|
||||
print ' ' x length $1, "$1\n\n";
|
||||
} else {
|
||||
print " (no repeat)\n\n";
|
||||
}
|
||||
}
|
||||
32
Task/Rep-string/Prolog/rep-string.pro
Normal file
32
Task/Rep-string/Prolog/rep-string.pro
Normal file
|
|
@ -0,0 +1,32 @@
|
|||
:- use_module(library(func)).
|
||||
|
||||
%% Implementation logic:
|
||||
|
||||
test_for_repstring(String, (String, Result, Reps)) :-
|
||||
( setof(Rep, repstring(String, Rep), Reps)
|
||||
-> Result = 'no repstring'
|
||||
; Result = 'repstrings', Reps = []
|
||||
).
|
||||
|
||||
repstring(Codes, R) :-
|
||||
RepLength = between(1) of (_//2) of length $ Codes,
|
||||
length(R, RepLength),
|
||||
phrase( (rep(R), prefix(~,R)),
|
||||
Codes).
|
||||
|
||||
rep(X) --> X, X.
|
||||
rep(X) --> X, rep(X).
|
||||
|
||||
|
||||
%% Demonstration output:
|
||||
|
||||
test_strings([`1001110011`, `1110111011`, `0010010010`, `1010101010`,
|
||||
`1111111111`, `0100101101`, `0100100`, `101`, `11`, `00`, `1`]).
|
||||
|
||||
report_repstring((S,Result,Reps)):-
|
||||
format('~s -- ~w: ', [S, Result]),
|
||||
foreach(member(R, Reps), format('~s, ', [R])), nl.
|
||||
|
||||
report_repstrings :-
|
||||
Results = maplist(test_for_repstring) $ test_strings(~),
|
||||
maplist(report_repstring, Results).
|
||||
23
Task/Rep-string/Python/rep-string-1.py
Normal file
23
Task/Rep-string/Python/rep-string-1.py
Normal file
|
|
@ -0,0 +1,23 @@
|
|||
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):
|
||||
if text.startswith(text[x:]): return x
|
||||
return 0
|
||||
|
||||
matchstr = """\
|
||||
1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1
|
||||
"""
|
||||
for line in matchstr.split():
|
||||
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'))
|
||||
30
Task/Rep-string/Python/rep-string-2.py
Normal file
30
Task/Rep-string/Python/rep-string-2.py
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
>>> def reps(text):
|
||||
return [text[:x] for x in range(1, 1 + len(text) // 2)
|
||||
if text.startswith(text[x:])]
|
||||
|
||||
>>> matchstr = """\
|
||||
1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1
|
||||
"""
|
||||
>>> print('\n'.join('%r has reps %r' % (line, reps(line)) for line in matchstr.split()))
|
||||
'1001110011' has reps ['10011']
|
||||
'1110111011' has reps ['1110']
|
||||
'0010010010' has reps ['001']
|
||||
'1010101010' has reps ['10', '1010']
|
||||
'1111111111' has reps ['1', '11', '111', '1111', '11111']
|
||||
'0100101101' has reps []
|
||||
'0100100' has reps ['010']
|
||||
'101' has reps []
|
||||
'11' has reps ['1']
|
||||
'00' has reps ['0']
|
||||
'1' has reps []
|
||||
>>>
|
||||
25
Task/Rep-string/Python/rep-string-3.py
Normal file
25
Task/Rep-string/Python/rep-string-3.py
Normal file
|
|
@ -0,0 +1,25 @@
|
|||
import re
|
||||
|
||||
matchstr = """\
|
||||
1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1"""
|
||||
|
||||
def _checker(matchobj):
|
||||
g0, (g1, g2, g3, g4) = matchobj.group(0), matchobj.groups()
|
||||
if not g4 and g1 and g1.startswith(g3):
|
||||
return '%r repeats %r' % (g0, g1)
|
||||
return '%r is not a rep-string' % (g0,)
|
||||
|
||||
def checkit(txt):
|
||||
print(re.sub(r'(.+)(\1+)(.*)|(.*)', _checker, txt))
|
||||
|
||||
checkit(matchstr)
|
||||
1
Task/Rep-string/README
Normal file
1
Task/Rep-string/README
Normal file
|
|
@ -0,0 +1 @@
|
|||
Data source: http://rosettacode.org/wiki/Rep-string
|
||||
52
Task/Rep-string/REXX/rep-string-1.rexx
Normal file
52
Task/Rep-string/REXX/rep-string-1.rexx
Normal file
|
|
@ -0,0 +1,52 @@
|
|||
/* REXX ***************************************************************
|
||||
* 11.05.2013 Walter Pachl
|
||||
* 14.05.2013 Walter Pachl extend to show additional rep-strings
|
||||
**********************************************************************/
|
||||
Call repstring '1001110011'
|
||||
Call repstring '1110111011'
|
||||
Call repstring '0010010010'
|
||||
Call repstring '1010101010'
|
||||
Call repstring '1111111111'
|
||||
Call repstring '0100101101'
|
||||
Call repstring '0100100'
|
||||
Call repstring '101'
|
||||
Call repstring '11'
|
||||
Call repstring '00'
|
||||
Call repstring '1'
|
||||
Exit
|
||||
|
||||
repstring:
|
||||
Parse Arg s
|
||||
sq=''''s''''
|
||||
n=length(s)
|
||||
Do l=length(s)%2 to 1 By -1
|
||||
If substr(s,l+1,l)=left(s,l) Then Leave
|
||||
End
|
||||
If l>0 Then Do
|
||||
rep_str=left(s,l)
|
||||
Do i=1 By 1
|
||||
If substr(s,i*l+1,l)<>rep_str Then
|
||||
Leave
|
||||
End
|
||||
If left(copies(rep_str,n),length(s))=s Then Do
|
||||
Call show_rep rep_str /* show result */
|
||||
Do i=length(rep_str)-1 To 1 By -1 /* look for shorter rep_str-s */
|
||||
rep_str=left(s,i)
|
||||
If left(copies(rep_str,n),length(s))=s Then
|
||||
Call show_rep rep_str
|
||||
End
|
||||
End
|
||||
Else
|
||||
Call show_norep
|
||||
End
|
||||
Else
|
||||
Call show_norep
|
||||
Return
|
||||
|
||||
show_rep:
|
||||
Parse Arg rs
|
||||
Say right(sq,12) 'has a repetition length of' length(rs) 'i.e.' ''''rs''''
|
||||
Return
|
||||
show_norep:
|
||||
Say right(sq,12) 'is not a repeated string'
|
||||
Return
|
||||
13
Task/Rep-string/REXX/rep-string-2.rexx
Normal file
13
Task/Rep-string/REXX/rep-string-2.rexx
Normal file
|
|
@ -0,0 +1,13 @@
|
|||
/*REXX pgm determines if a str is a repStr, returns minimum len. repStr.*/
|
||||
s=1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 11 00 1
|
||||
/* [↑] a list of binary strings.*/
|
||||
do k=1 for words(s); _=word(s,k) /*process all the binary strings.*/
|
||||
say right(_,25) repString(_) /*show the original & the result.*/
|
||||
end /*k*/ /* [↑] "result" may be negatory.*/
|
||||
exit /*stick a fork in it, we're done.*/
|
||||
/*───────────────────────────────────REPSTRING subroutine───────────────*/
|
||||
repString: procedure; parse arg x; L=length(x); r@= ' rep string='
|
||||
do j=1 for L-1 while j<=L%2; p=left(x,j) /*WHILE tests max*/
|
||||
if left(copies(p,L),L)==x then return r@ left(p,15) '[length' j"]"
|
||||
end /*j*/ /* [↑] we have found a repString*/
|
||||
return ' (no repetitions)' /*(sigh)∙∙∙ a failure to find rep*/
|
||||
26
Task/Rep-string/Racket/rep-string.rkt
Normal file
26
Task/Rep-string/Racket/rep-string.rkt
Normal file
|
|
@ -0,0 +1,26 @@
|
|||
#lang racket
|
||||
|
||||
(define (rep-string str)
|
||||
(define len (string-length str))
|
||||
(for/or ([n (in-range 1 len)])
|
||||
(and (let loop ([from n])
|
||||
(or (>= from len)
|
||||
(let ([m (min (- len from) n)])
|
||||
(and (equal? (substring str from (+ from m))
|
||||
(substring str 0 m))
|
||||
(loop (+ n from))))))
|
||||
(<= n (quotient len 2))
|
||||
(substring str 0 n))))
|
||||
|
||||
(for ([str '("1001110011"
|
||||
"1110111011"
|
||||
"0010010010"
|
||||
"1010101010"
|
||||
"1111111111"
|
||||
"0100101101"
|
||||
"0100100"
|
||||
"101"
|
||||
"11"
|
||||
"00"
|
||||
"1")])
|
||||
(printf "~a => ~a\n" str (or (rep-string str) "not a rep-string")))
|
||||
16
Task/Rep-string/Ruby/rep-string.rb
Normal file
16
Task/Rep-string/Ruby/rep-string.rb
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
ar = %w(1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1)
|
||||
|
||||
ar.each do |str|
|
||||
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
|
||||
27
Task/Rep-string/Scala/rep-string.scala
Normal file
27
Task/Rep-string/Scala/rep-string.scala
Normal file
|
|
@ -0,0 +1,27 @@
|
|||
object RepString extends App {
|
||||
def repsOf(s: String) = s.trim match {
|
||||
case s if s.length < 2 => Nil
|
||||
case s => (1 to (s.length/2)).map(s take _)
|
||||
.filter(_ * s.length take s.length equals s)
|
||||
}
|
||||
|
||||
val tests = Array(
|
||||
"1001110011",
|
||||
"1110111011",
|
||||
"0010010010",
|
||||
"1010101010",
|
||||
"1111111111",
|
||||
"0100101101",
|
||||
"0100100",
|
||||
"101",
|
||||
"11",
|
||||
"00",
|
||||
"1"
|
||||
)
|
||||
def printReps(s: String) = repsOf(s) match {
|
||||
case Nil => s+": NO"
|
||||
case r => s+": YES ("+r.mkString(", ")+")"
|
||||
}
|
||||
val todo = if (args.length > 0) args else tests
|
||||
todo.map(printReps).foreach(println)
|
||||
}
|
||||
30
Task/Rep-string/Seed7/rep-string.seed7
Normal file
30
Task/Rep-string/Seed7/rep-string.seed7
Normal file
|
|
@ -0,0 +1,30 @@
|
|||
$ include "seed7_05.s7i";
|
||||
|
||||
const func integer: repeatLength (in string: text) is func
|
||||
result
|
||||
var integer: length is 0;
|
||||
local
|
||||
var integer: pos is 0;
|
||||
begin
|
||||
for pos range succ(length(text) div 2) downto 1 until length <> 0 do
|
||||
if startsWith(text, text[pos ..]) then
|
||||
length := pred(pos);
|
||||
end if;
|
||||
end for;
|
||||
end func;
|
||||
|
||||
const proc: main is func
|
||||
local
|
||||
var string: line is "";
|
||||
var integer: length is 0;
|
||||
begin
|
||||
for line range [] ("1001110011", "1110111011", "0010010010", "1010101010",
|
||||
"1111111111", "0100101101", "0100100", "101", "11", "00", "1") do
|
||||
length := repeatLength(line);
|
||||
if length = 0 then
|
||||
writeln("No rep-string for " <& literal(line));
|
||||
else
|
||||
writeln("Longest rep-string for " <& literal(line) <& ": " <& literal(line[.. length]));
|
||||
end if;
|
||||
end for;
|
||||
end func;
|
||||
11
Task/Rep-string/Tcl/rep-string-1.tcl
Normal file
11
Task/Rep-string/Tcl/rep-string-1.tcl
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
proc repstring {text} {
|
||||
set len [string length $text]
|
||||
for {set i [expr {$len/2}]} {$i > 0} {incr i -1} {
|
||||
set sub [string range $text 0 [expr {$i-1}]]
|
||||
set eq [string repeat $sub [expr {int(ceil($len/double($i)))}]]
|
||||
if {[string equal -length $len $text $eq]} {
|
||||
return $sub
|
||||
}
|
||||
}
|
||||
error "no repetition"
|
||||
}
|
||||
12
Task/Rep-string/Tcl/rep-string-2.tcl
Normal file
12
Task/Rep-string/Tcl/rep-string-2.tcl
Normal file
|
|
@ -0,0 +1,12 @@
|
|||
foreach sample {
|
||||
"1001110011" "1110111011" "0010010010" "1010101010" "1111111111"
|
||||
"0100101101" "0100100" "101" "11" "00" "1"
|
||||
} {
|
||||
if {[catch {
|
||||
set rep [repstring $sample]
|
||||
puts [format "\"%s\" has repetition (length: %d) of \"%s\"" \
|
||||
$sample [string length $rep] $rep]
|
||||
}]} {
|
||||
puts [format "\"%s\" is not a repeated string" $sample]
|
||||
}
|
||||
}
|
||||
35
Task/Rep-string/UNIX-Shell/rep-string.sh
Normal file
35
Task/Rep-string/UNIX-Shell/rep-string.sh
Normal file
|
|
@ -0,0 +1,35 @@
|
|||
is_repeated() {
|
||||
local str=$1 len rep part
|
||||
for (( len = ${#str} / 2; len > 0; len-- )); do
|
||||
part=${str:0:len}
|
||||
rep=""
|
||||
while (( ${#rep} < ${#str} )); do
|
||||
rep+=$part
|
||||
done
|
||||
if [[ ${rep:0:${#str}} == $str ]] && (( $len < ${#str} )); then
|
||||
echo "$part"
|
||||
return 0
|
||||
fi
|
||||
done
|
||||
return 1
|
||||
}
|
||||
|
||||
while read test; do
|
||||
if part=$( is_repeated "$test" ); then
|
||||
echo "$test is composed of $part repeated"
|
||||
else
|
||||
echo "$test is not a repeated string"
|
||||
fi
|
||||
done <<END_TESTS
|
||||
1001110011
|
||||
1110111011
|
||||
0010010010
|
||||
1010101010
|
||||
1111111111
|
||||
0100101101
|
||||
0100100
|
||||
101
|
||||
11
|
||||
00
|
||||
1
|
||||
END_TESTS
|
||||
Loading…
Add table
Add a link
Reference in a new issue