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
13 changes: 13 additions & 0 deletions Bonus1&2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# Bonus question number 1
y = [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}]
newlist = sorted(y, key=lambda d: d['color'])
print(newlist)


# Bonus question number 1
original_list = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
even_numbers = list(filter(lambda x: x%2 == 0, original_list))
odd_numbers = list(filter(lambda x: x%2 != 0, original_list))
print('original list:',original_list)
print('Even numbers: ', even_numbers)
print('Even numbers: ', odd_numbers)
22 changes: 22 additions & 0 deletions answer1.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
from math import factorial


def pascal_triangel(n):
"""
This function prints out
the first n rows of Pascal's triangle
"""
for i in range(n):
for j in range(n-i+1):
print(end=' ') # left space

for j in range(i+1):

# nCr = n!/((n-r)!*r!)
print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ")

print()



pascal_triangel(5)
14 changes: 14 additions & 0 deletions answer2.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
def sort_hyphen_phrase(phrase):
"""
This function accepts a hyphen-separated
sequence of words as input and prints
the words in a hyphen-separated sequence after
sorting them alphabetically
"""
splitted_phrase = phrase.split('-') # split '-' words
sorted_phrase = sorted(splitted_phrase) # sort all words
output = '-'.join(sorted_phrase) # join words agian and seperate them by '-'
print(output)


sort_hyphen_phrase('green-red-yellow-black-white')
17 changes: 17 additions & 0 deletions answer3.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
def check_perfect(n):
"""
This function checks if the given number
is a perfect number or not
"""
temp = 0
for i in range(1,n+1):
temp += i
if (n/i)% 2 == 0 and temp == n: # check if n is perfect number
print('Perfect number')
break
else:
print('Not a perfect number ')



check_perfect(6)
54 changes: 54 additions & 0 deletions answer4.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
from tkinter import *
from tkinter import filedialog
import random
import string

def generate_passowrd():
"""
This function is used to generate random password
and display the password in tkinter screen
"""
try:
global user_entry
pass_len = int(user_entry.get()) # get password length from user
rand_num = random.randrange(0, 10) # random number from 0 to 9
lower_case = string.ascii_lowercase # random upper case letter
upper_case = string.ascii_uppercase # random upper case letter
special_char = string.punctuation # random special character
rand_pass = ''.join([lower_case,upper_case,special_char,str(rand_num)])
rand_pass = random.sample(rand_pass,pass_len) # generate the random password
password_label.config(text=rand_pass)
except ValueError:
password_label.config(text='ERROR: Please Enter a number')




# start screen
screen = Tk()
title = screen.title('Password generator')
canvas = Canvas(screen, width=500, height=500)
canvas.pack()

# declare labels and buttons
greeting_label = Label(screen, text='WELCOME TO PASSWORD GENERATOR ', font=('Times', 15))
user_entry = Entry(screen, text='password length', font=('Arial', 15))
username_label = Label(screen, text='Enter your password length: ', font=('Calibri', 15))
password_label = Label(screen, text= "", font=('Arial', 15))
btn = Button(screen, bg='dark blue', padx='22', pady='5', font=('Arial', 15)
, fg='light blue', text='Generate password',command= generate_passowrd)


# Place to the canvas
canvas.create_window(250, 100, window=greeting_label)
canvas.create_window(250, 170, window=username_label)
canvas.create_window(250, 220, window=user_entry)
canvas.create_window(250, 280, window=btn)
canvas.create_window(250, 325, window=password_label)


screen.mainloop()