ACIT 1515 Midterm Exam

ACIT 1515作业代写、代做Python编程语言作业、代写D2L留学生作业、代做Python课程设计作业
ACIT 1515 Midterm Exam
This exam has three questions. Marks will be assigned for each question by testing with the instructors test data.
Part marks are awarded.
Marks will be deducted for:
incorrect output and/or return values: function does not meet specification
incorrect or unnecessarily complex function design strategies (your algorithm)
inappropriate use of data structures and variables
inappropriate length and/or use of functions
poor commenting, variable names, and program layout
code that is clearly cut/paste from internet, where the code does not reflect techniques learned in class or techniques that can’t be easily explained by the student
Question 1: Bug Fixing
/ 6

Question 2: Code Completion
/ 16

Question 3: Program Creation
/ 20

Save and upload a single file for each of the questions, named as specified.
Save your programs as you finish them, as you will be required to upload your final copies to D2L at the end of the exam. It is your responsibility to keep track of your files, and to upload the correct versions. Only the last version of each copy will be checked.
Rules:
1.Turn off your phone. Yes, off
2.Place your phone on your desk in front of you, where the instructor can see it.
3.No phones, no talking, no getting help from anyone else – which means no email, text, chat etc..
4.You CAN use your textbook, previous assignments, and code.
5.You CAN use any websites as long as you do not post a question anywhere or otherwise communicate anyone.
6.The test is timed: 2 hours max – at this point the D2L dropbox is closed

Question 1: Bug Fixing
Using the following source code to create a file named reverse.py. Repair the errors that it generates so it is able to match the Example Usage below when run.
Filename
reverse.py
Source Code
#! /usr/bin/env python3

def reverse(phrase):
num_letters = len(phrase)
letter_count = 0

while letter_count < num_letters:
reversed_phrase += phrase[num_letters - letter_count]
letter_count += 1

return reversed_phrase


def main():

phrases = []
phrases.append("Exams are not the most authentic of assessments")
phrases.append("Loops need to end")
phrases.append("Exams are not the most authentic of assessments")

for phrase in phrases:
print(reverse(phrase))


if __name__ == '__main__':
main()
Example Usage
PS>.\reverse.py
stnemssessa fo citnehtua tsom eht ton era smax
dne ot deen spoo
stnemssessa fo citnehtua tsom eht ton era smax

Question 2: Code Completion
Using the source code below as a starting point create a file shuffle.py that meets the requirements outlined in the comments of source code and the matches the outputs in the Example Usage.
FILENAME
shuffle.py
Source Code
'''
This module builds a deck of cards, shuffles them, and deals out a list of hands
of a specified number of cards for the specified number of players
'''
import pprint
import random

suits = ['Hearts', 'Diamonds', 'Spades', 'Clubs']

card_names = {
2: 2,
3: 3,
4: 4,
5: 5,
6: 6,
7: 7,
8: 8,
9: 9,
10: 10,
11: 'Jack',
12: 'Queen',
13: 'King',
14: 'Ace'
}


def make_suit(suit):
'''
Generates a list containing all of the cards in a single suit

Use the card_names dictionary to generate each card "name" based
on its "value" from 2 to 14 combined with its suit.

For example a card with value of 11 would have a name of 'Jack'

Args:
suit (str): the suit for which to generate the cards one of:
Hearts, Diamonds, Spades, or Clubs.

Returns:
List[str]: a list containing all the cards in the suit.
each card is a string with the card value
followed by the suit in the format:

<Value> of <Suit>

Examples:
4 of Hearts
8 of Diamonds
10 of Clubs
Queen of Hearts
Ace of Spades
'''
suit_cards = []

# your code goes here

return suit_cards


def make_deck():
'''
Generates a list containing a string for each of the cards in a standard
playing deck

A "standard" deck of playing cards consists of 52 Cards in each of the 4
suits of Spades, Hearts, Diamonds, and Clubs. Each suit contains 13 cards:
2, 3, 4, 5, 6, 7, 8, 9, 10, Jack, Queen, King, and Ace

The deck is created by creating the cards for each suit in a loop.

Returns:
List[str]: List containing a string for each of the cards in a standard
deck. See make_suit() for string format
'''
deck = []

# your code goes here

return deck


def shuffle_deck(deck):
'''
Shuffles deck of cards in place, i.e. modifies the passed deck parameter
but doesn't return a new value

Args:
deck (List[str]): List containing strings, each representing cards in a
standard deck
'''
random.shuffle(deck)


def deal(num_of_players, cards_per_player, deck):
'''
Creates a nested list of hands for the requested number of players.
Each hand is a list holding the requested number of cards.

The cards will be 'dealt' from the start of the deck (which is a list)

The size of the deck slice given to each player is the number specified in
the cards_per_player parameter

Each hand will be dealt starting with the first card after the
last card assigned to the previous players hand.

After dealing the deck will no longer include the cards that have been
dealt to the players.


Args:
num_of_players (int): Number of players in the game,
ie the number of hands to create

cards_per_player (int): The number of cards each player should receive

deck (List[str]): the deck of cards from which to deal out hands

Returns:
List[List[str]]: a nested list of cards. One list for each player,
with each list containing the number of requested
cards
'''
hands = []

# your code goes here

# Remove the dealt cards from the deck
del deck[0:(num_of_players * cards_per_player)]

return hands


def main():
'''
This function performs the following operations:
- Create a new card deck and store it in the variable deck
- print the deck
- shuffle the deck
- create a nested list of hands - by invoking the deal function, store
this list in the variable hands
- print the list of hands
- print the remaining deck
'''
# Create a card deck named deck
# your code goes here

# Print the previously created deck
print('\nDeck:')
pprint.pprint(deck)

# Shuffle the deck
# your code goes here

# create a list of hands for 4 players with 5 cards per player
# your code goes here

# Print the list of hands
print('\nHands:')
pprint.pprint(hands)

# Print the deck list
print('\nDeck after Dealing Cards:')
pprint.pprint(deck)


if __name__ == '__main__':
main()


Example Usage
Note: that the exact hands and order of the cards remaining in the deck will vary due to the random nature of the shuffling.
PS>.\shuffle.py

Deck:
['2 of Hearts',
'3 of Hearts',
'4 of Hearts',
'5 of Hearts',
'6 of Hearts',
'7 of Hearts',
'8 of Hearts',
'9 of Hearts',
'10 of Hearts',
'Jack of Hearts',
'Queen of Hearts',
'King of Hearts',
'Ace of Hearts',
'2 of Diamonds',
'3 of Diamonds',
'4 of Diamonds',
'5 of Diamonds',
'6 of Diamonds',
'7 of Diamonds',
'8 of Diamonds',
'9 of Diamonds',
'10 of Diamonds',
'Jack of Diamonds',
'Queen of Diamonds',
'King of Diamonds',
'Ace of Diamonds',
'2 of Spades',
'3 of Spades',
'4 of Spades',
'5 of Spades',
'6 of Spades',
'7 of Spades',
'8 of Spades',
'9 of Spades',
'10 of Spades',
'Jack of Spades',
'Queen of Spades',
'King of Spades',
'Ace of Spades',
'2 of Clubs',
'3 of Clubs',
'4 of Clubs',
'5 of Clubs',
'6 of Clubs',
'7 of Clubs',
'8 of Clubs',
'9 of Clubs',
'10 of Clubs',
'Jack of Clubs',
'Queen of Clubs',
'King of Clubs',
'Ace of Clubs']

Hands:
[['2 of Spades',
'7 of Diamonds',
'Queen of Hearts',
'10 of Clubs',
'Jack of Clubs'],
['2 of Hearts',
'8 of Diamonds',
'King of Clubs',
'10 of Spades',
'8 of Hearts'],
['King of Spades',
'8 of Clubs',
'6 of Clubs',
'9 of Diamonds',
'5 of Diamonds'],
['3 of Spades',
'7 of Clubs',
'5 of Clubs',
'King of Diamonds',
'King of Hearts']]

Deck after Dealing Cards:
['4 of Diamonds',
'Queen of Diamonds',
'Ace of Clubs',
'Ace of Diamonds',
'6 of Spades',
'Queen of Clubs',
'4 of Spades',
'3 of Hearts',
'4 of Hearts',
'9 of Spades',
'6 of Hearts',
'4 of Clubs',
'Jack of Hearts',
'7 of Spades',
'2 of Clubs',
'3 of Clubs',
'7 of Hearts',
'9 of Hearts',
'10 of Hearts',
'Queen of Spades',
'5 of Hearts',
'5 of Spades',
'Jack of Spades',
'Ace of Spades',
'Jack of Diamonds',
'10 of Diamonds',
'9 of Clubs',
'2 of Diamonds',
'6 of Diamonds',
'8 of Spades',
'3 of Diamonds',
'Ace of Hearts']

Question 3: Program Creation
You will create a script: message_encryptor.py, that encrypts a series of messages by swapping individual letters based on an encryption cipher.
Your code will be based on the source code supplied below, please read the comments. You will complete your program by finishing the supplied source code.
DO NOT change any function signatures, all of the parameters for all functions must remain untouched from the supplied source code.
FILENAME
message_encryptor.py
DESCRIPTION
This module has a main function that creates a list of messages (this is done for you).
The main function then creates a cipher, in this case a dictionary that maps every ASCII letter to a different ASCII letter.
The main function will print out this encryption cipher.
The main function will create the corresponding decryption cipher using gen_rev_cipher (this is supplied)
Use the encryption cipher (i.e.?dictionary) to transform the supplied messages so that they are encrypted (scrambled).
Print the encrypted messages.
Use the decryption cipher (i.e.?dictionary) to transform the encrypted messages so that they are plain text (unscrambled).
Source Code
#! /usr/bin/env python3
import random
import string


def generate_cipher():
'''
Generates a dictionary that maps each ASCII letter (upper and lower case)i
to its scambled equivalent.

Necessary steps:
1. Create list of ASCII letters

2. Create scrambled list of ASCII letters

3. Create dictionary using the ASCII letter list for the keys and the
scrambled list as the values. This should be done in a loop with
a counter. This way you can retrieve the item at a particular
position from two lists in the same loop.

i.e. list1[counter]
list2[counter]

can be used to get the value from each list for the same position.

4. Return the dictionary that maps the ASCII letters to the scrambled
letters

Notes:
1. The statement:
list(string.ascii_letters)
generates a list of a the ASCII letter characters.

1. The expression:
random.shuffle(list1)
will shuffle list1 in place, i.e. scramble its order

Returns:
Dict[str, str]: cipher dictionary (original letter: transformed letter)
with one item per ASCII letter
'''

# your code goes here


def generate_rev_cypher(cipher):
'''
Returns and reverse cipher to map from encrypted cipher text back to plain
text. It creates the reverse cipher by looping over the passed cipher and
creating a new dictionary that maps uses the value of the cipher parameter
as the reverse cipher key and the key of the original as the reverse cipher
value.

Args:
cipher (Dict[str,str]): encryption cipher to be reversed

Returns:
Dict[str,str]: decryption cipher that preforms the reverse mapping of
the input cipher
'''
rev_cipher = {}
for orig, trans in cipher.items():
rev_cipher[trans] = orig

return rev_cipher


def encode_message(cipher, message):
'''
This function converts each letter in the message to the one specified in
the cipher dictionary. It then returns the transformed message. It leaves
non-letter characters unchanged.

Args:
cipher (Dict[str,str]): a dictionary that maps an input character to
its transformed equivalent

message (str): A string to be transformed using the cipher

Returns:
str: the transformed string
'''

# your code goes here


def main():
'''
Creates an encryption cipher dictionary using generate_cipher()
Creates an decryption cipher dictionary using generate_rev_cipher()

For each of the individual messages in messages:
- encrypt the message using encode_message()
- print the encrypted message
- decrypt the message using encode_message()
- print the decrypted original message.
'''
messages = ["Tests aren't easy",
"Documentation makes programming easier",
"This is the last message"]

# your code goes here


if __name__ == '__main__':
main()


Example Usage
Note: since your encryption cipher has been randomized, it and the decryption cipher will vary from the example. The encrypted messages will also not match those in the example.
PS>.\message_encryptor.py

Cipher:
{'A': 'j',
'B': 'x',
'C': 'i',
'D': 'q',
'E': 'f',
'F': 'X',
'G': 'V',
'H': 'I',
'I': 'U',
'J': 'w',
'K': 'Z',
'L': 'R',
'M': 'M',
'N': 'e',
'O': 'L',
'P': 'F',
'Q': 'E',
'R': 'P',
'S': 'z',
'T': 'Y',
'U': 'N',
'V': 'o',
'W': 'C',
'X': 'K',
'Y': 'J',
'Z': 'k',
'a': 'l',
'b': 'T',
'c': 'b',
'd': 'g',
'e': 'a',
'f': 'h',
'g': 'S',
'h': 'm',
'i': 'u',
'j': 'n',
'k': 'H',
'l': 'v',
'm': 'c',
'n': 'y',
'o': 'r',
'p': 'A',
'q': 's',
'r': 'p',
's': 'd',
't': 'O',
'u': 'G',
'v': 'W',
'w': 'B',
'x': 'D',
'y': 'Q',
'z': 't'}

Reverse Cipher:
{'A': 'p',
'B': 'w',
'C': 'W',
'D': 'x',
'E': 'Q',
'F': 'P',
'G': 'u',
'H': 'k',
'I': 'H',
'J': 'Y',
'K': 'X',
'L': 'O',
'M': 'M',
'N': 'U',
'O': 't',
'P': 'R',
'Q': 'y',
'R': 'L',
'S': 'g',
'T': 'b',
'U': 'I',
'V': 'G',
'W': 'v',
'X': 'F',
'Y': 'T',
'Z': 'K',
'a': 'e',
'b': 'c',
'c': 'm',
'd': 's',
'e': 'N',
'f': 'E',
'g': 'd',
'h': 'f',
'i': 'C',
'j': 'A',
'k': 'Z',
'l': 'a',
'm': 'h',
'n': 'j',
'o': 'V',
'p': 'r',
'q': 'D',
'r': 'o',
's': 'q',
't': 'z',
'u': 'i',
'v': 'l',
'w': 'J',
'x': 'B',
'y': 'n',
'z': 'S'}

Scrambled Message: YadOd lpay'O aldQ
Original Message: Tests aren't easy
Scrambled Message: qrbGcayOlOury clHad AprSplccuyS alduap
Original Message: Documentation makes programming easier
Scrambled Message: Ymud ud Oma vldO caddlSa
Original Message: This is the last message

http://www.daixie0.com/contents/3/2098.html

因为专业,所以值得信赖。如有需要,请加QQ99515681 或邮箱:[email protected] 

微信:codinghelp

猜你喜欢

转载自www.cnblogs.com/okjavanewpython/p/10013178.html