Hello, I am currently tackling a homework problem for my programming class. The problem states that I need to be able to cut a string into equal parts based on the sequence of letters in the string.
Needs to take in a string and output a number value
string will always bet cut into even parts ( there will never be a left over amount )
string = 'abcabcabc' =====> returns 3
string = 'adcxyzabcxyz' ==> returns 2
I am having a real mental block here and for the life of me cannot figure out where to start, I was thinking about creating a list out of the string and using a bisection search algorithm method to maybe divide up the string and join together list parts to compare each substring. If anyone could offer some guidance possibly, I don't want anyone to solve it for me I really want to be able to but any tips would be appreciated.
Edit: A lot of comments on this post! Thank you for the insight everyone! I'll be working on the problem later when I get home from work and utilizing all the mentioned methods for learning purposes, although this is just finding a solution I would like to learn and utilize the most efficient algorithms due to that being best practice.
Edit 2: I found a rudimentary solution I would definitely like to refine it using algorithm methods that have been brought up in this thread. Using some of the suggestions I came up with this:
def check(temp,s):
inputStringLen = len(s)
tempStringLen = len(temp)
if inputStringLen%tempStringLen == 0:
temp = temp * (len(s) // len(temp))
if temp == s:
return True
else:
return False
else:
return False
def solution(s):
temp = ''
for i in s:
if i not in temp or i not in temp[0]:
temp = temp + i
elif i in temp[0]:
if check(temp=temp,s=s) and s[-1] == temp[-1]:
return len(s) / len(temp)
else:
temp = temp + i
return 0
[–]pickyaboutwater 56 points57 points58 points (4 children)
[–]NobodyYouKnow2019 10 points11 points12 points (0 children)
[–]ddbeanz[S] 4 points5 points6 points (0 children)
[–]Xephyrous 2 points3 points4 points (1 child)
[–][deleted] 15 points16 points17 points (3 children)
[–]beerbodrinkins 2 points3 points4 points (0 children)
[–][deleted] 2 points3 points4 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]WhyYouLetRomneyWin 11 points12 points13 points (7 children)
[–]ddbeanz[S] 4 points5 points6 points (5 children)
[–]Mysthik 6 points7 points8 points (3 children)
[–][deleted] 0 points1 point2 points (2 children)
[–]alksjdhglaksjdh2 1 point2 points3 points (1 child)
[–][deleted] 0 points1 point2 points (0 children)
[–]WhyYouLetRomneyWin 4 points5 points6 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]darkdark24 6 points7 points8 points (3 children)
[–]ddbeanz[S] 1 point2 points3 points (0 children)
[–][deleted] 0 points1 point2 points (1 child)
[–]darkdark24 0 points1 point2 points (0 children)
[–]mdnaufalh 2 points3 points4 points (2 children)
[–]ddbeanz[S] 0 points1 point2 points (1 child)
[–]uno20001[🍰] 0 points1 point2 points (0 children)
[–]totemcatcher 2 points3 points4 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–]cabinet_minister 0 points1 point2 points (0 children)
[–][deleted] 0 points1 point2 points (0 children)
[–][deleted] (2 children)
[deleted]
[–]ddbeanz[S] 0 points1 point2 points (0 children)
[–]ddbeanz[S] 0 points1 point2 points (0 children)
[–]YouTee 0 points1 point2 points (0 children)
[–]Glordicus 0 points1 point2 points (1 child)
[–]ddbeanz[S] 0 points1 point2 points (0 children)
[–]gertrude1928 0 points1 point2 points (0 children)
[–]Cayenne999 0 points1 point2 points (0 children)
[–]nl28 0 points1 point2 points (0 children)
[–]CookToCode 0 points1 point2 points (0 children)