Skip to content

Commit fca53f7

Browse files
bites 187
1 parent 2921045 commit fca53f7

File tree

3 files changed

+75
-0
lines changed

3 files changed

+75
-0
lines changed

116/files.py

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
from glob import glob
22
import os
3+
import random
34

45
ONE_KB = 1024
56

187/howold.py

+36
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
from dataclasses import dataclass
2+
3+
from dateutil import utils
4+
5+
6+
@dataclass
7+
class Actor:
8+
name: str
9+
born: str
10+
11+
12+
@dataclass
13+
class Movie:
14+
title: str
15+
release_date: str
16+
17+
18+
def get_age(actor: Actor, movie: Movie) -> str:
19+
"""Calculates age of actor / actress when movie was released,
20+
return a string like this:
21+
22+
{name} was {age} years old when {movie} came out.
23+
e.g.
24+
Wesley Snipes was 28 years old when New Jack City came out.
25+
"""
26+
actor_born = utils.datetime.strptime(actor.born, "%B %d, %Y")
27+
movie_born = utils.datetime.strptime(movie.release_date, "%B %d, %Y")
28+
# actor_born = dateutil.utils.datetime.strptime()
29+
if actor_born.month < movie_born.month:
30+
year = movie_born.year-actor_born.year
31+
else:
32+
#By comparing year, we assume the end of the year
33+
year = movie_born.year-actor_born.year-1
34+
return f"{actor.name} was {year} years old when {movie.title} came out."
35+
36+
get_age(Actor('Wesley Snipes', 'July 31, 1962'),Movie('New Jack City', 'January 17, 1991'))

187/test_howold.py

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import pytest
2+
3+
from howold import Actor, Movie, get_age
4+
5+
6+
actors = [
7+
Actor('Wesley Snipes', 'July 31, 1962'),
8+
Actor('Robert de Niro', 'August 17, 1943'),
9+
Actor('Jennifer Aniston', 'February 11, 1969'),
10+
Actor('Mikey Rourke', 'September 16, 1952'),
11+
Actor('Al Pacino', 'July 31, 1962'),
12+
Actor('Alec Baldwin', 'July 31, 1962'),
13+
Actor('Michelle Pfeiffer', 'April 29, 1958'),
14+
]
15+
movies = [
16+
Movie('New Jack City', 'January 17, 1991'),
17+
Movie('Goodfellas', 'October 19, 1990'),
18+
Movie('Horrible Bosses', 'September 16, 2011'),
19+
Movie('Harley Davidson and the Marlboro Man', 'December 28, 1991'),
20+
Movie('Heat', 'December 15, 1995'),
21+
Movie('Glengarry Glen Ross', 'September 29, 1992'),
22+
Movie('Scarface', 'March 12, 1984'),
23+
]
24+
return_strings = [
25+
'Wesley Snipes was 28 years old when New Jack City came out.',
26+
'Robert de Niro was 47 years old when Goodfellas came out.',
27+
'Jennifer Aniston was 42 years old when Horrible Bosses came out.',
28+
'Mikey Rourke was 39 years old when Harley Davidson and the Marlboro Man came out.', # noqa E501
29+
'Al Pacino was 33 years old when Heat came out.',
30+
'Alec Baldwin was 30 years old when Glengarry Glen Ross came out.',
31+
'Michelle Pfeiffer was 25 years old when Scarface came out.',
32+
]
33+
34+
35+
@pytest.mark.parametrize('actor, movie, expected',
36+
zip(actors, movies, return_strings))
37+
def test_get_age(actor, movie, expected):
38+
assert get_age(actor, movie) == expected

0 commit comments

Comments
 (0)