Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the starter code provided on the picture, there are 3 functions (partially written), you must code the empty segments so the three functions work

In the starter code provided on the picture, there are 3 functions (partially written), you must code the empty segments so the three functions work according to the following requirements:

findNextOpr(txt) [30 pts]

  • txt is a non-empty string that contains an arithmetic expression that may include numeric values, five arithmetic operators (+, -, /, * and ^) and extra spaces. An example of such expression is -4.75 * 5 - 2.01 / 3 * 7 + 2 ^ 5

  • It returns the position of the leftmost operator in txt, -1 if no operator was found. findNextOpr( 356) returns -1 findNextOpr( 3 * 4 - 5 ) returns 4

txt

0

1

2

3

4

5

6

7

8

9

10

3

*

4

-

5

isNumber(txt) [20 pts]

  • txt is a non-empty string

  • It returns a boolean value, True if txt is a string convertible to float, otherwise False. Note

    that -25.22222 is a string convertible to float but -22 33 and 122 ; 45 are

    not.

  • An easy way to check if str to float is possible is with a try-except block

    isNumber(Hello) returns False isNumber( 2658 ) returns True isNumber( 2 8 ) returns False

getNextNumber(expr, pos) [50 pts]

returns the operator position in expr[pos:]

getNextNumber

callboolean value

isNumber

  • expr is a non-empty string, pos is an integer greater than or equal to 0

  • It returns 3 values:

    o 1st returned value: the next number (as float) after pos and before the next operator (If there is no single number in it, return None)

    o 2nd returned value: the next operator (If no such operator return None)

    o 3rd returned value: the next operator position (If no such operator return None)

  • The next number must account for negative numbers (see examples in the doctest)

  • The next operator position is its position in expr

  • It MUST use the functions findNextOpr and isNumber effectively, otherwise, no

callcfindNextOpr

returns a

credit is given to this function

getNextNumber( 356, 0) returns (356.0, None, None) getNextNumber( 3 * 4 - 5 , 5) returns (4.0, '-', 8)

pos

1st returned value

3rd returned value

2nd returned value

0

1

2

3

4

5

6

7

8

9

10

11

3

*

4

-

5

getNextNumber( 3 * 4 - 5 , 2) returns (None, '*', 4)

1st returned 2nd returned value value (None)

getNextNumber(" 3 * -4 + 5 ", 5) returns (-4.0, '+', 9)

pos

3rd returned value

0

1

2

3

4

5

6

7

8

9

10

11

3

*

4

-

5

pos

1st returned value (-4.0)

3rd returned value

2nd returned value

0

1

2

3

4

5

6

7

8

9

10

11

12

3

*

-

4

+

5

Testing and Debugging are important!

  • - Check the first two functions individually first

  • - Input some parameters and check the returned value(s)

  • - Use the Python debugger discussed on Module 2 to help you debug your code

  • - Use Pythons unittest module to run extensive cases on your code to ensure full credit

    Deliverables:

    Include all the functions in your script named HW3.py and submit it to the HW3 Gradescope assignment before the due date

image text in transcribedimage text in transcribedimage text in transcribed

def findNextOpr (txt): I1 II TI >>> findNextOpr(' 3*4 -5') >>> findNextOpr ('89 4 5') I T1 1 if not isinstance (txt,str) or len (txt)>> isNumb False er ("- 156.3) 29.99999999 True 5.9999% ') False I T1 1 if not isinstance (txt, str) or len (txt) 0: return "error: isNumber" YOUCODE STARTS HERE if not isinstance (txt, str) or len (txt)0: return "error: isNumber" YOU CODE STARTS HERE # def getNextNumber (expr, pos): In T1 1 >>> getNextNumber '8 + 5-2',0) >>> getNextNumber '8+ 5-2', 4) >>>getNextNumber ('4.5 + 3.15 (-5.0, None, None) >>>getNextNumber ('4.5 3.15 (None, '/', 19) 1-5',20) 5',10) 1 if not isinstance (expr, str) or not isinstance (pos, int) or len (expr)-0 or pos-len (expr): return None, None, "error: getNextNumber" # YOU CODE STARTS HERE

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

Beginning ASP.NET 4.5 Databases

Authors: Sandeep Chanda, Damien Foggon

3rd Edition

1430243805, 978-1430243809

More Books

Students also viewed these Databases questions