From 921ef551623a2f1c6acdd58c4be8af914131b03e Mon Sep 17 00:00:00 2001 From: Glockrover Date: Wed, 18 Dec 2024 20:05:39 +0200 Subject: [PATCH] done with some func while others fail to pass --- functions_to_test.py | 89 +++++++++++++++++++++++++++++++++++++------- 1 file changed, 76 insertions(+), 13 deletions(-) diff --git a/functions_to_test.py b/functions_to_test.py index 330fa02..44191f8 100644 --- a/functions_to_test.py +++ b/functions_to_test.py @@ -1,34 +1,90 @@ # Placeholder functions for Python basics, to be implemented later def add_numbers(a, b): - pass + return a+b def find_maximum(a, b, c): - pass + return max(a,b,c) def is_palindrome(string): - pass + return string[::-1] == string def count_word_occurrences(text, word): - pass + return text.count(word) def read_file_lines(filepath): - pass + counter = 0 + with open(filepath, "r") as file: + for line in file.readline(): + counter += 1 + return counter + def factorial(n): - pass + try: + counter = 1 + if n < 0: + raise ValueError + elif n == 0 or n == 1: + return counter + else: + for i in range(n, 1, -1): + counter *= i + return counter + except TypeError: + pass def is_prime(n): - pass + if n < 0 : + raise ValueError + else: + counter = 0 + for i in range(1, n+1): + if n % i == 0: + counter += 1 + if counter == 2: + return True + else: + return False def sort_numbers(numbers): - pass - + try: + for i in numbers: + if i is int: + if numbers == []: + return [] + else: + return sorted(numbers) + else: + raise TypeError + except TypeError: + pass + def factorial(n): - pass + counter = 1 + if n < 0: + return "" + elif n == 0 or n == 1: + return counter + else: + for i in range(n, 1, -1): + counter *= i + return counter def fibonacci(n): - pass + if n == 0: + return None + if n == 1: + return 0 + else: + list_of_fib = list() + a, b = 0, 1 + for _ in range(n): + list_of_fib.append(a) + a, b = b, a + b + return list_of_fib[-1] + + def tower_of_hanoi(n, source, auxiliary, target): @@ -52,10 +108,17 @@ def tower_of_hanoi(n, source, auxiliary, target): class Person: def __init__(self, name, age): - pass + self.name = name + self.age = age if __name__ == "__main__": # Placeholder functions for Python basics, to be implemented later #to test your functions, you can use the following code - print(add_numbers(3, 5)) #e.g \ No newline at end of file + print(add_numbers(3, 5)) #e.g + print(factorial(0)) + # print(sort_numbers([-3, -1, -2])) + print(fibonacci(5)) + print(is_prime(3)) + print(factorial(9)) + print(read_file_lines("database.csv")) \ No newline at end of file