Python Forum
Finding repetition in string
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Finding repetition in string
#1
I am a python newbie and I am trying to make a function that returns True if a given str is nonrepetitive and False otherwise.

For example,
>>> nonrepetitive("abcab")
True
>>> nonrepetitive("12341341")
False

Hints I was given was that I could ask if two strings are equal to each other and to use slicing

The following is what I have so far:

def repetitive(s):
     for i in s:
          if i == [i]+1:
               return True
          else:
               false
Please if anyone can help me out that would be appreciated
Reply
#2
There appears to be repetition in both strings.
1st has ab repeated,
2nd has 34 and 41 repeated.
What are the complete rules on what constitutes repetitiveness?
Reply
#3
(Oct-15-2017, 04:26 AM)Larz60+ Wrote: There appears to be repetition in both strings.
1st has ab repeated,
2nd has 34 and 41 repeated.
What are the complete rules on what constitutes repetitiveness?

A nonrepetitive word is a word that does not contain any subword twice in a row. Examples:
ana is nonrepetitive.
borborygmus is not nonrepetitive, since it has subword, bor twice in a row.
abracadabra is nonrepetitive.
repetitive is not nonrepetitive since subword ti is repeated twice in a row.
grammar is not nonrepetitive since subword m is repeated twice in a row.
gaga is not s nonrepetitive since subword ga is repeated twice in a row.
rambunctious is nonrepetitive.
abcab is nonrepetitive.
abacaba is nonrepetitive.
zrtzghtghtghtq is not nonrepetitive since subword ght is repeated twice (in fact three times, but it is enough to find two
repetitions to conclude that the word is not nonrepetitive).
aa is not nonrepetitive since subword a is repeated twice.
zatabracabrac is not nonrepetitive since subword abrac is repeated twice in a row.
Reply
#4
This is going to take a couple loops: you will need to loop through indexes and lengths. For each index (starting character) you need to check each length to see if a subword of the string is repeated twice.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#5
(Oct-15-2017, 06:10 PM)ichabod801 Wrote: This is going to take a couple loops: you will need to loop through indexes and lengths. For each index (starting character) you need to check each length to see if a subword of the string is repeated twice.

How do you check each length of a string?
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Finding how many times substring is in a string using re module ranbarr 4 2,944 May-21-2021, 06:14 PM
Last Post: nilamo
  Imported function causes prompt repetition ward 3 2,558 Feb-06-2019, 02:16 AM
Last Post: perfringo
  Finding and storing all string with character A at middle position Pippi 2 2,672 Jan-20-2019, 08:23 AM
Last Post: Pippi

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020