Python代写:CS116 Four Question

代写四个Python的练习题,从list用法到应用小程序。

Assignment Guidelines

  • This assignment covers material in Module 04, and must be completed using Python 3.
  • Do NOT use global variable or Python constructs from later modules (e.g. loops).
  • Do NOT import any modules other than the check and math modules.
  • Review the Piazza “Allowable Functions” post to see which built-in functions and operators are allowed.
  • The solutions you submit must be entirely your own work. Do not look up either full or partial solutions on the Internet or in printed sources.
  • Submitting your solutions:
    • Solutions to these questions must be placed in files a04q1.py, a04q2.py, a04q3.py, and a04q4.py, respectively.
    • Download the interface file from the course Web page to ensure that all function names are spelled correctly and each function has the correct number and order of parameters. Be sure to use any provided string constants.
    • All solutions must be submitted to MarkUs. No solutions will be accepted through email, even if you are having issues with MarkUs.
    • Verify using MarkUs and your basic test results that your files were properly submitted and are readable on MarkUs.
    • For full style marks, your program must follow the Python section of the CS116 Style Guide.
    • Be sure to review the Academic Integrity policy on the Assignments page
  • Testing your solutions:
    • Download the testing module from the course web page, and import it in each of your files.
    • Use a tolerance of 0.00001 when testing with check.within.
    • Test data for all questions will always meet the stated assumptions for consumed values.

Question 1

Write a Python function clicker_results that consumes two lists of strings, solutions and student, corresponding to the correct answers and a student’s answers for a collection of clicker questions. The function produces (returns) a list containing three natural numbers: [c,a,u], where

  • c is the number of questions answered correctly by the student (the number of times the entries in the corresponding positions in student and solutions match),
  • a is the number of questions answered by the student (which includes both those answered correctly and incorrectly), and
  • u is the number of questions not answered by the student.

The values in solutions will be the strings ‘A’, ‘B’, ‘C’, ‘D’, ‘E’, corresponding to the single correct answer to the each question. The values in student will be one of these strings (when a question was answered by the student) or the empty string (when a question was not answered by the student). The lists solutions and student will always be the same length as each other.
For example,

1
2
clicker_results(['A', 'B', 'C', 'B', 'A', 'D', 'D', 'E', 'A'],
['A', 'B', '', 'D', 'A', 'E', 'E', 'E', '' ]) => [4,7,2]

Question 2

Write a Python function put_in_bounds that consumes a list of integers, values, and two integers, lower and upper (where lower <= upper), and produces (returns) the number of entries in values which are less than lower or greater than upper. In addition, put_in_bounds mutates values so that any entry in values which is less than lower is set to lower, and any value greater than upper is set to upper. All other entries in values stay the same. (Note: the updated values is NOT printed or returned by the function.)

For example, suppose v = [-4,80,104,87,-1,110,100,75], then put_in_bounds (v, 0, 100) => 4, and v is mutated to [0,80,100,87,0,100,100,75].

Question 3

In this question, you will analyze a square grid associated with a game, to determine if a game is over and who won. Two grids are shown below: the one on the left is a completed game (as the grid is full) and the other is still in progress.

The grid for the game will be represented in our program by a list of lists: each row is represented by one list of strings (letters ‘X’, ‘O’ for places that have been played on, and a space for places that have not been played).

Write a Python function game_status, that consumes a Board, b, and produces (returns) one of the following strings:

  • ‘Game over. Tied’ if the board contains no spaces and the number of X and O characters are equal.
  • ‘Game over. X wins’ if the board contains no spaces and there are more X than O characters.
  • ‘Game over. O wins’ if the board contains no spaces and there are more O than X characters.
  • ‘Game in progress’ if the board contains any spaces.

Question 4

This question uses the same Board type described in Question 3. In this question, you will implement one move from the game. Suppose a player is X. The X is placed on a specific (row,column) location, and that position is updated to X (note that it may already be X, or could be O, or it count be blank). At the same time, the adjacent positions indicated below are flipped (if they were X, they switch to O; similarly, if O, they switch to X; if blank, they remain blank). The image below is for the general situation of placing X.

Placing X will flip non-blank characters in the highlighted adjacent places (from X to O, or O to X).

Write a Python function turn, that consumes a Board, b, and a string piece (which is one of ‘X’, ‘O’), and produces None. The function performs the following tasks:

  • Print Board b, each row on a separate line, using the dash (‘-‘) for all spaces. (See example below for more information on format.)
  • Prompt the user to input the row and column in which to place their piece (count columns from left to right, starting from 0; count rows from top to bottom, starting from 0). (See examples and interface file for the proper format of the prompt.)
  • Update b[row][column] to be piece.
  • Flip (‘X’ to ‘O’, ‘O’ to ‘X’, ‘ ‘ unchanged) the values in the indicated positions adjacent to b[row][column]. Pay special attention when piece is placed in either the first or last row or column, as there are fewer adjacent places along the border of the grid.
  • Print updated Board b.

For example, consider the following example using b6x6 (from Question 3). Check the comments next to the sample for additional information.