diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.4/calculator.py b/1.4/calculator.py deleted file mode 100644 index 1db6f59..0000000 --- a/1.4/calculator.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Exercise 1. 4 - Calculator -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def calculator(): - """ - Calculator - """ - choice = 0 - first = 0 - second = 0 - - while True: - try: - print(""" -1. Enter two integers -2. Add -3. Subtract -4. Multiply -5. Divide -0. Exit""") - choice = int(input('Your choice: ')) - if choice > 5 or choice < 0: - raise ValueError - except ValueError: - print('Input must be either 0,1,2,3,4 or 5.') - continue - - if choice == 0: - print('Exiting ... Goodbye !') - return - - if choice == 1: - try: - first = int(input('Input first number : ')) - second = int(input('Input second number : ')) - except ValueError: - print('Input must be an integer!') - continue - elif choice == 2: - # Addition - print(f'The result is {first + second}') - elif choice == 3: - # Subtraction - print(f'The result is {first - second}') - elif choice == 4: - # Multiplication - print(f'The result is {first * second}') - elif choice == 5: - # Division - print(f'The result is {first // second}') - - -if __name__ == '__main__': - calculator() diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.4/calculator.py b/1.4/calculator.py deleted file mode 100644 index 1db6f59..0000000 --- a/1.4/calculator.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Exercise 1. 4 - Calculator -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def calculator(): - """ - Calculator - """ - choice = 0 - first = 0 - second = 0 - - while True: - try: - print(""" -1. Enter two integers -2. Add -3. Subtract -4. Multiply -5. Divide -0. Exit""") - choice = int(input('Your choice: ')) - if choice > 5 or choice < 0: - raise ValueError - except ValueError: - print('Input must be either 0,1,2,3,4 or 5.') - continue - - if choice == 0: - print('Exiting ... Goodbye !') - return - - if choice == 1: - try: - first = int(input('Input first number : ')) - second = int(input('Input second number : ')) - except ValueError: - print('Input must be an integer!') - continue - elif choice == 2: - # Addition - print(f'The result is {first + second}') - elif choice == 3: - # Subtraction - print(f'The result is {first - second}') - elif choice == 4: - # Multiplication - print(f'The result is {first * second}') - elif choice == 5: - # Division - print(f'The result is {first // second}') - - -if __name__ == '__main__': - calculator() diff --git a/1.5/pythagoras.py b/1.5/pythagoras.py deleted file mode 100644 index d2758a0..0000000 --- a/1.5/pythagoras.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Exercise 1.5 - Pythagora's Theoreme -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import math - - -def pythagoras(_a, _b): - """ - Calculate the hypotenuse for a right-angle triangle. - :return: Length of hypotenuse - :param _a: Length of first side - :param _b: Length of second side - """ - - return math.sqrt(_a ** 2 + _b ** 2) - - -if __name__ == '__main__': - for i in [[2, 3], [3, 4], [0, 0]]: - print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.4/calculator.py b/1.4/calculator.py deleted file mode 100644 index 1db6f59..0000000 --- a/1.4/calculator.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Exercise 1. 4 - Calculator -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def calculator(): - """ - Calculator - """ - choice = 0 - first = 0 - second = 0 - - while True: - try: - print(""" -1. Enter two integers -2. Add -3. Subtract -4. Multiply -5. Divide -0. Exit""") - choice = int(input('Your choice: ')) - if choice > 5 or choice < 0: - raise ValueError - except ValueError: - print('Input must be either 0,1,2,3,4 or 5.') - continue - - if choice == 0: - print('Exiting ... Goodbye !') - return - - if choice == 1: - try: - first = int(input('Input first number : ')) - second = int(input('Input second number : ')) - except ValueError: - print('Input must be an integer!') - continue - elif choice == 2: - # Addition - print(f'The result is {first + second}') - elif choice == 3: - # Subtraction - print(f'The result is {first - second}') - elif choice == 4: - # Multiplication - print(f'The result is {first * second}') - elif choice == 5: - # Division - print(f'The result is {first // second}') - - -if __name__ == '__main__': - calculator() diff --git a/1.5/pythagoras.py b/1.5/pythagoras.py deleted file mode 100644 index d2758a0..0000000 --- a/1.5/pythagoras.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Exercise 1.5 - Pythagora's Theoreme -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import math - - -def pythagoras(_a, _b): - """ - Calculate the hypotenuse for a right-angle triangle. - :return: Length of hypotenuse - :param _a: Length of first side - :param _b: Length of second side - """ - - return math.sqrt(_a ** 2 + _b ** 2) - - -if __name__ == '__main__': - for i in [[2, 3], [3, 4], [0, 0]]: - print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.6/units_and_tens.py b/1.6/units_and_tens.py deleted file mode 100644 index 350e278..0000000 --- a/1.6/units_and_tens.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -Exercise 1.6 - Units and Tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def unit(num: int) -> int: - """ - Return the 'ones' digit. - - :return: Ones digit - :param num: Input number - """ - - return int(str(num)[-1:]) - - -def ten(num: int) -> int: - """ - Return the 'tens' digit. - - :return: Tens digit - :param num: Input number - """ - - return int(str(num)[-2:-1]) - - -if __name__ == '__main__': - print(f'{unit(123) = }') - print(f'{ten(123) = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.4/calculator.py b/1.4/calculator.py deleted file mode 100644 index 1db6f59..0000000 --- a/1.4/calculator.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Exercise 1. 4 - Calculator -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def calculator(): - """ - Calculator - """ - choice = 0 - first = 0 - second = 0 - - while True: - try: - print(""" -1. Enter two integers -2. Add -3. Subtract -4. Multiply -5. Divide -0. Exit""") - choice = int(input('Your choice: ')) - if choice > 5 or choice < 0: - raise ValueError - except ValueError: - print('Input must be either 0,1,2,3,4 or 5.') - continue - - if choice == 0: - print('Exiting ... Goodbye !') - return - - if choice == 1: - try: - first = int(input('Input first number : ')) - second = int(input('Input second number : ')) - except ValueError: - print('Input must be an integer!') - continue - elif choice == 2: - # Addition - print(f'The result is {first + second}') - elif choice == 3: - # Subtraction - print(f'The result is {first - second}') - elif choice == 4: - # Multiplication - print(f'The result is {first * second}') - elif choice == 5: - # Division - print(f'The result is {first // second}') - - -if __name__ == '__main__': - calculator() diff --git a/1.5/pythagoras.py b/1.5/pythagoras.py deleted file mode 100644 index d2758a0..0000000 --- a/1.5/pythagoras.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Exercise 1.5 - Pythagora's Theoreme -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import math - - -def pythagoras(_a, _b): - """ - Calculate the hypotenuse for a right-angle triangle. - :return: Length of hypotenuse - :param _a: Length of first side - :param _b: Length of second side - """ - - return math.sqrt(_a ** 2 + _b ** 2) - - -if __name__ == '__main__': - for i in [[2, 3], [3, 4], [0, 0]]: - print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.6/units_and_tens.py b/1.6/units_and_tens.py deleted file mode 100644 index 350e278..0000000 --- a/1.6/units_and_tens.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -Exercise 1.6 - Units and Tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def unit(num: int) -> int: - """ - Return the 'ones' digit. - - :return: Ones digit - :param num: Input number - """ - - return int(str(num)[-1:]) - - -def ten(num: int) -> int: - """ - Return the 'tens' digit. - - :return: Tens digit - :param num: Input number - """ - - return int(str(num)[-2:-1]) - - -if __name__ == '__main__': - print(f'{unit(123) = }') - print(f'{ten(123) = }') diff --git a/1.7/swap_units_and_tens.py b/1.7/swap_units_and_tens.py deleted file mode 100644 index 5aa4c52..0000000 --- a/1.7/swap_units_and_tens.py +++ /dev/null @@ -1,21 +0,0 @@ -""" -Exercise 1.7 - Swap Units and tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def swap_units_and_tens(num: int) -> int: - """ - Swap the last with the second last digit. - - :return: Swapped number - :param num: Input number - """ - string = str(num) - - return int(string[:-2] + string[:-3:-1]) - - -if __name__ == '__main__': - print(f'{swap_units_and_tens(123) = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.4/calculator.py b/1.4/calculator.py deleted file mode 100644 index 1db6f59..0000000 --- a/1.4/calculator.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Exercise 1. 4 - Calculator -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def calculator(): - """ - Calculator - """ - choice = 0 - first = 0 - second = 0 - - while True: - try: - print(""" -1. Enter two integers -2. Add -3. Subtract -4. Multiply -5. Divide -0. Exit""") - choice = int(input('Your choice: ')) - if choice > 5 or choice < 0: - raise ValueError - except ValueError: - print('Input must be either 0,1,2,3,4 or 5.') - continue - - if choice == 0: - print('Exiting ... Goodbye !') - return - - if choice == 1: - try: - first = int(input('Input first number : ')) - second = int(input('Input second number : ')) - except ValueError: - print('Input must be an integer!') - continue - elif choice == 2: - # Addition - print(f'The result is {first + second}') - elif choice == 3: - # Subtraction - print(f'The result is {first - second}') - elif choice == 4: - # Multiplication - print(f'The result is {first * second}') - elif choice == 5: - # Division - print(f'The result is {first // second}') - - -if __name__ == '__main__': - calculator() diff --git a/1.5/pythagoras.py b/1.5/pythagoras.py deleted file mode 100644 index d2758a0..0000000 --- a/1.5/pythagoras.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Exercise 1.5 - Pythagora's Theoreme -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import math - - -def pythagoras(_a, _b): - """ - Calculate the hypotenuse for a right-angle triangle. - :return: Length of hypotenuse - :param _a: Length of first side - :param _b: Length of second side - """ - - return math.sqrt(_a ** 2 + _b ** 2) - - -if __name__ == '__main__': - for i in [[2, 3], [3, 4], [0, 0]]: - print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.6/units_and_tens.py b/1.6/units_and_tens.py deleted file mode 100644 index 350e278..0000000 --- a/1.6/units_and_tens.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -Exercise 1.6 - Units and Tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def unit(num: int) -> int: - """ - Return the 'ones' digit. - - :return: Ones digit - :param num: Input number - """ - - return int(str(num)[-1:]) - - -def ten(num: int) -> int: - """ - Return the 'tens' digit. - - :return: Tens digit - :param num: Input number - """ - - return int(str(num)[-2:-1]) - - -if __name__ == '__main__': - print(f'{unit(123) = }') - print(f'{ten(123) = }') diff --git a/1.7/swap_units_and_tens.py b/1.7/swap_units_and_tens.py deleted file mode 100644 index 5aa4c52..0000000 --- a/1.7/swap_units_and_tens.py +++ /dev/null @@ -1,21 +0,0 @@ -""" -Exercise 1.7 - Swap Units and tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def swap_units_and_tens(num: int) -> int: - """ - Swap the last with the second last digit. - - :return: Swapped number - :param num: Input number - """ - string = str(num) - - return int(string[:-2] + string[:-3:-1]) - - -if __name__ == '__main__': - print(f'{swap_units_and_tens(123) = }') diff --git a/1.8/to_upper.py b/1.8/to_upper.py deleted file mode 100644 index 65413ea..0000000 --- a/1.8/to_upper.py +++ /dev/null @@ -1,22 +0,0 @@ -""" -Exercise 1.8 - Transform by using ASCII Code -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def to_upper(string: str) -> str: - """ - Convert a string to uppercase. - :param string: Input string - :return: Uppercase string - """ - - return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) - - -if __name__ == '__main__': - print(f'{to_upper("test") = }') - print(f'{to_upper("TEST") = }') - print(f'{to_upper("tEsT") = }') - print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.01/fizzbuzz.py b/1.01/fizzbuzz.py new file mode 100644 index 0000000..fc0b3ad --- /dev/null +++ b/1.01/fizzbuzz.py @@ -0,0 +1,31 @@ +""" +Exercise 1.1 - FizzBuzz +Kattis problem https://open.kattis.com/problems/fizzbuzz +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def fizzbuzz(_x, _y, _n): + """ + FizzBuzz + :param _x: Divisor for 'Fizz' + :param _y: Divisor for 'Buzz' + :param _n: Range [1;n] + """ + print(f'\n{_x = }, {_y = }, {_n = }') + for i in range(1, _n + 1): + output = '' + if i % _x == 0: + output += 'Fizz' + if i % _y == 0: + output += 'Buzz' + if output == '': + output += str(i) + print(output) + + +if __name__ == '__main__': + fizzbuzz(2, 3, 7) + fizzbuzz(2, 4, 7) + fizzbuzz(3, 5, 7) diff --git a/1.02/mult_table.py b/1.02/mult_table.py new file mode 100644 index 0000000..3491953 --- /dev/null +++ b/1.02/mult_table.py @@ -0,0 +1,19 @@ +""" +Exercise 1.2 - Multiplication table +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def mult_table(_n: int): + """ + mult_table + :param _n: Factor + """ + for i in range(10): + print(f'{i} * {_n} = {i * _n}') + + +if __name__ == '__main__': + print('This program prints out a multiplication table.') + mult_table(int(input('Enter a number: '))) diff --git a/1.03/guess_the_number.py b/1.03/guess_the_number.py new file mode 100644 index 0000000..10380fb --- /dev/null +++ b/1.03/guess_the_number.py @@ -0,0 +1,38 @@ +""" +Exercise 1.3 - Guess the Number +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import random +import sys + + +def guess_the_number(): + """ + Guess a number between 1 and 100. Interactive. + """ + rand = random.randrange(100) + 1 + player = rand + 1 + guesses = 0 + while True: + try: + player = int(input('Guess a number: ')) + if player < 1 or player > 100: + raise ValueError + except ValueError: + print('Wrong input! Please enter a number between 1 and 100') + continue + + guesses += 1 + if rand == player: + break + print(f'You are too {"low" if player < rand else "high"}, try again!') + print(f'Correct guess! It took you {guesses} guesses!') + + +if __name__ == '__main__': + while True: + guess_the_number() + if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': + sys.exit(0) diff --git a/1.04/calculator.py b/1.04/calculator.py new file mode 100644 index 0000000..1db6f59 --- /dev/null +++ b/1.04/calculator.py @@ -0,0 +1,58 @@ +""" +Exercise 1. 4 - Calculator +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def calculator(): + """ + Calculator + """ + choice = 0 + first = 0 + second = 0 + + while True: + try: + print(""" +1. Enter two integers +2. Add +3. Subtract +4. Multiply +5. Divide +0. Exit""") + choice = int(input('Your choice: ')) + if choice > 5 or choice < 0: + raise ValueError + except ValueError: + print('Input must be either 0,1,2,3,4 or 5.') + continue + + if choice == 0: + print('Exiting ... Goodbye !') + return + + if choice == 1: + try: + first = int(input('Input first number : ')) + second = int(input('Input second number : ')) + except ValueError: + print('Input must be an integer!') + continue + elif choice == 2: + # Addition + print(f'The result is {first + second}') + elif choice == 3: + # Subtraction + print(f'The result is {first - second}') + elif choice == 4: + # Multiplication + print(f'The result is {first * second}') + elif choice == 5: + # Division + print(f'The result is {first // second}') + + +if __name__ == '__main__': + calculator() diff --git a/1.05/pythagoras.py b/1.05/pythagoras.py new file mode 100644 index 0000000..d2758a0 --- /dev/null +++ b/1.05/pythagoras.py @@ -0,0 +1,23 @@ +""" +Exercise 1.5 - Pythagora's Theoreme +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. +import math + + +def pythagoras(_a, _b): + """ + Calculate the hypotenuse for a right-angle triangle. + :return: Length of hypotenuse + :param _a: Length of first side + :param _b: Length of second side + """ + + return math.sqrt(_a ** 2 + _b ** 2) + + +if __name__ == '__main__': + for i in [[2, 3], [3, 4], [0, 0]]: + print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.06/units_and_tens.py b/1.06/units_and_tens.py new file mode 100644 index 0000000..350e278 --- /dev/null +++ b/1.06/units_and_tens.py @@ -0,0 +1,32 @@ +""" +Exercise 1.6 - Units and Tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def unit(num: int) -> int: + """ + Return the 'ones' digit. + + :return: Ones digit + :param num: Input number + """ + + return int(str(num)[-1:]) + + +def ten(num: int) -> int: + """ + Return the 'tens' digit. + + :return: Tens digit + :param num: Input number + """ + + return int(str(num)[-2:-1]) + + +if __name__ == '__main__': + print(f'{unit(123) = }') + print(f'{ten(123) = }') diff --git a/1.07/swap_units_and_tens.py b/1.07/swap_units_and_tens.py new file mode 100644 index 0000000..5aa4c52 --- /dev/null +++ b/1.07/swap_units_and_tens.py @@ -0,0 +1,21 @@ +""" +Exercise 1.7 - Swap Units and tens +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def swap_units_and_tens(num: int) -> int: + """ + Swap the last with the second last digit. + + :return: Swapped number + :param num: Input number + """ + string = str(num) + + return int(string[:-2] + string[:-3:-1]) + + +if __name__ == '__main__': + print(f'{swap_units_and_tens(123) = }') diff --git a/1.08/to_upper.py b/1.08/to_upper.py new file mode 100644 index 0000000..65413ea --- /dev/null +++ b/1.08/to_upper.py @@ -0,0 +1,22 @@ +""" +Exercise 1.8 - Transform by using ASCII Code +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def to_upper(string: str) -> str: + """ + Convert a string to uppercase. + :param string: Input string + :return: Uppercase string + """ + + return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) + + +if __name__ == '__main__': + print(f'{to_upper("test") = }') + print(f'{to_upper("TEST") = }') + print(f'{to_upper("tEsT") = }') + print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.09/count_vowels.py b/1.09/count_vowels.py new file mode 100644 index 0000000..08d48b7 --- /dev/null +++ b/1.09/count_vowels.py @@ -0,0 +1,25 @@ +""" +Exercise 1.9 - Count Vowels +""" + + +# Copyright (c) 2021. Pascal Syma. All rights reserved. + +def count_vowels(string: str) -> int: + """ + Count the amount of vowels (a, e, i, o ,u) in a string. + :param string: Input string + :return: Amount of vowels + """ + vowels = 'aeiou' + count = 0 + for char in string: + if char in vowels: + count += 1 + + return count + + +if __name__ == '__main__': + print(f'{count_vowels("elephant") = }') + print(f'{count_vowels("apple") = }') diff --git a/1.1/fizzbuzz.py b/1.1/fizzbuzz.py deleted file mode 100644 index fc0b3ad..0000000 --- a/1.1/fizzbuzz.py +++ /dev/null @@ -1,31 +0,0 @@ -""" -Exercise 1.1 - FizzBuzz -Kattis problem https://open.kattis.com/problems/fizzbuzz -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def fizzbuzz(_x, _y, _n): - """ - FizzBuzz - :param _x: Divisor for 'Fizz' - :param _y: Divisor for 'Buzz' - :param _n: Range [1;n] - """ - print(f'\n{_x = }, {_y = }, {_n = }') - for i in range(1, _n + 1): - output = '' - if i % _x == 0: - output += 'Fizz' - if i % _y == 0: - output += 'Buzz' - if output == '': - output += str(i) - print(output) - - -if __name__ == '__main__': - fizzbuzz(2, 3, 7) - fizzbuzz(2, 4, 7) - fizzbuzz(3, 5, 7) diff --git a/1.2/mult_table.py b/1.2/mult_table.py deleted file mode 100644 index 3491953..0000000 --- a/1.2/mult_table.py +++ /dev/null @@ -1,19 +0,0 @@ -""" -Exercise 1.2 - Multiplication table -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def mult_table(_n: int): - """ - mult_table - :param _n: Factor - """ - for i in range(10): - print(f'{i} * {_n} = {i * _n}') - - -if __name__ == '__main__': - print('This program prints out a multiplication table.') - mult_table(int(input('Enter a number: '))) diff --git a/1.3/guess_the_number.py b/1.3/guess_the_number.py deleted file mode 100644 index 10380fb..0000000 --- a/1.3/guess_the_number.py +++ /dev/null @@ -1,38 +0,0 @@ -""" -Exercise 1.3 - Guess the Number -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import random -import sys - - -def guess_the_number(): - """ - Guess a number between 1 and 100. Interactive. - """ - rand = random.randrange(100) + 1 - player = rand + 1 - guesses = 0 - while True: - try: - player = int(input('Guess a number: ')) - if player < 1 or player > 100: - raise ValueError - except ValueError: - print('Wrong input! Please enter a number between 1 and 100') - continue - - guesses += 1 - if rand == player: - break - print(f'You are too {"low" if player < rand else "high"}, try again!') - print(f'Correct guess! It took you {guesses} guesses!') - - -if __name__ == '__main__': - while True: - guess_the_number() - if input('Would you like to play again?\n\n Y(es) / N(o) ').lower() != 'y': - sys.exit(0) diff --git a/1.4/calculator.py b/1.4/calculator.py deleted file mode 100644 index 1db6f59..0000000 --- a/1.4/calculator.py +++ /dev/null @@ -1,58 +0,0 @@ -""" -Exercise 1. 4 - Calculator -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def calculator(): - """ - Calculator - """ - choice = 0 - first = 0 - second = 0 - - while True: - try: - print(""" -1. Enter two integers -2. Add -3. Subtract -4. Multiply -5. Divide -0. Exit""") - choice = int(input('Your choice: ')) - if choice > 5 or choice < 0: - raise ValueError - except ValueError: - print('Input must be either 0,1,2,3,4 or 5.') - continue - - if choice == 0: - print('Exiting ... Goodbye !') - return - - if choice == 1: - try: - first = int(input('Input first number : ')) - second = int(input('Input second number : ')) - except ValueError: - print('Input must be an integer!') - continue - elif choice == 2: - # Addition - print(f'The result is {first + second}') - elif choice == 3: - # Subtraction - print(f'The result is {first - second}') - elif choice == 4: - # Multiplication - print(f'The result is {first * second}') - elif choice == 5: - # Division - print(f'The result is {first // second}') - - -if __name__ == '__main__': - calculator() diff --git a/1.5/pythagoras.py b/1.5/pythagoras.py deleted file mode 100644 index d2758a0..0000000 --- a/1.5/pythagoras.py +++ /dev/null @@ -1,23 +0,0 @@ -""" -Exercise 1.5 - Pythagora's Theoreme -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. -import math - - -def pythagoras(_a, _b): - """ - Calculate the hypotenuse for a right-angle triangle. - :return: Length of hypotenuse - :param _a: Length of first side - :param _b: Length of second side - """ - - return math.sqrt(_a ** 2 + _b ** 2) - - -if __name__ == '__main__': - for i in [[2, 3], [3, 4], [0, 0]]: - print(f'{i[0]}² + {i[1]}² = {pythagoras(i[0], i[1])}²') diff --git a/1.6/units_and_tens.py b/1.6/units_and_tens.py deleted file mode 100644 index 350e278..0000000 --- a/1.6/units_and_tens.py +++ /dev/null @@ -1,32 +0,0 @@ -""" -Exercise 1.6 - Units and Tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def unit(num: int) -> int: - """ - Return the 'ones' digit. - - :return: Ones digit - :param num: Input number - """ - - return int(str(num)[-1:]) - - -def ten(num: int) -> int: - """ - Return the 'tens' digit. - - :return: Tens digit - :param num: Input number - """ - - return int(str(num)[-2:-1]) - - -if __name__ == '__main__': - print(f'{unit(123) = }') - print(f'{ten(123) = }') diff --git a/1.7/swap_units_and_tens.py b/1.7/swap_units_and_tens.py deleted file mode 100644 index 5aa4c52..0000000 --- a/1.7/swap_units_and_tens.py +++ /dev/null @@ -1,21 +0,0 @@ -""" -Exercise 1.7 - Swap Units and tens -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def swap_units_and_tens(num: int) -> int: - """ - Swap the last with the second last digit. - - :return: Swapped number - :param num: Input number - """ - string = str(num) - - return int(string[:-2] + string[:-3:-1]) - - -if __name__ == '__main__': - print(f'{swap_units_and_tens(123) = }') diff --git a/1.8/to_upper.py b/1.8/to_upper.py deleted file mode 100644 index 65413ea..0000000 --- a/1.8/to_upper.py +++ /dev/null @@ -1,22 +0,0 @@ -""" -Exercise 1.8 - Transform by using ASCII Code -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def to_upper(string: str) -> str: - """ - Convert a string to uppercase. - :param string: Input string - :return: Uppercase string - """ - - return ''.join([chr(ord(i) - 0x20) if 0x60 < ord(i) <= 0x7A else i for i in string]) - - -if __name__ == '__main__': - print(f'{to_upper("test") = }') - print(f'{to_upper("TEST") = }') - print(f'{to_upper("tEsT") = }') - print(f'{to_upper("tEsT !#5@-}") = }') diff --git a/1.9/count_vowels.py b/1.9/count_vowels.py deleted file mode 100644 index 08d48b7..0000000 --- a/1.9/count_vowels.py +++ /dev/null @@ -1,25 +0,0 @@ -""" -Exercise 1.9 - Count Vowels -""" - - -# Copyright (c) 2021. Pascal Syma. All rights reserved. - -def count_vowels(string: str) -> int: - """ - Count the amount of vowels (a, e, i, o ,u) in a string. - :param string: Input string - :return: Amount of vowels - """ - vowels = 'aeiou' - count = 0 - for char in string: - if char in vowels: - count += 1 - - return count - - -if __name__ == '__main__': - print(f'{count_vowels("elephant") = }') - print(f'{count_vowels("apple") = }')