QUIZ 5 COMP9021 PRINCIPLES OF PROGRAMMING $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 1 In base 2, 1 reads as 1 Incorrect encoding! $...

please pay attention to outputs. do not hard code it. I'll attach reference code as well


QUIZ 5 COMP9021 PRINCIPLES OF PROGRAMMING $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 1 In base 2, 1 reads as 1 Incorrect encoding! $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 12345 In base 2, 12345 reads as 11000000111001 Incorrect encoding! $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 3 In base 2, 3 reads as 11 It encodes: [1] $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: [1] In base 2, [1] reads as [1] It is encoded by 3 $ python3 quiz_5_sol.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 51315663 In base 2, 51315663 reads as 11000011110000001111001111 It encodes: [4891] $ python3 quiz_5_sol.py Input either a strictly positive integer or a nonempty list of strictly positive integers: [4891] In base 2, [4891] reads as [1001100011011] It is encoded by 51315663 $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 424896 In base 2, 424896 reads as 1100111101111000000 It encodes: [11, 24] $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: [11, 24] In base 2, [11, 24] reads as [1011, 11000] It is encoded by 424896 Date: Trimester 2, 2019. 2 COMP9021 PRINCIPLES OF PROGRAMMING $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 857310204 In base 2, 857310204 reads as 110011000110011000001111111100 It encodes: [10, 20, 30] $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: [10, 20, 30] In base 2, [10, 20, 30] reads as [1010, 10100, 11110] It is encoded by 857310204 $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: 13609683913728 In base 2, 13609683913728 reads as 11000110000011000000011000000000110000000000 It encodes: [2, 4, 8, 16, 32] $ python3 quiz_5.py Input either a strictly positive integer or a nonempty list of strictly positive integers: [2, 4, 8, 16, 32] In base 2, [2, 4, 8, 16, 32] reads as [10, 100, 1000, 10000, 100000] It is encoded by 13609683913728 # Prompts the user for a nonnegative integer that codes a set S as follows: # - Bit 0 codes 0 # - Bit 1 codes -1 # - Bit 2 codes 1 # - Bit 3 codes -2 # - Bit 4 codes 2 # - Bit 5 codes -3 # - Bit 6 codes 3 # ... # Computes a derived nonnegative number that codes the set of running sums # of the members of S when those are listed in increasing order. # # Computes the ordered list of members of a coded set. # # Written by Sanchita and Eric Martin for COMP9021 import sys try: encoded_set = int(input('Input a positive integer: ')) if encoded_set < 0:="" raise="" valueerror="" except="" valueerror:="" print('incorrect="" input,="" giving="" up.')="" sys.exit()="" def="" display(l):="" print('{',="" end='' )="" print(',="" '.join(str(e)="" for="" e="" in="" l),="" end='' )="" print('}')="" def="" decode(encoded_set):="" """="">>> decode(0) [] >>> decode(1) [0] >>> decode(100) [-3, 1, 3] >>> decode(123) [-3, -2, -1, 0, 2, 3] >>> decode(1234) [-4, -1, 2, 3, 5] """ list_encoded_set = [] encoded_set = bin(encoded_set)[2:][::-1] for i in range(len(encoded_set)): if int(encoded_set[i]): d, m = divmod(i, 2) if m == 0: list_encoded_set.append(d) else: list_encoded_set.append((d + m) * -1) list_encoded_set=sorted(list_encoded_set) return list_encoded_set def code_derived_set(encoded_set): derived_set = set() running_sum = 0 encoded_running_sum = 0 for i in encoded_set: running_sum = running_sum + i derived_set.add(running_sum) for n in sorted(derived_set): if n < 0: encoded_running_sum = pow(2, abs(n) * 2 - 1) + encoded_running_sum else: encoded_running_sum = pow(2, n * 2) + encoded_running_sum return encoded_running_sum encoded_set = decode(encoded_set) print('the encoded set is: ', end='') display(encoded_set) code_of_derived_set = code_derived_set(encoded_set) print('the derived encoded set is: ', end='') display(decode(code_of_derived_set)) print(' it is encoded by:',code_of_derived_set) 0:="" encoded_running_sum="pow(2," abs(n)="" *="" 2="" -="" 1)="" +="" encoded_running_sum="" else:="" encoded_running_sum="pow(2," n="" *="" 2)="" +="" encoded_running_sum="" return="" encoded_running_sum="" encoded_set="decode(encoded_set)" print('the="" encoded="" set="" is:="" ',="" end='' )="" display(encoded_set)="" code_of_derived_set="code_derived_set(encoded_set)" print('the="" derived="" encoded="" set="" is:="" ',="" end='' )="" display(decode(code_of_derived_set))="" print('="" it="" is="" encoded="">
Jul 12, 2021COMP9021
SOLUTION.PDF

Get Answer To This Question

Related Questions & Answers

More Questions »

Submit New Assignment

Copy and Paste Your Assignment Here