diff --git a/answer-1.py b/answer-1.py new file mode 100644 index 0000000..377842c --- /dev/null +++ b/answer-1.py @@ -0,0 +1,28 @@ + +row_number = 0 + +def print_pascal_triangle(n): + ''' A recursive function to print the first n rows of Pascal triangle. + + Returns the the nth row as a list of integers. ''' + + # initialize the row as a list of 1s of size n. + row = [1] * n + + # The base condition of the recursion, when n is 1. + if n == 0: + return [1] + else: + previous_row = print_pascal_triangle(n - 1) + + # calculate the row valuse except the first and last values as they are always 1. + for i in range(1, n - 1): + row[i] = previous_row[i-1] + previous_row[i] + + # print the current row. + print(" " * (row_number - n), end="") # padding + print(row) + return row + +row_number = int(input('Enter the numbers of rows from Pascal triangle you want to print: ')) +print_pascal_triangle(row_number) diff --git a/answer-2.py b/answer-2.py new file mode 100644 index 0000000..5b864e7 --- /dev/null +++ b/answer-2.py @@ -0,0 +1,15 @@ + +# Allow the user to input the sequence. +input_sequence = input("Enter the input sequence as words seperated by a hyphen: ") + +# Make a list of the words in the input sequence. +list_sequence = input_sequence.split("-") + +# Sort the words in the list. +list_sequence.sort() + +# Join the words in the list with a hyphen. +output_sequence = "-".join(list_sequence) + +# Print the output sequence. +print(output_sequence) diff --git a/answer-3.py b/answer-3.py new file mode 100644 index 0000000..b0bd410 --- /dev/null +++ b/answer-3.py @@ -0,0 +1,30 @@ + +def is_perfect_number(number): + ''' A function to check if a number is a perfect number. + + Returns True or False. ''' + + # If the number is negative or 0, return False. + if number <= 0: + return False + + # Calculating the divisors of the number. + divisors = [1] + for i in range(2, number): + if number % i == 0: + divisors.append(i) + + # Calculating the sum of the divisors and checking if the number is equal to the sum of its divisors. + sum_of_divisors = sum(divisors) + if number == sum_of_divisors: + return True + else: + return False + + +number = int(input("Enter a number to check if it's a perect number: ")) + +if is_perfect_number(number): + print (f"Number {number} is a perfect number") +else: + print (f"Number {number} is NOT a perfect number") \ No newline at end of file diff --git a/answer-4/GUI.py b/answer-4/GUI.py new file mode 100644 index 0000000..da2ec0e --- /dev/null +++ b/answer-4/GUI.py @@ -0,0 +1,65 @@ + +import password_generator +import tkinter as tk +import os + +current_path = os.path.dirname(os.path.realpath(__file__)) + +def generate_btn_handler(): + """ A function to handle the press event of the generate button """ + + # Getting the values entered by the user + lower_case_letters = int(lower_case_entry.get()) + upper_case_letters = int(upper_case_entry.get()) + numbers = int(numbers_entry.get()) + special_chars = int(special_chars_entry.get()) + + # Creating the password from the values entered by the user using the password_generator module from password_generator.py + password = password_generator.Generate_random_password(lower_case_letters, upper_case_letters, numbers, special_chars) + + # Clearing the password field and displaying the generated password + password_field.config(state="normal") + password_field.delete(0, tk.END) + password_field.insert(0, password) + password_field.config(state="readonly") + + +# Creating the main window +screen = tk.Tk() +screen.title("Password Generator") +canvas = tk.Canvas(screen, width=500, height=450) +canvas.pack() + +# Displaying the logo image +logo = tk.PhotoImage(file = current_path + "\psw.png") +logo = logo.subsample(3, 3) +canvas.create_image(250, 90, image = logo) +canvas.create_text(250, 200, text = "How would you like your password?", font=("Arial", 17, "bold"), fill= "#556080") + +canvas.create_text(250, 250, text = "Lower-case Letters: ", font = ("Arial", 11, "bold"), anchor="e", fill= "#556080") +canvas.create_text(250, 270, text = "Upper-case Letters: ", font = ("Arial", 11, "bold"), anchor="e", fill= "#556080") +canvas.create_text(250, 290, text = "Numbers: ", font = ("Arial", 11, "bold"), anchor="e", fill= "#556080") +canvas.create_text(250, 310, text = "Special Characters: ", font = ("Arial", 11, "bold"), anchor="e", fill= "#556080") + +# Entry boxes for the password generation options with default values of 3 +lower_case_entry, upper_case_entry, numbers_entry, special_chars_entry = tk.Entry(screen), tk.Entry(screen), tk.Entry(screen), tk.Entry(screen) +lower_case_entry.insert(0, 3) +canvas.create_window(250, 250, window = lower_case_entry, anchor="w") +upper_case_entry.insert(0, 3) +canvas.create_window(250, 270, window = upper_case_entry, anchor="w") +numbers_entry.insert(0, 3) +canvas.create_window(250, 290, window = numbers_entry, anchor="w") +special_chars_entry.insert(0, 3) +canvas.create_window(250, 310, window = special_chars_entry, anchor="w") + +# Create the generate button handler function and assignment to it to gen +generate_btn = tk.Button(screen, text = "Generate Password", command = generate_btn_handler, relief="groove") +generate_btn.config(fg="#556080", font=("Arial", 15)) +canvas.create_window(250, 350, window = generate_btn) + +# creating the password field to display the generated password +password_field = tk.Entry(screen, font=("Arial", 20, "bold"), fg="#e4c05c", justify="center", relief="flat", state="readonly") +canvas.create_window(250, 400, window = password_field) + +screen.mainloop() + diff --git a/answer-4/password_generator.py b/answer-4/password_generator.py new file mode 100644 index 0000000..406319a --- /dev/null +++ b/answer-4/password_generator.py @@ -0,0 +1,37 @@ + +import random + +SPECIAL_CHARS = """ !"#$%&'()*+,-./:;<=>?@[\]^_`{|}~""" +NUMBERS = "0123456789" +UPPER_LETTERS = "ABCDEFGHIJKLMNOPQRSTUVWXYZ" +LOWER_LETTERS = "abcdefghijklmnopqrstuvwxyz" + + +def Generate_random_password(number_of_lower_case_letters = 0, number_of_upper_case_letters = 0, number_of_numbers = 0, number_of_special_chars = 0): + """ A function to generate a random password. + + Accepts the number of upper-case letters, lower-casw letters, digits and special characters. + + Returns the password as a string.""" + + # Initialize a list to store the password characters. + password_elements = [] + + # Adding the password characters to the list. + for i in range(number_of_lower_case_letters): + password_elements.append(random.choice(LOWER_LETTERS)) + for i in range(number_of_upper_case_letters): + password_elements.append(random.choice(UPPER_LETTERS)) + for i in range(number_of_numbers): + password_elements.append(random.choice(NUMBERS)) + for i in range(number_of_special_chars): + password_elements.append(random.choice(SPECIAL_CHARS)) + + # Shuffle the elements of the list. + random.shuffle(password_elements) + + # Convert the list to a string and return the final password string. + password = ''.join(password_elements) + return password + + \ No newline at end of file diff --git a/answer-4/psw.png b/answer-4/psw.png new file mode 100644 index 0000000..e44f6f9 Binary files /dev/null and b/answer-4/psw.png differ diff --git a/bonus.ipynb b/bonus.ipynb new file mode 100644 index 0000000..7bb614a --- /dev/null +++ b/bonus.ipynb @@ -0,0 +1,99 @@ +{ + "cells": [ + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Write a Python program to sort a list of dictionaries using Lambda. Original list of dictionaries : [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]\n", + "\n", + "Sorting the List of dictionaries : [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}]" + ] + }, + { + "cell_type": "code", + "execution_count": 1, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}]\n" + ] + } + ], + "source": [ + "input_list = [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]\n", + "\n", + "output_list = sorted(input_list, key = lambda x: int(x['model']), reverse = True)\n", + "\n", + "print(output_list)\n" + ] + }, + { + "attachments": {}, + "cell_type": "markdown", + "metadata": {}, + "source": [ + "Write a Python program to filter a list of integers using Lambda.\n", + "\n", + "Original list of integers: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "\n", + "Even numbers from the said list: [2, 4, 6, 8, 10]\n", + "\n", + "Odd numbers from the said list: [1, 3, 5, 7, 9]" + ] + }, + { + "cell_type": "code", + "execution_count": 2, + "metadata": {}, + "outputs": [ + { + "name": "stdout", + "output_type": "stream", + "text": [ + "[2, 4, 6, 8, 10]\n", + "[1, 3, 5, 7, 9]\n" + ] + } + ], + "source": [ + "original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]\n", + "even_numbers = list(filter(lambda x: x % 2 == 0, original_list))\n", + "odd_numbers = list(filter(lambda x: x % 2!= 0, original_list))\n", + "\n", + "print(even_numbers)\n", + "print(odd_numbers)" + ] + } + ], + "metadata": { + "kernelspec": { + "display_name": "Python 3", + "language": "python", + "name": "python3" + }, + "language_info": { + "codemirror_mode": { + "name": "ipython", + "version": 3 + }, + "file_extension": ".py", + "mimetype": "text/x-python", + "name": "python", + "nbconvert_exporter": "python", + "pygments_lexer": "ipython3", + "version": "3.10.9" + }, + "orig_nbformat": 4, + "vscode": { + "interpreter": { + "hash": "2797e5e3c6ba5d7debb5cc5ee17065306a7394527f15851b12060374d87054f5" + } + } + }, + "nbformat": 4, + "nbformat_minor": 2 +}