Quiz
Quiz
This quiz tests advanced Python skills, focusing on algorithmic problem-solving and file
input/output operations. The problems involve calculating the longest binary gap and
checking for palindromes, combined with file operations for storing and retrieving results.
Instructions
• Time Allocation: 45 minutes (40 minutes for coding, 5 minutes for final preparations and
packaging).
• Submission Format: Submit two Python files:
• algorithms.py: Contains all necessary function definitions and logic.
• main_script.py: This main file should use the functions from algorithms.py to
perform designated tasks.
• Error Handling: Implement robust error handling, focusing on built-in exceptions.
• Project Layers: Each task builds on the previous, emphasizing both algorithmic problem-
solving and practical file operations.
Quiz Problems
Hint:
1. binary.strip('0'):
• The strip('0') method removes all leading and trailing zeros from the binary string.
• This is important because a binary gap is defined as a sequence of consecutive zeros that
is surrounded by ones at both ends. Zeros at the beginning or end of the binary string do
not count as they are not enclosed by ones.
2. .split('1'):
• The split('1') method divides the string at each occurrence of '1'.
• This results in a list where each element is a sequence of zeros that were between ones in
the original string.
Combined Explanation
Let's consider a binary number, for example, n = 1041. When converted to binary, it is represented
as 10000010001.
• Binary Representation (with prefix and suffix zeros): 10000010001
• After strip('0'): 10000010001 (in this case, there are no leading or trailing zeros to remove, but if
the binary was 0100000100010, it would become 10000010001).
• Result of split('1'): The string splits into ['', '00000', '000', ''].
Part 2: Palindrome Detection
• algorithms.py: Implement a function is_palindrome(s) that checks if the given string s is
a palindrome, ignoring case and non-alphanumeric characters.
Note you can use this expression to concatenate characters without spacing
S = ''.join(some_list_of_chars)
Note you can use this expression to check if the character alphabetic or not
char.isalnum()