Quiz 5
Prompts the user for a positive 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
...
Computes a derived positive integer that codes the set of running sums ot the members of S when those are listed in increasing order.
$ python3 quiz_5.pyInput a positive integer: 0
The encoded set is: {}
The derived encoded set is: {}
It is encoded by: 0
$ python3 quiz_5.pyInput a positive integer: 1
The encoded set is: {0}
The derived encoded set is: {0}
It is encoded by: 1
$ python3 quiz_5.pyInput a positive integer: 2
The encoded set is: {-1}
The derived encoded set is: {-1}
It is encoded by: 2
$ python3 quiz_5.pyInput a positive integer: 3
The encoded set is: {-1, 0}
The derived encoded set is: {-1}
It is encoded by: 2
$ python3 quiz_5.pyInput a positive integer: 4
The encoded set is: {1}
The derived encoded set is: {1}
It is encoded by: 4
$ python3 quiz_5.pyInput a positive integer: 5
The encoded set is: {0, 1}
The derived encoded set is: {0, 1}
It is encoded by: 5
$ python3 quiz_5.pyInput a positive integer: 6
The encoded set is: {-1, 1}
The derived encoded set is: {-1, 0}
It is encoded by: 3
$ python3 quiz_5.pyInput a positive integer: 7
The encoded set is: {-1, 0, 1}
The derived encoded set is: {-1, 0}
It is encoded by: 3
Date: Trimester 1, 2019.
2 COMP9021 PRINCIPLES OF PROGRAMMING
$ python3 quiz_5.pyInput a positive integer: 123
The encoded set is: {-3, -2, -1, 0, 2, 3}
The derived encoded set is: {-6, -5, -4, -3, -1}
It is encoded by: 2722
$ python3 quiz_5.pyInput a positive integer: 1234
The encoded set is: {-4, -1, 2, 3, 5}
The derived encoded set is: {-5, -4, -3, 0, 5}
It is encoded by: 1697
#我的解法
# Prompts the user for a positive 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 positive integer that codes the set of running sums
# ot the members of S when those are listed in increasing order.
#
# Written by *** and Eric Martin for COMP9021
from itertools import accumulate
import sys
import re
try:
encoded_set = int(input('Input a positive integer: '))
if encoded_set < 0:
raise ValueError
except ValueError:
print('Incorrect input, giving up.')
sys.exit()
#list_g=[]
# POSSIBLY DEFINE OTHER FUNCTIONS
def display_encoded_set(encoded_set):
display_list=[]
a=bin(encoded_set)
encoded_list=list(re.sub('0b', '', a))
#global display_list=[]
for i in range(-1,-len(encoded_list)-1,-1):
if encoded_list[i]=='1':
if i%2!=0:
display_list.append(int(-(i+1)/2))
else:
display_list.append(int(i/2))
display_list.sort()
display_str=', '.join(str(i) for i in display_list)
print('{'+display_str+'}')
#stri=','.join(display_list)
return display_list
# REPLACE pass ABOVE WITH CODE TO PRINT OUT ENCODED SET (WITH print() STATEMENTS)
def code_derived_set(encoded_set):
encoded_running_sum = 0
a=list(accumulate(list_g))
a.sort()
cs=set(a)
#print(cs)
for x in cs:
if x>=0:
y=2*x
encoded_running_sum+=2**y
else:
y=(-2)*x-1
encoded_running_sum+=2**y
return encoded_running_sum
print('The encoded set is: ', end = '')
list_g=display_encoded_set(encoded_set)
encoded_running_sum = code_derived_set(encoded_set)
print('The derived encoded set is: ', end = '')
display_encoded_set(encoded_running_sum)
print(' It is encoded by:', encoded_running_sum)