CPSC 120 B




Lecture
MWF 10:50am - 11:50am
Lab
MWF 12:00pm - 1:00pm

Scotty Smith

Office
Trexler 365-B
Office Hours
Monday / Thursday
3:00pm - 5:00pm
Email
chssmithATroanoke.edu

< Back
Printable Copy

Lecture 29 - More With Lists


As usual, create two directories for today's class. Create a directory called lecture29 under activities, and a directory called lab29 under labs.


Survey says!

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.


Lists

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.


Lab Activity 1
Who Wrote It?

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.

Details

Create a program in a file called authorship_detection.py. Your program should do the following:

  1. Read a piece of text from standard input using the input function.
  2. Remove all punctuation from the string by replacing them with a single space. You can use the replace method of strings to replace periods with the " " character.
  3. Create a list to represent a set of words, essentially a list of all of the words from the input string without duplicates.
  4. Perform the same process for an additional piece of text.
  5. Print a count of the number of words that are in both lists.

Example

$ 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

Hint

  1. Create a function word_set(text), which takes a string of text and returns a list representing the set of all words in the text file. This is essentially the list of all words, without duplicates. You can get a list of all words using the split method from the string objects.

    >> s = "This is a test case"
    >> print(s.split())
    ["This", "is", "a", "test", "case"]
    

    With this split list, you can create a set_of_words by looping through split list, adding each word if it is not already in set_of_words.

  2. Write a function count_duplicates(list_1, list_2), which takes two lists of strings as parameters. This function should loop through list_1, and increment a counter for every word from that list you find in list_2.

 

Challenge

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

Lab Assignment 29
Blackjack

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.

Details

Create a program in a file called blackjack.py that plays a single hand of blackjack. The program should do the following:

  1. 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.

  2. Deal two cards from the deck to the player's hand list.

  3. Repeatedly prompt the user if they want to add more cards and print the player's hand.

  4. Print the total points of the player's hand.

Example

Your Hand: 10♦ K♠
Enter h to hit: h
Your Hand: 10♦ K♠ 2♥
Enter h to hit: s
You have 22 points.

Hint

This program can be broken into several functions:

  1. 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.

  2. 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.

  3. 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.

 

Challenge

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.
  

 

Challenge

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.

Submission

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!


Last modified: Thu Nov 7 21:44:28 EST 2013