[Python] Custom String Decompression [LUA] [closed]

Discussão em 'Python' iniciado por Stack, Maio 27, 2025.

  1. Stack

    Stack Membro Participativo

    First time posting anything, I have been working on a way to compress and decompress the amount of characters used for my Rhythm game. I have the compression part done in python which is running fine, but now I have some issues with my LUA code...

    local T = "P*-0|A-o|s-P|6A{-P>|^PoP5<op6P*P*3E|Ao|s-P|&A-P|P<-o<|OoIo0i-o|qu-3E|i-o|I-P|wi-P|P-o|Aw-o|"
    local NewT = T
    local OG = 'P*-0|A-o|s-P|6A-P|^P-o|P5-o|p6-o|P*-o|P*-3E|A-o|s-P|&A-P|P-o|O-o|I-o|0i-o|qu-3E|i-o|I-P|wi-P|P-o|Aw-o|'

    -- T is test input text
    -- OG is what it should spit out

    function CON(t1)
    local s = ""
    local last = 0
    for i,v in pairs(t1) do
    if i - last ~= 1 then
    for a = 1,i-last do
    s = s..' '
    end
    end
    s = s..v
    last = i
    end
    return s
    end


    local CompKey = "<,>.{[}" --Compkey + 1 for the iterations. "." would be the key it is + 5?
    local Decom = {}
    local DecCount = 0
    local Skip = false
    for i = 1,#T do
    print('---')
    NewT = CON(Decom)
    print(NewT)
    local c = string.sub(T,i,i)

    if Skip then
    print('skipping',c)
    print('\n')
    Skip = false
    goto continue
    end
    DecCount = DecCount + 1

    local Pos = string.find(CompKey,c,1,true)
    if Pos then
    local SearchChar = string.sub(T,i+1,i+1)
    local E = string.find(NewT,'%s',1,false)
    local Searchable = string.sub(NewT,1,E)
    local Delta = string.find(Searchable:reverse(),SearchChar,1,true)
    print("\nADDING: ",SearchChar)
    print('Delta: ',Delta)

    for i = 0,Pos do
    print(CON(Decom))
    Decom[DecCount+(Delta*i)] = SearchChar
    end
    Skip = true
    else

    while Decom[DecCount] do
    DecCount = DecCount + 1
    end
    Decom[DecCount] = string.sub(T,i,i)
    end
    ::continue::
    end


    Decom = CON(Decom)
    print("OG: ",OG)
    print("ME: ",Decom)
    print("ST: ",T)

    if OG ~= Decom then
    print("NOPE")
    end


    And here is the python code as well, in case it's something to do with that?

    Song = '''

    abababababababcdscds

    '''

    Song = Song.strip()



    OGSONG = Song

    BASE62_ALPHABET = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    def tobase62(n):
    if n == 0:
    return BASE62_ALPHABET[0]
    base62 = []
    while n > 0:
    n, rem = divmod(n, 62)
    base62.append(BASE62_ALPHABET[rem])
    return ''.join(reversed(base62))


    #The Base 62 compression

    Base62String = ""
    Number = False
    Num = ''
    for i in Song:
    if i == "-":
    Number = True
    Base62String = Base62String+i
    continue
    elif i == "|":
    Base = tobase62(int(Num))
    Base62String = Base62String+Base
    Number = False
    Num = ''

    if Number:
    Num = Num+i
    else:
    Base62String = Base62String+i





    Song = Base62String

    #SavedText{Character}:[[Position,LengthBetweenLast], ...]

    #Use < for 2 and > for 3 etc, once detected a streak of 3+, at the second interval, use the
    #Key + the Character so: >s or <s. Then, decoding, when finding "<s" look for the last s,
    #use the delta between the two of those, making sure to subtract all "<,>." characters
    #and then place the character according to the amount for the key

    SavedText = {}
    Count = 0
    for i in Song:
    SinceLast = 0
    i = str(i)
    if not i in SavedText:
    SavedText = []
    SavedText.append([Count,SinceLast])
    else:
    SinceLast = SavedText[len(SavedText)-1][0]
    SavedText.append([Count,Count-SinceLast])
    Count += 1
    print(SavedText)
    #Saved Text counts will go through every character and mark where it is and the "delta" when it
    #was last used. We can use this in the next section to then look for patterns in the delta
    #and see if there are any repetitions that can be simplified by using a key chart to indicate it will
    #need to be repeated


    Streaks = []
    for i in SavedText:
    Last = ""
    Streak = 1
    PotentialStreak = []
    it = -1
    for a in SavedText:
    it += 1
    print(a)
    if Last != "":
    if a[1] == Last:#Continue Streak
    PotentialStreak.append(a[0])
    Streak += 1
    elif Streak >= 3 and Streak <= 7:#Streak end
    print('HERE',a[1],Streak)
    PotentialStreak.append(i)
    Streaks.append(PotentialStreak)
    Streak = 1
    PotentialStreak = []
    elif Streak > 7:#Streak end 8+
    PotentialStreak.append(i)
    Streaks.append(PotentialStreak)
    Streak = 1
    PotentialStreak = []
    else: #No Streak
    print(Streak)
    Streak = 1
    Last = a[1]
    PotentialStreak = [a[0]]
    if len(SavedText)-1 == it and Streak >= 3: #Just in case it ends while it was on a streak
    if Streak <= 7:
    print('HERE',a[1],Streak)
    PotentialStreak.append(i)
    Streaks.append(PotentialStreak)
    Streak = 1
    PotentialStreak = []
    else:
    PotentialStreak.append(i)
    Streaks.append(PotentialStreak)
    Streak = 1
    PotentialStreak = []
    Streak = 1

    else:
    Last = a[1]
    Streak = 1

    FinalCompression = list(Song)

    CompKey = "<<<,>.{[}"

    for i in Streaks:
    if len(i) <= 8:
    FinalCompression[i[0]] = CompKey[len(i)-2:len(i)-1] + i[len(i)-1]
    else:
    FinalCompression[i[0]] = "]" + str(len(i)-2) + i[len(i)-1]
    Cnt = 0
    for num in i:
    Cnt+=1
    if Cnt > 1 and type(num) == int:
    FinalCompression[num] = ''

    FinalCompression = ''.join(FinalCompression)




    print('RAW: '+str(len(OGSONG)))
    print('Saved with Base62: '+str(len(OGSONG)-len(Song)))
    print('Saved with RepetitionCompression: '+str(len(Song)-len(FinalCompression)))
    print('Total Saved: '+str( len(OGSONG)- len(FinalCompression)))
    print('Final Length: '+str(len(FinalCompression))+"\n")
    print(Song)
    print('----')
    print(FinalCompression)




    I can't seem to really nail down the problem and would greatly like some assistance. I've kind of tried a little bit of everything I think? Switching things around, adding a new string to pull the delta from etc. Also, I understand that the code is probably not the best written...

    And an example output of the ending / results:

    OG: P*-0|A-o|s-P|6A-P|^P-o|P5-o|p6-o|P*-o|P*-3E|A-o|s-P|&A-P|P-o|O-o|I-o|0i-o|qu-3E|i-o|I-P|wi-P|P-o|Aw-o|
    ME: P*-0|A-o|s-P|6A-P|^P-oP|5op6P|-o*P*|3E|A-o|s-P|&A-P|P-o|O-oI|o0i-o|qu-3E|i-o|I-P|wi-P|P-o|Aw-o|
    ST: P*-0|A-o|s-P|6A{-P>|^PoP5<op6P*P*3E|Ao|s-P|&A-P|P<-o<|OoIo0i-o|qu-3E|i-o|I-P|wi-P|P-o|Aw-o|
    NOPE```

    Continue reading...

Compartilhe esta Página