Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

In the file ascii-art.py. you'll find a Python script that prints pictures to the console window, after open- ing and reading data from a

image

imageimage

In the file ascii-art.py. you'll find a Python script that prints pictures to the console window, after open- ing and reading data from a compressed file. The objective of this question is to modify a couple of the functions in this script. You are invited to read the whole script, but you won't have to make changes to the trickiest parts of it. The script reads a text file containing some data which represents an image. There are several example files given for you to try. The data files are encoded using a technique called run-length encoding. Once the data is read from the file, it is decoded, and turned into a 2-dimensional list of single character strings. For example: | [['+', '+', '+'], ['+', '+', ' -'], [' + ', ' - ', ' - ' ]] Each sub-list represents a row: here we have 3 rows and three columns. We call this list of lists an image. When displayed to the console, each character is displayed on a line, row by row: +++ ++- +-- This example is not very artistic, but try out some of the examples! There are 2 functions in the script we will be studying, and they have the following behaviour flip_updown (image) - Its input image is a list-of-lists. - Creates a new image containing all the rows of the original input image, but in reverse order; this corresponds to a flip across a horizontal axis. - Returns the new image. - Does not modify the original image. flip_leftright (image) - Its input image is a list-of-lists. - Creates a new image containing all the rows of the original input image in the same order, but each row in the new image is reversed; this corresponds to a flip across a vertical axis. - Returns the new image. - Does not modify the original image. We will not be concerned with the other functions in the script. You can read the code, but don't be concerned if the details are a bit tricky. Your task is to rewrite these two functions and change their behaviour to the following: flip_updown (image) - Its input is a list-of-lists. - Modifies the image so the rows are in reverse order; this corresponds to a flip across a horizontal axis. - Returns None flip_leftright () - Its input is a list-of-lists. - Modifies the image so the columns are in reverse order; this corresponds to a flip across a vertical axis. - Returns None You'll have to adapt the script that calls these two functions (near the bottom of the file). This is part of the exercise. When you are done, your scripts produce the same images as the original script in the same order. What to hand in: Hand in your working program in a file called a2q2.py. def flip_updown (image): 1 Purpose: 0 Flip the image upside down Preconditions: image: a list of lists containing single-character strings Post-Conditions: None Return: a new list with the rows in reverse order new_image= [] for row in image: new_image= [row] + new_image return new_image def flip_leftright (image): 3 || || || Flip the image left to right image: a list of lists newimage = [] for row in image: new_row = [] for char in row: new_row [char] + new_row newimage.append(new_row) return newimage In the file ascii-art.py. you'll find a Python script that prints pictures to the console window, after open- ing and reading data from a compressed file. The objective of this question is to modify a couple of the functions in this script. You are invited to read the whole script, but you won't have to make changes to the trickiest parts of it. The script reads a text file containing some data which represents an image. There are several example files given for you to try. The data files are encoded using a technique called run-length encoding. Once the data is read from the file, it is decoded, and turned into a 2-dimensional list of single character strings. For example: | [['+', '+', '+'], ['+', '+', ' -'], [' + ', ' - ', ' - ' ]] Each sub-list represents a row: here we have 3 rows and three columns. We call this list of lists an image. When displayed to the console, each character is displayed on a line, row by row: +++ ++- +-- This example is not very artistic, but try out some of the examples! There are 2 functions in the script we will be studying, and they have the following behaviour flip_updown (image) - Its input image is a list-of-lists. - Creates a new image containing all the rows of the original input image, but in reverse order; this corresponds to a flip across a horizontal axis. - Returns the new image. - Does not modify the original image. flip_leftright (image) - Its input image is a list-of-lists. - Creates a new image containing all the rows of the original input image in the same order, but each row in the new image is reversed; this corresponds to a flip across a vertical axis. - Returns the new image. - Does not modify the original image. We will not be concerned with the other functions in the script. You can read the code, but don't be concerned if the details are a bit tricky. Your task is to rewrite these two functions and change their behaviour to the following: flip_updown (image) - Its input is a list-of-lists. - Modifies the image so the rows are in reverse order; this corresponds to a flip across a horizontal axis. - Returns None flip_leftright () - Its input is a list-of-lists. - Modifies the image so the columns are in reverse order; this corresponds to a flip across a vertical axis. - Returns None You'll have to adapt the script that calls these two functions (near the bottom of the file). This is part of the exercise. When you are done, your scripts produce the same images as the original script in the same order. What to hand in: Hand in your working program in a file called a2q2.py. def flip_updown (image): 1 Purpose: 0 Flip the image upside down Preconditions: image: a list of lists containing single-character strings Post-Conditions: None Return: a new list with the rows in reverse order new_image= [] for row in image: new_image= [row] + new_image return new_image def flip_leftright (image): 3 || || || Flip the image left to right image: a list of lists newimage = [] for row in image: new_row = [] for char in row: new_row [char] + new_row newimage.append(new_row) return newimage

Step by Step Solution

3.41 Rating (154 Votes )

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_2

Step: 3

blur-text-image_3

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

Molecular Cell Biology

Authors: Harvey Lodish, Arnold Berk, Chris A. Kaiser, Monty Krieger, Anthony Bretscher, Hidde Ploegh, Angelika Amon, Matthew P. Scott

7th edition

1464183393, 1464183392, 978-1429234139

More Books

Students also viewed these Programming questions