. Using the variables base=10 and digits=set(range(base)), write a dictionary co
ID: 3584825 • Letter: #
Question
. Using the variables base=10 and digits=set(range(base)), write a dictionary comprehension that maps each integer between zero and nine hundred ninety nine to the list of three digits that represents that integer in base 10. That is, the value should be {0: [0, 0, 0], 1: [0, 0, 1], 2: [0, 0, 2], 3: [0, 0, 3], ..., 10: [0, 1, 0], 11: [0, 1, 1], 12: [0, 1, 2], ..., 999: [9, 9, 9]} 2 Your expression should work for any base. For example, if you instead assign 2 to base and assign {0,1} to digits, the value should be {0: [0, 0, 0], 1: [0, 0, 1], 2: [0, 1, 0], 3: [0, 1, 1], ..., 7: [1, 1, 1]}
Explanation / Answer
'''
pass parameter range and base to function as fun(r,b)
'''
def fun(r,b):
alphabet = range(r)
base = b
return dict((x*base**2+y*base+z,(x,y,z)) for x in alphabet
for y in alphabet
for z in alphabet )
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.