As usual, create two directories for today's class. Create a
directory called lecture29
under activities, and
a directory called lab29
under labs.
No randomizer this week. Instead, you will get 5 points for filling out the survey currently hosted on Inquire. This survey asks questions about the test, so you will be provided with a reference for the questions on the test.
You saw on Wednesday how to build lists using the append
method of lists. Today, we will explore how we can mutate
lists further, by using slicing and removing elements from a list.
You may have heard some crazy conspiracy theories that William Shakespeare did not write his own plays. You have likely read at least one of his plays, but not much else is known about him. It is speculated that perhaps Shakespeare did not write all (or even any) of his own plays. In fact, there are a few conspiracy theories that say that other well known authors wrote the Shakespeare plays. Maybe we could help the world out, and exonerate some of the common suspects
Theoretically, figuring out who wrote a piece of text is easy: Compare a sample of writing by a known author, versus the text whose author is to be identified. If there are enough similarities between the writings, then there is enough evidence to state that there is more than a coincidental chance that the two texts were written by the same person, the author of the known text. This process is much more complicated when put into actual practice. The difficulty arises in defining what similarities are important for comparison, and what "enough similarities" actually means.
One potential metric for measuring similarity between two arbitrary texts are the words used within the text. It is seems like a decent assumption that an authors vocabulary does not drastically change between written texts. Given this assumption, we could say that if the set words used in the two sample texts are similar enough, they were likely written by the same author. In order to determine this, we need to be able to get the set words used out of some text.
Create a program in a file called
authorship_detection.py
. Your program should do the
following:
input
function.
replace
method of
strings to replace periods with the " " character.
$ cat test_set1.in This is a simplified text set. There should be only two shared words. Small count of shared words means different authors. $ python3 authorship_detection.py < test_set1.in There were 2 shared word(s) in the sets
The word sets discussed above would likely indicate a lot of similarities between two arbitrary texts. This is because the two texts were probably written in the same language, where things like conjunctions, indefinite articles, and even pronouns are much more common than other words. A better comparison would use the unique words: the set of all of the words that occur exactly once within the text. Modify your program so it only compares this new set of unique words.
$ cat test_set2.in There are a lot of repeat words here. There are a lot of repeat words here. likely not here, though. There are fewer repeated words here, so the word set should be larger. $ python3 authorship_detection.py < test_set2.in There are 0 shared word(s) in the sets
Scotty is a card shark. If you want to play blackjack with him and not lose all of your money, you will need to practice. If you don't know the game blackjack, it's simple. Each player is dealt two cards. Face cards are worth 10 points, aces are worth 11 points, and all other cards are worth face value. You can hit, or ask for, as many more cards as you want. The player with the most points, without going over 21 is the winner.
Create a program in a file called blackjack.py
that plays
a single hand of blackjack. The program should do the following:
Initialize the deck to contain 52 strings representing the cards of
a standard playing deck. You can shuffle the deck using the random.shuffle
function.
Deal two cards from the deck to the player's hand list.
Repeatedly prompt the user if they want to add more cards and print the player's hand.
Print the total points of the player's hand.
Your Hand: 10♦ K♠ Enter h to hit: h Your Hand: 10♦ K♠ 2♥ Enter h to hit: s You have 22 points.
This program can be broken into several functions:
The function player_turn(deck, hand)
should have a
while loop runs while the user's input is not "h". The
body of the loop should print the hand list and then prompt the user
to enter an "h". If the user enters an "h", the
last item in the deck list should be popped and appended to the hand
list.
The function card_points(card)
should return the point
value of the card parameter. A card string can be converted to its
point value by slicing off the value and converting it to an
int. The strings "A", "K", "Q", and
"J" can be converted using an if statement. The strings
"1" through "10" can be converted using the
built-in int
function.
The function hand_points(hand)
should return the sum
of all of the cards in the hand list. It should iterate over the
hand list and use the card_points(card)
function to
convert each card string into an int.
This solitary version of Blackjack is kind of boring. Add a dealer to the mix. The game is still very similar. At the beginning, you will deal cards to both the player and the dealer. You should display one of the dealer's cards to the player, as is typical with a game of blackjack.
After the player hits until they are content, it is then the dealers turn. On the dealer's turn, repeatedly add cards to the dealer's hand until the total is 17 points or more.
After the dealer's turn, you should show the entirety of the dealers hand, display the total points for both the player and the dealer, and print who won
Dealer's Hand: 2♠ ## ~~~~~Your Turn~~~~~ Your Hand: 10♦ K♠ Enter h to hit: h Your Hand: 10♦ K♠ 2♥ Enter h to hit: s ~~~Dealer's Turn~~~ Dealer hits. Dealer's Hand: 2♠ 10♣ 8♥ Dealer stays. You have 22 points. Dealer has 20 points. Dealer wins.
To really have a chance beating Scotty, you will need to count cards. By counting cards you can know when you have a higher probability of winning a hand. When you have a higher probability of winning you can bet more money. Unless you are a savant, counting all the cards difficult. Instead, it's not to difficult to estimate how many high cards are left in deck by using a hi-lo system. Every card in the deck is given a value.
Card Value | Hi-lo Value |
2 -- 6 | -1 |
7 -- 9 | 0 |
10 -- A | 1 |
When the sum of the hi-lo value of every dealt card is large, there is a higher probability that the next card dealt will be a 10 through ace. Modify your blackjack program to also print the hi-lo card count every time a card is dealt.
Dealer's Hand: 2♠ ## Hi-lo: -1 ~~~~~Your Turn~~~~~ Your Hand: 10♦ Hi-lo: 0 Your Hand: 10♦ K♠ Hi-lo: 1 Enter h to hit: h Your Hand: 10♦ K♠ 2♥ Hi-lo: 0 Enter h to hit: s ~~~Dealer's Turn~~~ Dealer hits. Dealer's Hand: 2♠ 10♣ 8♥ Dealer stays. You have 22 points. Dealer has 20 points. Dealer wins.
When you have finished, create a tar file of your lab29
directory. To create a tar file, execute the following commands:
cd ~/cs120/labs tar czvf lab29.tgz lab29/
To submit your activity, go to cseval.roanoke.edu. You should
see an available assignment called Lab Assignment 29
. Only
one of your pair should submit your activity. Make sure both partners
are listed in the header of your files.
Do not forget to email your partner today's files!