diff --git a/Assignment_1.py b/Assignment_1.py new file mode 100644 index 0000000..e2bbacc --- /dev/null +++ b/Assignment_1.py @@ -0,0 +1,16 @@ +# Answer 1 +# Write a Python function that prints out the first n rows of Pascal's triangle. Note : Pascal's triangle is an arithmetic and geometric figure first imagined by Blaise Pascal. + +# Sample Pascal's triangle : + + +from math import factorial +def number_of_rows(n): + for i in range(n): + for j in range(n - i + 1): + print(end=" ") + for j in range(i + 1): + print(factorial(i)//(factorial(j)*factorial(i-j)), end=" ") + print("") +n = 5 +number_of_rows(n) \ No newline at end of file diff --git a/Assignment_2.py b/Assignment_2.py new file mode 100644 index 0000000..dd6432f --- /dev/null +++ b/Assignment_2.py @@ -0,0 +1,13 @@ +#Answer 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# + +items = [] +def res(n): + for i in n: + items.append(i) + items.sort() + return "-".join(items) +user_input = input("Enter:").split("-") +print(res(user_input)) diff --git a/Assignment_3.py b/Assignment_3.py new file mode 100644 index 0000000..75bf9bf --- /dev/null +++ b/Assignment_3.py @@ -0,0 +1,19 @@ +# Assement 3 +# Write a Python function to check whether a number is perfect or not. + +# According to Wikipedia : In number theory, a perfect number is a positive integer that is equal to the sum of its proper positive divisors, that is, the sum of its positive divisors excluding the number itself (also known as its aliquot sum). Equivalently, a perfect number is a number that is half the sum of all of its positive divisors (including itself). + +# Example : The first perfect number is 6, because 1, 2, and 3 are its proper positive divisors, and 1 + 2 + 3 = 6. Equivalently, the number 6 is equal to half the sum of all its positive divisors: ( 1 + 2 + 3 + 6 ) / 2 = 6. The next perfect number is 28 = 1 + 2 + 4 + 7 + 14. This is followed by the perfect numbers 496 and 8128. + +def perfect_number(n): + sum = 0 + for i in range(1, n): + if n % i == 0: + sum += i + if (sum == n): + return "The number is a Perfect number!" + else: + return "The number is not a Perfect number!" +num = int(input("Enter any number: ")) +print(perfect_number(num)) + diff --git a/Assignment_4.py b/Assignment_4.py new file mode 100644 index 0000000..4260e1d --- /dev/null +++ b/Assignment_4.py @@ -0,0 +1,38 @@ +# Answer 4 +"""Create a user interface that generates a random password with the given +user input such as length, number of lowercase letters and uppercase letters and +adding special characters(!,?, .,etc.)""" + +from tkinter import * +import random +import string + + +screen = Tk() + +screen.title("Password Generator!") +screen.geometry('350x200') +screen.title("Password Generator!") + +length_text = Label(screen, text="Enter the number of characters") +length_text.pack() + +length = Entry(screen, width=20) +length.pack(padx=20, pady=20) + +lower = string.ascii_lowercase +upper = string.ascii_uppercase +num =string.digits +speialchar = string.punctuation +all = lower + upper + num + speialchar +def generate(): + l = int(length.get()) + temp = random.sample(all,l) + password= Label(screen) + password.configure(text ="Password: "+ "".join(temp)) + password.pack() +generate_btn = Button(screen, text="Generate Password", command=generate) +generate_btn.pack() + +screen.mainloop() + diff --git a/Bonus_1.py b/Bonus_1.py new file mode 100644 index 0000000..62cf5e0 --- /dev/null +++ b/Bonus_1.py @@ -0,0 +1,16 @@ +"""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'}]""" + + + +models_list = [{'make': 'Nokia', 'model': 216, 'color': 'Black'}, {'make': 'Mi Max', 'model': '2', 'color': 'Gold'}, {'make': 'Samsung', 'model': 7, 'color': 'Blue'}] +print("Original list of dictionaries:") +print(models_list) +sorted_models = sorted(models_list, key=lambda k: k['color']) + +print("\nSorted list of dictionaries:") +print(sorted_models) diff --git a/Bonus_2.py b/Bonus_2.py new file mode 100644 index 0000000..67ea7a1 --- /dev/null +++ b/Bonus_2.py @@ -0,0 +1,20 @@ +# Bonus 2 Answer +# 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] + +number = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +print("Original list of integers:") +print(number) + +print("Even numbers from the said list:") +even_nums = list(filter(lambda x: x%2 == 0, number)) +print(even_nums) + +print("Odd numbers from the said list:") +odd_nums = list(filter(lambda x: x%2 != 0, number)) +print(odd_nums) \ No newline at end of file diff --git a/README.md b/README.md index 2e600fc..6c7b28e 100644 --- a/README.md +++ b/README.md @@ -12,6 +12,7 @@ Sample Pascal's triangle : Pascal's triangle Each number is the two numbers above it added together + ## 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. diff --git a/imag_output/Ass 1.png b/imag_output/Ass 1.png new file mode 100644 index 0000000..39cafea Binary files /dev/null and b/imag_output/Ass 1.png differ diff --git a/imag_output/Ass 2.png b/imag_output/Ass 2.png new file mode 100644 index 0000000..2b111bd Binary files /dev/null and b/imag_output/Ass 2.png differ diff --git a/imag_output/Ass 3.png b/imag_output/Ass 3.png new file mode 100644 index 0000000..2d14ba5 Binary files /dev/null and b/imag_output/Ass 3.png differ diff --git a/imag_output/Ass 4.png b/imag_output/Ass 4.png new file mode 100644 index 0000000..a34c33d Binary files /dev/null and b/imag_output/Ass 4.png differ diff --git a/imag_output/bonus 1.png b/imag_output/bonus 1.png new file mode 100644 index 0000000..5e50cc6 Binary files /dev/null and b/imag_output/bonus 1.png differ diff --git a/imag_output/bonus 2.png b/imag_output/bonus 2.png new file mode 100644 index 0000000..12700be Binary files /dev/null and b/imag_output/bonus 2.png differ