Python Programs
Python Programs
if __name__ == '__main__':
N = int(input().strip())
result = []
for _ in range(N):
command = input().strip().split()
if command[0] == "insert":
result.insert(int(command[1]), int(command[2]))
elif command[0] == "print":
print(result)
elif command[0] == "remove":
result.remove(int(command[1]))
elif command[0] == "append":
result.append(int(command[1]))
elif command[0] == "sort":
result.sort()
elif command[0] == "pop":
result.pop()
elif command[0] == "reverse":
result.reverse()
2.
def nth_term_of_series(n):
if n % 2 == 0:
# Even terms: 2^(n/2 - 1)
return 2 ** (n // 2 - 1)
else:
# Odd terms: n/2
return 3 ** (n // 2)
3.
def custom_sort(lst):
# Split the list into two sublists: odd indices and even indices
odd_indices = lst[1::2]
even_indices = lst[::2]
return result
4.
def decrypt_message(encrypted_message):
decrypted_message = ""
for char in encrypted_message:
# Decrypt uppercase letters
if char.isupper():
decrypted_char = chr(((ord(char) - 65 - 3) % 26) + 65)
decrypted_message += decrypted_char
else:
decrypted_message += char # Keep non-alphabetic characters unchanged
return decrypted_message
# Test cases
input1 = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
input2 = "ATTACKATONCE"
6.
def celsius_to_fahrenheit(celsius):
return (celsius * 9/5) + 32
def celsius_to_kelvin(celsius):
return celsius + 273.15
def fahrenheit_to_celsius(fahrenheit):
return (fahrenheit - 32) * 5/9
def fahrenheit_to_kelvin(fahrenheit):
return (fahrenheit - 32) * 5/9 + 273.15
def kelvin_to_celsius(kelvin):
return kelvin - 273.15
def kelvin_to_fahrenheit(kelvin):
return (kelvin - 273.15) * 9/5 + 32
import temperature_converter
7.
# Initialize the answer key
answer_key = ['A', 'B', 'C', 'A']
def evaluate_responses(student_responses):
marks = 0
for i in range(len(answer_key)):
if student_responses[i] == answer_key[i]:
marks += 10
return marks
student_marks = evaluate_responses(student_responses)
print(f"Student {i} obtained {student_marks} marks.\n")
8.
class FlightBookingSystem:
def __init__(self):
self.flights = {}
def view_available_flights(self):
print("Available Flights:")
for flight_id, details in self.flights.items():
print(f"Flight ID: {flight_id}, Departure City: {details['departure_city']}, Arrival City: {details['arrival_city']}, Depar
ble Seats: {details['available_seats']}")
def view_booked_flights(self):
print("Booked Flights:")
for flight_id, details in self.flights.items():
booked_seats = details['available_seats']
available_seats = details['available_seats']
print(f"Flight ID: {flight_id}, Departure City: {details['departure_city']}, Arrival City: {details['arrival_city']}, Depar
Seats: {booked_seats}, Available Seats: {available_seats}")
# Example usage:
flight_system = FlightBookingSystem()
# Book a flight
flight_system.book_flight(2, 2)
# Cancel reservation
flight_system.cancel_reservation(2, 1)
num = 25
sqrt_num = math.sqrt(num)
print("Square root of", num, "is", sqrt_num)
import math
import math
num = 4.7
ceil_value = math.ceil(num)
floor_value = math.floor(num)
import random
import random
import random
numbers = [1, 2, 3, 4, 5]
random.shuffle(numbers)
print("Shuffled list:", numbers)
10.
import math
# Example usage:
print("Distance between two points in 2D space:", distance_2d(1, 2, 4, 6))
print("Distance between two points in 3D space:", distance_3d(1, 2, 3, 4, 5, 6))
import math
import matplotlib.pyplot as plt
for i in range(n):
x = x_values[-1]
y = y_values[-1]
slope = f(x, y)
x_new = x + h
y_new = y + h * slope
x_values.append(x_new)
y_values.append(y_new)
# Initial conditions
x0 = 0
y0 = 1
11.
import random
def roll_dice():
return random.randint(1, 6)
def simulate_rolling(num_trials):
results = {i: 0 for i in range(2, 13)} # Initialize dictionary to store results
for _ in range(num_trials):
die1 = roll_dice()
die2 = roll_dice()
total = die1 + die2
results[total] += 1
return results
def print_results(results):
print("Sum of Dice\tOccurrences")
print("-------------------------")
for total, count in results.items():
print(f"{total}\t\t{count}")
import random
import string
def generate_password(length):
characters = string.ascii_letters + string.digits + string.punctuation
return ''.join(random.choice(characters) for _ in range(length))
12.
import datetime
def find_nth_day(n):
# Get the current date
current_date = datetime.datetime.now().date()
print("Current date is", current_date.strftime("%d/%m/%Y"))
# Sample input
N=5
find_nth_day(N)
13.
import datetime
# Example usage:
date_str1 = "2024-03-26"
date_str2 = "2024-04-05"
print("Number of days between", date_str1, "and", date_str2, "is:", days_between_dates(date_str1, date_str2))