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

0. Administrivia

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

1. Problem: Add/Subtract

Input a list of numbers from the user (space-separated), output the result of adding all the "even'th" elements and subtracting the odd

In [2]:
nums = input("Enter a list of numbers: ").split()
total = 0

for index, num in enumerate(nums):
    if index % 2 == 0:
        total += float(num)
    else:
        total -= float(num)

print("Total: {}".format(total))
Enter a list of numbers: 1 3 5 6
Total: -3.0

2. Problem: Find in a List

Input a list of strings and a query string, output all indexes in which the query appears in the list

In [3]:
words = input("Enter a list of words: ").split()
query = input("Enter a query word: ")

result = []

for index, word in enumerate(words):
    if word == query:
        result.append(str(index))

if len(result) == 0:
    print("Query string not found")
elif len(result) == 1:
    print("Query found once: {}".format(result[0]))
else:
    print("Query found in the following locations: {}".format(", ".join(result)))
Enter a list of words: alice bob carol bob
Enter a query word: bob
Query found in the following locations: 1, 3

3. Problem: Two Common Multiples

Input two natural numbers (integers, >=0); find all natural numbers less than 100 that are multiples of both

In [4]:
num1 = int(input("Enter first number: "))
num2 = int(input("Enter second number: "))

mults1 = range(0, 100, num1)
mults2 = range(0, 100, num2)

result = []

for num in mults1:
    if num in mults2:
        result.append(str(num))

print("Common multiples: {}".format(", ".join(result)))
Enter first number: 3
Enter second number: 5
Common multiples: 0, 15, 30, 45, 60, 75, 90

3a. Challenge: Lots o' Common Multiples

Input some number of natural numbers; find all natural numbers less than 100 that are multiples of all

In [5]:
nums = input("Enter numbers: ").split()

mults = []
for num in nums:
    mults.append(range(0, 100, int(num)))

result = []
for num in mults[0]:
    all_good = True
    
    for mult_index in range(1, len(mults)):
        if num not in mults[mult_index]:
            all_good = False
    
    if all_good:
        result.append(str(num))

print("Common multiples: {}".format(", ".join(result)))
Enter numbers: 3 5 10 20
Common multiples: 0, 60

4. Problem: Guessing Game

Let the user guess numbers (1-100) until they get the "secret" - each time tell them whether their guess was too high/low; when the user guesses correctly, output congratulations and how many guesses

In [6]:
secret = 77

done = False
count = 0

while not done:
    guess = int(input("Guess a number (1-100): "))
    count += 1
    
    if guess > secret:
        print("Too high")
    elif guess < secret:
        print("Too low")
    else:
        done = True

ending = "es"
if count == 1:
    ending = ""
        
print("Congratulations - that took you only {} guess{}!".format(count, ending))
Guess a number (1-100): 50
Too low
Guess a number (1-100): 75
Too low
Guess a number (1-100): 88
Too high
Guess a number (1-100): 81
Too high
Guess a number (1-100): 78
Too high
Guess a number (1-100): 77
Congratulations - that took you only 6 guesses!

5. Problem: Income Tax

User enters income; given single tax bracket (https://www.nerdwallet.com/blog/taxes/federal-income-tax-brackets/), compute tax owed, highest bracket, effective rate

In [7]:
bracket = [ 12000, 0.37,
 [[     1,   9525, 0.10],
  [  9526,  38700, 0.12],
  [ 38701,  82500, 0.22],
  [ 82501, 157500, 0.24],
  [157501, 200000, 0.32],
  [200001, 500000, 0.35]]]

income = round(float(input("Enter income: ")))

agi = income - bracket[0]
if agi <= 0:
    print("You do not owe any taxes!")
else:
    taxes = 0
    toprate = 0
    
    for b in bracket[2]:
        lowest_agi = b[0]
        highest_agi = b[1]
        rate = b[2]
        
        if agi >= lowest_agi:    
            toprate = rate
            
            if agi > highest_agi:
                taxes += (highest_agi - lowest_agi + 1) * rate
            else:
                taxes += (agi - lowest_agi + 1) * rate
    
    top_salary_limit = bracket[2][len(bracket[2])-1][1]
    if agi > top_salary_limit:
        toprate = bracket[1]
        taxes += (agi - top_salary_limit) * toprate
    
    pattern = "You are in the {:.0f}% bracket; you owe ${:.0f} in taxes for an effective rate of {:.0f}%."
    
    print(pattern.format(toprate*100, taxes, 100 * (taxes / income)))
Enter income: 100000
You are in the 24% bracket; you owe $15410 in taxes for an effective rate of 15%.