Python代写:CSE101 Practice with Dictionaries

Python的基础练习lab作业,分为三个小应用,练习dictionaries的使用方法。

Assignment Objectives

This lab will give you some hands-on practice with dictionaries.

Getting Started

For the labs in this course you will write functions that solve computational problems. To help you get started on each assignment, we will give you on Blackboard a “bare bones” file with a name like lab7.py. These files will contain function stubs, which are functions that have no bodies. You will fill in the bodies of these functions for the assignments. Do not, under any circumstance, change the names of the functions or their parameter lists. The automated grading system will be looking for exactly those functions provided in lab7.py. You will be able to test your work by uploading your file to CodeLoad. Note that the CodeLoad tests are not exhaustive - you will want to write your own tests too!

Directions

  • Solve the following problems to the best of your ability.
  • At the top of the lab7.py file, include the following information in comments, with each item on a separate line:
    • your first and last name as they appear in Blackboard
    • your Stony Brook ID # (e.g., 110999999)
    • your Net ID (e.g., jsmith)
    • the course number (CSE 101)
    • the assignment name and number (Lab #7)
  • Your functions must be named as indicated below. Submissions that have the wrong function names can’t be graded by the automated grading system.
  • Upload your .py file to Blackboard by the indicated due date and time. Late work will not be accepted for grading. Work is late if it is submitted after the due date and time.
  • Code that crashes will likely receive a grade of zero, so make sure you upload your work to CodeLoad and also thoroughly test your work with additional tests before submitting it.

Dictionaries are unordered lists, your return values may be in random order. You don’t have to worry about this.

Part I: Caesar Cipher

Write a function caesar_dict() that takes in one parameter: an integer in the range -25 through 25 called shift_amount and returns a dictionary of letters from a to z mapped to their corresponding shifted alphabet. You may want to use ord() and chr().
Note: Since dictionaries are unordered lists, your return values may be in random order (as in ‘c’:’e’ comes before ‘b’:’d’ in the example below). You don’t have to worry about this. Just make sure all alphabets are present and are mapped to their respective shifted alphabet.

Examples

Function Call Return Value
caesar dict(5) {‘a’:’f’, ‘b’:’e’, … ‘y’:’d’, ‘z’:’e’}
caesar dict(0) {‘a’:’a’, ‘b’:’b’, … ‘y’:’y’, ‘z’:’y’}
caesar dict(-1) {‘a’:’z’, ‘b’:’a’, … ‘y’:’x’, ‘z’:’y’}

Remember: CodeLoad has additional tests for you to try! Upload your code there and see how your code matches up against harder tests.

Part II: Banking

Write a function transaction() that simulates a bank transaction and which takes four arguments, in this order: user_database, user, transaction_type and transaction_amount. The function returns nothing. user_database is a dictionary where the key is the name of the user, and the value associated with the key is current balance in the user’s account. user is the name of the user for whom the transaction will be made. transaction_type can be either ‘withdraw’ or ‘deposit’. transaction_amount is the amount to be deposited into or withdrawn from the user’s bank account.
Note: If transaction_type is ‘withdraw’ and transaction_amount is more than the user’s current balance or less than 0, then do nothing. If transaction_type is ‘deposit’ and transaction_amount is greater than 0, then do nothing. Otherwise, add/subtract transaction_amount to/from the user’s balance, as appropriate.
Finally, if transaction_type is ‘deposit’, transaction_amount is greater than 0 and the user does not exist in the user_database, add the user to dictionary with the given transaction_amount as the value.

Examples

In the examples shown below, the function calls are independent of each other. In other words, the function calls do not represent a series of transactions on the same database.

1
dict1 = {'Jay':1000, 'Yupeng':500}

Function Call Updated Dictionary
transaction(dict1, ‘Jay’, ‘withdraw’, 200) {‘Yupeng’:500, ‘Jay’:800}
transaction(dict1, ‘Jay’, ‘deposit’, 300) {‘Yupeng’:500, ‘Jay’:1300}
transaction(dict1, ‘Jay’, ‘withdraw’, -500) {‘Yupeng’:500, ‘Jay’:1000}
transaction(dict1, ‘Bob’, ‘deposit’, 200) {‘Yupeng’:500, ‘Jay’:1000, ‘Bob’:200}

Remember: CodeLoad has additional tests for you to try! Upload your code there and see how your code matches up against harder tests.

Part III: Autocorrection

Write a function autocorrect() that takes two arguments: a sentence of only lowercase letters called msg and a dictionary called mappings where the key is the correct spelling of a word and the value is a list of common misspellings of the word. Your function should replace all misspellings with the correct word.
For example: if msg is ‘hallo haw r u ?’ your return value should be: ‘hello how are you ?’

Examples

Note: Your code will be testing with different mappings dictionary from the one you see below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
mappings = {
'the': ['hte','teh'],
'this': ['thsi','tis','htis','tshi'],
'hey': ['hye','ehy','yhe'],
'you': ['yuo','ouy','uyo','u'],
'how': ['haw','hwo'],
'are': ['r','aer'],
'is': ['si'],
'test': ['tset','tets','etts'],
'am': ['ma','m'],
'best': ['bset','bets','btes'],
'me': ['em','mi'],
'hello':['hallo','heello','helo','hell']
}

Function Call Return Value
autocorrect(‘hye thsi si mi’, mappings) ‘hey this is me’
autocorrect(‘you are the best’, mappings) ‘you are the best’
autocorrect(‘’, mappings) ‘’

Note that the quotation marks displayed in the examples are there to emphasize that the argument and return value are strings. You should not add quotation marks to your return values.
Remember: CodeLoad has additional tests for you to try! Upload your code there and see how your code matches up against harder tests.

How to Submit Your Work for Grading

To submit your .py file for grading:

  1. Login and locate the course account for CSE 101.
  2. Go to the “Class Materials” folder for this week’s lab assignments.
  3. Click on the link for this lab assignment.
  4. Click the “Browse My Computer” button and locate the .py file you wish to submit. You should be submitting only one file!
  5. Click the “Submit” button to submit your work for grading.

Oops, I messed up and I need to resubmit a file!

No worries! Just follow the above directions again. We will grade only your last submission.