-1

I am new to python and I want to write a program that determines if a string consists of repetitive characters. The list of strings that I want to test are:

  • Str1 = "AAAA"
  • Str2 = "AGAGAG"
  • Str3 = "AAA"

The pseudo-code that I come up with:

WHEN len(str) % 2 with zero remainder:
- Divide the string into two sub-strings. 
- Then, compare the two sub-strings and check if they have the same characters, or not.
- if the two sub-strings are not the same, divide the string into three sub-strings and compare them to check if repetition occurs.   

I am not sure if this is applicable way to solve the problem, Any ideas how to approach this problem?

Thank you!

MEhsan
  • 2,184
  • 9
  • 27
  • 41
  • Why not just simply iterate through the string and compare the next character with the current one? – metatoaster Sep 16 '15 at 03:43
  • Some options [here](http://stackoverflow.com/q/26703839/198633) – inspectorG4dget Sep 16 '15 at 03:44
  • @metatoaster I am not sure if that will work for all the strings I have listed. Especially for Str2="AGAGAG" because the pattern here is a bit complex. – MEhsan Sep 16 '15 at 03:59
  • If you divide the string into `len(str)` substrings, you will always find that there is one repetition... :) – Karl Knechtel Sep 16 '15 at 04:05
  • 1
    possible duplicate of [How can I tell if a string repeats itself in Python?](http://stackoverflow.com/questions/29481088/how-can-i-tell-if-a-string-repeats-itself-in-python) – tzaman Sep 16 '15 at 04:12
  • 1
    What do you want as a result? – Dan D. Sep 16 '15 at 04:31
  • a print statement that outputs the repeated characters in the string @DanD. – MEhsan Sep 16 '15 at 04:34
  • Please provide what the expected outputs for each input are. You're still being quite vague as to what you are expecting. Your answers are varying widely as a result. – Mike McMahon Sep 16 '15 at 04:42

4 Answers4

1

You could use the Counter library to count the most common occurrences of the characters.

>>> from collections import Counter
>>> s = 'abcaaada'
>>> c = Counter(s)
>>> c.most_common()
[('a', 5), ('c', 1), ('b', 1), ('d', 1)]

To get the single most repetitive (common) character:

>>> c.most_common(1)
[('a', 5)]
Thane Plummer
  • 7,966
  • 3
  • 26
  • 30
0

You could do this using a RegX backreferences.

Jerome Anthony
  • 7,823
  • 2
  • 40
  • 31
0

To find a pattern in Python, you are going to need to use "Regular Expressions". A regular expression is typically written as:

match = re.search(pat, str)

This is usually followed by an if-statement to determine if the search succeeded.

for example this is how you would find the pattern "AAAA" in a string:

import re

string = ' blah blahAAAA this is an example'
match = re.search(r 'AAAA', string)

if match:
    print 'found', match.group()
else :
    print 'did not find'

This returns

found 'AAAA'

Do the same for your other two strings and it will work the same. Regular expressions can do a lot more than just this so work around with them and see what else they can do.

RF1991
  • 2,037
  • 4
  • 8
  • 17
Josh Dombal
  • 135
  • 1
  • 12
0

Assuming you mean the whole string is a repeating pattern, this answer has a good solution:

def principal_period(s):
    i = (s+s).find(s, 1, -1)
    return None if i == -1 else s[:i]
Community
  • 1
  • 1
Redoubts
  • 818
  • 9
  • 25