DS2000 (Spring 2019, NCH) :: Lecture 2b

0. Administrivia

  1. Due today @ 9pm: first homework (submit via Blackboard)
  2. Due before Monday's lecture: pre-class quiz (via Blackboard; feel free to use book/notes/Python)
  3. Due next Friday @ 9pm: HW2 (submit via Blackboard)

1. Problem: Big Spender

Input a list of expenses from the user and output (a) the sum, (b) flag those that are bigger than 100, (c) the sum of those smaller than 100, and (d) the sum of those bigger than 100. All outputs should show two decimal places and a currency symbol.

In [3]:
thresh = 100

expenses = input("Please enter expenses: ").split()
sum = 0
sum_small = 0
sum_big = 0

for expense in expenses:
    expense = float(expense)
    sum += expense
    if expense > thresh:
        print(" Flag: ${:.2f}".format(expense))
        sum_big += expense
    else:
        sum_small += expense

print("Sum: ${:.2f}".format(sum))
print("Small Sum: ${:.2f}".format(sum_small))
print("Big Sum: ${:.2f}".format(sum_big))
Please enter expenses: 120.1 70 25 40 115.15
 Flag: $120.10
 Flag: $115.15
Sum: $370.25
Small Sum: $135.00
Big Sum: $235.25

2. Problem: List o' Names

Input a list of words from the user as first1 last1 first2 last2 ... and create (and print) a list [[first1 last1], [first2 last2], ...]

In [6]:
names = []

flat_names = input("Please enter names: ").split()

first = True

for flat in flat_names:
    if first:
        names.append([flat])
    else:
        names[len(names)-1].append(flat)
    
    first = not first

print(names)
Please enter names: alice carol billy bob sally jane
[['alice', 'carol'], ['billy', 'bob'], ['sally', 'jane']]

3. Problem: Letter o' the Day

Input a list of words from the user (assume all lower case), as well as their favorite letter (also lower case); now spell each word emphasizing if the word is a favorite, and each letter as favorite or not (with OMG!!!).

In [11]:
words = input("Please enter words: ").split()
fave = input("Please enter favorite letter: ")

for word in words:
    if fave in word:
        print("{} (OMG!!!)".format(word))
    else:
        print(word)
    
    for letter in word:
        if letter == fave:
            print(" {} (OMG!!!)".format(letter))
        else:
            print(" {}".format(letter))
Please enter words: one two three four five
Please enter favorite letter: e
one (OMG!!!)
 o
 n
 e (OMG!!!)
two
 t
 w
 o
three (OMG!!!)
 t
 h
 r
 e (OMG!!!)
 e (OMG!!!)
four
 f
 o
 u
 r
five (OMG!!!)
 f
 i
 v
 e (OMG!!!)

4. Problem: Identifying Suspects

Given a list-of-lists of passengers (name, age, citizenship, passport number) and input search criteria (minimum age, maximum age, word to find in their name, and citizenship), find the suspect(s) that match (with a sad-face if no one found).

In [19]:
passengers = [
    [['alice', 'bob'], 39, 'usa', 111],
    [['bob', 'carol'], 42, 'usa', 22],
    [['carol', 'dan'], 27, 'france', 3],
    [['dan', 'erin'], 57, 'usa', 4444],
    [['erin', 'alice', 50, 'south africa', 55555]]
]

min_age = int(input("Enter minimum age: "))
max_age = int(input("Enter maximum age: "))
name_part = input("Enter either first or last name: ")
citizenship = input("Enter citizenship: ")

found = False

for person in passengers:
    if (name_part in person[0]) and (person[1] >= min_age) and (person[1] <= max_age) and (person[2] == citizenship):
        found = True
        print("Found: {} ({} {}, age {}, from {})".format(person[3], person[0][0], person[0][1], person[1], person[2]))

if not found:
    print("No suspects found :(")
Enter minimum age: 30
Enter maximum age: 50
Enter either first or last name: bob
Enter citizenship: usa
Found: 111 (alice bob, age 39, from usa)
Found: 22 (bob carol, age 42, from usa)
In [ ]: