Python代写:CS116 Iteration

代写四个小应用程序,练习iteration的用法。这个作业对recursion和lambda进行了使用限制。

Assignment Guidelines

  • This assignment covers material in Module 06.
  • Do not use recursion or the abstract list functions (map and filter). All repetition must be performed using iteration (while and for loops only).
  • Submission details:
    • Solutions to these questions must be placed in files a06q1.py, a06q2.py, a06q3.py, and a06q4.py, respectively, and must be completed using Python 3.
    • 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.
    • 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
  • Download the testing module from the course web page. Include import check in each solution file.
    • When a function produces a floating point value, you must use check.within for your testing. Unless told otherwise, you may use a tolerance of 0.00001 in your tests.
    • Test data for all questions will always meet the stated assumptions for consumed values.
  • Restrictions:
    • Do not import any modules other than math and check.
    • Do not use any other Python functions not discussed in class or explicitly allowed elsewhere. See the allowable functions post on Piazza. You are always allowed to define your own helper functions, as long as they meet the assignment restrictions.
    • While you may use global constants in your solutions, do not use global variables for anything other than testing.
    • Read each question carefully for additional restrictions.

array123

Write a Python function array123 that consumes a list of integers lst, return True if the sequence of numbers 1,2,3 appears in the list somewhere; otherwise, it will return False. For example,

  • array123([1,1,2,3,1]) => True
  • array123([1,1,2,4,1]) => False
  • array123([1,1,2,1,2,3]) => True

Note: You may assume the length of lst is equal to or greater than 3.

Ransom Note

Given an arbitrary ransom note string and another string containing letters from all the magazines, write a function can_construct(ransomNote, magazine) that will return True if ransomNote can be constructed from magazine; otherwise, it will return False. Each letter in the magazine string can only be used once in ransomNote. For example,

  • can_construct(“a”, “b”) => False
  • can_construct (“aa”, “ab”) => False
  • can_construct (“aa”, “aba”) => True
  • can_construct((“pswd”, “password”) => True

Note:
You may assume that both strings contain only lowercase letters.
You may assume that length of ransomNote is not larger than length of magazine.
You may assume that both ransomNote and magazine are not empty string.

Cancel Items

This question uses the following new type Sales_Order:

1
2
3
4
5
# A Sales_Order, s, is a list as [Nat, (listof Str)] where
# * s[0] is the order id (unique for an order)
# * s[1] is a list of the names of the items bought by the customer
# (if the customer bought more than one of an item, it will appear
# multiple times in this list) - these strings are nonempty

Write a Python function cancel_items that consumes a list of Sales_Orders (called orders) and a list of string (called items). This function produces a list of order ids of Sales_Orders in orders whose bought list includes at least one item in items. In addition, the function mutates orders as: all occurrences of items in the bought lists of orders are removed.

Note that if a Sales_Order in orders contains only item in items (no matter how many times), the Sales_Order will remain in orders with an empty bought list.

For example, suppose my_sales equals

[[2321, ['milk', 'bread', 'bread', 'potatoes', 'apples']],
[5521, ['bananas', 'milk', 'bread', 'orange juice']],
[3214, ['bread']],
[98241, ['toothpaste', 'dental floss', 'toothbrush']]]

Calling cancel_items(my_sales, [“bread”,”milk”]) =>

[2321,5521,3214], and mutates my_sales to 
[[2321, ['potatoes', 'apples']],
[5521, ['bananas', 'orange juice']],
[3214, []],
[98241, ['toothpaste', 'dental floss', 'toothbrush']]]

Tic Tac Teo Play

In Question 3 from Assignment 3, we tried how to draw game board of Tic Tac Teo. In the following question, we try to complete this game with user interactions.

This question deals with handling user input and display the win information. When a player (say player 1) wants to place an X on the screen, they can’t just click on a terminal. So we are going to approximate this clicking simply by asking the user for a coordinate of where they want to place their piece.

Write a Python function play() that consumes no parameter, and asks Player 1 and Player 2 in turns what their move is (in the format row,col). Finally, display the game over information when there is a winner or the board is filled. A Tic Tac Toe win is 3 in a row - either in a row, a column, or a diagonal. Also, display a hint information when the players try to fill a nonempty place.

This process may best be understood by looking at a couple of examples of game play (red: user input):

>>>play()
Player 1 input: 1,1
Player 2 input: 2,1
Player 1 input: 3,1
Player 2 input: 3,2
Player 1 input: 2,2
Player 2 input: 3,1
Position on board already taken.
Player 1 input: 3,3
Game over - Player 1 wins

>>>play()
Player 1 input: 1,1
Player 2 input: 1,2
Player 1 input: 1,3
Player 2 input: 2,1
Player 1 input: 2,3
Player 2 input: 2,2
Player 1 input: 3,1
Player 2 input: 3,3
Player 1 input: 3,2
Game over - the board is filled

Note: User input format is as: coordinates in the form “row,col” - a number, then a comma, then a number and numbers are between 1 and 3 (inclusive).