Introduction to Computer Science II

Homework and Lab 3

Due by 3:10pm on Tuesday, January 27

Reading

Read sections 8.1 through 8.4 and the week 3 lecture notes.

Lab

Monday 1:30pm-3:00 in room 512 in 14 E Jackson or via Zoom.

Problems

Solve the following by implementing the corresponding functions in homework3.py. When done, submit that file through D2L.


1.    Overload appropriate operators for class Card so that you can compare cards based on rank. Use the following ranking: 2 < 3 < 4 < 5 < 6 < 7 < 8 < 9 < 10 < J < Q < K < A.
Usage:
>>> d = Deck()
>>> d.shuffle()
>>> a = d.dealCard()
>>> b = d.dealCard()
>>> c = d.dealCard()
>>> a
Card('8', '♣')
>>> b
Card('2', '♡')
>>> c
Card('7', '♣')
>>> a < b
False
>>> c > b
True
>>> a <= a
True
>>> a >= c
True

2.    Overload the appropriate operator for class Card so that the cards are "pretty-printed" as shown below.
Usage:
>>> d = Deck()
>>> d.shuffle()
>>> print(d.dealCard())
+---+
|  ♠|
|   |
|10 |
+---+

>>> print(d.dealCard())
+---+
|  ♣|
|   |
|3  |
+---+

>>> print(d.dealCard())
+---+
|  ♠|
|   |
|5  |
+---+

>>> print(d.dealCard())
+---+
|  ♣|
|   |
|8  |
+---+

>>> print(d.dealCard())
+---+
|  ♡|
|   |
|10 |
+---+


3.     Develop a class Textfile that provides methods to analyze a text file. The class Textfile will support a constructor that takes as input a file name (as a string) and instantiates a Textfile object associated with the corresponding text file.  The Textfile class should support methods nchars(), nwords(), and nlines() that return the number of characters, words, and lines, respectively, in the associated text file. The class should also support methods read() and readlines() that return the content of the text file as a string or as a list of lines, respectively, just as we would expect for file objects. Finally, the class should support method grep() that takes a target string as input and searches for lines in the text file that contain the target string. The method returns the lines in the file containing the target string; in addition, the method should print the line number, where line numbering starts with 0.

Test your implementation using the test file raven.txt.
Usage:
>>> t = Textfile('raven.txt')
>>> t.nchars()
6299
>>> t.nwords()
1125
>>> t.nlines()
126
>>> print(t.read())
Once upon a midnight dreary, while I pondered weak and weary, ... Shall be lifted - nevermore!
>>> t.grep('nevermore')
75: Of `Never-nevermore.`
89: She shall press, ah, nevermore!
124: Shall be lifted - nevermore!


4.    Add method words() to class Textfile. It takes no input and returns a list, without duplicates, of words in the file.
Usage:
>>> t = Textfile('raven.txt')
>>> t.words()
['Ah', 'Aidenn', 'And', 'As', 'Back', 'Be', 'Bird', 'But', 'By', 'Caught', 'Clasp', 'Darkness',
...
'wondering', 'word', 'wore', 'wrought', 'yet', 'yore', 'you', 'your']



5.    Add method occurrences() to class Textfile. It takes no input and returns a dictionary mapping each word in the file (the key) to the number of times it occurs in the file (the value).
Usage:
>>> t = Textfile('raven.txt')
>>> t.occurences()
{'Once': 1, 'upon': 4, 'a': 15, 'midnight': 1, 'dreary': 1, 'while': 1, 'I': 32, 'pondered': 1,
...
 'streaming': 1, 'throws': 1, 'shadow': 2, 'lies': 1, 'floating': 1, 'Shall': 1, 'lifted': 1}


6.
    [Lab exercise] Implement class Hand that represents a hand of playing cards. The class should have a constructor that takes as input the player ID (a string). It should support method addCard() that takes a Card object as input and adds it to the hand as well as the len operator that returns the number of cards in the hand as shown below. The appropriate string representation method should also be overloaded to achieve the printing behavior shown below.
Usage:
>>> d = Deck()
>>> d.shuffle()
>>> h = Hand('House')
>>> h.addCard(d.dealCard())
>>> h.addCard(d.dealCard())
>>> h.addCard(d.dealCard())
>>> len(h)        
3
>>> print(h)
House:   7 ♢   Q ♠   Q ♢


7.    [Lab exercise] Implement class BlackjackHand that is exactly the same as the above Hand class except for an additional method total that returns the total value of a blackjack hand.

Usage:
>>> d = Deck()
>>> d.shuffle()
>>> p = BlackjackHand('player')
>>> h = BlackjackHand('house')
>>> p.addCard(d.dealCard())
>>> h.addCard(d.dealCard())
>>> p.addCard(d.dealCard())
>>> h.addCard(d.dealCard())
>>> print(p)
player:   8 ♠   5 ♢
>>> print(h)
house:   2 ♠   2 ♣
>>> p.total()
13
>>> h.total()
4