Starting from:

$29

HW 2: Python


HW 2: Python
Points 35 Submitting a file upload
Problem 1 (5 points) Explain in words the output of the program here
(https://camino.instructure.com/courses/87658/files/6534997?wrap=1)
(https://camino.instructure.com/courses/87658/files/6534997/download?download_frd=1)
You can format this in paragraphs, bullet points, or by adding comments to the code, whatever works
for you.
Please name the file you upload for this problem: HW2P1Lastname.pdf (where Lastname is
your last name)
Problem 2 (5 points) Explain in words the output of the program here
(https://camino.instructure.com/courses/87658/files/6534996?wrap=1)
(https://camino.instructure.com/courses/87658/files/6534996/download?download_frd=1)
Please name the file you upload for this problem: HW2P2Lastname.pdf (where Lastname is
your last name)
Problem 3 (5 points) Explain in words the output of the program here
(https://camino.instructure.com/courses/87658/files/6604148?wrap=1)
(https://camino.instructure.com/courses/87658/files/6604148/download?download_frd=1)
Please name the file you upload for this problem: HW2P3Lastname.pdf (where Lastname is
your last name) If you prefer you can submit 1-3 together as HW2P123Lastname.pdf
Problem 4 (5 points) Please note, we will discuss these topics Monday 1/23
a. Write a line of code that creates a new list by taking a slice of an existing list of length 10 in the
following way. It will take every third element of the list, starting with the second element.
b. Write a list comprehension that creates a list containing only the items in the original list of type int,
but with each int from the original list converted into a string.
Please name the file you upload for this problem: HW2P4Lastname.py (where Lastname is
your last name)
Problem 5 (15 points) In this problem, you will write a Python program to solve the 8-Queens
problem. In chess, a queen can move any number of squares horizontally, vertically, or diagonally.
The 8-queens problem is the problem of trying to place eight queens on an empty chessboard in such
a way that no queen can attack any other queen. This problem is intriguing because there is no
4/3/23, 7:18 PM HW 2: Python
https://camino.instructure.com/courses/87658/assignments/619980?module_item_id=1469265 2/2
efficient algorithm known for solving the general problem. Rather, the straightforward algorithm of
trying all possible placements is most often used in practice, with the only optimization being that
each queen must be placed in a separate row and column:
1. Starting with the first row, try to place a queen in the current column.
2. If you can safely place a queen in that column, move on to the next column
3. If you are unable to safely place a queen in that column, go back to the previous column, and
move that queen down to the next row where it can safely be placed. Move on to the next
column.
Write a program in python to solve the 8-queens problem. Your program should produce as output an
8X8 diagram of the chessboard, with a 1 indicating the presence of a queen in a square and a 0
indicating the absence of a queen. You need to show the first solution found.
Please name the file you upload for this problem: HW2P5Lastname.py (where Lastname is
your last name)
Hints: You can represent the board as either an 8X8 list or as a one-dimensional list with the ith item
representing the row number of the queen in column i. You can solve this problem recursively or
iteratively, but the recursive solution is usually much easier.
You may wish to use this site: https://www.chess.com/analysis (https://www.chess.com/analysis)
 If you click on the garbage can icon on the righthand side of the screen, toward the middle, that will
clear the board and you can then place pieces as you like. Thanks to the student who suggested the
site.

More products