Skip to content

Commit 53b6810

Browse files
committed
Solved all chapter 8 problems
1 parent b8f5ac3 commit 53b6810

27 files changed

+1408
-0
lines changed

Chapter 8/Problem8_20.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
class BankAccount:
2+
3+
def __init__(self,money=0):
4+
self.money = money
5+
6+
def withdraw(self, amount):
7+
self.money -= amount
8+
9+
def deposit(self, amount):
10+
self.money += amount
11+
12+
def balance(self):
13+
return self.money
14+
15+

Chapter 8/Problem8_21.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import math
2+
class Polygon:
3+
4+
def __init__(self, sides, lenght):
5+
self.n = sides
6+
self.s = lenght
7+
8+
def perimeter(self):
9+
return self.n * self.s
10+
11+
def area(self):
12+
return (((self.s**2) * self.n)/(4 * (math.tan(math.pi/self.n))))
13+
14+

Chapter 8/Problem8_22.py

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
class Worker:
2+
3+
def __init__ (self, stringname, hourlypay):
4+
self.name = stringname
5+
self.wage = hourlypay
6+
7+
def changeRate(self, newrate):
8+
self.wage = newrate
9+
10+
def pay(self, hours):
11+
print('Not Implemented')
12+
13+
class HourlyWorker(Worker):
14+
15+
def pay(self, hours):
16+
money = 0
17+
for i in range(1, hours + 1):
18+
if i <= 40:
19+
money += self.wage
20+
else:
21+
money += (self.wage) * 2
22+
return money
23+
24+
class SalariedWorker(Worker):
25+
26+
def pay(self, hours=0):
27+
return self.wage * 40
28+
29+

Chapter 8/Problem8_23.py

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
import math
2+
class Point:
3+
4+
def __init__(self,x=0, y=0):
5+
self.cordx = x
6+
self.cordy = y
7+
8+
class Segment:
9+
10+
def __init__(self, point1, point2):
11+
self.ponto1 = point1
12+
self.ponto2 = point2
13+
14+
def length(self):
15+
seg = math.sqrt(((self.ponto2.cordx - self.ponto1.cordx)**2)+((self.ponto2.cordy - self.ponto1.cordy)**2))
16+
return seg
17+
18+
def slope(self):
19+
slop = ((self.ponto2.cordx - self.ponto1.cordx)/(self.ponto2.cordy - self.ponto1.cordy))
20+
return slop

Chapter 8/Problem8_24.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
import time
2+
class Person:
3+
4+
def __init__(self, name, byear):
5+
self.nome = name
6+
self.birthyear = byear
7+
8+
def age(self):
9+
idade = (int(time.strftime('%Y', time.localtime()))) - self.birthyear
10+
return idade
11+
12+
def name(self):
13+
return self.nome
14+

Chapter 8/Problem8_25.py

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
class Textfile:
2+
3+
def __init__(self, filename):
4+
5+
file = open(filename)
6+
content = file.read()
7+
file.close
8+
self.file = content
9+
10+
def nchars(self):
11+
12+
charnum = 0
13+
content = self.file
14+
charnum = len(content)
15+
return charnum
16+
17+
def nwords(self):
18+
19+
numwords = 0
20+
content= self.file
21+
content = content.split()
22+
numwords = len(content)
23+
return numwords
24+
25+
def nlines(self):
26+
27+
numlines = 0
28+
content = self.file
29+
content = content.split('\n')
30+
numlines = len(content)
31+
return numlines
32+
33+
def read(self):
34+
35+
return self.file
36+
37+
def readlines(self, n):
38+
39+
content = self.file
40+
content = content.split('\n')
41+
return content
42+
43+
def grep(self, string):
44+
45+
result = ''
46+
lines = []
47+
content = self.file
48+
content = content.split('\n')
49+
for i in range(len(content)):
50+
if string in content[i]:
51+
lines.append(i)
52+
for line in lines:
53+
result += '{}: '.format(line)
54+
result +='{} \n'.format(content[line])
55+
print(result)
56+

Chapter 8/Problem8_26.py

Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
class Textfile:
2+
3+
def __init__(self, filename):
4+
5+
file = open(filename)
6+
content = file.read()
7+
file.close
8+
self.file = content
9+
10+
def nchars(self):
11+
12+
charnum = 0
13+
content = self.file
14+
charnum = len(content)
15+
return charnum
16+
17+
def nwords(self):
18+
19+
numwords = 0
20+
content= self.file
21+
content = content.split()
22+
numwords = len(content)
23+
return numwords
24+
25+
def nlines(self):
26+
27+
numlines = 0
28+
content = self.file
29+
content = content.split('\n')
30+
numlines = len(content)
31+
return numlines
32+
33+
def read(self):
34+
35+
return self.file
36+
37+
def readlines(self, n):
38+
39+
content = self.file
40+
content = content.split('\n')
41+
return content
42+
43+
def grep(self, string):
44+
45+
result = ''
46+
lines = []
47+
content = self.file
48+
content = content.split('\n')
49+
for i in range(len(content)):
50+
if string in content[i]:
51+
lines.append(i)
52+
for line in lines:
53+
result += '{}: '.format(line)
54+
result +='{} \n'.format(content[line])
55+
print(result)
56+
57+
def words(self):
58+
59+
content = self.file
60+
content = content.split()
61+
content = list(set(content))
62+
return content
63+

Chapter 8/Problem8_27.py

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
class Textfile:
2+
3+
def __init__(self, filename):
4+
5+
file = open(filename)
6+
content = file.read()
7+
file.close
8+
self.file = content
9+
10+
def nchars(self):
11+
12+
charnum = 0
13+
content = self.file
14+
charnum = len(content)
15+
return charnum
16+
17+
def nwords(self):
18+
19+
numwords = 0
20+
content= self.file
21+
content = content.split()
22+
numwords = len(content)
23+
return numwords
24+
25+
def nlines(self):
26+
27+
numlines = 0
28+
content = self.file
29+
content = content.split('\n')
30+
numlines = len(content)
31+
return numlines
32+
33+
def read(self):
34+
35+
return self.file
36+
37+
def readlines(self, n):
38+
39+
content = self.file
40+
content = content.split('\n')
41+
return content
42+
43+
def grep(self, string):
44+
45+
result = ''
46+
lines = []
47+
content = self.file
48+
content = content.split('\n')
49+
for i in range(len(content)):
50+
if string in content[i]:
51+
lines.append(i)
52+
for line in lines:
53+
result += '{}: '.format(line)
54+
result +='{} \n'.format(content[line])
55+
print(result)
56+
57+
def words(self):
58+
59+
content = self.file
60+
content = content.split()
61+
content = list(set(content))
62+
return content
63+
64+
def occurrences(self):
65+
66+
ocur = {}
67+
content = self.file
68+
content = content.split()
69+
content = set(content)
70+
for word in content:
71+
ocur[word] = 0
72+
content = self.file.split()
73+
for word in content:
74+
ocur[word] += 1
75+
return ocur
76+
77+

Chapter 8/Problem8_28.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
class Textfile:
2+
3+
def __init__(self, filename):
4+
5+
file = open(filename)
6+
content = file.read()
7+
file.close
8+
self.file = content
9+
10+
def nchars(self):
11+
12+
charnum = 0
13+
content = self.file
14+
charnum = len(content)
15+
return charnum
16+
17+
def nwords(self):
18+
19+
numwords = 0
20+
content= self.file
21+
content = content.split()
22+
numwords = len(content)
23+
return numwords
24+
25+
def nlines(self):
26+
27+
numlines = 0
28+
content = self.file
29+
content = content.split('\n')
30+
numlines = len(content)
31+
return numlines
32+
33+
def read(self):
34+
35+
return self.file
36+
37+
def readlines(self, n):
38+
39+
content = self.file
40+
content = content.split('\n')
41+
return content
42+
43+
def grep(self, string):
44+
45+
result = ''
46+
lines = []
47+
content = self.file
48+
content = content.split('\n')
49+
for i in range(len(content)):
50+
if string in content[i]:
51+
lines.append(i)
52+
for line in lines:
53+
result += '{}: '.format(line)
54+
result +='{} \n'.format(content[line])
55+
print(result)
56+
57+
def words(self):
58+
59+
content = self.file
60+
content = content.split()
61+
content = list(set(content))
62+
return content
63+
64+
def occurrences(self):
65+
66+
ocur = {}
67+
content = self.file
68+
content = content.split()
69+
content = set(content)
70+
for word in content:
71+
ocur[word] = 0
72+
content = self.file.split()
73+
for word in content:
74+
ocur[word] += 1
75+
return ocur
76+
77+
def average(self):
78+
content = self.file
79+
for symbol in '.?!':
80+
content = content.replace(symbol, '%')
81+
content = content.replace('\n', '')
82+
content = content.split('%')
83+
avg = 0
84+
wordssentence = []
85+
morewords = 0
86+
fewerwords = 100
87+
for i in range(len(content)-1):
88+
wordssentence.append(content[i].split())
89+
for listwords in wordssentence:
90+
for word in listwords:
91+
avg += 1
92+
avg = avg / len(content)
93+
for i in range(len(wordssentence)):
94+
if len(wordssentence[i]) > morewords:
95+
morewords = len(wordssentence[i])
96+
for i in range(len(wordssentence)-1):
97+
if len(wordssentence[i])<fewerwords:
98+
fewerwords = len(wordssentence[i])
99+
tupla = (avg, morewords, fewerwords)
100+
return tupla
101+
102+

0 commit comments

Comments
 (0)