Python Assignment- 3
Instructions: You must use exception handling (try-except), input-output operations, and
modules. Direct solutions without proper exception handling will not be accepted. Classes
are NOT allowed.
(1) Read numbers from a file, compute their prime factorization, and store them in a
dictionary. If a number is invalid, skip it and log errors in another file.
● (Edge cases: Missing file, corrupted data, negative numbers, non-integers.)
(2) Generate an infinitely repeating sequence where each term is the sum of the squares of
its previous term’s digits. Stop when a cycle is detected and write the cycle length to a file.
● (Edge cases: Stack overflow (recursion), invalid input, file permission issues.)
(3) Find all numbers from input that remain prime when any one digit is removed. Ensure
input validity and handle unexpected formats.
● (Edge cases: Empty input, strings, zero-padding, extremely large numbers.)
(4) Count numbers from 1 to N that have exactly K distinct prime factors. Ensure valid input
and handle computation errors.
● (Edge cases: Overflow errors, division by zero, invalid K values.)
(5) Compute a nested dictionary where each key is a number from 1 to N, and its value is
another dictionary mapping its modular inverses for all bases 2 to N. Handle cases where an
inverse doesn’t exist.
● (Edge cases: Division by zero, invalid moduli, negative numbers.)
(6) Extract all unique n-grams (substrings of length n) from a text file and write them to
another file. Handle file errors and log invalid lines separately.
● (Edge cases: File not found, corrupted file, special characters.)
(7) Read a large text file, count word frequencies, and write compressed output using
run-length encoding. Handle errors in file reading and edge cases in word splitting.
● (Edge cases: Memory errors, punctuation handling, empty file.)
(8) Find all Pythagorean triplets up to N, ensuring valid input and handling performance
issues.
● (Edge cases: Overflow, non-integer input.)
(9) Find the longest sequence in the Collatz conjecture up to N. If input is too large, use
memoization to speed up.
● (Edge cases: Stack overflow, non-integer input, excessive memory use.)
(10) Compute the determinant of a 3×3 matrix, ensuring valid input and handling singular
matrices.
● (Edge cases: Non-square matrices, zero determinant leading to singularity.)
(11) Create an infinite generator of numbers that are both Fibonacci and triangular, handling
memory constraints efficiently.
● (Edge cases: Large numbers, infinite loops.)
(12) Generate an N × N magic square with modular constraints. Ensure input is valid and
handle cases where a solution does not exist.
● (Edge cases: Invalid N, computational limits.)
(13) Construct a nested dictionary representing a recursive subdivision of an image grid,
avoiding excessive recursion depth.
● (Edge cases: Stack overflow, invalid grid dimensions.)
(14) Count the number of unique valid Tic-Tac-Toe board states and determine how many
are immediate wins. Ensure inputs do not violate the game rules.
● (Edge cases: Invalid board states, excessive computation.)
(15) Generate all 3D voxel coordinates inside a sphere of radius R, ensuring
memory-efficient computation.
● (Edge cases: Large R, memory limits.)
(16) Write a function happy_numbers(N, file_path) that reads numbers from a file and finds
all happy numbers up to N. If the file does not exist or contains invalid numbers, handle the
error gracefully.
● (Edge cases: FileNotFoundError, ValueError, ensure cycle detection.)
(17) Write a function extract_emails(file_path) that extracts all valid email addresses from a
text file. If the file does not exist or is empty, return an appropriate error message.
● (Edge cases: FileNotFoundError, empty file, regex handling of special cases.)
(18) Write a function most_common_word(file_path) that finds the most frequently occurring
word in a file, ignoring case and punctuation. Handle errors for missing files and empty
content.
● (Edge cases: FileNotFoundError, No valid words found, case insensitive.)
(19) Write a function armstrong_numbers(start, end) that finds all Armstrong numbers
between start and end. Handle errors for invalid input values.
● (Edge cases: Ignore negatives, ValueError, large range (1 to 10^6) efficient
computation.)
(20) Write a function harshad_numbers(N, file_path) that reads an integer N from a file and
finds the first N Harshad numbers. Handle errors if the file is missing or contains invalid
values.
● (Edge cases: FileNotFoundError, ValueError, very large N (10^6) efficiency.)
*********************************************************************************************************