Skip to content

Commit d089072

Browse files
author
KitN
committed
Starting to work on the OOP material
1 parent fe98c10 commit d089072

6 files changed

Lines changed: 114104 additions & 1 deletion

File tree

exersizes/ana.shelf

11.6 MB
Binary file not shown.

exersizes/anadb.py

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,19 @@
1-
import anagram_sets
1+
import anagram_sets as anset
2+
import shelve
3+
4+
def store_anagrams(anadict, savefile):
5+
""" Stores the anagram dictionary in a pickle 'shelf'"""
6+
db = shelve.open(savefile)
7+
for key, grams in anadict.items():
8+
db[key] = grams
9+
10+
def read_anagrams(target, readfile):
11+
""" Finds the anagrams for a given word in the db."""
12+
sig = anset.signature(target)
13+
db = shelve.open(readfile)
14+
anagrams = db[sig]
15+
return anagrams
16+
17+
if __name__ == "__main__":
18+
shelffile = "ana.shelf"
19+
print(read_anagrams('vile', shelffile))

exersizes/classes/Point1.py

Lines changed: 142 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,142 @@
1+
"""This module contains a code example related to
2+
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
6+
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
10+
"""
11+
12+
from __future__ import print_function, division
13+
import math
14+
15+
16+
17+
class Point:
18+
"""Represents a point in 2-D space.
19+
20+
attributes: x, y
21+
"""
22+
23+
24+
def print_point(p):
25+
"""Print a Point object in human-readable format."""
26+
print('(%g, %g)' % (p.x, p.y))
27+
28+
29+
class Rectangle:
30+
"""Represents a rectangle.
31+
32+
attributes: width, height, corner.
33+
"""
34+
35+
36+
def find_center(rect):
37+
"""Returns a Point at the center of a Rectangle.
38+
39+
rect: Rectangle
40+
41+
returns: new Point
42+
"""
43+
p = Point()
44+
p.x = rect.corner.x + rect.width/2.0
45+
p.y = rect.corner.y + rect.height/2.0
46+
return p
47+
48+
49+
def grow_rectangle(rect, dwidth, dheight):
50+
"""Modifies the Rectangle by adding to its width and height.
51+
52+
rect: Rectangle object.
53+
dwidth: change in width (can be negative).
54+
dheight: change in height (can be negative).
55+
"""
56+
rect.width += dwidth
57+
rect.height += dheight
58+
59+
def move_rectangle(rect, dx, dy):
60+
"""Modifies the Rectangle by moving its corner.
61+
62+
rect: Rectangle object.
63+
dx: How many units to shift the corner (right is positive)
64+
dy: How many units to shift the corner (up is positive)
65+
"""
66+
rect.corner.x += dx
67+
rect.corner.y += dy
68+
69+
def deep_move_rectangle(rect, dx, dy):
70+
"""Modifies the Rectangle by moving its corner.
71+
Returns a new, deep copy of the rect.
72+
73+
rect: Rectangle object.
74+
dx: How many units to shift the corner (right is positive)
75+
dy: How many units to shift the corner (up is positive)
76+
"""
77+
import copy
78+
rect2 = copy.deepcopy(rect)
79+
rect2.corner.x += dx
80+
rect2.corner.y += dy
81+
return rect2
82+
83+
def distance_bw_points(alpha, beta):
84+
"""Takes two points, alpha and beta, and returns a float according to their
85+
Euclidean distance.
86+
87+
alpha, beta: Point objects
88+
"""
89+
x1, y1 = alpha.x, alpha.y
90+
x2, y2 = beta.x, beta.y
91+
distance = math.sqrt((x2-x1) ** 2 + (y2-y1)**2)
92+
return distance
93+
94+
class Circle:
95+
""" Represents a circle in 2D space
96+
97+
centrepoint: a Point
98+
radius: the radius
99+
"""
100+
101+
def point_in_circle(pnt, crc):
102+
""" Returns true if and only if the point is in the circle
103+
104+
pnt: a Point
105+
crc: a Circle
106+
returns: True or False, when the point is in or out.
107+
"""
108+
dist_from_cent = distance_bw_points(pnt, crc.centre)
109+
if dist_from_cent <= crc.radius:
110+
return True
111+
else:
112+
return False
113+
114+
def rect_in_circle(rect, crc):
115+
"""Returns true iff all corners of a rectangle are in a circle.
116+
117+
crc: a Circle
118+
rect: a Rectangle
119+
120+
returns: True or False
121+
"""
122+
123+
def main():
124+
plato = Circle()
125+
plato.centre = Point()
126+
plato.centre.x = 150
127+
plato.centre.y = 100
128+
plato.radius = 75
129+
130+
hole = Point()
131+
hole.x = 160
132+
hole.y = 94
133+
134+
hole = Point()
135+
hole.x = 500
136+
hole.y = 0
137+
138+
print(f"The point is in the circle: ", point_in_circle(hole, plato))
139+
140+
if __name__ == '__main__':
141+
main()
142+

exersizes/classes/Time1.py

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
"""This module contains a code example related to
2+
3+
Think Python, 2nd Edition
4+
by Allen Downey
5+
http://thinkpython2.com
6+
7+
Copyright 2015 Allen Downey
8+
9+
License: http://creativecommons.org/licenses/by/4.0/
10+
"""
11+
12+
from __future__ import print_function, division
13+
14+
15+
class Time:
16+
"""Represents the time of day.
17+
18+
attributes: hour, minute, second
19+
"""
20+
21+
22+
def print_time(t):
23+
"""Prints a string representation of the time.
24+
25+
t: Time object
26+
"""
27+
print('%.2d:%.2d:%.2d' % (t.hour, t.minute, t.second))
28+
29+
30+
def int_to_time(seconds):
31+
"""Makes a new Time object.
32+
33+
seconds: int seconds since midnight.
34+
"""
35+
time = Time()
36+
minutes, time.second = divmod(seconds, 60)
37+
time.hour, time.minute = divmod(minutes, 60)
38+
return time
39+
40+
41+
def time_to_int(time):
42+
"""Computes the number of seconds since midnight.
43+
44+
time: Time object.
45+
"""
46+
minutes = time.hour * 60 + time.minute
47+
seconds = minutes * 60 + time.second
48+
return seconds
49+
50+
51+
def add_times(t1, t2):
52+
"""Adds two time objects.
53+
54+
t1, t2: Time
55+
56+
returns: Time
57+
"""
58+
assert valid_time(t1) and valid_time(t2)
59+
seconds = time_to_int(t1) + time_to_int(t2)
60+
return int_to_time(seconds)
61+
62+
63+
def valid_time(time):
64+
"""Checks whether a Time object satisfies the invariants.
65+
66+
time: Time
67+
68+
returns: boolean
69+
"""
70+
if time.hour < 0 or time.minute < 0 or time.second < 0:
71+
return False
72+
if time.minute >= 60 or time.second >= 60:
73+
return False
74+
return True
75+
76+
def is_after(time1, time2):
77+
"""Checks whether time2 is after time1
78+
"""
79+
one = time_to_int(time1)
80+
two = time_to_int(time2)
81+
return two > one
82+
83+
def increment(time, seconds):
84+
"""Modifies the given time by a number of seconds.
85+
"""
86+
secinc = seconds % 60
87+
mininc = seconds % 3600
88+
hourinc = seconds // 3600
89+
time.seconds += secinc
90+
time.minutes += mininc
91+
time.hours += hourinc
92+
return # This func is A modifier
93+
94+
def main():
95+
# if a movie starts at noon...
96+
noon_time = Time()
97+
noon_time.hour = 12
98+
noon_time.minute = 0
99+
noon_time.second = 0
100+
101+
print('Starts at', end=' ')
102+
print_time(noon_time)
103+
104+
# and the run time of the movie is 109 minutes...
105+
movie_minutes = 109
106+
run_time = int_to_time(movie_minutes * 60)
107+
print('Run time', end=' ')
108+
print_time(run_time)
109+
110+
# what time does the movie end?
111+
end_time = add_times(noon_time, run_time)
112+
print('Ends at', end=' ')
113+
print_time(end_time)
114+
115+
print(is_after(noon_time, end_time))
116+
117+
118+
if __name__ == '__main__':
119+
main()

exersizes/classes/timecube.py

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
from datetime import datetime
2+
from datetime import date
3+
4+
def what_weekday():
5+
today = date.today()
6+
wday = today.weekday()
7+
english = today.strftime('%A')
8+
print(f"Today is {english}")
9+
10+
def get_user_birthday():
11+
"""Requests user input from stdinput
12+
13+
returns: a date object
14+
"""
15+
# TODO: Error check input
16+
year = input('What year were you born? >')
17+
month = input('What month were you born? >')
18+
day = input('What day of the month were you born? >')
19+
bdstring = '-'.join([year, month, day])
20+
bday = datetime.strptime(bdstring, '%Y-%m-%d' )
21+
return bday
22+
23+
def user_age(bday):
24+
"""Given a user's birthday, prints their age to stdout
25+
bday: a datetime
26+
27+
returns: void. prints to stdout
28+
"""
29+
today = datetime.today()
30+
age = abs(today - bday)
31+
yearsold = age.days // 365
32+
return yearsold
33+
34+
def main():
35+
what_weekday()
36+
bd = get_user_birthday()
37+
print(user_age(bd))
38+
pass
39+
40+
if __name__ == "__main__":
41+
main()

0 commit comments

Comments
 (0)