From 914bbb1a443f089e11405672cf8d9b00ab2e5be6 Mon Sep 17 00:00:00 2001 From: ltbatista30 Date: Thu, 2 Jul 2020 12:27:13 -0300 Subject: [PATCH 1/6] Exercises ch 2 --- code/exercicses/chapter-2/breakfast.py | 17 +++++++++++++++++ code/exercicses/chapter-2/calc-vol.py | 14 ++++++++++++++ code/exercicses/chapter-2/cover-book.py | 20 ++++++++++++++++++++ 3 files changed, 51 insertions(+) create mode 100644 code/exercicses/chapter-2/breakfast.py create mode 100644 code/exercicses/chapter-2/calc-vol.py create mode 100644 code/exercicses/chapter-2/cover-book.py diff --git a/code/exercicses/chapter-2/breakfast.py b/code/exercicses/chapter-2/breakfast.py new file mode 100644 index 0000000..90ccc4a --- /dev/null +++ b/code/exercicses/chapter-2/breakfast.py @@ -0,0 +1,17 @@ + + +def main(): + e_pace = 8*60+15 ## easy pace in seconds + t_mile = 7*60+12 ## tempo in seconds + # I run 1 mile at Easy Pace + 3 Miles at tempo + 1 more at Easy Pace + total_min = (e_pace+3*t_mile+e_pace)/60 #Total in minutes + ini_hour_m = 6*60+52 ## departure time in minutes + return_hour_h = (total_min+ini_hour_m)/60 + #print(return_hour_h) + h = int(str(return_hour_h).split('.')[0]) + m = int(float('0.'+str(return_hour_h).split('.')[1][0:2])*60) + + print('Eu voltarei para casa as {}:{}'.format(h,m)) + +if __name__ == "__main__": + main() diff --git a/code/exercicses/chapter-2/calc-vol.py b/code/exercicses/chapter-2/calc-vol.py new file mode 100644 index 0000000..606cf86 --- /dev/null +++ b/code/exercicses/chapter-2/calc-vol.py @@ -0,0 +1,14 @@ +def sphere_vol(r): + pi = 3.14 + vol = (4/3)*pi*r**3 + return vol + +def main(): + print("Digite o raio: ") + raio = int(input()) + volume = sphere_vol(raio) + print("O volume de uma esfera com raio {} é: {}".format(raio, round(volume,2))) + +if __name__ == "__main__": + main() + diff --git a/code/exercicses/chapter-2/cover-book.py b/code/exercicses/chapter-2/cover-book.py new file mode 100644 index 0000000..7e9546c --- /dev/null +++ b/code/exercicses/chapter-2/cover-book.py @@ -0,0 +1,20 @@ +def calc_price(cb_price, copies): + ## calculates the 40% off + price = cb_price*0.6 + ## calculates the shipping cost + price = price + 3 + if copies > 1: + addcopy = (copies-1)*0.75 + else: + addcopy = 0 + return price + addcopy + +def main(): + cb_price = 24.95 + qtd_cp = 60 + price = calc_price(cb_price, qtd_cp) + print(price) + +if __name__ == "__main__": + main() + From c6dd085404a14599b1c26bd9eac131f4a2378458 Mon Sep 17 00:00:00 2001 From: ltbatista30 Date: Thu, 2 Jul 2020 18:07:54 -0300 Subject: [PATCH 2/6] correcao nome da pasta --- code/{exercicses => exercises}/chapter-2/breakfast.py | 0 code/{exercicses => exercises}/chapter-2/calc-vol.py | 0 code/{exercicses => exercises}/chapter-2/cover-book.py | 0 3 files changed, 0 insertions(+), 0 deletions(-) rename code/{exercicses => exercises}/chapter-2/breakfast.py (100%) rename code/{exercicses => exercises}/chapter-2/calc-vol.py (100%) rename code/{exercicses => exercises}/chapter-2/cover-book.py (100%) diff --git a/code/exercicses/chapter-2/breakfast.py b/code/exercises/chapter-2/breakfast.py similarity index 100% rename from code/exercicses/chapter-2/breakfast.py rename to code/exercises/chapter-2/breakfast.py diff --git a/code/exercicses/chapter-2/calc-vol.py b/code/exercises/chapter-2/calc-vol.py similarity index 100% rename from code/exercicses/chapter-2/calc-vol.py rename to code/exercises/chapter-2/calc-vol.py diff --git a/code/exercicses/chapter-2/cover-book.py b/code/exercises/chapter-2/cover-book.py similarity index 100% rename from code/exercicses/chapter-2/cover-book.py rename to code/exercises/chapter-2/cover-book.py From abdb36968096357b2eb9622ad7d78ccd89775189 Mon Sep 17 00:00:00 2001 From: ltbatista30 Date: Thu, 2 Jul 2020 18:52:07 -0300 Subject: [PATCH 3/6] correcao exercicio --- code/exercises/chapter-2/cover-book.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/code/exercises/chapter-2/cover-book.py b/code/exercises/chapter-2/cover-book.py index 7e9546c..3778f85 100644 --- a/code/exercises/chapter-2/cover-book.py +++ b/code/exercises/chapter-2/cover-book.py @@ -2,18 +2,19 @@ def calc_price(cb_price, copies): ## calculates the 40% off price = cb_price*0.6 ## calculates the shipping cost - price = price + 3 if copies > 1: addcopy = (copies-1)*0.75 + price_add = price*(copies-1) else: addcopy = 0 - return price + addcopy + price_add = 0 + return price + addcopy + price_add + 3 def main(): cb_price = 24.95 qtd_cp = 60 price = calc_price(cb_price, qtd_cp) - print(price) + print(round(price,2)) if __name__ == "__main__": main() From 5e1d77258147bf1d0cffe5e3b1c58a5f92d59801 Mon Sep 17 00:00:00 2001 From: ltbatista Date: Sun, 12 Jul 2020 19:58:06 -0700 Subject: [PATCH 4/6] exercises chapter 3 --- code/exercises/chapter-3/do_four.py | 13 +++++++ code/exercises/chapter-3/grid.py | 44 +++++++++++++++++++++++ code/exercises/chapter-3/right-justify.py | 7 ++++ 3 files changed, 64 insertions(+) create mode 100644 code/exercises/chapter-3/do_four.py create mode 100644 code/exercises/chapter-3/grid.py create mode 100644 code/exercises/chapter-3/right-justify.py diff --git a/code/exercises/chapter-3/do_four.py b/code/exercises/chapter-3/do_four.py new file mode 100644 index 0000000..2b0469e --- /dev/null +++ b/code/exercises/chapter-3/do_four.py @@ -0,0 +1,13 @@ +def do_twice(f, a): + f(a) + f(a) + +def print_spam(): + print('spam') + +def print_twice(bruce): + print(bruce) + print(bruce) + +if __name__ == "__main__": + do_twice(print_twice, 'spam') diff --git a/code/exercises/chapter-3/grid.py b/code/exercises/chapter-3/grid.py new file mode 100644 index 0000000..c86c76a --- /dev/null +++ b/code/exercises/chapter-3/grid.py @@ -0,0 +1,44 @@ +def print_mais(): + print('+', end = ' ') + +def print4traco(): + print('-'*4, end = ' ') + +def print1pipe(): + print('|', end = ' ') + +def print1espaco(): + print(' ', end = ' ') + +def print4espaco(): + print(' '*4, end = ' ') + +def linha_traco(): + print_mais() + print4traco() + +def linha_espaco(): + print1pipe() + print4espaco() + +def do_four(f): + f() + f() + f() + f() + +def linha4brancopipe(): + do_four(linha_espaco) + print1pipe() + print('\n') + +def draw_grid(): + do_four(linha_traco) + print_mais() + print('\n') + do_four(linha4brancopipe) + +if __name__ == '__main__': + do_four(draw_grid) + do_four(linha_traco) + print_mais() diff --git a/code/exercises/chapter-3/right-justify.py b/code/exercises/chapter-3/right-justify.py new file mode 100644 index 0000000..9758be0 --- /dev/null +++ b/code/exercises/chapter-3/right-justify.py @@ -0,0 +1,7 @@ +def right_justify(s): + print(((70 - len(s))*' ')+s) + +if __name__ == "__main__": + print('right-justify\ninput a string: ') + inps = input() + right_justify(inps) From 6c44790c2059a1153e0d57fbdd5fef8daeb49710 Mon Sep 17 00:00:00 2001 From: ltbatista Date: Mon, 20 Jul 2020 20:58:47 -0700 Subject: [PATCH 5/6] chap 4 --- .../exercises/chapter-4/.vscode/settings.json | 3 + code/exercises/chapter-4/42.py | 101 ++++++++++++++++++ code/exercises/chapter-4/431.py | 10 ++ code/exercises/chapter-4/433.py | 10 ++ code/exercises/chapter-4/434.py | 10 ++ code/exercises/chapter-4/435.py | 10 ++ .../chapter-4/46-interface-design.py | 18 ++++ code/exercises/chapter-4/47-refactoring.py | 32 ++++++ code/exercises/chapter-4/mypolygon.py | 10 ++ code/exercises/chapter-4/polygon.py | 95 ++++++++++++++++ 10 files changed, 299 insertions(+) create mode 100644 code/exercises/chapter-4/.vscode/settings.json create mode 100644 code/exercises/chapter-4/42.py create mode 100644 code/exercises/chapter-4/431.py create mode 100644 code/exercises/chapter-4/433.py create mode 100644 code/exercises/chapter-4/434.py create mode 100644 code/exercises/chapter-4/435.py create mode 100644 code/exercises/chapter-4/46-interface-design.py create mode 100644 code/exercises/chapter-4/47-refactoring.py create mode 100644 code/exercises/chapter-4/mypolygon.py create mode 100644 code/exercises/chapter-4/polygon.py diff --git a/code/exercises/chapter-4/.vscode/settings.json b/code/exercises/chapter-4/.vscode/settings.json new file mode 100644 index 0000000..9622a5c --- /dev/null +++ b/code/exercises/chapter-4/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "python.pythonPath": "C:\\Users\\Lucas\\AppData\\Local\\Programs\\Python\\Python36\\python.exe" +} \ No newline at end of file diff --git a/code/exercises/chapter-4/42.py b/code/exercises/chapter-4/42.py new file mode 100644 index 0000000..578c247 --- /dev/null +++ b/code/exercises/chapter-4/42.py @@ -0,0 +1,101 @@ +"""This module contains a code example related to + +Think Python, 2nd Edition +by Allen Downey +http://thinkpython2.com + +Copyright 2015 Allen Downey + +License: http://creativecommons.org/licenses/by/4.0/ +""" + +from __future__ import print_function, division + +import math +import turtle + + +def square(t, length): + """Draws a square with sides of the given length. + + Returns the Turtle to the starting position and location. + """ + for i in range(4): + t.fd(length) + t.lt(90) + + +def polyline(t, n, length, angle): + """Draws n line segments. + + t: Turtle object + n: number of line segments + length: length of each segment + angle: degrees between segments + """ + for i in range(n): + t.fd(length) + t.lt(angle) + + +def polygon(t, n, length): + """Draws a polygon with n sides. + + t: Turtle + n: number of sides + length: length of each side. + """ + angle = 360.0/n + polyline(t, n, length, angle) + + +def arc(t, r, angle): + """Draws an arc with the given radius and angle. + + t: Turtle + r: radius + angle: angle subtended by the arc, in degrees + """ + arc_length = 2 * math.pi * r * abs(angle) / 360 + n = int(arc_length / 4) + 3 + step_length = arc_length / n + step_angle = float(angle) / n + + # making a slight left turn before starting reduces + # the error caused by the linear approximation of the arc + t.lt(step_angle/2) + polyline(t, n, step_length, step_angle) + t.rt(step_angle/2) + + +def circle(t, r): + """Draws a circle with the given radius. + + t: Turtle + r: radius + """ + arc(t, r, 360) + + +# the following condition checks whether we are +# running as a script, in which case run the test code, +# or being imported, in which case don't. + +if __name__ == '__main__': + bob = turtle.Turtle() + + # draw a circle centered on the origin + radius = 150 + bob.pu() + bob.fd(radius) + bob.lt(90) + bob.pd() + for i in range(7): + arc(bob, radius, 100) + bob.lt(90) + for j in range(7): + arc(bob, radius, 100) + bob.lt(90) + turlte.mainloop() + + # wait for the user to close the window diff --git a/code/exercises/chapter-4/431.py b/code/exercises/chapter-4/431.py new file mode 100644 index 0000000..803e27c --- /dev/null +++ b/code/exercises/chapter-4/431.py @@ -0,0 +1,10 @@ +import turtle + +def square(t, length): + for i in range(4): + t.fd(length) + t.lt(90) + turtle.mainloop() + +bob = turtle.Turtle() +square(bob, 100) diff --git a/code/exercises/chapter-4/433.py b/code/exercises/chapter-4/433.py new file mode 100644 index 0000000..3159502 --- /dev/null +++ b/code/exercises/chapter-4/433.py @@ -0,0 +1,10 @@ +import turtle + +def polygon(t, length, n): + for i in range(n): + t.fd(50) + t.lt(360/n) + turtle.mainloop() + +bob = turtle.Turtle() +polygon(bob, 100, 7) diff --git a/code/exercises/chapter-4/434.py b/code/exercises/chapter-4/434.py new file mode 100644 index 0000000..faebaa4 --- /dev/null +++ b/code/exercises/chapter-4/434.py @@ -0,0 +1,10 @@ +import turtle + +def circle(t, r): + for i in range(36): + t.fd(r) + t.lt(360/36) + turtle.mainloop() + +bob = turtle.Turtle() +circle(bob, 15) diff --git a/code/exercises/chapter-4/435.py b/code/exercises/chapter-4/435.py new file mode 100644 index 0000000..0944f6b --- /dev/null +++ b/code/exercises/chapter-4/435.py @@ -0,0 +1,10 @@ +import turtle + +def arc(t, r, angle): + for i in range(36): + t.fd(r) + t.lt(angle/36) + turtle.mainloop() + +bob = turtle.Turtle() +arc(bob, 3, 360) diff --git a/code/exercises/chapter-4/46-interface-design.py b/code/exercises/chapter-4/46-interface-design.py new file mode 100644 index 0000000..f158986 --- /dev/null +++ b/code/exercises/chapter-4/46-interface-design.py @@ -0,0 +1,18 @@ +import math +import turtle + +def polygon(t, n, length): + angle = 360/n + for i in range(n): + t.fd(length) + t.lt(angle) + +def circle(t, r): + circumference = 2 * math.pi * r + n = 50 + length = circumference / n + polygon(t, n, length) + +jasmin = turtle.Turtle() +circle(jasmin, 100) +turtle.mainloop() diff --git a/code/exercises/chapter-4/47-refactoring.py b/code/exercises/chapter-4/47-refactoring.py new file mode 100644 index 0000000..4676965 --- /dev/null +++ b/code/exercises/chapter-4/47-refactoring.py @@ -0,0 +1,32 @@ +import turtle +import math + +def arc(t, r, angle): + arc_length = 2 * math.pi * r * angle / 360 + n = int(arc_length / 3) + 1 + step_length = arc_length / n + step_angle = float(angle) / n + polyline(t, n, step_length, step_angle) + for i in range(n): + t.fd(step_length) + t.lt(step_angle) + +def polyline(t, n, length, angle): + for i in range(n): + t.fd(length) + t.lt(angle) + +def polygon(t, n, length): + angle = 360.0 / n + polyline(t, n, length, angle) + +def circle(t, r): + arc(t, r, 360) + +bob = turtle.Turtle() + +## chamar a funcao +turtle.mainloop() + + + diff --git a/code/exercises/chapter-4/mypolygon.py b/code/exercises/chapter-4/mypolygon.py new file mode 100644 index 0000000..269604b --- /dev/null +++ b/code/exercises/chapter-4/mypolygon.py @@ -0,0 +1,10 @@ +import turtle + +bob = turtle.Turtle() +print(bob) + +for i in range(4): + bob.fd(250) + bob.lt(90) + +turtle.mainloop() diff --git a/code/exercises/chapter-4/polygon.py b/code/exercises/chapter-4/polygon.py new file mode 100644 index 0000000..2517b7e --- /dev/null +++ b/code/exercises/chapter-4/polygon.py @@ -0,0 +1,95 @@ +"""This module contains a code example related to + +Think Python, 2nd Edition +by Allen Downey +http://thinkpython2.com + +Copyright 2015 Allen Downey + +License: http://creativecommons.org/licenses/by/4.0/ +""" + +from __future__ import print_function, division + +import math +import turtle + + +def square(t, length): + """Draws a square with sides of the given length. + + Returns the Turtle to the starting position and location. + """ + for i in range(4): + t.fd(length) + t.lt(90) + + +def polyline(t, n, length, angle): + """Draws n line segments. + + t: Turtle object + n: number of line segments + length: length of each segment + angle: degrees between segments + """ + for i in range(n): + t.fd(length) + t.lt(angle) + + +def polygon(t, n, length): + """Draws a polygon with n sides. + + t: Turtle + n: number of sides + length: length of each side. + """ + angle = 360.0/n + polyline(t, n, length, angle) + + +def arc(t, r, angle): + """Draws an arc with the given radius and angle. + + t: Turtle + r: radius + angle: angle subtended by the arc, in degrees + """ + arc_length = 2 * math.pi * r * abs(angle) / 360 + n = int(arc_length / 4) + 3 + step_length = arc_length / n + step_angle = float(angle) / n + + # making a slight left turn before starting reduces + # the error caused by the linear approximation of the arc + t.lt(step_angle/2) + polyline(t, n, step_length, step_angle) + t.rt(step_angle/2) + + +def circle(t, r): + """Draws a circle with the given radius. + + t: Turtle + r: radius + """ + arc(t, r, 360) + + +# the following condition checks whether we are +# running as a script, in which case run the test code, +# or being imported, in which case don't. + +if __name__ == '__main__': + bob = turtle.Turtle() + + # draw a circle centered on the origin + radius = 100 + bob.pu() + bob.fd(radius) + bob.lt(90) + bob.pd() + circle(bob, radius) + + # wait for the user to close the window From fce320eafc72fda1617078707076bac37740e6af Mon Sep 17 00:00:00 2001 From: ltbatista Date: Mon, 20 Jul 2020 21:54:02 -0700 Subject: [PATCH 6/6] exerc 5.1 and 5.2 --- code/exercises/chapter-5/51.py | 3 +++ code/exercises/chapter-5/52.py | 17 +++++++++++++++++ 2 files changed, 20 insertions(+) create mode 100644 code/exercises/chapter-5/51.py create mode 100644 code/exercises/chapter-5/52.py diff --git a/code/exercises/chapter-5/51.py b/code/exercises/chapter-5/51.py new file mode 100644 index 0000000..c531e41 --- /dev/null +++ b/code/exercises/chapter-5/51.py @@ -0,0 +1,3 @@ +import time + +print(time.strftime('%Y-%m-%d %H:%M:%S', time.localtime(time.time()))) diff --git a/code/exercises/chapter-5/52.py b/code/exercises/chapter-5/52.py new file mode 100644 index 0000000..af0ee61 --- /dev/null +++ b/code/exercises/chapter-5/52.py @@ -0,0 +1,17 @@ +def check_fermat(a, b, c, n): + if n >= 2: + if (a**n+b**n) == c**n: + print("Holy smokes, Fermat was wrong!") + else: + print("No, that doesn't work.") + +def input_number(): + a = int(input('Digit o valor de a: \n')) + b = int(input('Digit o valor de b: \n')) + c = int(input('Digit o valor de c: \n')) + n = int(input('Digit o valor de n: \n')) + check_fermat(a, b, c, n) + +if __name__ == "__main__": + input_number() +