diff --git a/Assignment2.py b/Assignment2.py new file mode 100644 index 0000000..f6497bc --- /dev/null +++ b/Assignment2.py @@ -0,0 +1,10 @@ + +# Assignment 2 +''' this program accepts a hyphen-separated sequence of words as input and prints them + after sorting them alphabetically ''' + +words = input(("Enter the words separated by a hyphen as a list: "))# asking to give the words in the sequence +words_list = words.split('-') # put the words in a list +words_list.sort() # Sorting the words +result = '-'.join(words_list) # Joining the words with a hyphen +print("the words after sorting them alphabetically: " + result) # Printing the output diff --git a/Assignment3.py b/Assignment3.py new file mode 100644 index 0000000..7fa115a --- /dev/null +++ b/Assignment3.py @@ -0,0 +1,15 @@ +# # Assignment 3 +''' this function is used to check whether a number is perfect or not ''' +def perf_num(n): + sum = 0 + for i in range(1, n): # the number n is not included + if(n % i == 0): + sum += i + return sum == n + +# asking to give a number to check +n = int(input("Please enter a number: ")) +if perf_num(n): + print(f" {n} is a perfect number!") +else: + print(f" {n} is not a perfect number!") \ No newline at end of file diff --git a/Assignment4.py b/Assignment4.py new file mode 100644 index 0000000..ded16b0 --- /dev/null +++ b/Assignment4.py @@ -0,0 +1,12 @@ +# # Assignment 4 +''' this program composes a random password ''' +import random +import string + +def random_password(length): + items = string.ascii_letters + string.digits + string.punctuation # upper & lowercase letters , numbers and special symbols + password = ''.join(random.choice(items) for i in range(length)) # generating the password from the random choices + print("here is your random password :", password) +# # examples +random_password(8) +random_password(10) diff --git a/bonus.py b/bonus.py new file mode 100644 index 0000000..80bb8f0 --- /dev/null +++ b/bonus.py @@ -0,0 +1,8 @@ +# Bonus 2 +my_list = [1, 5, 4, 6, 8, 11, 3, 12] +even_list = list(filter(lambda x: (x%2 == 0) , my_list)) # using lambda to filter even numbers +print("the even numbers in the list are: ") +print(even_list) +odd_list = list(filter(lambda x: (x%2 != 0) , my_list)) # using lambda to filter odd numbers +print("the odd numbers in the list are: ") +print(odd_list) \ No newline at end of file