From 6d5c3c71fce9ffdf3b7a0ec8846be4ccff45016e Mon Sep 17 00:00:00 2001 From: elen-ladder Date: Tue, 31 Jan 2023 02:45:13 +0100 Subject: [PATCH 1/3] Home work Week 3 First part --- answ_2.py | 13 +++++++++++++ answ_3.py | 19 +++++++++++++++++++ bonus_1.py | 13 +++++++++++++ bonus_2.py | 22 ++++++++++++++++++++++ 4 files changed, 67 insertions(+) create mode 100644 answ_2.py create mode 100644 answ_3.py create mode 100644 bonus_1.py create mode 100644 bonus_2.py diff --git a/answ_2.py b/answ_2.py new file mode 100644 index 0000000..b061e16 --- /dev/null +++ b/answ_2.py @@ -0,0 +1,13 @@ +## Assignment 2 + +# Write a Python program that accepts a hyphen-separated sequence of words as input and prints the words in a hyphen-separated sequence after sorting them alphabetically. + +# Sample Items : green-red-yellow-black-white +# Expected Result : black-green-red-white-yellow + + +my_str=input('Insert a hyphen-separated sequence of words,please!') +my_list=my_str.split ("-") #making a list +my_list_sort=sorted(my_list) #making sorted list +my_str_sort='-'.join(my_list_sort) #making sorted string +print("After sorting alphabetically it is look like so:", my_str_sort) #output result \ No newline at end of file diff --git a/answ_3.py b/answ_3.py new file mode 100644 index 0000000..62c3e18 --- /dev/null +++ b/answ_3.py @@ -0,0 +1,19 @@ +## Assignment 3 + +#Write a Python function to check whether a number is perfect or not. + + +def perf_check(num): + """function to check whether a number is perfect or not""" + sum_d=0 + for i in range (1,num): + if num%i==0: + sum_d +=i + if sum_d==num: + return("Perfect") + else: + return("Not perfect") + + +num=int(input(("Insert a number - ")))#call the function with integer input +print(perf_check(num)) \ No newline at end of file diff --git a/bonus_1.py b/bonus_1.py new file mode 100644 index 0000000..a2a2ce9 --- /dev/null +++ b/bonus_1.py @@ -0,0 +1,13 @@ +### Bonus 1 + +# 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'}] + +# Sorting the List of dictionaries : +# [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}] + +original = [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}] +original.sort(key=lambda x:x['color']) +print(original) + diff --git a/bonus_2.py b/bonus_2.py new file mode 100644 index 0000000..2173708 --- /dev/null +++ b/bonus_2.py @@ -0,0 +1,22 @@ +### Bonus 2 + +# Write a Python program to filter a list of integers using Lambda. + +# Original list of integers: +# [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +# Even numbers from the said list: +# [2, 4, 6, 8, 10] + +# Odd numbers from the said list: +# [1, 3, 5, 7, 9] + +my_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] + +even_list = list(filter(lambda x: (x%2 == 0) , my_list))# Even numbers from the list + +print(even_list) + +odd_list= list(filter(lambda x: (x%2 == 1), my_list)) # Odd numbers from the list + +print(odd_list) \ No newline at end of file From 11b31923fd48dd1e1ae7fef92d1692b3e1aca518 Mon Sep 17 00:00:00 2001 From: elen-ladder Date: Tue, 31 Jan 2023 18:56:15 +0100 Subject: [PATCH 2/3] Create answ_1.py --- answ_1.py | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) create mode 100644 answ_1.py diff --git a/answ_1.py b/answ_1.py new file mode 100644 index 0000000..c95bd2f --- /dev/null +++ b/answ_1.py @@ -0,0 +1,18 @@ +## Assignment 1 + +# Write a Python function that prints out the first n rows of Pascal's triangle. + +N=int(input("Insert number of rows in Pascal's triangle ")) +P=[] + +for i in range (N): + row = [1]*(i+1) #this is first 1 + for j in range (i+1): + if j!=0 and j!=i: # if itis not first and last position, then we make a calculation + row[j]=P[i-1][j-1] + P[i-1][j] #calculation acording to the formula + P.append(row) # make a list with nested lists + +for r in P: + print(r) #print every list from new line + + From b299ccf0348859661cacf8142402427b02632ad4 Mon Sep 17 00:00:00 2001 From: elen-ladder Date: Tue, 31 Jan 2023 20:34:25 +0100 Subject: [PATCH 3/3] Create answ_4.py --- answ_4.py | 15 +++++++++++++++ 1 file changed, 15 insertions(+) create mode 100644 answ_4.py diff --git a/answ_4.py b/answ_4.py new file mode 100644 index 0000000..28569b6 --- /dev/null +++ b/answ_4.py @@ -0,0 +1,15 @@ +import random +import string + +print('Password generator') #name of programm +len = int(input('Insert password length')) #input length +u_case=string.ascii_uppercase #upper case letter +l_case=string.ascii_lowercase #lower case letter +char=string.punctuation # character +numb=string.digits #numbers 0-9 +rand_list=l_case+u_case+char+numb #all together +password=''.join(random.sample(rand_list,len))# generate the random password +print('Password -',password) + + + \ No newline at end of file