Introduction to Programming using Python 1st Edition

Published by Pearson
ISBN 10: 0132747189
ISBN 13: 978-0-13274-718-9

Chapter 15 - Recursion - Programming Exercises - Page 525: 15.22

Answer

code

Work Step by Step

# 15.22 (Hex to decimal) Write a recursive function that parses a hex number as a string # into a decimal integer. The function header is as follows: # def hexToDecimal(hexString): # Write a test program that prompts the user to enter a hex string and displays its # decimal equivalent. hexs = {'A': 10, 'B': 11, 'C': 12, 'D': 13, 'E': 14, 'F': 15} def hexToDecimal(hexString): return hexToDecimalHelper(hexString, 0, len(hexString) - 1) def hexToDecimalHelper(hexString, low, high): if high < low: return 0 else: if hexString[high].upper() in hexs.keys(): dec = hexs[hexString[high].upper()] else: dec = int(hexString[high]) return hexToDecimalHelper(hexString, low, high - 1) * 16 + dec hex = input("Enter a hex number: ").strip() print(hex + " is decimal " + str(hexToDecimal(hex)))
Update this answer!

You can help us out by revising, improving and updating this answer.

Update this answer

After you claim an answer you’ll have 24 hours to send in a draft. An editor will review the submission and either publish your submission or provide feedback.