Detect string repetition in python without regular expression [closed] - python

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Closed 8 years ago.
This question appears to be off-topic because it lacks sufficient information to diagnose the problem. Describe your problem in more detail or include a minimal example in the question itself.
Questions asking for code must demonstrate a minimal understanding of the problem being solved. Include attempted solutions, why they didn't work, and the expected results. See also: Stack Overflow question checklist
Improve this question
I would like a function to detect string reputation, specifically
repetition("abcabcabc")
abc
repetition("aaaaaaa")
a
repetition("ababab")
ab
repetition("abcd")
abcd
I am thinking of doing it in a recursive way but I am confused
Thanks for any help in advance!
I am trying something like
def repetition(r):
if len(r) == 2:
if r[0] == r[1]:
return r[0]
half = len(r) / 2
repetition(r[:half])
if r[:half] == r[half:]:
return r[:half]

There probably is a better way to do this, but my first thought would be this:
def repetition(string):
substring = ''
for character in string:
substring += character
if len(string) % len(substring) == 0:
if (len(string) / len(substring)) * substring == string:
return substring

Using regular expressions:
import re
def repetitions(s):
r = re.compile(r"(.+?)\1+")
for match in r.finditer(s):
if len(match.group()) != len(s):
return s
return match.group(1)
Test:
repetitions("oblabla")
#output: "oblabla"
repetitions("blabla")
#output: "bla"

Related

convert multiple columns vertical text to horizontal [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 1 year ago.
Improve this question
I tried these, but its only for one column.
using notepad++ and python. Any solution that can do for multiple columns of text?
# Input:
tim
oso
d n
a d
y a
y
# Output:
today
is
monday
You can do this fairly easily with a loop, something like:
s = "tim\noso\nd n\na d\ny a\n y"
bits = s.split('\n')
lines = ['' for i in range(0, len(bits[0]))]
for bit in bits:
for i in range(0, len(bit)):
if bit[i] != ' ':
lines[i] += bit[i]
'\n'.join(lines)

What does i for i in s mean? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
def CodelandUsernameValidation(s):
if len(s)>4 and len(s)<25 and s[0].isalpha() and [i for i in s if i.isalnum() or i=="_"]!=[] and s[-1]!="_":
return True
else:
return False
# keep this function call here
print(CodelandUsernameValidation(input()))
It's a list comprehension if you expand, it'll look like this ->
result = []
for i in s: # will fetch element one by one from iterable
if i.isalnum() or i=="_": # checking condition
result.append(i) # if true, then append it to the list
This above code can be rewritten as -
result = [i for i in s if i.isalnum() or i=="_"]
That produces a list of those characters in s that are either alphanumeric or underlines. The code is actually incorrect, because it will pass if ANY of the characters are alphanumeric or underline, whereas the intent surely was that ALL of the characters must be alphanumeric or underline. Here's a better way to write it:
def CodelandUsernameValidation(s):
return 4 < len(s) < 25 and s[0].isalpha() and all(i.isalnum() or i=='_' for i in s) and s[-1] != '_'
print(CodelandUsernameValidation(input()))

shortest repeated substring [PYTHON] [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 6 years ago.
Improve this question
Is there a quick method to find the shortest repeated substring and how many times it occurs? If there is non you only need to return the actual string ( last case ).
>>> repeated('CTCTCTCTCTCTCTCTCTCTCTCT')
('CT', 12)
>>> repeated('GATCGATCGATCGATC')
('GATC', 4)
>>> repeated('GATCGATCGATCGATCG')
('GATCGATCGATCGATCG', 1)
Because some people think it's 'homework' I can show my efforts:
def repeated(sequentie):
string = ''
for i in sequentie:
if i not in string:
string += i
items = sequentie.count(string)
if items * len(string) == len(sequentie):
return (string, items)
else:
return (sequentie, 1)
Your method unfortunately won't work, since it assumes that the repeating substring will have unique characters. This may not be the case:
abaabaabaabaabaaba
You were somewhat on the right track, though. The shortest way that I can think of is to just try and check over and over if some prefix indeed makes up the entire string:
def find_shorted_substring(s):
for i in range(1, len(s) + 1):
substring = s[:i]
repeats = len(s) // len(substring)
if substring * repeats == s:
return (substring, repeats)
It's not very efficient, but it works. There are better ways of doing it.

How to count frequencys in python? [closed]

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I had a file called 'words.txt' which contains things such as. #+=%£%= and i need to go through and count each symbol without allowing any duplicates and then print my answer. for example it should look like this when printed:
# : 1
+ : 1
= : 2
% : 2
£ : 1
I know I need to use a for loop in this and that I need to use a set so It doesnt't allow any duplicates but how would I do it? Thanl you
A set isn't so useful here as it doesn't have a place to store the counts
from collections import Counter
with open("words.txt") as fin:
c = Counter(fin.read())
for item in c.items():
print("{} : {}".format(*item))
Use python dictionary:
symbols = {}
for c in string:
if c not in symbols:
symbols[c] = 0
symbols[c] += 1
Look up dictionary.
Don't want to post more as it would only spoil the exercise.

Please help me clean up my function [closed]

Closed. This question is off-topic. It is not currently accepting answers.
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 9 years ago.
Improve this question
Here is my function's objective:
Define a procedure, find_last, that takes as input two strings, a
search string and a target string,and returns the last position in the
search string where the target string appears, or -1 if there are no
occurrences.
Here is a link to the question.
And here's my function so far:
def find_last(target, search):
find = target.find(search, 0)
if find != -1:
targets = target.find(search, find)
while targets != -1:
find = find + 1
targets = target.find(search, find)
return find - 1
else:
return -1
The code returns the answer I'm looking for with return find - 1, but I know there is a better way to go about doing this.
Any help would be greatly appreciated!
You basically implemented target.rfind(search).
Why dont you use rfind ?
>>> d = "ball foo ball"
>>> f = "ball"
>>> d.rfind(f)
12
So your method becomes a 1 liner :)
def find_last(target, search):
return target.rfind(search)
You can return target.rfind(search) and in the calling method check for -1 and handle appropriately.
Can't resist to quote this awesome piece from XKCD

Categories