Sample questions soutions
# 1. Fibonacci Even/Odd Game
def fibonacci(n):
a, b = 0, 1
for _ in range(n):
a, b = b, a + b
return a
n = int(input("Enter a position in the Fibonacci sequence: "))
print("Even" if fibonacci(n) % 2 == 0 else "Odd")
# 2. Split and Reverse Sentences
paragraph = input("Enter a paragraph: ")
sentences = [Link]('. ')
print('. '.join(sentences[::-1]))
# 3. Count Prime Numbers
def is_prime(n):
if n < 2:
return False
for i in range(2, int(n**0.5) + 1):
if n % i == 0:
return False
return True
nums = [int(x) for x in input("Enter numbers: ").split()]
print("Prime count:", sum(1 for num in nums if is_prime(num)))
# 4. Multiplication Table
num = int(input("Enter a number: "))
for i in range(1, 11):
print(f"{num} x {i} = {num * i}")
# 5. Perfect Square
import math
num = int(input("Enter a number: "))
print("Perfect Square" if [Link](num)**2 == num else "Not a Perfect Square")
# 6. Reverse a String
s = input("Enter a string: ")
rev = ''
for char in s:
rev = char + rev
print(rev)
# 7. Remove Duplicate Characters
s = input("Enter a string: ")
print(''.join(sorted(set(s), key=[Link])))
# 8. Replace Spaces with Underscores
s = input("Enter a string: ")
print([Link](" ", "_"))
# 9. Second Largest & Smallest
nums = sorted(set(map(int, input("Enter numbers: ").split())))
print("Second Smallest:", nums[1], "Second Largest:", nums[-2])
# 10. Copy File
with open('[Link]', 'r') as src, open('[Link]', 'w') as dest:
[Link]([Link]())
# 11. Word Count in File
word = input("Enter word to count: ")
with open('[Link]', 'r') as file:
print([Link]().count(word))
# 12. Append to File
text = input("Enter text to append: ")
with open('[Link]', 'a') as file:
[Link](text)
# 13. Merge Dictionaries
d1 = {'a': 1, 'b': 2}
d2 = {'b': 3, 'c': 4}
merged = {k: [Link](k, 0) + [Link](k, 0) for k in set(d1) | set(d2)}
print(merged)
# 14. Dictionary Mapping Numbers to Squares
n = int(input("Enter a number: "))
print({x: x**2 for x in range(1, n+1)})
# 15. Reverse String Function
def reverse_string(s):
return s[::-1]
# 16. Sum of Digits
def sum_digits(n):
return sum(int(digit) for digit in str(n))
# 17. Largest Value without max()
nums = list(map(int, input("Enter numbers: ").split()))
largest = nums[0]
for num in nums:
if num > largest:
largest = num
print("Largest:", largest)
# 18. Inheritance Example
class Animal:
def speak(self):
print("I make a sound")
class Dog(Animal):
def speak(self):
print("I bark")
a = Animal()
d = Dog()
[Link]()
[Link]()
# 19. Factorial using Recursion
def factorial(n):
return 1 if n == 0 else n * factorial(n - 1)
# 20. LCM and GCD
import math
a, b = map(int, input("Enter two numbers: ").split())
print("GCD:", [Link](a, b))
print("LCM:", (a * b) // [Link](a, b))
# 21. Compound Interest
p = float(input("Enter principal: "))
r = float(input("Enter rate: "))
t = int(input("Enter time: "))
ci = p * (1 + r/100)**t
print("Compound Interest:", ci)
# 22. Palindrome Check
s = input("Enter a string: ")
print("Palindrome" if s == s[::-1] else "Not a Palindrome")
# 23. Vowel and Consonant Count
s = input("Enter a string: ").lower()
vowels = sum(1 for c in s if c in 'aeiou')
consonants = sum(1 for c in s if [Link]() and c not in 'aeiou')
print("Vowels:", vowels, "Consonants:", consonants)
# 24. Merge Sorted Lists
l1 = sorted(map(int, input("Enter sorted list 1: ").split()))
l2 = sorted(map(int, input("Enter sorted list 2: ").split()))
print(sorted(l1 + l2))
# 25. Rotate List
lst = list(map(int, input("Enter list: ").split()))
k = int(input("Enter rotation count: "))
print(lst[k:] + lst[:k])
# 26. Remove Duplicates Without Sets
lst = list(map(int, input("Enter list: ").split()))
unique = []
for item in lst:
if item not in unique:
[Link](item)
print(unique)
# 27. Dot Product of Two Vectors
v1 = list(map(int, input("Enter vector 1: ").split()))
v2 = list(map(int, input("Enter vector 2: ").split()))
print(sum(a * b for a, b in zip(v1, v2)))
# 28. Count Word in File (Repeated)
word = input("Enter word to count: ")
with open('[Link]', 'r') as file:
print([Link]().count(word))
# 29. Check Anagrams
from collections import Counter
s1, s2 = input("Enter two strings: ").split()
print("Anagrams" if Counter(s1) == Counter(s2) else "Not Anagrams")
# 30. Ordered List Check
lst = sorted(map(int, input("Enter list: ").split()))
check = int(input("Enter number to check: "))
print("Present" if check in lst else "Not Present")