Fall.2008.Solution

# Fall.2008.Solution - DECEMBER 2008 Final Examination —...

This preview shows pages 1–4. Sign up to view the full content.

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: DECEMBER 2008 Final Examination — Solutions CSC108H1F Question 1. [6 marks] A deck of cards has 52 cards. Each card has a suit and a rank. The suits are Clubs, Diamonds, Hearts, and Spades. The ranks are Ace, 2, 3, ..., 9, 10, Jack, Queen, and King. Blackjack is a game that starts off with each player getting (being dealt) one card face down and one card face up. In this question, you will be reading the help for classes Card and Deck (which are in modules card and deck , respectively), and writing code that uses those two classes. class Card: | A card with a rank and a suit that can be | either face up or face down. | | Methods defined here: | | __init__(self, s, r) | A new face-down Card with suit str s | and rank str r. | | turn_over(self) | Flip this card over. | | value(self) | Return this card’s rank and suit as a | str, or "XX" if this card is face down. class Deck: | A deck of cards. | | Methods defined here: | | __init__(self) | A new deck with 52 cards. | | get_next_card(self) | Remove and return this Deck’s next card. | | shuffle(self) | Randomly rearrange the cards. | |------------------------------------------------ | Data and other attributes defined here: | | RANKS = [’A’, ’2’, ’3’, ’4’, ’5’, ’6’, ’7’, ’8’, | ’9’, ’T’, ’J’, ’Q’, ’K’] | | SUITS = [’C’, ’D’, ’H’, ’S’] Page 1 of 17 cont’d . . . CSC108H1F Final Examination — Solutions DECEMBER 2008 Fill in the missing code in the spaces provided. import deck import card def print_cards(p): ’’’Print player p’s cards.’’’ for card in p: print card.value(), print def deal(deck, p): ’’’Deal one Card face-down and once Card face-up from the deck into list of Cards p. The face-up card should be the last item in p.’’’ p.append(deck.get_next_card()) p.append(deck.get_next_card()) p[-1].turn_over() if __name__ == ’__main__’: # Create a deck and shuffle it. deck = deck.Deck() deck.shuffle() # The cards for the two players. The top card is the last card in the list. p1_cards = p2_cards = # Put one face-down and one face-up card into each player’s list. deal(deck, p1_cards) deal(deck, p2_cards) # Print the cards for each player. print_cards(p1_cards) print_cards(p2_cards) Page 2 of 17 cont’d . . . DECEMBER 2008 Final Examination — Solutions CSC108H1F Question 2. [12 marks] Complete the following functions according to their docstring descriptions. Part (a) [6 marks] def input_to_dict(r): ’’’Given a reader r that contains lines of the form INSTRUCTOR:COURSE:YEAR return a dictionary in which each key is an INSTRUCTOR (str) and each value is a list of (str, int) tuples where each tuple is of the form (COURSE, YEAR) representing a course the instructor has taught and the year they taught the course.’’’ teaching_dict = {} for line in r: line_list = line.strip().split(’:’) if not line_list[0] in teaching_dict: teaching_dict[line_list[0]] = teaching_dict[line_list[0]].append((line_list[1], line_list[2])) return teaching_dict Page 3 of 17 cont’d . . . CSC108H1F...
View Full Document

{[ snackBarMessage ]}

### Page1 / 17

Fall.2008.Solution - DECEMBER 2008 Final Examination —...

This preview shows document pages 1 - 4. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online