14. Problem Solving Exercises
14. Problem Solving Exercises
29. Program to Find Sum of Odd and Even Numbers (Using Range)
n = int(input("Enter the limit: "))
odd_sum = sum(i for i in range(1, n + 1) if i % 2 != 0)
even_sum = sum(i for i in range(1, n + 1) if i % 2 == 0)
print(f"Sum of odd numbers: {odd_sum}")
print(f"Sum of even numbers: {even_sum}")
# Example usage
sorted_list = [1, 3, 5, 7, 9]
number = int(input("Enter the number to insert: "))
result = insert_sorted(sorted_list, number)
print("List after inserting:", result)
34. Program to Guess an Integer Number in a Range
import random
def guess_number():
number_to_guess = random.randint(1, 100)
attempts = 0
while True:
guess = int(input("Guess a number between 1 and 100: "))
attempts += 1
guess_number()
# Example usage
num_disks = int(input("Enter the number of disks: "))
towers_of_hanoi(num_disks, 'A', 'C', 'B')
36. Program to Calculate the Square Root of a Number (Newton's Method)
def newton_sqrt(number, tolerance=1e-10):
estimate = number / 2.0
while True:
new_estimate = (estimate + number / estimate) / 2
if abs(new_estimate - estimate) < tolerance:
return new_estimate
estimate = new_estimate
# Example usage
num = float(input("Enter a number to find its square root: "))
result = newton_sqrt(num)
print(f"The square root of {num} is approximately {result:.6f}")
# Example usage
word_count('sample.txt')
# Example usage
copy_file('source.txt', 'destination.txt')
colors = ['Red', 'Blue', 'Green', 'Pink', 'Black', 'Yellow', 'Orange', 'White', 'Purple', 'Brown']
score = 0
time_left = 30
def start_game(event):
if time_left == 30:
countdown()
next_color()
def next_color():
global score, time_left
if time_left > 0:
color_entry.focus_set()
if color_entry.get().lower() == colors[1].lower():
score += 1
color_entry.delete(0, tkinter.END)
random.shuffle(colors)
color_label.config(fg=str(colors[1]), text=str(colors[0]))
score_label.config(text="Score: " + str(score))
def countdown():
global time_left
if time_left > 0:
time_left -= 1
time_label.config(text="Time left: " + str(time_left))
time_label.after(1000, countdown)
root = tkinter.Tk()
root.title("Color Game")
root.geometry("400x200")
def calculate_age():
birth_year = int(entry_year.get())
current_year = date.today().year
age = current_year - birth_year
label_result.config(text=f"Your Age is: {age} years")
root = Tk()
root.title("Age Calculator")
Label(root, text="Enter Birth Year:").pack()
entry_year = Entry(root)
entry_year.pack()
root.mainloop()
import turtle
import time
import random
delay = 0.1
score = 0
high_score = 0
# Snake head
head = turtle.Turtle()
head.speed(0)
head.shape("square")
head.color("black")
head.penup()
head.goto(0, 0)
head.direction = "stop"
# Food
food = turtle.Turtle()
food.speed(0)
food.shape("circle")
food.color("red")
food.penup()
food.goto(0, 100)
def go_down():
if head.direction != "up":
head.direction = "down"
def go_left():
if head.direction != "right":
head.direction = "left"
def go_right():
if head.direction != "left":
head.direction = "right"
def move():
if head.direction == "up":
y = head.ycor()
head.sety(y + 20)
if head.direction == "down":
y = head.ycor()
head.sety(y - 20)
if head.direction == "left":
x = head.xcor()
head.setx(x - 20)
if head.direction == "right":
x = head.xcor()
head.setx(x + 20)
# Keyboard bindings
window.listen()
window.onkeypress(go_up, "w")
window.onkeypress(go_down, "s")
window.onkeypress(go_left, "a")
window.onkeypress(go_right, "d")
window.mainloop()
class Person:
def init (self, name, age):
self.name = name
self.age = age
def display(self):
print(f"Name: {self.name}, Age: {self.age}")
# Example usage
person1 = Person("Alice", 30)
person1.display()
@classmethod
def from_birth_year(cls, name, birth_year):
return cls(name, birth_year)
# Example usage
person1 = Person.from_birth_year("Alice", 1993)
print(f"Name: {person1.name}, Birth Year: {person1.birth_year}")
# Example usage
calc = Calculator()
print(calc(10, 20)) # Output: 30