Question: This function must have the following header: def conv _ num ( num _ str ) . This function takes in a string and converts

This function must have the following header: def conv_num(num_str). This function takes in a string and converts it into a base 10 number, and returns it. It has the following specifications:
Must be able to handle strings that represent integers
Must be able to handle strings that represent floating-point numbers
Must be able to handle hexadecimal numbers with the prefix 0x
Must be case insensitive (both the prefix and what follows)
Negative numbers are indicated with a - like -0xFF
Only integers are valid inputs for hexadecimal numbers (i.e., no 0xFF.02)
The type returned must match the type sent. For example, if a string of an integer is passed in, conv_num must return an int.
Invalid formats should return None (n.b. this is not a string, but the actual None value), including, but not limited to:
strings with multiple decimal points
strings with alpha that aren't part of a hexadecimal number
strings with a hexadecimal number without the proceeding 0x
values for num_str that are not strings or are empty strings
Some examples:
conv_num('12345') returns 12345
conv_num('-123.45') returns -123.45
conv_num('.45') returns 0.45
conv_num('123.') returns 123.0
conv_num('0xAD4') returns 2772
conv_num('0Xad4') returns 2772
conv_num('0xAZ4') returns None
conv_num('12345A') returns None
conv_num('12.3.45') returns None
You are not permitted to use any of the following:
int()
float()
hex()
eval()
literal_eval()

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!