mardi 29 juin 2021

how would I convert the string into the dictionary and have it check it

how would I add an if statement that check if it's already in the dictionary.

def betterCompression(s):
    
    d = {}
    
    for i in range(0, len(s)):
        result = ""
        count = 1
        result += s[0]
        
        for i in range(len(s)-1):
            if (s[i] == s[i+1]):
                count += 1
            else:
                if(count > 1):
                    result += str(count)
                result += s[i+1]
                count = 1
            
                
        if(count > 1):
            result += str(count)
        return result

Aucun commentaire:

Enregistrer un commentaire