From 0e52b3db9fd385c2ce23f6c8aa413bf932745096 Mon Sep 17 00:00:00 2001 From: David Venegas Date: Tue, 23 Nov 2021 20:56:34 -0600 Subject: [PATCH] Lab completed David V --- your-code/challenge-1.py | 34 ++++++++++++++++++++++++++++++++-- your-code/challenge-2.py | 19 ++++++++++++++++++- your-code/challenge-3.py | 18 +++++++++++++++++- 3 files changed, 67 insertions(+), 4 deletions(-) diff --git a/your-code/challenge-1.py b/your-code/challenge-1.py index 2cd11bc..8c3038c 100644 --- a/your-code/challenge-1.py +++ b/your-code/challenge-1.py @@ -8,13 +8,21 @@ The code is very long and messy. Refactor it according to what you have learned about code simplicity and efficiency. """ - +''' +from num2words import num2words print('Welcome to this calculator!') print('It can add and subtract whole numbers from zero to five') a = input('Please choose your first number (zero to five): ') b = input('What do you want to do? plus or minus: ') c = input('Please choose your second number (zero to five): ') +valid_values = ['zero','one','two','three','four','five'] +valid_oper = ['plus','minus'] +valid_val = {} + +if a in valid_values and b in valid_oper and c in valid_values: + print(f'{a} {b} {c} equals ') + if a == 'zero' and b == 'plus' and c == 'zero': print("zero plus zero equals zero") if a == 'zero' and b == 'plus' and c == 'one': @@ -162,8 +170,30 @@ if a == 'five' and b == 'minus' and c == 'five': print("five minus five equals zero") - if (not a == 'zero' and not a == 'one' and not a == 'two' and not a == 'three' and not a == 'four' and not a == 'five') or (not c == 'zero' and not c == 'one' and not c == 'two' and not c == 'three' and not c == 'four' and not c == 'five') or (not b == 'plus' and not b == 'minus'): print("I am not able to answer this question. Check your input.") print("Thanks for using this calculator, goodbye :)") +''' + +from num2words import num2words +from word2number import w2n + +print('Welcome to this calculator!') +print('It can add and subtract whole numbers from zero to five') +a = input('Please choose your first number (zero to five): ') +b = input('What do you want to do? plus or minus: ') +c = input('Please choose your second number (zero to five): ') + +valid_values = ['zero','one','two','three','four','five'] +valid_oper = ['plus','minus'] +if a in valid_values and b in valid_oper and c in valid_values: + if b == 'minus': + res = num2words(w2n.word_to_num(a)-w2n.word_to_num(c)) + else: + res = num2words(w2n.word_to_num(a)+w2n.word_to_num(c)) + print(f'{a} {b} {c} equals {res}') +else: + print("I am not able to answer this question. Check your input.") + +print("Thanks for using this calculator, goodbye :)") \ No newline at end of file diff --git a/your-code/challenge-2.py b/your-code/challenge-2.py index af36e81..3323fa5 100644 --- a/your-code/challenge-2.py +++ b/your-code/challenge-2.py @@ -5,7 +5,7 @@ The code is functional but has a lot of room for improvement. Use what you have learned about simple and efficient code, refactor the code. -""" + def RandomStringGenerator(l=12, a=['a','b','c','d','e','f','g','h','i','j','k','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','0','1','2','3','4','5','6','7','8','9']): p = 0 @@ -36,3 +36,20 @@ def BatchStringGenerator(n, a=8, b=12): n = input('How many random strings to generate? ') print(BatchStringGenerator(int(n), int(a), int(b))) + +""" +import random +import string +import sys + +def BatchStringGenerator(n, a=8, b=12): + characters = string.ascii_lowercase + string.digits #random.sample(characters, n) + if a > b: + sys.exit('Incorrect min and max string lengths. Try again.') + return [''.join(random.sample(characters, random.randint(a,b) ) ) for x in range(n) ] + +a = input('Enter minimum string length: ') +b = input('Enter maximum string length: ') +n = input('How many random strings to generate? ') + +print(BatchStringGenerator(int(n), int(a), int(b))) diff --git a/your-code/challenge-3.py b/your-code/challenge-3.py index 236bd6a..27020d5 100644 --- a/your-code/challenge-3.py +++ b/your-code/challenge-3.py @@ -10,7 +10,7 @@ The following function shows one way to solve the problem but the code is not ideal or efficient. Refactor the code based on what you have learned about code simplicity and efficiency. -""" + def my_function(X): solutions = [] @@ -28,3 +28,19 @@ def my_function(X): X = input("What is the maximal length of the triangle side? Enter a number: ") print("The longest side possible is " + str(my_function(int(X)))) +""" + +def my_function(X): + for x in reversed(range(5, X)): + for y in reversed(range(4, x)): + for z in reversed(range(3, y)): + if (x*x==y*y+z*z): + return x + + + +X = input("What is the maximal length of the triangle side? Enter a number: ") + +print("The longest side possible is " + str(my_function(int(X)))) + +