Click here to Skip to main content
14,924,336 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
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:
Python
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:
Python
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:
Python
<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.
Posted
Updated 25-Mar-21 5:25am
v3
Comments
Richard MacCutchan 25-Mar-21 10:42am
   
Here is a suggestion: Either use the debugger to step through your code and work out what is going on, or add some print statements to check the intermediate values of your data.
The Keepers Of Tomarrow 25-Mar-21 10:49am
   
Thank you for the input, i have edited the code now please take a second look at the question.
Richard MacCutchan 25-Mar-21 11:26am
   
See my solution below

1 solution

Try this:
Python
def compress():
    tempData = inputData.split(' ')
    multiplier = 1
    for i in range(len(tempData) - 1):
        if tempData[i] == tempData[i + 1]:
            multiplier += 1
        else:
            if multiplier > 1:
                applyMultiplier(multiplier)
            multiplier = 1
    if multiplier > 1:
        applyMultiplier(multiplier)
    print("compressed...")
   
Comments
The Keepers Of Tomarrow 25-Mar-21 13:35pm
   
I love you dude, thank you xD
Richard MacCutchan 25-Mar-21 17:03pm
   
Ha ha, thank you.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)




CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900