python output
python output
Ques : Find the sum of all the primes below two million.
if sieve[i]:
sieve[j] = False
prime_sum = sum_of_primes_below(2000000)
Output :
Ques : By considering the terms in the Fibonacci sequence whose values do not exceed four
million, WAP to find the sum of the even-valued terms.
a, b = 1, 2
total = 0
total += a
a, b = b, a + b
return total
even_fib_sum = sum_even_fibonacci(4000000)
Output :
Sum of even Fibonacci numbers below 4 million: 4613732
EXPERIMENT -6
Ques : Write a program to count the numbers of characters in the string and store them in a
dictionary data structure
char_count = {}
if char in char_count:
char_count[char] += 1
else:
char_count[char] = 1
return char_count
result = count_characters(user_input)
print(result)
OUTPUT :
ANS :
birthdays = {
"Alice": "12/05/1995",
"Bob": "23/08/1992",
"Charlie": "10/01/2000"
name_parts = full_name.split()
first_name = name_parts[0]
if first_name in birthdays:
birthday = birthdays[first_name]
date_parts = birthday.split("/")
formatted_birthday = "-".join(date_parts)
else:
OUTPUT :
Ques :Write a program to count frequency of characters in a given file. Can you use character
frequency to tell whether the given file is a Python program file, C program file or a text file?
ANS :
def count_char_frequency(filename):
freq = {}
try:
with open(filename, 'r', encoding='utf-8') as file:
content = file.read()
for char in content:
if char in freq:
freq[char] += 1
else:
freq[char] = 1
return freq
except FileNotFoundError:
print("File not found.")
return {}
def guess_file_type(freq_dict):
python_keywords = ['def', 'import', 'self', ':', '#']
c_keywords = ['#include', 'int', 'main', '{', '}', ';']
all_chars = ''.join([k * v for k, v in freq_dict.items()])
score = {'Python': 0, 'C': 0, 'Text': 0}
for word in python_keywords:
if word in all_chars:
score['Python'] += 1
for word in c_keywords:
if word in all_chars:
score['C'] += 1
if score['Python'] == 0 and score['C'] == 0:
score['Text'] += 1
return max(score, key=score.get)
file_path = input("Enter file path: ")
freq_result = count_char_frequency(file_path)
if freq_result:
print("\nCharacter Frequency:")
for char, count in sorted(freq_result.items()):
print(f"'{char}': {count}")
file_type = guess_file_type(freq_result)
print(f"\n🔍 Based on character frequency, this looks like a **{file_type}** file.")
# Example usage
file_path = input("Enter the file path: ")
print("\nReversed lines:")
print_lines_in_reverse(file_path)
QUES : Write a program to compute the number of characters, words and lines in a file.
OUTPUT :
len_a = len(a)
len_b = len(b)
if len_a == len_b:
return diff == 1
if abs(len_a - len_b) == 1:
a, b = b, a
i = j = diff = 0
if a[i] != b[j]:
if diff:
return False
diff += 1
else:
i += 1
j += 1
return False
print(nearly_equal("cat", "cats"))
print(nearly_equal("cats", "cat"))
print(nearly_equal("cat", "dog"))
print(nearly_equal("cat", "cast"))
print(nearly_equal("cat", "cart"))
QUES : Write function to compute gcd, lcm of two numbers. Each function shouldn’t exceed
one line.
Ans :
from math import gcd
def main():
if __name__ == "__main__":
main()
Output :
if len(arr) > 1:
mid = len(arr) // 2
left_half = arr[:mid]
right_half = arr[mid:]
merge_sort(left_half)
merge_sort(right_half)
i=j=k=0
arr[k] = left_half[i]
i += 1
else:
arr[k] = right_half[j]
j += 1
k += 1
arr[k] = left_half[i]
i += 1
k += 1
arr[k] = right_half[j]
j += 1
k += 1
def print_array(arr):
print()
if __name__ == "__main__":
print("Original array:")
print_array(arr)
merge_sort(arr)
print("\nSorted array:")
print_array(arr)
OUTPUT :
Original array:
38 27 43 3 9 82 10
Sorted array:
3 9 10 27 38 43 82
Ques : Write a program to implement Selection sort, Insertion sort.
def selection_sort(arr):
n = len(arr)
for i in range(n):
min_index = i
min_index = j
def print_array(arr):
print()
if __name__ == "__main__":
print("Original array:")
print_array(arr)
selection_sort(arr)
print_array(arr)
Output :
Original array:
64 25 12 22 11
11 12 22 25 64
2. INSERTION SORT
def insertion_sort(arr):
key = arr[i]
j=i-1
arr[j + 1] = arr[j]
j -= 1
arr[j + 1] = key
def print_array(arr):
print()
if __name__ == "__main__":
print("Original array:")
print_array(arr)
insertion_sort(arr)
print_array(arr)
OUTPUT :
Original array:
64 34 25 12 22 11 90
11 12 22 25 34 64 90