def num_to_digit_rec(num, base): \"\"\" Return a list of digits for num with given...

50.1K

Verified Solution

Question

Programming

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 Truedef 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()
  • num_to_digit_rec(num, base): Given a num andits base, convert the num to a list of digits in its baserepresentation. 0 is converted to an empty list. You mustimplement this function using recursion. Do not use anyloops/iterations.
  • digit_sum(num, base), Given a num and itsbase, find out sum of all of its digits in its base representation.You should use the num_to_digit_rec() function'sresult which is a list of all the digits. This function is an easyone-liner.
  • digit_str(num, base): Given a num and itsbase, convert the num to a string representation for that base,using Capital Letters A-Z to represent digit 10-35. This functiononly works for base between [2, 36]. Do make sure that 0 isconverted to \"0\" for a valid base.
  • uses_only(word, letters): The same function inChapter 9. It is a helper function needed to check a stringrepresentation's validity for the digit_to_num() function.
  • digit_to_num(rep, base): Given a stringrepresentation rep and its base, convert the string representationwith a base to a number. For example digit_to_num(\"1001\", 2) = 9,digit_to_num(\"z\", 36) = 35. You may use uses_only() function tocheck that the string representation only uses valid digits for thegiven base.

Example:

Enter a num: 0Enter a base: 5Digit list is []Digit sum is 0String Rep is 0Enter a string rep of a num with base 5: 3The number is 3

Answer & Explanation Solved by verified expert
3.9 Ratings (438 Votes)
Short SummaryProvided the source code and sample    See Answer
Get Answers to Unlimited Questions

Join us to gain access to millions of questions and expert answers. Enjoy exclusive benefits tailored just for you!

Membership Benefits:
  • Unlimited Question Access with detailed Answers
  • Zin AI - 3 Million Words
  • 10 Dall-E 3 Images
  • 20 Plot Generations
  • Conversation with Dialogue Memory
  • No Ads, Ever!
  • Access to Our Best AI Platform: Flex AI - Your personal assistant for all your inquiries!
Become a Member

Other questions asked by students