Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

a. extract_num(s, begin, end) The trickiest part of this problem is this: we have found one or more digits in a string, and we need

image text in transcribedimage text in transcribedimage text in transcribed

a. extract_num(s, begin, end) The trickiest part of this problem is this: we have found one or more digits in a string, and we need to extract that number properly or decide to omit it. The extract_num() function is decomposed out to focus on that sub-problem. def extract_num(s, begin, end): Given string s, and "begin" is the index of the first of one or more digits, and "end" is the index one beyond the last digit. Parse out and return the int value of the number, accounting for possible '$' and '*'. Return -1 if the number should be skipped. >>> extract num('xx123$', 2, 5) 321 3$, muld be sible. One test is provided. Add tests so there are at least five tests. Test varying numbers of digits, with and without "' and 'S'. It is easier to build and perfect extract_num() in isolation here vs. in the midst of processing a whole file. b. parse_line(s) Given a string, such as a line from a data file, extract all the numbers as described above from the line and return them in order in a list. If the line contains no numbers, return the empty list. One test is provided. Add tests so there are at least five tests. The helper function extract_num() has its own tests, so here you can focus on pulling out a series of numbers. You may find that you need to go back and debug extract_num() more if a flaw is exposed at this stage. When you are feeling brave, add a test made from the first example: 800!)176b006$ (46$*#632*16$*06$25^ If you have a reverse(s) function from HW4, you can paste it in to this file and use it here as a helper. Later we'll see how to share functions across files, but for now just paste the helper function in. Any helper functions, such as reverse(), should have Pydoc and Doctests. e. solve_mystery(). Parse all the numbers from the given filename. Figure out the width and height of the desired image. The code to create a blank image and loop over it is the same as in week 2 - it's included in the starter file. Starter code in solve mystery(): width = ??? # determine proper width and height values height = ??? image = SimpleImage.blank (width, height) for y in range(image.height): for x in range (image.width): pixel = image.get_pixel(x, y) # use pixel.red etc. in here # This displays image on screen image.show() Edit this code to set the pixel.red etc. of every pixel in the image using the parsed out values, using the provide range/y/x loops. This is a bit of a code puzzle. The range/y/x loops are going over the 2-d image. Each time through that loop, you wan to grab the right parsed value. Think about which value you want the first time the loop runs, the second time, the third time. etc. to work out a pattern. The provided main() is set up to call your solve mystery() function when there is just 1 command line argument, like this: $ python3 parse-mystery.py 480k.txt def extract_num(s, begin, end): Given string s, and "begin" is the index of the first of one or more digits, and "end" is the index one beyond the last digit. Parse out and return the int value of the number, accounting for possible '$' and in. Return -1 if the number should be skipped. >>> extract_num( 'xx123$', 2, 5) 321 >>> # add Doctests here pass defuparse line(s): Given a string s, parse the ints out of it and return them as a list of int values. >>> parse_line( '12x76$') [12, 67] >>> # Add Doctests here pass def parse_file(filename): Given filename, parse out and return a list of all that file's int values. (test provided) >>> parse_file('3lines.txt') [800, 600, 64, 63, 61, 60, 74, 81, 55, 56] def solve_mystery(filename): ""Solve the mystery as described in the handout." # SimpleImage boilerplate provided as a starting point width = 10 # correct values needed here height = 10 image = SimpleImage. blank(width, height), for y in range( image. height): for x in range( image.width): pixel = image.get_pixel(x, y) # use pixel. red etc. in here a. extract_num(s, begin, end) The trickiest part of this problem is this: we have found one or more digits in a string, and we need to extract that number properly or decide to omit it. The extract_num() function is decomposed out to focus on that sub-problem. def extract_num(s, begin, end): Given string s, and "begin" is the index of the first of one or more digits, and "end" is the index one beyond the last digit. Parse out and return the int value of the number, accounting for possible '$' and '*'. Return -1 if the number should be skipped. >>> extract num('xx123$', 2, 5) 321 3$, muld be sible. One test is provided. Add tests so there are at least five tests. Test varying numbers of digits, with and without "' and 'S'. It is easier to build and perfect extract_num() in isolation here vs. in the midst of processing a whole file. b. parse_line(s) Given a string, such as a line from a data file, extract all the numbers as described above from the line and return them in order in a list. If the line contains no numbers, return the empty list. One test is provided. Add tests so there are at least five tests. The helper function extract_num() has its own tests, so here you can focus on pulling out a series of numbers. You may find that you need to go back and debug extract_num() more if a flaw is exposed at this stage. When you are feeling brave, add a test made from the first example: 800!)176b006$ (46$*#632*16$*06$25^ If you have a reverse(s) function from HW4, you can paste it in to this file and use it here as a helper. Later we'll see how to share functions across files, but for now just paste the helper function in. Any helper functions, such as reverse(), should have Pydoc and Doctests. e. solve_mystery(). Parse all the numbers from the given filename. Figure out the width and height of the desired image. The code to create a blank image and loop over it is the same as in week 2 - it's included in the starter file. Starter code in solve mystery(): width = ??? # determine proper width and height values height = ??? image = SimpleImage.blank (width, height) for y in range(image.height): for x in range (image.width): pixel = image.get_pixel(x, y) # use pixel.red etc. in here # This displays image on screen image.show() Edit this code to set the pixel.red etc. of every pixel in the image using the parsed out values, using the provide range/y/x loops. This is a bit of a code puzzle. The range/y/x loops are going over the 2-d image. Each time through that loop, you wan to grab the right parsed value. Think about which value you want the first time the loop runs, the second time, the third time. etc. to work out a pattern. The provided main() is set up to call your solve mystery() function when there is just 1 command line argument, like this: $ python3 parse-mystery.py 480k.txt def extract_num(s, begin, end): Given string s, and "begin" is the index of the first of one or more digits, and "end" is the index one beyond the last digit. Parse out and return the int value of the number, accounting for possible '$' and in. Return -1 if the number should be skipped. >>> extract_num( 'xx123$', 2, 5) 321 >>> # add Doctests here pass defuparse line(s): Given a string s, parse the ints out of it and return them as a list of int values. >>> parse_line( '12x76$') [12, 67] >>> # Add Doctests here pass def parse_file(filename): Given filename, parse out and return a list of all that file's int values. (test provided) >>> parse_file('3lines.txt') [800, 600, 64, 63, 61, 60, 74, 81, 55, 56] def solve_mystery(filename): ""Solve the mystery as described in the handout." # SimpleImage boilerplate provided as a starting point width = 10 # correct values needed here height = 10 image = SimpleImage. blank(width, height), for y in range( image. height): for x in range( image.width): pixel = image.get_pixel(x, y) # use pixel. red etc. in 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

The Structure Of The Relational Database Model

Authors: Jan Paredaens ,Paul De Bra ,Marc Gyssens ,Dirk Van Gucht

1st Edition

3642699588, 978-3642699580

More Books

Students also viewed these Databases questions

Question

What instruments are used in the forward market?

Answered: 1 week ago

Question

What is the orientation toward time?

Answered: 1 week ago

Question

4. How is culture a contested site?

Answered: 1 week ago