From 18008fcd99b2263cb8fba917ef864d5ae5d7d6a2 Mon Sep 17 00:00:00 2001 From: DAKursad <122346253+DAKursad@users.noreply.github.com> Date: Mon, 30 Jan 2023 06:28:33 +0100 Subject: [PATCH] Week 3 homeworks --- Dict Sorting W Lambda.py | 4 ++++ Filter w Lambda.py | 8 ++++++++ Pascal Triangle.py | 9 +++++++++ Password Generator.py | 35 +++++++++++++++++++++++++++++++++++ Perfect Number.py | 12 ++++++++++++ Word sorting.py | 8 ++++++++ 6 files changed, 76 insertions(+) create mode 100644 Dict Sorting W Lambda.py create mode 100644 Filter w Lambda.py create mode 100644 Pascal Triangle.py create mode 100644 Password Generator.py create mode 100644 Perfect Number.py create mode 100644 Word sorting.py diff --git a/Dict Sorting W Lambda.py b/Dict Sorting W Lambda.py new file mode 100644 index 0000000..b820b34 --- /dev/null +++ b/Dict Sorting W Lambda.py @@ -0,0 +1,4 @@ +#Python program to sort a list of dictionaries using Lambda +ldict = [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}] +a = sorted(ldict, key=lambda x:x['color']) +print(f"\n {a} \n") \ No newline at end of file diff --git a/Filter w Lambda.py b/Filter w Lambda.py new file mode 100644 index 0000000..2aec6d1 --- /dev/null +++ b/Filter w Lambda.py @@ -0,0 +1,8 @@ +# Python program to filter a list of integers using Lambda +lint = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +even = lambda x: x%2==0 +odd = lambda x: x%2==1 + +print("Even numbers of the list:", list(filter(even,lint))) +print("Odd numbers of the list:", list(filter(odd,lint))) \ No newline at end of file diff --git a/Pascal Triangle.py b/Pascal Triangle.py new file mode 100644 index 0000000..47f8151 --- /dev/null +++ b/Pascal Triangle.py @@ -0,0 +1,9 @@ +n = int(input('Line amount of Pascal Triangle to be printed: ')) +triangle = [] +for r in range(n): + row = [1]*(r+1) + if r>1: # exclude first 2 rows from repetetive task below + for c in range(1,r): + row[c] = triangle[r-1][c-1]+triangle[r-1][c] + triangle.append(row) + print(" ".join( repr(e) for e in triangle[r] ).center(5*n)) \ No newline at end of file diff --git a/Password Generator.py b/Password Generator.py new file mode 100644 index 0000000..ea2d1cc --- /dev/null +++ b/Password Generator.py @@ -0,0 +1,35 @@ +import random as rnd +from string import punctuation + +# size variables of each character groups and total length +pass_lenght = int(input('\nPasword length: ')) +nlower = int(input('Number of Lowercase characters: ')) +nUpper = int(input('Number of Uppercase characters: ')) +nspec_chars = int(input('Number of special characters: ')) +nint = pass_lenght - nlower - nUpper - nspec_chars +# +pchar_list=[] + +def pick_lowercase(l): + # function returns required amount of lowercase letter characters + lower_chars = list(map(chr, range(97, 123))) + return [rnd.choice(lower_chars) for i in range(l)] + +def pick_uppercase(u): + # function returns required amount of uppercase letter characters + upper_chars = list(map(chr, range(65, 91))) + return [rnd.choice(upper_chars) for i in range(u)] + +def spec_chars(s): + # function returns required amount of special characters + spec_chars = list(set(punctuation)) + return [rnd.choice(spec_chars) for i in range(s)] + +int_chars = [str(rnd.randint(0, 9)) for i in range(nint)] + + +pchar_list = pick_lowercase(nlower) + pick_uppercase(nUpper) + spec_chars(nspec_chars) + int_chars +rnd.shuffle(pchar_list) + +password = ''.join(pchar_list) +print(f"\nPassword: {password}\n") diff --git a/Perfect Number.py b/Perfect Number.py new file mode 100644 index 0000000..fd790e5 --- /dev/null +++ b/Perfect Number.py @@ -0,0 +1,12 @@ +def isPerfect(n): + if sum([i for i in range(1,n) if n % i == 0]) == n: + return True + +n = int(input('Number to be checked if it is perfect: ')) +if isPerfect(n): + print(f"{n} is a perfect number") +else: + print(f"{n} is not a perfect number") + + + diff --git a/Word sorting.py b/Word sorting.py new file mode 100644 index 0000000..77748a8 --- /dev/null +++ b/Word sorting.py @@ -0,0 +1,8 @@ +# program to sort hyphen-separated text in words alphabetically. +txt = input('Hyphen delimited text: ') +delimiter = '-' + +def sortspecial(txt, sep): + return sep.join(sorted(list(txt.split(sep)))) + +print(sortspecial(txt, delimiter)) #print sorted hyphen delimited txt \ No newline at end of file