0% found this document useful (0 votes)
6 views9 pages

Python Programs for Data Structures

The document contains multiple Python programs including implementations for a stack using lists, user authentication with CSV file handling, and sorting techniques (bubble and insertion sort). Each program includes a description, code, and sample output demonstrating its functionality. Additionally, it lists various programming tasks related to file handling, number manipulation, and string processing.

Uploaded by

jeyansindhu08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views9 pages

Python Programs for Data Structures

The document contains multiple Python programs including implementations for a stack using lists, user authentication with CSV file handling, and sorting techniques (bubble and insertion sort). Each program includes a description, code, and sample output demonstrating its functionality. Additionally, it lists various programming tasks related to file handling, number manipulation, and string processing.

Uploaded by

jeyansindhu08
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

1 [Link].M.C.A., [Link]., [Link].

, PGT IN COMPUTER SCIENCE

Program:-13
Write a python program to implement a stack using list

def isempty(stk):
if stk==[]:
return True
else:
return False

def push(stk,item):
[Link](item)
top=len(stk)-1

def pop(stk):
if isempty(stk):
return "underflow"
else:
item=[Link]()
if len(stk)==0:
top=None
else:
top=len(stk)-1
return item

def peek(stk):
if isempty(stk):
return "underflow"
1 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE
else:
top=len(stk)-1
return stk[top]

def display(stk):
if isempty(stk):
print('stack is empty')
else:
top=len(stk)-1
print(stk[top],'<-top')
for i in range(top-1,-1,-1):
print(stk[i])

#Driver Code

def main():
stk=[]
top=None
while True:
print('''stack operation
[Link]
[Link]
[Link]
[Link]
[Link]''')
choice=int (input('enter choice:'))
if choice==1:
item=int(input('enter item:'))
push(stk,item)
elif choice==2:
item=pop(stk)
1 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE
if item=="underflow":
print('stack is underflow')
else:
print('poped')
elif choice==3:
item=peek(stk)
if item=="underflow":
print('stack is underflow')
else:
print('top most item is:',item)
elif choice==4:
display(stk)
elif choice==5:
break
else:
print('invalid')
exit()
main()
output
stack operation
[Link]
[Link]
[Link]
[Link]
[Link]
enter choice:1
enter item:2
stack operation
[Link]
[Link]
[Link]
2 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE
[Link]
[Link]
enter choice:3
top most item is: 2
stack operation
[Link]
[Link]
[Link]
[Link]
[Link]
enter choice:4
2 <-top
stack operation
[Link]
[Link]
[Link]
[Link]
[Link]
2 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE

Program No: - 14

create a csv file by entering user id and password, read and search the password for
given userid program in python

Program

import time
import sys

text1 = input("\n Write username ")


text2 = input("\n Write password ")

saveFile = open('usernames+passwords', 'a')


[Link](text1 + ' ' + text2 + '\n')

saveFile = open('usernames+passwords', 'r')


[Link](0)
uap = [Link]()

[Link]()

max_attempts = 3
attempts = 0

while True:
print("Username")
username = input("")
2 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE
print("Password")
password = input("")

if username in uap and password in uap:


print("Access Granted")
else:
attempts+=1
if attempts >= max_attempts:
print(f"reached max attempts of {attempts} ")
[Link]()
print("Try Again (10 sec)")
[Link](10)
continue
break

Output
Write username shiva
Write password shiva@123
Username shiva
Password shyyju
Try Again (10 sec)
Username shiva
Password Shiva@123
Access Granted
2 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE

Program No: - 15

Write a python program to implement sorting techniques based on user choice using
a list data-structure. (bubble/insertion)

#BUBBLE SORT FUNCTION


def Bubble_Sort(nlist):
for passnum in range(len(nlist)-1,0,-1):
for i in range(passnum):
if nlist[i]>nlist[i+1]:
temp = nlist[i]
nlist[i] = nlist[i+1]
nlist[i+1] = temp
#INSERTION SORT FUNCTION
def Insertion_Sort(nlist):
for index in range(1,len(nlist)):
currentvalue = nlist[index]
position = index
while position>0 and nlist[position-1]>currentvalue:
nlist[position]=nlist[position-1]
position = position-1
nlist[position]=currentvalue
# DRIVER CODE
def main():
print ("SORT MENU")
print ("1. BUBBLE SORT")
print ("2. INSERTION SORT")
print ("3. EXIT")
choice=int(input("Enter your Choice [ 1 - 3 ]: "))
2 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE

nlist = [14,46,43,27,57,41,45,21,70]
if choice==1:
print("Before Sorting: ",nlist)
Bubble_Sort(nlist)
print("After Bubble Sort: ",nlist)
elif choice==2:
print("Before Sorting: ",nlist)
Insertion_Sort(nlist)
print("After Insertion Sort: ",nlist)
else:
print("Quitting.....!")

main()

output
SORT MENU
1. BUBBLE SORT
2. INSERTION SORT
3. EXIT
Enter your Choice [ 1 - 3 ]: 1
Before Sorting: [14, 46, 43, 27, 57, 41, 45, 21, 70]
After Bubble Sort: [14, 21, 27, 41, 43, 45, 46, 57, 70]
SORT MENU
1. BUBBLE SORT
2. INSERTION SORT
3. EXIT
Enter your Choice [ 1 - 3 ]: 3
Quitting.....!
2 [Link].M.C.A., [Link]., [Link]., PGT IN COMPUTER SCIENCE

SNO PYTHON PROGRAM

1 Recursively find the factorial of a natural number.

2 Input a string and determine whether it is a palindrome or not

3 Find the largest/smallest number in a list/tuple


Remove all the lines that contain the character `a' in a file and write it to another
4
file
5 Write a program to remove all odd number from the given list

6 Write a program for input any number from user and check Wheater prime or not

7 Read a text file line by line and display each word separated by a #.
Read a text file and display the number of vowels/ consonants/ uppercase/
8
lowercase characters and other than character and digit in the file.
Remove all the lines that contain the character `a' in a file and write it to another
9
file
Create a binary file with the name and roll number. Search for a given roll number
10
and display the name, if not found display appropriate message
Create a binary file with roll number, name and marks. Input a roll number and
11
update details
Write a random number generator that generates random numbers between 1 and 6
12
(simulates a dice).
13 Write a python program to implement a stack using list
create a csv file by entering user id and password, read and search the password for
14
given userid program in python
Write a python program to implement sorting techniques based on user choice
15
using a list data-structure. (bubble/insertion)

You might also like