8 10
8 10
a) Write a python program to find the whether the given input is palindrome or not (for both
string and integer) using the concept of polymorphism and inheritance.
class PalindromeChecker:
def is_palindrome(self, input_value):
raise NotImplementedError("Subclasses must implement this
method")
class StringPalindrome(PalindromeChecker):
def is_palindrome(self, input_value):
input_value = input_value.lower().replace(" ", "")
# Ignore case and spaces
return input_value == input_value[::-1]
class IntegerPalindrome(PalindromeChecker):
def is_palindrome(self, input_value):
return str(input_value) == str(input_value)[::-1]
if __name__ == "__main__":
input_value = input("Enter a string or an integer: ")
if input_value.isdigit():
checker = IntegerPalindrome()
else:
checker = StringPalindrome()
PROGRAM
import os
import requests
from IPython.display import Image, display
def download_xkcd_comics(output_folder):
os.makedirs(output_folder, exist_ok=True)
url = 'https://xkcd.com/{}/info.0.json'
comic_number = 1
while True:
response = requests.get(url.format(comic_number))
if response.status_code == 200:
comic_data = response.json()
image_url = comic_data['img']
image_response = requests.get(image_url)
if image_response.status_code == 200:
image_name = os.path.basename(image_url)
image_path = os.path.join(output_folder,image_name)
display(Image(filename=image_path))
else:
print(f"Failed to download image for comic
{comic_number}")
else:
break
comic_number += 1
if __name__ == "__main__":
output_folder = 'xkcd_comics'
download_xkcd_comics(output_folder)
9
b) Demonstrate python program to read the data from the spreadsheet and write the data in to
the spreadsheet
PROGRAM
import openpyxl
def read_spreadsheet(file_path):
workbook = openpyxl.load_workbook(file_path)
sheet = workbook.active
workbook.close()
def write_spreadsheet(file_path):
workbook = openpyxl.Workbook()
sheet = workbook.active
sheet["A1"] = "Name"
sheet["B1"] = "Age"
sheet["C1"] = "City"
data = [
("A", 28, "Bangalore"),
("B", 32, "Mumbai"),
("C", 24, "Delhi")
]
for row in data:
sheet.append(row)
workbook.save(file_path)
workbook.close()
if __name__ == "__main__":
read_file_path = "C:\\Users\\DELL\\data.xlsx"
write_file_path = "C:\\Users\\DELL\\output.xlsx"
10
a) Write a python program to combine select pages from many PDFs
PROGRAM
import PyPDF2
output_pdf = 'combined_pages.pdf'
10 b) Write a python program to fetch current weather data from the JSON file
def fetch_weather_data(file_name):
try:
with open(file_name, 'r') as json_file:
weather_data = json.load(json_file)
return weather_data
except FileNotFoundError:
print(f"Error: File '{file_name}' not found.")
return None
except json.JSONDecodeError:
print(f"Error: Invalid JSON format in '{file_name}'.")
return None
def display_weather_data(weather_data):
if weather_data is not None:
print("Weather Information:")
print(f"City: {weather_data['city']}")
print(f"Temperature: {weather_data['temperature']}°F")
print(f"Conditions: {weather_data['conditions']}")