Python Programs - File Handling and Stack (Set 2)
Q1 (Option 1): Count lines starting with a Vowel or Consonant
# Program to count number of lines starting with vowel or consonant in [Link]
def count_lines():
vowels = "AEIOUaeiou"
vowel_count = 0
consonant_count = 0
f = open("[Link]", "r")
lines = [Link]()
[Link]()
for line in lines:
line = [Link]()
if len(line) == 0:
continue # skip empty lines
if line[0] in vowels:
vowel_count += 1
elif line[0].isalpha():
consonant_count += 1
print("Number of lines starting with a vowel:", vowel_count)
print("Number of lines starting with a consonant:", consonant_count)
# --- Main Program ---
count_lines()
Q1 (Option 2): Count 'me' and 'my' in Text File
# Program to count the number of times 'me' or 'my' appears in a text file [Link]
def count_words():
f = open("[Link]", "r")
text = [Link]().lower()
[Link]()
count_me = [Link]().count("me")
count_my = [Link]().count("my")
print("Count of 'me':", count_me)
print("Count of 'my':", count_my)
# --- Main Program ---
count_words()
Q2: Stack of Student Records [Stud_Name, Marks]
MyStack = [] # stack to store student records
# Function to push records if marks > 70
def Push(lst):
if lst[1] > 70:
[Link](lst)
print("Record pushed:", lst)
else:
print("Record not pushed (marks <= 70):", lst)
# Function to pop a record and display details
def Pop():
if len(MyStack) == 0:
print("Stack is empty!")
else:
rec = [Link]()
print("Popped Record:")
print("Student Name:", rec[0])
print("Marks:", rec[1])
# --- Main Program ---
n = int(input("Enter number of student records: "))
for i in range(n):
name = input("Enter Student Name: ")
marks = int(input("Enter Marks: "))
Push([name, marks])
Pop()