Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 18 additions & 0 deletions answ_1.py
Original file line number Diff line number Diff line change
@@ -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


13 changes: 13 additions & 0 deletions answ_2.py
Original file line number Diff line number Diff line change
@@ -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
19 changes: 19 additions & 0 deletions answ_3.py
Original file line number Diff line number Diff line change
@@ -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))
15 changes: 15 additions & 0 deletions answ_4.py
Original file line number Diff line number Diff line change
@@ -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)



13 changes: 13 additions & 0 deletions bonus_1.py
Original file line number Diff line number Diff line change
@@ -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)

22 changes: 22 additions & 0 deletions bonus_2.py
Original file line number Diff line number Diff line change
@@ -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)