Skip to content

Instantly share code, notes, and snippets.

@MarketaP
Created February 7, 2019 16:56
Show Gist options
  • Save MarketaP/112950b50dda25d26d2eae50e693cefd to your computer and use it in GitHub Desktop.
Save MarketaP/112950b50dda25d26d2eae50e693cefd to your computer and use it in GitHub Desktop.
# -*- coding: utf-8 -*-
"""
Spyder Editor
This is a temporary script file.
"""
"""
Homework 3.
We will be submitting this assignment on Canvas.
It's due before Feb 4th 11:59pm.
"""
# Problem 1
# This exercise is taken from Think Python 2nd Edition
# The following functions are all intended to check whether a string contains
# any lowercase letters, but at least some of them are wrong.
# For each function, describe what the function actually does
# (assuming that the parameter is a string).
# Write your answer in this file.
# This function checks if the first letter is upper or lower case, but ends
# every time after chacking the first (one) letter.
def any_lowercase1(s):
for c in s:
if c.islower():
return True
else:
return False
# This function checks if "c" is lowecase which is always "True" as a string
# and not a boolean value, therefore this function doesn't work either.
def any_lowercase2(s):
for c in s:
if 'c'.islower():
return 'True'
else:
return 'False'
# For every letter in string it writes T/F in flag, but overwrites for each letter,
# so it will return if the last letter is lowercase. This function does not give us what we want.
def any_lowercase3(s):
for c in s:
flag = c.islower()
return flag
# This function works. Logical OR: accumulating the value by using the logical OR operator.
# It starts with False as a value of the flag for the case when all letters in a
# string are upper case c.islower() is always False. Whenever there is a lowercase letter c.islower()
# will be true. Once the flag is true it will always be true, because any combination
# (True, False) = True.
def any_lowercase4(s):
flag = False
for c in s:
flag = flag or c.islower()
return flag
# This function doesn't work for our purpose. It works the opposite way,
# it checks if all the letters are lower case. If there is one or more upper
# case it returns False
def any_lowercase5(s):
for c in s:
if not c.islower():
return False
return True
# Problem 2
# You retrieved data from a file and get timestamps
# The timestamps are strings are on a 24 hr clock.
# They are formatted like this: "1990-12-01 1300" "1990-01-01 0100"
# The timestamps are contained in a list.
# Write code that creates a list of dictionaries where each dictionary
# looks like {"year": 1990, "month": 1, "day": 1, "hour": 1, "minute": 0}
# Note that you need to convert the times to ints
### NEEDS CORRECT SORTING
data = ["1990-12-01 1300", "1990-01-01 0100"]
def create_date_dictionary(date_string):
'''Creates a dictionary from a date_time string in format "yyyy-mm-dd hhmm"'''
date = (date_string[:10])
time = date_string[11:]
year = int(date[:4])
month = int(date[5:7])
day = int(date[8:10])
hour = int(time[:2])
minute = int(time[2:])
return {"year" : year,
"month" : month,
"day" : day,
"hour" : hour,
"minute" : minute}
new_list = [create_date_dictionary(element) for element in data]
print(new_list)
# Problem 3
# You will have the oscar_awards for some years as a list. The list is separated by: year of award (integer),
# first name of actress (string), last name of actress (string), role (string), movie(string),date of birth (string).
# Create a dictionary using the the first name as a key, and the rest of the info as a list.
# You may use a dictionary comprehension or a for loop.
oscar_list = [
[2012, 'Jennifer', 'Lawrence', 'Tiffany Maxwell', 'Silver Linings Playbook', '08/15/1990'],
[2011, 'Meryl', 'Streep', 'Margaret Thatcher', 'The Iron Lady', '06/22/1949'],
[2010, 'Natalie', 'Portman', 'Nina Sayers', 'Black Swan', '06/09/1981'],
[2009, 'Sandra', 'Bullock', 'Leigh Anne Tuohy', 'The Blind Side', '07/26/1964']
]
def create_oscar_dictionary(sequence):
oscar_dictionary = {}
for oscar in sequence:
first_name = oscar[1]
the_rest = oscar[:1] + oscar[2:]
oscar_dictionary.update({first_name : the_rest})
return oscar_dictionary
print(create_oscar_dictionary(oscar_list))
# Problem 4
# You will have a dictionary of the oscar_awards for some years and the name of actress as a key.
# The last value of each list contains the timestamp of the actress's birthday.
# Get the age on 01/01/2019 of each winner.
# Print their first name and age e.g. Sandra 20 (not her real age in the example)
from datetime import date
oscar_awards = {
'Sandra': [2009, 'Leigh Anne Tuohy', 'The Blind Side', '07/26/1964'],
'Natalie': [2010, 'Nina Sayers', 'Black Swan', '06/09/1981'],
'Meryl': [2011, 'Margaret Thatcher', 'The Iron Lady', '06/22/1949'],
'Jennifer': [2012, 'Tiffany Maxwell', 'Silver Linings Playbook', '08/15/1990']
}
def create_date_from_string(string, delimiter = "/"):
my_date = string.split(delimiter)
my_date_list = [int(number) for number in my_date]
return date(my_date_list[2], my_date_list[0], my_date_list[1])
print(create_date_from_string('06/22/1949'))
def calculate_age(born):
new_year_2019 = create_date_from_string('01/01/2019')
born_date = create_date_from_string(born)
return new_year_2019.year - born_date.year - ((new_year_2019.month, new_year_2019.day) < (born_date.month, born_date.day))
for first_name, the_rest in oscar_awards.items():
date_of_birth = the_rest[-1]
print(first_name, calculate_age(date_of_birth))
# Problem 5
# Same dataset of problem 4. The last value of the list contain the timestamp of their birthday
# Replace each last index by their age in 2019. Output:
# Oscar_awards = {'Sandra': [2009, 'Leigh Anne Tuohy', 'The Blind Sid', '55 yrs'],
# 'Natalie': [2010, 'Nina Sayers', 'Black Swan', '38 yrs'],
# 'Meryl': [2011, 'Margaret Thatcher', 'The Iron Lady', '70 yrs'],
# 'Jennifer': [2012, 'Tiffany Maxwell', 'Silver Linings Playbook', '28 yrs']
# }
oscar_awards = {
'Sandra': [2009, 'Leigh Anne Tuohy', 'The Blind Side', '07/26/1964'],
'Natalie': [2010, 'Nina Sayers', 'Black Swan', '06/09/1981'],
'Meryl': [2011, 'Margaret Thatcher', 'The Iron Lady', '06/22/1949'],
'Jennifer': [2012, 'Tiffany Maxwell', 'Silver Linings Playbook', '08/15/1990']
}
for first_name, the_rest in oscar_awards.items():
date_of_birth = the_rest[-1]
the_rest[-1] = calculate_age(date_of_birth)
print(oscar_awards)
# Problem 6
# With the dataset of problem 4 and the birthplace list provided below,
# append the birthplace to the end of each actors list.
oscar_awards = {
'Sandra': [2009, 'Leigh Anne Tuohy', 'The Blind Side', '07/26/1964'],
'Natalie': [2010, 'Nina Sayers', 'Black Swan', '06/09/1981'],
'Meryl': [2011, 'Margaret Thatcher', 'The Iron Lady', '06/22/1949'],
'Jennifer': [2012, 'Tiffany Maxwell', 'Silver Linings Playbook', '08/15/1990']
}
birthplaces = [
['Jennifer Lawrence', 'Kentucky'],
['Meryl Streep', 'New Jersey'],
['Natalie Portman', 'Jerusalem'],
['Sandra Bullock', 'Virginia']
]
birthplace_dict = {}
for birthplace in birthplaces:
first_name_2 = birthplace[0].split(" ")[0]
birthplace_dict.update({first_name_2 : birthplace[1]})
for first_name, the_rest in oscar_awards.items():
the_rest.append(birthplace_dict[first_name])
print(oscar_awards)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment