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
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

.vscode/settings.json
24 changes: 24 additions & 0 deletions answer_1/answer_1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import string
import os

all_letters = string.ascii_letters

current_path = os.path.dirname(os.path.realpath(__file__))
file_path = os.path.join(current_path, "letters.txt") # The path to the result file

# take user input, only accept integers between 1 and 52
while True:
try:
num_of_letters_in_line = int(input(f"How many letters in a line? [between 1 and {len(all_letters)}] "))
assert 0 < num_of_letters_in_line <= len(all_letters)
break
except ValueError:
print("Error: not an integer!")
except AssertionError:
print(f"Error: not between 1 and {len(all_letters)}")

# write the letters to the file
with open(file_path, "w") as f:
for i in range(0, len(all_letters), num_of_letters_in_line):
f.writelines(all_letters[i:i+num_of_letters_in_line] + '\n')

18 changes: 18 additions & 0 deletions answer_1/letters.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
abc
def
ghi
jkl
mno
pqr
stu
vwx
yzA
BCD
EFG
HIJ
KLM
NOP
QRS
TUV
WXY
Z
15 changes: 15 additions & 0 deletions answer_2/answer_2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import string
import os

current_path = os.path.dirname(os.path.realpath(__file__))
folder_path = os.path.join(current_path, "text_files")

# Create a folder if it does not exist
if not os.path.exists(folder_path):
os.mkdir(folder_path)
os.chdir(folder_path)

# Create a file for each letter in the alphabet
for letter in string.ascii_uppercase:
open(f"{letter}.txt", "w").close()

Empty file added answer_2/text_files/A.txt
Empty file.
Empty file added answer_2/text_files/B.txt
Empty file.
Empty file added answer_2/text_files/C.txt
Empty file.
Empty file added answer_2/text_files/D.txt
Empty file.
Empty file added answer_2/text_files/E.txt
Empty file.
Empty file added answer_2/text_files/F.txt
Empty file.
Empty file added answer_2/text_files/G.txt
Empty file.
Empty file added answer_2/text_files/H.txt
Empty file.
Empty file added answer_2/text_files/I.txt
Empty file.
Empty file added answer_2/text_files/J.txt
Empty file.
Empty file added answer_2/text_files/K.txt
Empty file.
Empty file added answer_2/text_files/L.txt
Empty file.
Empty file added answer_2/text_files/M.txt
Empty file.
Empty file added answer_2/text_files/N.txt
Empty file.
Empty file added answer_2/text_files/O.txt
Empty file.
Empty file added answer_2/text_files/P.txt
Empty file.
Empty file added answer_2/text_files/Q.txt
Empty file.
Empty file added answer_2/text_files/R.txt
Empty file.
Empty file added answer_2/text_files/S.txt
Empty file.
Empty file added answer_2/text_files/T.txt
Empty file.
Empty file added answer_2/text_files/U.txt
Empty file.
Empty file added answer_2/text_files/V.txt
Empty file.
Empty file added answer_2/text_files/W.txt
Empty file.
Empty file added answer_2/text_files/X.txt
Empty file.
Empty file added answer_2/text_files/Y.txt
Empty file.
Empty file added answer_2/text_files/Z.txt
Empty file.
23 changes: 23 additions & 0 deletions answer_3/answer_3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import math


def lcm(*numbers):
''' A recursive function to find the least common multiple of a list of numbers. '''
if len(numbers) == 2:
# the LCM of two numbers: LCM(a, b) = a * b / gcd(a, b)
return int(numbers[0] * numbers[1]/ math.gcd(numbers[0], numbers[1]))
else:
# the LCM of the first number and the LCM of the rest of the numbers
return lcm(numbers[0], lcm(*numbers[1:]))


numbers = []
# ask the user to enter 4 numbers, only accept integers
while len(numbers) < 4:
try:
numbers.append(int(input("Enter a number: ")))
except ValueError:
print("Error: Not a number! Try again.")

# calculate the least common multiple of the 4 numbers
print(f"L.C.M of {numbers} is: {lcm(*numbers)}")
42 changes: 42 additions & 0 deletions answer_4/answer_4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
import os
import sys

current_path = os.path.dirname(os.path.realpath(__file__))
operations_directory = os.path.join(current_path, 'operations_functions\\')
sys.path.append(operations_directory) # Adds the operations_functions directory to the Python path

# Imports the functions from the operations_functions directory
import add_function, sub_function, multiply_function, divide_function

def input_and_check(prompt, accepted_inputs, error_prompt='Error: Invalid input!'):
''' A function to check if the user input is in the accepted inputs. Returns the input if it is accepted. Otherwise, repeat.'''
while True:
input_string = input(prompt)
try:
if accepted_inputs == 'numbers': # if the accepted input is a number (float)
return float(input_string)
else:
assert input_string in accepted_inputs
return input_string
except:
print(error_prompt)


# The main loop
while True:
number_1 = input_and_check('Enter first number: ', 'numbers', 'Error: Input must be a float value!')
operation = input_and_check('Choose an operation: [+, -, *, /]: ', ['+', '-', '*', '/'], 'Error: Invalid operation! Try again.')
number_2 = input_and_check('Enter second number: ', 'numbers', 'Error: Input must be a float value!')

if operation == '+':
print(f'{number_1} + {number_2} = {add_function.add(number_1, number_2)}')
elif operation == '-':
print(f'{number_1} - {number_2} = {sub_function.sub(number_1, number_2)}')
elif operation == '*':
print(f'{number_1} * {number_2} = {multiply_function.multiply(number_1, number_2)}')
elif operation == '/':
print(f'{number_1} / {number_2} = {divide_function.divide(number_1, number_2)}')

if input_and_check('Do you want to continue? (Y/N): ', ['Y', 'y', 'N', 'n']) in ['N', 'n']:
break

Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
4 changes: 4 additions & 0 deletions answer_4/operations_functions/add_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import math

def add(a, b):
return math.ceil(a + b)
9 changes: 9 additions & 0 deletions answer_4/operations_functions/divide_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
from decimal import DivisionByZero
import math

def divide(a, b):
try:
return math.ceil(a / b)
except ZeroDivisionError:
return "Cannot divide by zero"

4 changes: 4 additions & 0 deletions answer_4/operations_functions/multiply_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import math

def multiply(a, b):
return math.ceil(a * b)
4 changes: 4 additions & 0 deletions answer_4/operations_functions/sub_function.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
import math

def sub(a, b):
return math.ceil(a - b)