Sunteți pe pagina 1din 17

INPUT:

aList=[15,6,13,22,3,52,2]
print("Original list is:",aList)
n=len(aList)
for i in range(n):
for j in range(0,n-1-1):
if aList[j]>aList[j+1]:
aList[j],aList[j+1]=aList[j+1],aList[j]
print("List after sorting:",aList)
OUTPUT:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8
2019, 19:29:22) [MSC v.1916 32 bit (Intel)] on
win32
Type "help", "copyright", "credits" or "license()" for
more information.
>>>
= RESTART:
C:/Users/zeb/AppData/Local/Programs/Python/Pyt
hon37-32/bubble.py =
Original list is: [15, 6, 13, 22, 3, 52, 2]
List after sorting: [3, 6, 13, 15, 22, 52, 2]
INPUT:
aList=[15,6,13,22,3,52,2]
print("Original list is:",aList)
for i in range(1,len(aList)):
key=aList[i]
j=i-1
while j>=0 and key<aList[j]:
aList[j+1]=aList[j]
j=j-1
else:
aList[j+1]=key
print("List after sorting:",aList)

OUTPUT:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC
v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:/Users/zeb/AppData/Local/Programs/Python/Python37-
32/insertion.py
Original list is: [15, 6, 13, 22, 3, 52, 2]
List after sorting: [2, 3, 6, 13, 15, 22, 52]
>>>
INPUT:
def arCalc(x,y):
return x+y,x-y,x*y,x/y,x%y
#_main_
num1=int(input("Enter number 1:"))
num2=int(input("Enter number 2:"))
add,sub,mult,div,mod=arCalc(num1,num2)
print("Sum of given numbers:",add)
print("Subtraction of given numbers:",sub)
print("Product of given numbers:",mult)
print("Division of given numbers:",div)
print("Modulo of given numbers:",mod)

OUTPUT:
Enter number 1:13
Enter number 2:7
Sum of given numbers: 20
Subtraction of given numbers: 6
Product of given numbers: 91
Division of given numbers: 1.8571428571428572
Modulo of given numbers: 6
INPUT:
num=int(input("Enter a number:"))
print("Number entered=",num)
onum=oct(num)
hnum=hex(num)
print("Octal conversion yields",onum)
print("Hexadecimal conversion yields",hnum)

OUTPUT:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC v.1916 32
bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
RESTART: C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\afsha
pathan.py
Enter a number:17
Number entered= 17
Octal conversion yields 0o21
Hexadecimal conversion yields 0x11
INPUT:
def bp(sg,n):
if n>0:
print(sg[n],end='')
bp(sg,n-1)
elif n==0:
print(sg[0])

#_main_
s=input("Enter a string:")
bp(s,len(s)-1)

OUTPUT:
Enter a string:NEWS
SWEN
>>>
===============================================
RESTART: C:/Users/pc/AppData/Local/Programs/Python/Python38-
32/AFS,PY.py
==============================================
Enter a string:COMPUTER SCIENCE
ECNEICS RETUPMOC
INPUT:
def fib(n):
if n==1:
return 0
elif n==2:
return 1
else:
return fib(n-1)+fib(n-2)

#_main_
n=int(input("Enter last term required:"))
for i in range(1,n+1):
print(fib(i),end=',')
print("...")

OUTPUT:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC
v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=== RESTART:
C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\ak.py
===
Enter last term required:8
0,1,1,2,3,5,8,13,...

INPUT:
def factorial(n):
if n<2:
return 1
return n*factorial(n-1)
#_main_
n=int(input("Enter a number(>0):"))
print("Factorial of",n,"is",factorial(n))

OUTPUT:
Python 3.7.4 (tags/v3.7.4:e09359112e, Jul 8 2019, 19:29:22) [MSC
v.1916 32 bit (Intel)] on win32
Type "help", "copyright", "credits" or "license()" for more information.
>>>
=== RESTART:
C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\fac.py
===
Enter a number(>0):4
Factorial of 4 is 24
>>>
=== RESTART:
C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\fac.py
===
Enter a number(>0):5
Factorial of 5 is 120
INPUT:

def Lsearch(AR,ITEM):

i=0

while i<len(AR)and AR[i]!=ITEM:

i+=1

if i<len(AR):

return i

else:

return False

#----main----

N=int(input("Enter desired linear-list size(max.50)..."))

print("\nEnter element for linear list\n")

AR=[0]*N

for i in range(N):

AR[i]=int(input("Element"+str(i)+":"))

ITEM=int(input("\nEnter Element to be searched for..."))

index=Lsearch(AR,ITEM)

if index:

print("\nElement found at index:",index,",Position:",(index+1))

else:

print("\nSorry!!Given element could not be found.\n")

OUTPUT:

Enter desired linear-list size(max.50)...7

Enter element for linear list

Element0:88

Element1:77

Element2:44

Element3:33

Element4:22

Element5:11

Element6:10

Enter Element to be searched for...11

Element found at index: 5 ,Position: 6


INPUT:
def Bsearch(AR,ITEM):

beg=0

last=len(AR)-1

while(beg<=last):

mid=int((beg+last)/2)

if(AR[mid]==ITEM):

return mid

elif(ITEM>AR[mid]):

beg=mid+1

else:

last=mid-1

else:

return False

#_main_

N=int(input("Enter desired linear-list size(max.50)..."))

print("\nEnter elements for linear list in ASCENDING ORDER\n")

AR=[0]*N

for i in range(N):

AR[i]=int(input("Element"+str(i)+":"))

ITEM=int(input("\nEnter Element to be searched for..."))

index=Bsearch(AR,ITEM)

if index:

print("\nElement found at index:",index,",Position:",(index+1))

else:

print("\nSorry!!Given element could not be found.\n")

OUTPUT:

Enter desired linear-list size(max.50)...5

Enter elements for linear list in ASCENDING ORDER

Element0:11

Element1:15

Element2:18

Element3:21

Element4:23
Enter Element to be searched for...2

Element found at index: 3 ,Position: 4

INPUT:
def FindPos(AR,item):

size=len(AR)

if item<AR[0]:

return 0

else:

pos=-1

for i in range(size-1):

if (AR[i]<=item and item<AR[i+1]):

pos=i+1

break

if (pos==-1 and i<=size-1):

pos=size

return pos

def Shift(AR,pos):

AR.append(None)

size=len(AR)

i=size-1

while i>=pos:

AR[i]=AR[i-1]

i=i-1

#_main_

myList=[10,20,30,40,50,60,70]

print("The list in sorted order is")

print(myList)

ITEM=int(input("Enter new element to be inserted:"))

position=FindPos(myList,ITEM)

Shift(myList,position)

myList[position]=ITEM

print("The list after inserting",ITEM,"is")

print(myList)

OUTPUT:
The list in sorted order is

[10, 20, 30, 40, 50, 60, 70]

Enter new element to be inserted:80


The list after inserting 80 is

[10, 20, 30, 40, 50, 60, 70, 80]

INPUT:
def Bsearch(AR,ITEM):

beg=0

last=len(AR)-1

while beg<=last:

mid=int((beg+last)/2)

if(ITEM==AR[mid]):

return mid

elif(ITEM>AR[mid]):

beg=mid+1

else:

last=mid-1

else:

return False

#_main_

myList=[10,20,30,40,50,60,70]

print("The list in sorted order is")

print(myList)

ITEM=int(input("Enter element to be deleted:"))

position=Bsearch(myList,ITEM)

if position:

del myList[position]

print(myList)

else:

print("SORRY!No such element in the list")

OUTPUT:
The list in sorted order is

[10, 20, 30, 40, 50, 60, 70]

Enter element to be deleted:45

SORRY!No such element in the list

RESTART: C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\del.py ===

The list in sorted order is

[10, 20, 30, 40, 50, 60, 70]

Enter element to be deleted:40

[10, 20, 30, 50, 60, 70]


INPUT:

def traverse(AR):

size=len(AR)

for i in range(size):

print(AR[i],end='')

#_main_

size=int(input("Enter the size of Linear List to be input:"))

AR=[None]*size

print("Enter elements for the Linear List")

for i in range(size):

AR[i]=int(input("Element"+str(i)+":"))

print("Traversing the list:")

traverse(AR)

OUTPUT:

== RESTART: C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\anshi.py ==

Enter the size of Linear List to be input:6

Enter elements for the Linear List

Element0:12

Element1:23

Element2:34

Element3:45

Element4:56

Element5:67

Traversing the list:

12 23 34 45 56 67
Input:
def isEmpty(stk):

if stk==[]:

return True

else:

return False

def Push(stk,item):

stk.append(item)

top=len(stk)-1

def Pop(stk):

if isEmpty(stk):

return"Underflow"

else:

item=stk.pop()

if len(stk)==0:

top=None

else:

top=len(stk)-1

return item

def Peek(stk):

if isEmpty(stk):

return"Underflow"

else:

top=len(stk)-1

return stk[top]

def Display(stk):

if isEmpty(stk):

print("Stack empty")

else:

top=len(stk)-1

print(stk[top],"<-top")

for a in range(top-1,-1,-1):

print(stk[a])

#_main_
Stack=[]

top=None

while True:

print("STACK OPERATIONS")

print("1.Push")

print("2.Pop")

print("3.Peek")

print("4.Display stack")

print("5.Exit")

ch=int(input("Enter your choice(1-5):"))

if ch==1:

item=int(input("enter item:"))

Push(Stack,item)

elif ch==2:

item=Pop(Stack)

if item=="underflow":

print("underflow!stack empty!")

else:

print("popped item is",item)

elif ch==3:

item=Peek(Stack)

if item=="underflow":

print("underflow!stack is empty!")

else:

print("Topmost item is",item)

elif ch==4:

Display(Stack)

elif ch==5:

break

else:

print("Invalid choice!")

Output:
=== RESTART: C:\Users\zeb\AppData\Local\Programs\Python\Python37-32\pro.py ===

STACK OPERATIONS

1.Push

2.Pop
3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):1

enter item:6

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):1

enter item:8

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):1

enter item:2

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):1

enter item:4

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):4

4 <-top

2
8

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):3

Topmost item is 4

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):4

4 <-top

STACK OPERATIONS

1.Push

2.Pop

3.Peek

4.Display stack

5.Exit

Enter your choice(1-5):5

S-ar putea să vă placă și