diff --git a/projects/006-run-length-decoding/README.md b/projects/006-run-length-decoding/README.md index 235cead..b67597f 100644 --- a/projects/006-run-length-decoding/README.md +++ b/projects/006-run-length-decoding/README.md @@ -1,22 +1,22 @@ # Run-Length Decoding -Run-length encoding is a simple data compression technique that can be effective -when repeated values occur at adjacent positions within a list. +Run-length encoding is a simple data compression technique that can be effective +when repeated values occur at adjacent positions within a list. -Compression is achieved by replacing groups of repeated values with one copy of the value, -followed by the number of times that it should be repeated. +Compression is achieved by replacing groups of repeated values with one copy of the value, +followed by the number of times that it should be repeated. For example, the list `["A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "A", "B", "B", "B","B","A","A","A","A","A","A","B"]` -would be compressed as `["A",12,"B",4,"A",6,"B",1]`. +would be compressed as `["A",12,"B",4,"A",6,"B",1]`. Decompression is performed by replicating each value in the list the number of times indicated. -Write a recursive function that decompresses a run-length encoded list. -Your recursive function will take a run-length compressed list as its only argument. -It will return the decompressed list as its only result. +Write a recursive function that decompresses a run-length encoded list. +Your recursive function will take a run-length compressed list as its only argument. +It will return the decompressed list as its only result. Create a main program that displays a run-length encoded list and the result of decoding it. - + # Documentation For this project solution you may use: diff --git a/projects/006-run-length-decoding/python/main.py b/projects/006-run-length-decoding/python/main.py index e69de29..ebc036e 100644 --- a/projects/006-run-length-decoding/python/main.py +++ b/projects/006-run-length-decoding/python/main.py @@ -0,0 +1,37 @@ +def decode_compressed_list(comp_ls): + if len(comp_ls)>0: + elem=comp_ls[0] + comp_ls.remove(comp_ls[0]) + num=comp_ls[0] + comp_ls.remove(comp_ls[0]) + return [elem]*num+decode_compressed_list(comp_ls) + else: + return [] + +def main(): + StructureIsWrong=True + while StructureIsWrong: + SyntaxIsWrong=True + while SyntaxIsWrong: + try: + comp_ls=eval(input('Please, enter a string of this type [string,integer,string,integer...]: ')) + SyntaxIsWrong=False + except SyntaxError: + print("Wrong list syntax, retry!") + + StructureIsWrong=False + if len(comp_ls)%2>0: + StructureIsWrong=True + else: + strings=comp_ls[0:2:] + ints=comp_ls[1:2:] + for i,j in zip(strings,ints): + if not (type(i)==str and type(j)==int): + StructureIsWrong=True + + ls=comp_ls.copy() + decomp_list=decode_compressed_list(ls) + print('Compressed list: {}\n\nDecompressed list: {}'.format(comp_ls,decomp_list)) + +if __name__=='__main__': + main() \ No newline at end of file