In: Computer Science
def num_to_digit_rec(num, base): """ Return a list of digits for num with given base; Return an empty list [] if base < 2 or num <= 0 """ # Write your code here return [] def digit_sum(num, base): """ Return the sum of all digits for a num with given base Your implementation should use num_to_digit_rec() function """ # Write your code here return 0 def digit_str(num, base): """ Given a number and a base, for base between [2, 36] inclusive Converts the number to its string representation using digits 0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ to represent digits 0 to 35. Return the string representation of the number Return an empty string '' if base is not between [2, 36] Your implementation should use num_to_digit_rec() function """ digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" # Can not find str representations for base not in [2, 36] if base > 36 or base < 2: return "" # Calculate and return the str representation for num for the given base # Write your code here return "" def uses_only(word, letters): """ Return True if word only uses characters from letters; otherwise return False """ # Write your code here return True def digit_to_num(rep, base): """ Return -1 if base is not between [2,36] inclusive; or if the string rep contains characters not a digit for the base; Return the number represented by the string for the given base otherwise. For example digit_to_num("1001", 2) is 9; digit_to_num("ABC", 16) is 2748. """ # Check if the base is valid if base > 36 or base < 2: return -1 digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ" rep = rep.upper() # Check if the rep only uses proper digits # Write your code here return 0 def run(): num = int(input("Enter a num: ")) base = int(input("Enter a base: ")) print(f"Digit list is {num_to_digit_rec(num, base)}") print(f"Digit sum is {digit_sum(num, base)}") print(f"String Rep is {digit_str(num, base)}") rep = input(f"Enter a string rep of a num with base {base}: ") print(f"The number is {digit_to_num(rep, base)}") if __name__ == "__main__": run()
Example:
Enter a num: 0 Enter a base: 5 Digit list is [] Digit sum is 0 String Rep is 0 Enter a string rep of a num with base 5: 3 The number is 3
Short Summary:
**************Please upvote the answer and appreciate our time.************
Source Code:
def num_to_digit_rec(num, base):
"""
Return a list of digits for num with given base;
Return an empty list [] if base < 2 or num <= 0
"""
# Write your code here
if base < 2 or num <= 0:
return []
if num == 0:
return num_to_digit_rec(num//base, base) + [num%base]
def digit_sum(num, base):
"""
Return the sum of all digits for a num with given base
Your implementation should use num_to_digit_rec() function
"""
numList = num_to_digit_rec(num,base)
total =0
for ele in range(0, len(numList)):
total = total + numList[ele]
# Write your code here
return total
def digit_str(num, base):
"""
Given a number and a base, for base between [2, 36] inclusive
Converts the number to its string representation using digits
0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ
to represent digits 0 to 35.
Return the string representation of the number
Return an empty string '' if base is not between [2, 36]
Your implementation should use num_to_digit_rec() function
"""
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
# Can not find str representations for base not in [2, 36]
if base > 36 or base < 2:
return ""
# Calculate and return the str representation for num for the given
base
# Write your code here
if num < base:
return digits[num]
else:
return digit_str(num//base,base) + digits[num%base]
def uses_only(word, letters):
"""
Return True if word only uses characters from letters;
otherwise return False
"""
# Write your code here
for character in word:
if character == letters:
return True
else:
return False
def digit_to_num(rep, base):
"""
Return -1 if base is not between [2,36] inclusive;
or if the string rep contains characters not a digit for the
base;
Return the number represented by the string for the given base
otherwise.
For example digit_to_num("1001", 2) is 9; digit_to_num("ABC", 16)
is 2748.
"""
# Check if the base is valid
if base > 36 or base < 2:
return -1
digits = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZ"
rep = rep.upper()
# Check if the rep only uses proper digits
# Write your code here
if not uses_only(rep,digits):
return (int(rep,base))
return 0
def run():
num = int(input("Enter a num: "))
base = int(input("Enter a base: "))
print(f"Digit list is {num_to_digit_rec(num, base)}")
print(f"Digit sum is {digit_sum(num, base)}")
print(f"String Rep is {digit_str(num, base)}")
rep = input(f"Enter a string rep of a num with base {base}:
")
print(f"The number is {digit_to_num(rep, base)}")
if __name__ == "__main__":
run()
Refer the following screenshots for code indentation:
Sample Run:
**************************************************************************************
Feel free to rate the answer and comment your questions, if you have any.
Please upvote the answer and appreciate our time.
Happy Studying!!!
**************************************************************************************