c.s Practical File Hassium Class 12[1]
c.s Practical File Hassium Class 12[1]
Session – 2024-25
Subject – Computer science
___________ ___________
Signature of Student Signature of
Teacher Remarks
INDEX: -
# Example usage
remove_lines_with_a('input.txt', 'output.txt')
4. Create a binary file with name and roll number.
Search for a given roll number and display the
name, if not found display appropriate message.
import pickle
# Example usage
create_binary_file('students.bin')
search_roll_number('students.bin', 102)
5. Create a binary file with roll number, name and
marks. Input a roll number and update the
marks.
import pickle
# Example usage
create_student_file('students_with_marks.bin')
update_marks('students_with_marks.bin', 102,
95)
6. Write a random number generator that
generates random numbers between 1 and 6
(simulates a dice).
import random
def roll_dice():
return random.randint(1, 6)
# Example usage
print(f'Rolled: {roll_dice()}')
7. Write a Python program to implement a stack
using list.
class Stack:
def __init__(self):
self.stack = []
def pop(self):
if len(self.stack) > 0:
return self.stack.pop()
else:
return "Stack is empty"
def peek(self):
if len(self.stack) > 0:
return self.stack[-1]
else:
return "Stack is empty"
def is_empty(self):
return len(self.stack) == 0
def size(self):
return len(self.stack)
# Example usage
stack = Stack()
stack.push("John")
stack.push("Alice")
print(stack.pop()) # Alice
print(stack.peek()) # John
8. Create a CSV file by entering user-id and
password, read and search the password for a
given user-id.
import csv
# Example usage
create_csv_file('users.csv')
print(search_password('users.csv', 'user2'))
9. Database Management: Create a student table
and insert data.
-- SQL script to create a student table and insert
data
CREATE TABLE Student (
RollNo INT PRIMARY KEY,
Name VARCHAR(100),
Age INT,
Marks INT
);
def pop(self):
if self.stack:
student = self.stack.pop()
print(f'{student} removed from the
stack')
else:
print('Stack is empty')
# Example usage
student_stack = Stack()
student_stack.push('John Doe')
student_stack.push('Alice Johnson')
student_stack.pop() # Alice Johnson removed
11.Consider the following DEPT and EMPLOYEE
tables. Write SQL queries for (1) to (4) and find
outputs for SQL queries (5) to (8).
# Non-void function
converted = convert_to_rupees(usd_amount,
rate)
print(f"Converted Amount: {converted} Rupees")
# Void function
convert_to_rupees_void(usd_amount, rate)
# Example usage
generate_series(1, 7) # Output: [1, 3, 5, 7]
15. Write a program that reads a text file and
creates another file that is identical except that
every sequence of consecutive blank spaces is
replaced by a single space.
def replace_blank_spaces(input_file, output_file):
with open(input_file, 'r') as infile:
content = infile.read()
# Example usage
replace_blank_spaces('input.txt', 'output.txt')
# Example usage
filename = 'student.dat'
# Example usage
classify_and_store_characters()
# Example usage
create_file('Book.dat')
ii. Write a function CountRec(Author) in Python
which accepts the Author name as a parameter
and counts and returns the number of books by
the given Author stored in the binary file
Book.dat.
import pickle
# Example usage
author = input("Enter Author Name to search: ")
book_count = count_rec('Book.dat', author)
print(f"Number of books by {author}:
{book_count}")
def lucky_draw():
# Token IDs are from 1 to 600
token_ids = list(range(1, 601))
# Randomly select a token ID
winner_token = random.choice(token_ids)
print(f"The winner of the lucky draw is Token
ID: {winner_token}")
# Example usage
lucky_draw()