Computerscience
Computerscience
Computerscience
Functions (5 to 6 programs)
*Output:*
120
*Output:*
True
False
*Output:*
*Output:*
15
*Output:*
olleH
*Output:*
*Output:*
*Output:*
Hello, World!
*Output:*
*Output:*
copy_file('example.txt', 'example_copy.txt')
*Output:*
*Output:*
*Output:*
Hello, World!
Append this text.
*Output:*
*Output:*
*Output:*
def pop(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def peek(self):
if not self.is_empty():
return self.items[-1]
def size(self):
return len(self.items)
stack = Stack()
stack.push(1)
stack.push(2)
print(stack.pop()) # Output: 2
print(stack.peek()) # Output: 1
print(stack.size()) # Output: 1
*Output:*
2
1
1
def dequeue(self):
if not self.is_empty():
return self.items.pop()
def is_empty(self):
return len(self.items) == 0
def size(self):
return len(self.items)
queue = Queue()
queue.enqueue(1)
queue.enqueue(2)
print(queue.dequeue()) # Output: 1
print(queue.size()) # Output: 1
*Output:*
1
1
class LinkedList:
def __init__(self):
self.head = None
def print_list(self):
current = self.head
while current:
print(current.data, end=' ')
current = current.next
ll = LinkedList()
ll.insert(1)
ll.insert(2)
ll.insert(3)
ll.print_list() # Output: 3 2 1
*Output:*
3 2 1
def inorder(root):
if root:
inorder(root.left)
print(root.val, end=' ')
inorder(root.right)
root = TreeNode(50)
root = insert(root, 30)
root = insert(root, 20)
root = insert(root, 40)
root = insert(root, 70)
root = insert(root, 60)
root = insert(root, 80)
inorder(root) # Output: 20 30 40 50 60 70 80
*Output:*
20 30 40 50 60 70 80
python
class HashTable:
def __init__(self):
self.size = 10
self.table = [[] for _ in range(self.size)]
hash_table = HashTable()
hash_table.insert(1, 'One')
hash_table.insert(2, 'Two')
hash_table.insert(12, 'Twelve')
print(hash_table.search(1)) # Output: One
print(hash_table.search(12)) # Output: Twelve
print(hash_table.search(5)) # Output: None
*Output:*
One
Twelve
None
python
class Graph:
def __init__(self):
self.graph = {}
def display(self):
for node in self.graph:
print(node, '->', ' -> '.join([str(neighbor) for neighbor in
self.graph[node]]))
graph = Graph()
graph.add_edge(0, 1)
graph.add_edge(0, 2)
graph.add_edge(1, 2)
graph.add_edge(2, 0)
graph.add_edge(2, 3)
graph.add_edge(3, 3)
graph.display()
*Output:*
0 -> 1 -> 2
1 -> 2
2 -> 0 -> 3
3 -> 3
python
from collections import deque
graph = {
0: [1, 2],
1: [2],
2: [0, 3],
3: [3]
}
bfs(graph, 2) # Output: 2 0 3 1
*Output:*
2 0 3 1
python
def dfs(graph, start, visited=None):
if visited is None:
visited = set()
visited.add(start)
print(start, end=' ')
for next in set(graph[start]) - visited:
dfs(graph, next, visited)
graph = {
0: [1, 2],
1: [2],
2: [0, 3],
3: [3]
}
dfs(graph, 2) # Output: 2 0 1 3
*Output:*
2 0 1 3
python
def insertion_sort(arr):
for i in range(1, len(arr)):
key = arr[i]
j = i - 1
while j >= 0 and key < arr[j]:
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
return arr
*Output:*
python
import mysql.connector
def connect_db():
return mysql.connector.connect(
host='localhost',
user='yourusername',
password='yourpassword',
database='yourdatabase'
)
db = connect_db()
if db.is_connected():
print('Connected to MySQL database')
*Output:*
python
def create_tables():
db = connect_db()
cursor = db.cursor()
cursor.execute('''
CREATE TABLE IF NOT EXISTS Staff (
staff_id INT AUTO_INCREMENT PRIMARY KEY,
name VARCHAR(255) NOT NULL,
position VARCHAR(255) NOT NULL,
salary DECIMAL(10, 2)
)
''')
cursor.execute('''
CREATE TABLE IF NOT EXISTS Flights (
flight_id INT AUTO_INCREMENT PRIMARY KEY,
airline VARCHAR(255) NOT NULL,
destination VARCHAR(255) NOT NULL,
departure_time TIME NOT NULL
)
''')
db.close()
create_tables()
*Output:*
python
def insert_staff(name, position, salary):
db = connect_db()
cursor = db.cursor()
sql = 'INSERT INTO Staff (name, position, salary) VALUES (%s, %s, %s)'
val = (name, position, salary)
cursor.execute(sql, val)
db.commit()
db.close()
*Output:*
python
def fetch_staff():
db = connect_db()
cursor = db.cursor()
cursor.execute('SELECT * FROM Staff')
result = cursor.fetchall()
for row in result:
print(row)
db.close()
def fetch_flights():
db = connect_db()
cursor = db.cursor()
cursor.execute('SELECT * FROM Flights')
result = cursor.fetchall()
for row in result:
print(row)
db.close()
fetch_staff()
fetch_flights()
*Output:*
python
def update_staff_salary(name, new_salary):
db = connect_db()
cursor = db.cursor()
sql = 'UPDATE Staff SET salary = %s WHERE name = %s'
val = (new_salary, name)
cursor.execute(sql, val)
db.commit()
db.close()
*Output:*
python
def delete_staff(name):
db = connect_db()
cursor = db.cursor()
sql = 'DELETE FROM Staff WHERE name = %s'
val = (name,)
cursor.execute(sql, val)
db.commit()
db.close()
def delete_flight(flight_id):
db = connect_db()
cursor = db.cursor()
sql = 'DELETE FROM Flights WHERE flight_id = %s'
val = (flight_id,)
cursor.execute(sql, val)
db.commit()
db.close()
delete_staff('Bob Smith')
delete_flight(2)
*Output:*
Table 1: Staff
sql
CREATE TABLE Staff (
staff_id INT PRIMARY KEY,
name VARCHAR(255),
position VARCHAR(255),
salary DECIMAL(10, 2)
);
##### Queries
*Output:*
+----------+--------------+------------------+----------+
| staff_id | name | position | salary |
+----------+--------------+------------------+----------+
| 1 | Alice Brown | Pilot | 95000.00 |
| 2 | Bob Smith | Flight Attendant | 45000.00 |
| 3 | Charlie Davis| Co-pilot | 70000.00 |
+----------+--------------+------------------+----------+
*Output:*
+----------+--------------+------------------+----------+
| staff_id | name | position | salary |
+----------+--------------+------------------+----------+
| 1 | Alice Brown | Pilot | 95000.00 |
| 3 | Charlie Davis| Co-pilot | 70000.00 |
+----------+--------------+------------------+----------+
*Output:*
+----------+
| COUNT(*) |
+----------+
| 3 |
+----------+
+-------------+
| AVG(salary) |
+-------------+
| 70000.0000 |
+-------------+
*Output:*
+----------+--------------+----------+----------+
| staff_id | name | position | salary |
+----------+--------------+----------+----------+
| 1 | Alice Brown | Pilot | 95000.00 |
+----------+--------------+----------+----------+
Table 2: Flights
sql
CREATE TABLE Flights (
flight_id INT PRIMARY KEY,
airline VARCHAR(255),
destination VARCHAR(255),
departure_time TIME
);
##### Queries
*Output:*
+----------+-----------+-------------+----------------+
| flight_id| airline | destination | departure_time |
+----------+-----------+-------------+----------------+
| 1 | Air India | New York | 10:30:00 |
| 2 | Lufthansa | Berlin | 14:00:00 |
| 3 | Emirates | Dubai | 09:00:00 |
+----------+-----------+-------------+----------------+
+----------+-----------+-------------+----------------+
| flight_id| airline | destination | departure_time |
+----------+-----------+-------------+----------------+
| 2 | Lufthansa | Berlin | 14:00:00 |
+----------+-----------+-------------+----------------+
*Output:*
+----------+
| COUNT(*) |
+----------+
| 3 |
+----------+
*Output:*
+--------------------+
| MIN(departure_time)|
+--------------------+
| 09:00:00 |
+--------------------+
*Output:*
+----------+-----------+-------------+----------------+
| flight_id| airline | destination | departure_time |
+----------+-----------+-------------+----------------+
| 3 | Emirates | Dubai | 09:00:00 |
+----------+-----------+-------------+----------------+