Intro to Python

RUF Advanced Class

bit.ly/rufpython1

Problem #1. Morse Code

We will provide you a dictionary that represents a simple substitution cipher. The dictionary is a mapping from a 5-symbol code to a single letter. Your task is to use this dictionary to decode an encrypted piece of text we provide. Download the dictionary from Github.

The format of the encoded text is groupings of five characters separated by spaces. For instance, if “abcde” corresponded to “h” and “zyxwv” corresponded to “i”, the encoding for “hi” would be “abcde zyxwv”

Bonus: if you find the above too easy, write a method to encrypt “plain” text with the same code

 

Problem #2. More Dictionaries

Write a function that, given two lists, one of keys and one of values, makes a dictionary of the key-value pairs that share the same index.  For instance, if the lists are

keys = ['one', 'two', 'three']

vales = [1, 2, 3]

the resulting dictionary should look like

{ 'one': 1, 'two': 2, 'three': 3 }

Call this function with lists such as the above keys and values and print out the resulting dictionary.

 

Problem #3. Looping Over Dictionaries

Santa has a dictionary of string-int key-value pairs, where each key is a kid's name and each value is their "goodness score."  Santa only gives presents to kids with a positive score. Write a function to iterate over the dictionary to count how many kids have positive scores to let Santa know how many presents his elves should make.  Print out that number.  Bonus: if any kids have negative scores, print out which kid has the most-negative score.

Example dictionary:

d = {

  'Harry': 500,

  'Ron': 450,

  'Draco': -325

}

With this example dictionary, 2 kids have positive scores. And for the bonus, Draco has the most-negative score.

 

Problem #4. Counting cards

Jeff Ma has started to count cards at the casino.  He has recorded which cards he has seen in cards.txt.

Make a dictionary that maps each suit to a set of numbers seen.  Read cards.txt to figure out which cards he has seen.  Each line in cards.txt will have a card's suit and number (1-13) e.g. Heart 12.  Once all cards have been read, figure out which cards Jeff hasn't yet seen so he can decide which play to make.  Print out the various cards he hasn't seen.

Hint: It may be useful to make a list of suits, such as SUITS = ['Spade', 'Club', 'Heart', 'Diamond']

 

Project: Store (Part 1)

For our project, we will be making a representation of a store.  For the first step in making our store, we want to greet our customer (perhaps with a randomly selected greeting out of a list of possible greetings?), display the items available for purchase, and ask the customer which item he would like to purchase.

An example of a list of store items is a selection of the food emojis, listed here: food.txt.  This list contains each item's emoji, full name, quantity in stock, and price, all of which can be displayed to the customer.  Feel free to use this list or to make your own.

Make it FUN 🥳

 

Project: Store (Part 2)

For part 2, we want to add functionality to our store.  Let's allow the customer to select which item he wants to purchase.  When the customer makes a selection, be sure to ask how much quantity he wants.  Then verify that the store has enough quantity to sell and that the customer has enough money to make the purchase.

Feel free to come up with additional functionality for the store.  Perhaps you allow the user to sell items to the store, like a pawn shop, and subsequently you can purchase the items back from the store.  Perhaps the store sells lottery tickets, whose winnings can be computed on the spot.  Or perhaps the store will give you a loan.  Be creative!

 

Project: Store (Part 3)

For part 3, we want to automate our store.  Now we will read our inventory from this file, prices from this file, and the customer purchases from this file.  Keep reading the purchases until you reach the "End of day" line.

- The inventory file is written with lines in the format <product>,<quantity>. For instance, the line "Pineapple,250" indicates that you have 250 pineapples to sell

- The prices file is written with lines in the format <product>,<price>. For instance, the line "Pineapple,1.75" indicates that you sell pineapples at a price of $1.75

- The purchases file is written with lines in the format <product>,<quantity>. For instance, the line "Pineapple 20" indicates that 20 pineapples are being purchased

Furthermore, we are introducing the concept of a store's cash on hand.  Let's assume that all of the store's money was used to purchase the inventory now listed in the inventory file.  Assuming that each customer can pay for the purchases listed in the purchase file, record how much revenue your store gains as a result of each sale.  For instance, selling 20 pineapples at a price of $1.75 earns your store $35 in revenue.  Tally up the revenue from all sales and print out he store's cash at the end of the day.

Note: keep in mind that if your store does not have sufficient inventory to fulfill an order, you can sell as much as you have, but no more.  So if you have 250 pineapples and customers wish to purchase 300, you can only sell the 250 you have.  Your revenue from the pineapple sales will be 250 x $1.75 = $437.50

 

Project: Store (Part 4)

For part 4, we want to take control of our inventory.  Rather than reading inventory from a file, we start with an empty store and some cash.  We then can use that cash to purchase items from a supplier at a low cost and then sell the items to customers at a higher cost.  Here is a file with the supplier's prices (assume the supplier has an infinite quantity of each item and can fulfill any purchase you wish to make).  Once you make your purchases from the supplier, you then read a new customer purchases file, fulfilling the customer's purchases the best you can.  Once again, you sell the items to customers at the prices listed in the prices file from part 3 and you read the customer purchases file until the "End of day" line.  Once you reach the end of the first day, you are then able to make more purchases from the supplier before continuing to read the purchases file for another day.  You are able to use the revenue from your Day 1 sales towards inventory purchases for Day 2.  For instance, if you start off with $10,000, spend $5,000 purchasing inventory before Day 1, and earn $6,000 in revenue from Day 1 sales, you now have $11,000 to spend towards inventory before Day 2.  Assume that inventory never spoils, so any unsold inventory remains available to sell in future days.

When it comes to purchasing inventory, you can either come up with a formula to let the program determine which purchases to make for you or you can make an interactive menu to allow the user to decide manually which items to purchase.

We will provide more customer purchase input files shortly.  For those who make the program make purchases automatically, we will provide another few input files and have a competition to see whose store is most profitable.