Home | | Problem Solving and Python Programming | Python Programs on Lists, Tuples, Dictionaries

Chapter: Problem Solving and Python Programming : Lists, Tuples, Dictionaries

Python Programs on Lists, Tuples, Dictionaries

Problem Solving and Python Programming : Lists, Tuples, Dictionaries

Illustrative programs:

 

Selection sort

a=input("Enter list:").split()

a=list(map(eval,a))

for i in range(0,len(a)):

smallest = min(a[i:])

sindex= a.index(smallest)

a[i],a[sindex] = a[sindex],a[i]

print (a)

Output

Enter list:23 78 45 8 32 56

[8,2 3, 32, 45,56, 78]



 

Insertion sort

a=input("enter a list:").split()

a=list(map(int,a))

for i in a:

j = a.index(i)

while j>0:

if a[j-1] > a[j]:

a[j-1],a[j] = a[j],a[j-1]

else:

break

j = j-1

print (a)

output

enter a list: 8 5 7 1 9 3

[1,3,5,7,8,9]



 

Merge sort

def merge(a,b):

c = []

while len(a) != 0 and len(b) != 0:

if a[0] < b[0]:

c.append(a[0])

a.remove(a[0])

else:

c.append(b[0])

b.remove(b[0])

if len(a) == 0:

c=c+b

else:

c=c+a

return c

def divide(x):

if len(x) == 0 or len(x) == 1:

return x

else:

middle = len(x)//2

a = divide(x[:middle])

b = divide(x[middle:])

return merge(a,b)

x=[38,27,43,3,9,82,10]

c=divide(x)

print(c)

output

[3,9,10,27,38,43,82]

 

 

Histogram

def histogram(a):

for i in a:

sum = ''

while(i>0):

sum=sum+'#'

i=i-1

print(sum)

a=[4,5,7,8,12]

histogram(a)

Output

****

*****

*******

********

************

 

Calendar program

import calendar

y=int(input("enter year:"))

m=int(input("enter month:"))

print(calendar.month(y,m))

Output

enter year:2017

enter month:11

November 2017

Mo Tu We Th Fr Sa Su

1 2 3 4 5

6 7 8 9 10 11 12

13 14 15 16 17 18 19

20 21 22 23 24 25 26

27 28 29 30

 

Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Problem Solving and Python Programming : Lists, Tuples, Dictionaries : Python Programs on Lists, Tuples, Dictionaries |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2024 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.