Rep-string: Difference between revisions

From Rosetta Code
Content added Content deleted
(Created page with "{{draft text}} Given a series of ones and zeroes in a string, define a repeated string as a string which is created by repeating a substring of the first N characters of the s...")
 
(New draft task and Python solution.)
Line 1: Line 1:
{{draft text}}
{{draft task}}
Given a series of ones and zeroes in a string, define a repeated string as a string which is created by repeating a substring of the first N characters of the string two or more times, truncated to the length of input string.
Given a series of ones and zeroes in a string, define a repeated string as a string which is created by repeating a substring of the ''first'' N characters of the string ''two or more times, truncated on the right to the length of the input string''.


For example, the string '10011001100' is a repeated string as the leftmost four characters of '1001' are repeated three times and truncated on the right to give the original string.
For example, the string <code>'10011001100'</code> is a repeated string as the leftmost four characters of <code>'1001'</code> are repeated three times and truncated on the right to give the original string.


The task is to:
The task is to:
# Write a function/subroutine/method/... that takes a strinmg and returns an indication of if it is a repeated string and the repeated string. (Either the string repeated, or the number of repeated characters would suffice).
* Write a function/subroutine/method/... that takes a string and returns an indication of if it is a repeated string and the repeated string. (Either the string that is repeated, or the number of repeated characters would suffice).
* Use the function to indicate the repeating substring if any, in the following
# EMERGENCY - FLAT TYRE! BACK LATER.
<pre>'1001110011'
'1110111011'
'0010010010'
'1010101010'
'1111111111'
'0100101101'
'0100100'
'101'
'1'</pre>
* Show your output on this page.

=={{header|Python}}==
<lang python>def is_repeated(text):
'check if the first part of the string is repeated throughout the string'
len_text = len(text)
for rep_len in range(len_text // 2, 0, -1):
reps = (len_text + rep_len) // rep_len
if (text[:rep_len] * reps).startswith(text):
return rep_len # equivalent to boolean True as will not be zero
return 0 # equivalent to boolean False

matchstr = """\
1001110011
1110111011
0010010010
1010101010
1111111111
0100101101
0100100
101
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 repeated string'))</lang>

{{out}}
<pre>'1001110011' has a repetition length of 5 i.e. '10011'
'1110111011' has a repetition length of 4 i.e. '1110'
'0010010010' has a repetition length of 3 i.e. '001'
'1010101010' has a repetition length of 4 i.e. '1010'
'1111111111' has a repetition length of 5 i.e. '11111'
'0100101101' has a repetition length of 0 i.e. *not* a repeated string
'0100100' has a repetition length of 3 i.e. '010'
'101' has a repetition length of 0 i.e. *not* a repeated string
'1' has a repetition length of 0 i.e. *not* a repeated string</pre>

Revision as of 19:43, 10 May 2013

Rep-string is a draft programming task. It is not yet considered ready to be promoted as a complete task, for reasons that should be found in its talk page.

Given a series of ones and zeroes in a string, define a repeated string as a string which is created by repeating a substring of the first N characters of the string two or more times, truncated on the right to the length of the input string.

For example, the string '10011001100' is a repeated string as the leftmost four characters of '1001' are repeated three times and truncated on the right to give the original string.

The task is to:

  • Write a function/subroutine/method/... that takes a string and returns an indication of if it is a repeated string and the repeated string. (Either the string that is repeated, or the number of repeated characters would suffice).
  • Use the function to indicate the repeating substring if any, in the following
'1001110011'
'1110111011'
'0010010010'
'1010101010'
'1111111111'
'0100101101'
'0100100'
'101'
'1'
  • Show your output on this page.

Python

<lang python>def is_repeated(text):

   'check if the first part of the string is repeated throughout the string'
   len_text = len(text)
   for rep_len in range(len_text // 2,  0, -1):
       reps = (len_text + rep_len) // rep_len
       if (text[:rep_len] * reps).startswith(text):
           return rep_len  # equivalent to boolean True as will not be zero
   return 0     # equivalent to boolean False

matchstr = """\ 1001110011 1110111011 0010010010 1010101010 1111111111 0100101101 0100100 101 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 repeated string'))</lang>
Output:
'1001110011' has a repetition length of 5 i.e. '10011'
'1110111011' has a repetition length of 4 i.e. '1110'
'0010010010' has a repetition length of 3 i.e. '001'
'1010101010' has a repetition length of 4 i.e. '1010'
'1111111111' has a repetition length of 5 i.e. '11111'
'0100101101' has a repetition length of 0 i.e. *not* a repeated string
'0100100' has a repetition length of 3 i.e. '010'
'101' has a repetition length of 0 i.e. *not* a repeated string
'1' has a repetition length of 0 i.e. *not* a repeated string