Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I am currently writing a program in python to reverse digits however it does not pass the tests required. What i currently have is shown

I am currently writing a program in python to reverse digits however it does not pass the tests required. What i currently have is shown in the second image.

image text in transcribed

image text in transcribed

image text in transcribed

Unfortunately it wont let me paste an indented version:

def reverse_digits(num): '''The input num should be an integer, if not, raise an exception and return with some instructive message to input int type data. When coded correctly, reverse_digits(12345) and reverse_digits(+12345) should return 54321, and reverse_digits(-12345) should return -54321. That is, besides reversion of digits, the signs need some addition attention. To avoid complications caused by int() removing leading 0's, such as int(0001)=1, you can return the reversed digits as a str, keeping all leading zeros if any. ''' #if type(num) != int: ##this does not work for numpy.int64 etc # raise Exception("Input is not an integer.") try: revnum = '' if num 0): remainder = num%10 num /=10 revnum+=str(remainder) return revnum except: raise Exception("Input is not an integer.")

#################################################################### def verify(num, revnum): #input num is int type, revnum is str type

sn = str(num); sinv = str(revnum) if sn[0]=='-': if sinv[0] != '-': return False if sn[0]=='+': if not sinv[0].isdigit(): return False if sn[0]=='-': sn = sn[1:]; sinv=sinv[1:] elif sn[0]=='+': sn = sn[1:] #use a very elementary method for verification for i in range(len(sn)): if sn[i] != sinv[-(i+1)]: return False

return True

#################################################################### if __name__=='__main__':

import numpy as np DEBUG = True

repeat=100

##if you use python in window OS, and you get complaint of int32 out of bound, ##then you need to reduce the range of numbers to be tested. the following code ##will do this automatically for you try: arynum = np.random.randint(-10**18, 10**18, repeat) except: arynum = np.random.randint(-10**9, 10**9, repeat)

TEST=[] for num in arynum: revnum=reverse_digits(num) if DEBUG: print(' num={} revnum={}'.format(num, revnum)) TEST.append(verify(num, revnum))

if all(TEST): print(" Congratulations, you have passed all {} tests for Problem 4. ".format(repeat)) else: print(" *** {} tests failed. Need more debugging. ".format(len([x for x in TEST if x==False])))

for num in [+1234500007890000, -50329320407933661000000, +66154332782429707000 ]: revnum=reverse_digits(num) print('the reverse of {} is {}'.format(num, revnum)) if verify(num, revnum): print('Passed') else: print('Failed, recheck your code')

image text in transcribed

Prob. 4 Develop a function that reverses digits of any given integer. def reverse.digits (num): The input num should be an integer, if not, raise an exception and return with some instructive message to input int type data When coded correctly reverse-digits(12345) and reverse.digits (+12345) should return 54321 and reverse.digits (-12345) should return-54321 That is,besides reversion of digits, the signs need some addition attention To avoid complication caused by int) removing leading 0's, such as int (0001)-1 you should return the reversed digits as a string 10

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

Oracle9i Database Administrator Implementation And Administration

Authors: Carol McCullough-Dieter

1st Edition

0619159006, 978-0619159009

More Books

Students also viewed these Databases questions

Question

=+free to pirate employees from competitors?

Answered: 1 week ago