# 1. WAP to do selection and insertion sorting.
"""
def selection():
arr = [20,9,16,3,5]
n = len(arr)
for i in range(n-1):
pos_smallest = i
for j in range(i+1, n):
if arr[j] < arr[pos_smallest]:
pos_smallest = j
arr[i], arr[pos_smallest] = arr[pos_smallest], arr[i]
print('Array after sorting')
print(arr)
def insertion():
arr = [20,9,16,3,5]
for i in range(1,len(arr)):
j=i
while arr[j-1]>arr[j] and j>0:
arr[j-1], arr[j] = arr[j] , arr[j-1]
j-=1
print("THE LIST AFTER SORTING:" , arr)
print("*"*80)
print("\t\t\t\tSorting")
print("*"*80)
ch='Y'
while ch=='Y':
print("\t\t 1. Selection Sort")
print("\t\t 2. Insertion Sort")
choice=int(input("Enter the Option 1/2:"))
if choice==1:
selection()
elif choice==2:
insertion()
print("\nDo u want to continue??")
ch=input("Enter Y/N:")
if ch=='N':
break
"""
#Pg02: WAP to convert binary to decimal and decimal to binary
"""
def BtoD():
print("Enter the Binary Number: ", end="")
num = input()
l=list(num)
[Link]()
sum=0
for i in range (len(l)):
sum= sum +int(l[i])*(2**i)
print("THE DECIMAL NO. IS =" ,sum)
def DtoB():
print("Enter Decimal Number: ", end="")
n= int(input())
list=[]
r=0
while n!=0:
r=n%2
n=n//2
[Link](r)
[Link]()
for i in list:
print(i,end="")
print("*"*80)
print("\t\t\t\tConvert Binary to decimal and Decimal to Binary")
print("*"*80)
ch='Y'
while ch=='Y':
print("\t\t [Link] Binary to decimal")
print("\t\t 2. Convert Decimal to Binary")
choice=int(input("Enter the Option 1/2:"))
if choice==1:
BtoD()
elif choice==2:
DtoB()
print("\nDo u want to continue??")
ch=input("Enter Y/N:")
if ch=='N':
break
"""
#PG:3Program to create a dictionary which stores name,dep,Gender of the employee
#and their salary
"""
def addEmployee():
num = int(input("Enter the number of employees whose data to be stored: "))
count = 1
while count <= num:
EmpCode=input("Enter the Employee_Code:")
name = input("Enter the name of the Employee: ")
dep=input("Enter the name of Department:")
Gender=input("Enter Gender:")
salary = int(input("Enter the salary: "))
employee[EmpCode] = [name,dep,Gender,salary]
count += 1
print(employee)
print("\n\nEMPLOYEE_nAME\tSALARY")
for k in employee:
print(k,'\t\t',employee[k][3])
def searchEmployee():
search=input("\t\tSearch by Code/Name/dep/Gender:")
for i in employee:
if i==search:
print(i,"\t\t",employee[i])
elif employee[i][0]==search:
print(i,"\t\t",employee[i])
elif employee[i][1]==search:
print(i,"\t\t",employee[i])
elif employee[i][2]==search:
print(i,"\t\t",employee[i])
def UpdateEmployee():
update=input("\t\t Enter Name/dep/Gender to be modified:")
Newvalue=input("\t\t Enter NewValue:")
for i in employee:
if employee[i][0]==update:
employee[i][0]=Newvalue
print("\n\t\tModified Value:")
print(i,"\t\t",employee[i])
elif employee[i][1]==update:
employee[i][1]=Newvalue
print("\n\t\tModified Value:")
print(i,"\t\t",employee[i])
elif employee[i][2]==update:
employee[i][2]=Newvalue
print("\n\t\tModified Value:")
print(i,"\t\t",employee[i])
print("*"*80)
print("\t\t\t\t Employee Management")
print("*"*80)
employee = dict()
ch='Y'
while ch=='Y':
print("\t\t 1. Add Employee")
print("\t\t 2. Search")
print("\t\t 3. Update")
choice=int(input("Enter the Option 1/2:"))
if choice==1:
addEmployee()
elif choice==2:
searchEmployee()
elif choice==3:
UpdateEmployee()
print("\nDo u want to continue??")
ch=input("Enter Y/N:")
if ch=='N':
break
"""
#PG4:Cities
"""Make a dictionary called cities. Use the names of three cities as keys in
your dictionary. Create a dictionary of information about each city and
include the country that the city is in, its approximate population, and
one fact about that city. The keys for each city’s dictionary should be
something like country, population, and fact. Print the name of each city
and all of the information you have stored about it."""
"""
cities = {
'santiago': {
'country': 'chile',
'population': 6158080,
'nearby mountains': 'andes',
},
'talkeetna': {
'country': 'alaska',
'population': 876,
'nearby mountains': 'alaska range',
},
'kathmandu': {
'country': 'nepal',
'population': 1003285,
'nearby mountains': 'himilaya',
}
}
for city, city_info in [Link]():
country = city_info['country'].title()
population = city_info['population']
mountains = city_info['nearby mountains'].title()
print("\n" + [Link]() + " is in " + country + ".")
print(" It has a population of about " + str(population) + ".")
print(" The " + mountains + " mountains are nearby.")
"""
#PG5:Write a Python Program to check if the two strings are anagram and Sort Words
in Alphabetical Order
"""
def anagram():
s1=input("Enter first string:")
s2=input("Enter second string:")
if(sorted(s1)==sorted(s2)):
print("The strings are anagrams.")
else:
print("The strings aren't anagrams.")
def sorting():
str_1 = input ("Enter a string")
words=[]
for word in str_1.split():
[Link]()
[Link](word)
[Link] ()
print ("The words sorted in alphabetical order are as follows:")
for word in words:
print (word)
print("*"*80)
ch='Y'
while ch=='Y':
print("\t\t 1. Anagram")
print("\t\t 2. Sorting")
choice=int(input("Enter the Option 1/2:"))
if choice==1:
anagram()
elif choice==2:
sorting()
print("\nDo u want to continue??")
ch=input("Enter Y/N:")
if ch=='N':
break
"""
# 6 WAP to print the Fibonacci series of n terms., factorial, Armstrong no.
"""
num = int(input("Enter a number: "))
sum = 0
temp = num
while temp > 0:
digit = temp % 10
sum += digit ** len(str(num))
temp //= 10
if num == sum:
print(num,"is an Armstrong number")
else:
print(num,"is not an Armstrong number")
num = 10
n1, n2 = 0, 1
print("Fibonacci Series:", n1, n2, end=" ")
for i in range(2, num):
n3 = n1 + n2
n1 = n2
n2 = n3
print(n3, end=" ")
print()
n=int(input("enter the no.="))
sum=1
for i in range (1,n+1):
sum= sum *i
print("the factorial=",sum)
"""
# 7. 7. WAP to generate the following pattern:
"""
rows=6
def pattern1(rows):
for i in range(1, rows+1):
for j in range(rows,0,-1):
if j >i:
print(" ", end=" ")
else:
print(j,end=" ")
print()
pattern1(5)
def pattern2():
x=64
for row in range (1,6):
for col in range (1,11):
print(" ",end=" ")
if row+col==6 or col-row==4:
x+=1
print(chr(x),end="")
elif row==5:
x+=1
print(chr(x),end="")
else:
print(end=" ")
print()
pattern2()
"""
#[Link] to demonstrate list manipulation- Insertion, deletion and Linear search
"""
terms = int(input("Enter no of terms in list : "))
list1 = []
for i in range(terms):
num = int(input("Enter the values: "))
[Link](num)
print("The list is : " , list1)
print("[Link] of values")
print("[Link] of values ")
print("[Link] search")
choice = int(input("Enter your choice : "))
if choice == 1 :
Insert = int(input("The value you want to insert : "))
Index = int(input("Which index you want to insert : "))
[Link](Insert , Index)
elif choice == 2:
Delete = int(input("Enter the value you want to delete: "))
[Link](Delete)
elif choice == 3:
Search = int(input("Enter the value to search: "))
Index = [Link](Search)
if Index == -1:
print("It is not present in the list")
else:
print("The index of " , Search , "is" , Index)
else:
print("Invalid choice!!!")
"""
#9 WAP to find the following series.
# S=1=4-9+16-25+36-………n terms
"""
sum_series = 0
n = int(input("Enter the no of terms: "))
for i in range(1 , n+1):
no = i**2
if (i%2 !=0 and i != 1):
no = -no*i
sum_series += no
print("The sum of series is :" , sum_series)
"""
# 10 Python program to print perfect numbers from the given list of integers
"""
plist=[]
n=int(input('no of terms:'))
list=[]
for i in range(n):
a= float(input('enter a number:'))
[Link](a)
temp = a**0.5
temp1=temp//1
if temp1<temp and temp1+1>temp:
continue
else:
[Link](a)
print(plist)
numbers_str = input("Enter a list of
integers separated by spaces: ")
numbers_list = [int(num) for num in
numbers_str.split()]
# Check if a number is a perfect number
for number in numbers_list:
sum_of_divisors = 0
# Find divisors and calculate their sum
for i in range(1, number):
if number % i == 0:
sum_of_divisors += i
# Check if the number is perfect
if sum_of_divisors == number:
print(f"{number} is a perfect
number.") else:
print(f"{number} is not a perfect
number.")
"""