Advertisement
Guest User

Possibile Soluzione Problema 1

a guest
Dec 7th, 2019
101
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.15 KB | None | 0 0
  1. def decompression(str):
  2. # @param str : string (compressa)
  3. # @return string (str decompressa)
  4.     res=''
  5.     for i in range(0,len(str)):
  6.         char=str[i]
  7.         if char.isdigit():
  8.            res=res+(int(char)-1)*str[i+1]
  9.         else:
  10.            res=res+char
  11.     return res
  12.    
  13. def compression(str):
  14. # @param str : string (decompressa e composta esclusivamente da caratteri alfabetici)
  15. # @return string (str compressa)
  16.     res=''
  17.     count=1
  18.     for i in range(0,len(str)):
  19.         char=str[i]
  20.         if char==str[min(i+1,len(str)-1)]:
  21.            if i==len(str)-1 and count!=2 and count!=1:
  22.               res=res+repr(count)+char
  23.               return res
  24.            elif i==len(str)-1 and count==2:
  25.               res=res+count*char
  26.               return res
  27.            elif i==len(str)-1 and count==1:
  28.               res=res+char
  29.               return res
  30.            count=count+1
  31.         elif char!=str[min(i+1,len(str)-1)] and count!=2 and count!=1:
  32.            res=res+repr(count)+char
  33.            count=1
  34.         elif count==2:
  35.            res=res+count*char
  36.            count=1
  37.         elif count==1:
  38.            res=res+char
  39.     return res
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement