computer science programs
computer science programs
Database Connectivity
create_table()
print("Table created successfully!")
query_data()
Sorting and Binary Search
# Example usage
numbers = [4, 1, 9, 2, 5]
print("Sorted list (Bubble Sort):", bubble_sort(numbers))
# Example usage
numbers = [4, 1, 9, 2, 5]
print("Sorted list (Selection Sort):", selection_sort(numbers))
# Example usage
sorted_list = [1, 2, 3, 4, 5, 6, 7, 8, 9]
target = 5
result = binary_search(sorted_list, target)
print(f"Element {target} found at index:", result)
Dictionaries
# Example usage
string = "hello world"
print("Character frequency:", count_characters(string))
# Example usage
dict1 = {'a': 1, 'b': 2, 'c': 3}
dict2 = {'b': 4, 'c': 5, 'd': 6}
print("Common keys:", common_keys(dict1, dict2))
# Example usage
data = {'apple': 50, 'banana': 20, 'cherry': 75}
print("Key with maximum value:", max_value_key(data))
4. Invert a Dictionary (Swap Keys and Values)
def invert_dictionary(d):
return {v: k for k, v in d.items()}
# Example usage
original_dict = {'a': 1, 'b': 2, 'c': 3}
print("Inverted dictionary:", invert_dictionary(original_dict))
# Example usage
lst = [1, 2, 2, 3, 3, 3, 4, 4, 4, 4]
print("Grouped by frequency:", group_by_frequency(lst))
Functions
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * factorial(n - 1)
# Example usage
num = 5
print(f"The factorial of {num} is {factorial(num)}")
def is_prime(n):
if n <= 1:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
# Example usage
number = 17
print(f"Is {number} a prime number? {'Yes' if is_prime(number) else 'No'}")
# Example usage
num1, num2 = 56, 98
print(f"The GCD of {num1} and {num2} is {gcd(num1, num2)}")
def fibonacci(n):
fib_sequence = [0, 1]
for i in range(2, n):
fib_sequence.append(fib_sequence[i - 1] + fib_sequence[i - 2])
return fib_sequence[:n]
# Example usage
terms = 10
print(f"The first {terms} terms of the Fibonacci sequence are: {fibonacci(terms)}")
def is_palindrome(s):
s = s.lower().replace(" ", "") # Normalize the string
return s == s[::-1]
# Example usage
string = "A man a plan a canal Panama"
print(f"Is the string '{string}' a palindrome? {'Yes' if is_palindrome(string) else 'No'}")
SQL Queries
1. Create a Table for Student Performance Details
UPDATE StudentPerformance
SET MathMarks = 88, ScienceMarks = 89, EnglishMarks = 90
WHERE Name = 'David';
Stack operations
1. Reverse a List Using a Stack
def reverse_list(lst):
stack = []
for item in lst:
stack.append(item)
reversed_list = []
while stack:
reversed_list.append(stack.pop())
return reversed_list
# Example usage
original_list = [1, 2, 3, 4, 5]
print("Original List:", original_list)
print("Reversed List:", reverse_list(original_list))
def infix_to_postfix(expression):
stack = []
postfix = []
for char in expression:
if char.isalnum():
postfix.append(char)
elif char == '(':
stack.append(char)
elif char == ')':
while stack and stack[-1] != '(':
postfix.append(stack.pop())
stack.pop()
else:
while stack and precedence(stack[-1]) >= precedence(char):
postfix.append(stack.pop())
stack.append(char)
while stack:
postfix.append(stack.pop())
return ''.join(postfix)
# Example usage
expression = "a+b*(c^d-e)^(f+g*h)-i"
print(f"Infix: {expression}")
print(f"Postfix: {infix_to_postfix(expression)}")
def pop(self):
if self.is_empty():
return "Stack is empty!"
return self.stack.pop()
def display(self):
return self.stack if not self.is_empty() else "Stack is empty!"
def is_empty(self):
return len(self.stack) == 0
# Example usage
s = Stack()
s.push(10)
s.push(20)
s.push(30)
print("Stack after pushes:", s.display())
print("Popped item:", s.pop())
print("Stack after pop:", s.display())
# Example usage
stack = [3, 1, 4, 1, 5, 9]
print("Maximum element in the stack:", find_max(stack))
# Example usage
postfix_expression = "231*+9-"
print(f"Postfix Expression: {postfix_expression}")
print(f"Evaluation Result: {evaluate_postfix(postfix_expression)}")