I have forgotten the term for a repeating pattern (eee is one with 3 elements, but eae is not one because it has an "a" between it. Anyway, i am trying to make a program that inputs like "38 38" and spits out "38*2", but my solution doesn't work at all. I have been trying this for hours and am very tired which could probably be a factor. Here is the code:
def applyMultiplier(multiplier):
printData = str(multiplier)
print(printData)
def compress():
tempData = inputData.split(' ')
multiplier = 1
for i in range(len(tempData)-1):
if tempData[i] == tempData[i - multiplier]:
multiplier += 1
if i == len(tempData[i])-1:
applyMultiplier(multiplier)
else:
applyMultiplier(multiplier)
multiplier = 0
break
print("compressed...")
print("write something")
inputData = input()
compress()
When i did an immediate print(str(multiplier)) at the end of the for loop before it worked great "ja ja ja" spit out 3 and "da da" spit out 2. Then I tried to apply it to the inputData string but it malfunctioned so i did the solution above, and it will not print the multiplier it just prints ("compressed..."). Why?
Edit: I made the following changes to the code:
def applyMultiplier(multiplier):
printData = str(multiplier)
print(printData)
def compress():
tempData = inputData.split(' ')
multiplier = 1
for i in range(len(tempData)-1):
if tempData[i] == tempData[i - multiplier]:
multiplier += 1
if i == len(tempData[i])-2:
applyMultiplier(multiplier)
else:
applyMultiplier(multiplier)
multiplier = 0
break
print("compressed...")
print("write something")
inputData = input()
compress()
now it almost works, now it just outputs "apa apa apa" as 3. "apa apa" is nothing, "ja ja ja" is 2 and "ja ja" is nothing, obviously it counts the characters instead of occurences of words. What?
Edit: I believe I am quite close to making this work. This is the code now:
<pre>def applyMultiplier(multiplier, inputData):
printData = str(multiplier)
print(inputData + "*" + printData)
def compress():
tempData = inputData.split(' ')
multiplier = 1
for i in range(len(tempData)-1):
if tempData.count(tempData[i]) <= 1:
continue
if tempData[i] == tempData[i - multiplier]:
multiplier += 1
if i == len(tempData)-2:
applyMultiplier(multiplier, tempData[i])
multiplier = 0
else:
applyMultiplier(multiplier, tempData[i])
multiplier = 0
continue
print("compressed...")
print("write something")
inputData = input()
compress()
It almost works "ja ja" prints "ja*2" and "ja ja ja ja" prints "ja*4". PERFECT! Just one problem though "ja ja hi" prints "ja*1", which should not be possible.
What I have tried:
I have tried everything it feels like, I have tried rewriting the code probably 5-6 times with varying results, this is the best results I have gotten but it still doesn't work.