Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 32 additions & 2 deletions your-code/challenge-1.py
Original file line number Diff line number Diff line change
Expand Up @@ -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':
Expand Down Expand Up @@ -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 :)")
19 changes: 18 additions & 1 deletion your-code/challenge-2.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)))
18 changes: 17 additions & 1 deletion your-code/challenge-3.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = []
Expand All @@ -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))))