Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Semantics In Databases Second International Workshop Dagstuhl Castle Germany January 2001 Revised Papers Lncs 2582

Authors: Leopoldo Bertossi ,Gyula O.H. Katona ,Klaus-Dieter Schewe ,Bernhard Thalheim

2003rd Edition

3540009574, 978-3540009573

More Books

Students also viewed these Databases questions