From f5f50dfc9bcc87d5898d01233f4b4bba2921a854 Mon Sep 17 00:00:00 2001 From: HaftomZ Date: Mon, 30 Jan 2023 00:17:59 +0100 Subject: [PATCH 1/5] Create Assignment_2.py --- Assignment_module_3/Assignment_2.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 Assignment_module_3/Assignment_2.py diff --git a/Assignment_module_3/Assignment_2.py b/Assignment_module_3/Assignment_2.py new file mode 100644 index 0000000..c1fbcb9 --- /dev/null +++ b/Assignment_module_3/Assignment_2.py @@ -0,0 +1,18 @@ +def word_sorting(words): + """a function that accepts a hyphen separated sequence of words + and return a sorted hyphen separated sequence of words""" + words=words.split("-") + words.sort() + return "-".join(words) +words =input('Enter a hyphen separated sequence of words \n') +print(word_sorting(words)) + +# the program below I wrote at first, which works perfectly but not efficient. +# input_words = input('Enter words seperated by - \n') +# input_words=list(input_words.split('-')) +# sorted_words = sorted(list(input_words)) +# for index, i in enumerate(sorted_words): +# if index < len(sorted_words)-1: +# print (i, end="-") +# else: +# print(i) From d9a49f950b5393fc5333f6e0eca75c9990149220 Mon Sep 17 00:00:00 2001 From: HaftomZ Date: Mon, 30 Jan 2023 01:08:57 +0100 Subject: [PATCH 2/5] Create Assignment_3.py --- Assignment_3.py | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) create mode 100644 Assignment_3.py diff --git a/Assignment_3.py b/Assignment_3.py new file mode 100644 index 0000000..a5963e8 --- /dev/null +++ b/Assignment_3.py @@ -0,0 +1,21 @@ +def is_perfect_number(number): + """ a function that accepts an integer and return True + if the number is a perfect number or False if not""" + sum_1 = 0 + for i in range(1, number): + if number % i == 0: + sum_1 += i + sum_2 = (sum_1 + number) / 2 + return sum_1 == number and sum_2 == number + +number = input('enter a positive enteger \n') +try: + number = int(number) +except: + print("Invalid input, please enter an integer") + quit() +if is_perfect_number(number): + print (f"The number {number} is a perfect number") +else: + print (f"The number {number} is not a perfect number") + \ No newline at end of file From 9332aea2cb93c058cd805024a81b334849c1de16 Mon Sep 17 00:00:00 2001 From: HaftomZ Date: Mon, 30 Jan 2023 16:31:31 +0100 Subject: [PATCH 3/5] modified assignment --- Assignment_1.py | 12 ++++++++++++ __pycache__/pascal_function.cpython-310.pyc | Bin 0 -> 553 bytes pascal_function.py | 16 ++++++++++++++++ 3 files changed, 28 insertions(+) create mode 100644 Assignment_1.py create mode 100644 __pycache__/pascal_function.cpython-310.pyc create mode 100644 pascal_function.py diff --git a/Assignment_1.py b/Assignment_1.py new file mode 100644 index 0000000..1e1ee9a --- /dev/null +++ b/Assignment_1.py @@ -0,0 +1,12 @@ +from pascal_function import pascal_triangle as pas_tri +while True: + number = (input('enter the number:\n')) + try: + number = int(number) + except: + print("Invalid input, please enter an integer") + quit() + pas_tri(number) + continues= input("if you want to continue write yes, if not any key \n") + if continues != 'yes': + break \ No newline at end of file diff --git a/__pycache__/pascal_function.cpython-310.pyc b/__pycache__/pascal_function.cpython-310.pyc new file mode 100644 index 0000000000000000000000000000000000000000..892e4c478c0bfef0e2994d4fd7ecc555e6c56d99 GIT binary patch literal 553 zcmZ8eO>5gg5S>}cMz~R8+|rm7T3;3FUUEr42mu@FOH9F(f>Bsx?K%~)R*rTBb&*f* zp_l%U{G!6%LQeSuJ>-&(g;Pui-edR8%-aW>`F;baS~tJr0|s!rzHAq1a7xfGi3AAz z0b`IT*d@z-2`fMl&Zt7*#z;et<!;MJkhX{t+u8NirNavr7mCuL4A0) z4ths`@2t}a&^l5Hi;cjswLDT6($?mAqKtKj0@v16kxpc8F&X>wNXOBPZ97I!-rC~pdi&QG3!;gs>6qB$wi?lxOep;BXnd+WpqL|6<7b!1~ z!j18?P_aoe^=7`XdmGK<38k9Jb!`2dsEIB%(Zd#D7oV{fHqiZ(`_vjg<@~=j&W8bg i7d5TNxr$QBc@=nzcB<}&v`TbRJ>-$(fwx1}4}SsBoq^i` literal 0 HcmV?d00001 diff --git a/pascal_function.py b/pascal_function.py new file mode 100644 index 0000000..81ae3b3 --- /dev/null +++ b/pascal_function.py @@ -0,0 +1,16 @@ +def pascal_triangle(number): + for i in range (number+1): + pascal_number = i + for index in range(i+1): + if index == 0: + print(index + 1, end=" ") + elif index == 1: + print(i, end=" ") + else: + pascal_number *= (i-(index-1))/(index) + print (int(pascal_number), end=" " ) + print('\n') +if __name__ == "__pascal_triangle__": + pascal_triangle() + +# pascal sequence: 1 , n , n(n−1)/2 , n(n−1)(n−2)/ 2*3 , n(n−1)(n−2)(n−3)/2*3*4...... From 863239e76deee10950667b2799fca322adc65e55 Mon Sep 17 00:00:00 2001 From: HaftomZ Date: Mon, 30 Jan 2023 17:00:34 +0100 Subject: [PATCH 4/5] pascal modified --- __pycache__/pascal_function.cpython-310.pyc | Bin 553 -> 589 bytes pascal_function.py | 32 +++++++++++++------- 2 files changed, 21 insertions(+), 11 deletions(-) diff --git a/__pycache__/pascal_function.cpython-310.pyc b/__pycache__/pascal_function.cpython-310.pyc index 892e4c478c0bfef0e2994d4fd7ecc555e6c56d99..ea9bbd046e1bb3056ef645d5596cfcc1e994609a 100644 GIT binary patch literal 589 zcmZ8dO=}cE5UuLoFNTdVUUt1Lh=e)pBIF{nEJ7B{#YG4rgb6Y`-C4)XOi#Ld&|P*; z$<>2D!(Sxy)r7sW=TQd3Tw2mg-e{n>3+>9X95Q|bx?DQTio);#3uL`hH$Yf zTB3agmWj@a{taM@KBEaXrzv7X(Y-`Mb_I}v-s0Lz*s#$kM8{pjm9Da7VZW>MawMbq>Z;jL@I*QM%%A>hTs$3S< zM6XhNR4t>!EH>u(_|4jWP{sJQ5>+P0@1#6Gh|1U`arXYKDiWKjVp6VMFwIh96P1@U ze=y@OJcP}Yzfh~wxL9R!GR+i?ni(ax_-lfK!+?~wHKC+ympU!1Kl*nIDB_%q!Q^`?2V=d5Ay HJM`fXNC|_0 literal 553 zcmZ8eO>5gg5S>}cMz~R8+|rm7T3;3FUUEr42mu@FOH9F(f>Bsx?K%~)R*rTBb&*f* zp_l%U{G!6%LQeSuJ>-&(g;Pui-edR8%-aW>`F;baS~tJr0|s!rzHAq1a7xfGi3AAz z0b`IT*d@z-2`fMl&Zt7*#z;et<!;MJkhX{t+u8NirNavr7mCuL4A0) z4ths`@2t}a&^l5Hi;cjswLDT6($?mAqKtKj0@v16kxpc8F&X>wNXOBPZ97I!-rC~pdi&QG3!;gs>6qB$wi?lxOep;BXnd+WpqL|6<7b!1~ z!j18?P_aoe^=7`XdmGK<38k9Jb!`2dsEIB%(Zd#D7oV{fHqiZ(`_vjg<@~=j&W8bg i7d5TNxr$QBc@=nzcB<}&v`TbRJ>-$(fwx1}4}SsBoq^i` diff --git a/pascal_function.py b/pascal_function.py index 81ae3b3..d1a2e6e 100644 --- a/pascal_function.py +++ b/pascal_function.py @@ -1,16 +1,26 @@ def pascal_triangle(number): - for i in range (number+1): - pascal_number = i - for index in range(i+1): - if index == 0: - print(index + 1, end=" ") - elif index == 1: - print(i, end=" ") - else: - pascal_number *= (i-(index-1))/(index) - print (int(pascal_number), end=" " ) - print('\n') + row =[1] + for i in range(number): + for j in range(i+1): + print(row[j], end=" ") + row = [x+y for x, y in zip([0] + row , row + [0])] + print('\n') if __name__ == "__pascal_triangle__": pascal_triangle() +# .... the program below I wrote at first, which works perfectly but not efficient........... +# def pascal_triangle(number): +# for i in range (number): +# pascal_number = i +# for index in range(i+1): +# if index == 0: +# print(index + 1, end=" ") +# elif index == 1: +# print(i, end=" ") +# else: +# pascal_number *= (i-(index-1))/(index) +# print (int(pascal_number), end=" " ) +# print('\n') + + # pascal sequence: 1 , n , n(n−1)/2 , n(n−1)(n−2)/ 2*3 , n(n−1)(n−2)(n−3)/2*3*4...... From 485b13e65054230f6134d26a0084e7f08cf3efa0 Mon Sep 17 00:00:00 2001 From: HaftomZ Date: Tue, 31 Jan 2023 14:02:50 +0100 Subject: [PATCH 5/5] Finalized assignment --- Assignment_4.py | 51 ++++++++++++++++++++ Bonus_1.py | 5 ++ Bonus_2.py | 5 ++ __pycache__/pascal_function.cpython-310.pyc | Bin 589 -> 644 bytes pascal_function.py | 6 ++- 5 files changed, 65 insertions(+), 2 deletions(-) create mode 100644 Assignment_4.py create mode 100644 Bonus_1.py create mode 100644 Bonus_2.py diff --git a/Assignment_4.py b/Assignment_4.py new file mode 100644 index 0000000..05bdca2 --- /dev/null +++ b/Assignment_4.py @@ -0,0 +1,51 @@ +import random +import string +import tkinter as tk + +def generate_password(): + length = int(length_entry.get()) + num_lower = int(lower_entry.get()) + num_upper = int(upper_entry.get()) + num_special = int(special_entry.get()) + num_digits = length - num_lower - num_upper - num_special + if num_digits == 0: + password = ''.join(random.choices(string. ascii_lowercase, k=num_lower)) + password += ''.join(random.choices(string. ascii_uppercase, k=num_upper)) + password += ''.join(random.choices(string. punctuation, k=num_special)) + password = ''.join(random.sample( password, len(password))) + password_label.config(text= password) + else: + password = "Error! No.of characters doesn't match length" + password_label.config(text= password) + +root = tk.Tk() +root.title("Password Generator") + +length_label = tk.Label(root, text="Password length") +length_label.grid(row=0, column=0) +length_entry = tk.Entry(root) +length_entry.grid(row=0, column=1) + +lower_label = tk.Label(root, text="Number of lowercase letters") +lower_label.grid(row=1, column=0) +lower_entry = tk.Entry(root) +lower_entry.grid(row=1, column=1) + +upper_label = tk.Label(root, text="Number of uppercase letters") +upper_label.grid(row=2, column=0) +upper_entry = tk.Entry(root) +upper_entry.grid(row=2, column=1) + +special_label = tk.Label(root, text="Number of special characters") +special_label.grid(row=3, column=0) +special_entry = tk.Entry(root) +special_entry.grid(row=3, column=1) + +generate_button = tk.Button(root, text="Generate", command=generate_password) +generate_button.grid(row=4, column=0, columnspan=2, pady=10) + +password_label = tk.Label(root, text="") +password_label.grid(row=5, column=0, columnspan=2) + + +root.mainloop() \ No newline at end of file diff --git a/Bonus_1.py b/Bonus_1.py new file mode 100644 index 0000000..25f2bce --- /dev/null +++ b/Bonus_1.py @@ -0,0 +1,5 @@ +lst = [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, + {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, + {'make': 'Samsung', 'model': 7, 'color': 'Blue'}] +lst.sort(key=lambda x: x ['color']) +print(lst) diff --git a/Bonus_2.py b/Bonus_2.py new file mode 100644 index 0000000..a24b606 --- /dev/null +++ b/Bonus_2.py @@ -0,0 +1,5 @@ +lst = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +even_numbers = list(filter(lambda x : x % 2 == 0, lst )) +odd_numbers = list(filter(lambda x : x % 2 != 0, lst)) +print(even_numbers) +print(odd_numbers) \ No newline at end of file diff --git a/__pycache__/pascal_function.cpython-310.pyc b/__pycache__/pascal_function.cpython-310.pyc index ea9bbd046e1bb3056ef645d5596cfcc1e994609a..cac6680b43f997151f5457704cc6b5dd27249b7d 100644 GIT binary patch delta 119 zcmX@h(!$D{&&$ij00a#$uP5hCzdDDB<|{g2dwF#GLq&qRhm+^qkcAct1_1B0-=Dw>aYC^AdA`B9m2^lzBLS NLL3Yn>|Bg2{{hbM9SHyc delta 64 zcmZo+J}co+b$iwna5 diff --git a/pascal_function.py b/pascal_function.py index d1a2e6e..57ad92e 100644 --- a/pascal_function.py +++ b/pascal_function.py @@ -1,6 +1,8 @@ def pascal_triangle(number): - row =[1] - for i in range(number): + """A function that accepts any integer n + and return the first n rows of pascal's triangle""" + row =[1] + for i in range(number): for j in range(i+1): print(row[j], end=" ") row = [x+y for x, y in zip([0] + row , row + [0])]