0% found this document useful (0 votes)
19 views2 pages

Quiz

The document outlines an advanced Python quiz that tests skills in algorithmic problem-solving and file I/O operations. It includes tasks such as calculating the longest binary gap, checking for palindromes, and implementing file operations with robust error handling. Participants are required to submit two Python files, algorithms.py and main_script.py, that contain the necessary functions and logic for the tasks.

Uploaded by

ziada00700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
19 views2 pages

Quiz

The document outlines an advanced Python quiz that tests skills in algorithmic problem-solving and file I/O operations. It includes tasks such as calculating the longest binary gap, checking for palindromes, and implementing file operations with robust error handling. Participants are required to submit two Python files, algorithms.py and main_script.py, that contain the necessary functions and logic for the tasks.

Uploaded by

ziada00700
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

Advanced Python Quiz Description

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

Part 1: Binary Gap


• algorithms.py: Write a function find_longest_binary_gap(n) that calculates the longest
sequence of consecutive zeros that is surrounded by ones at both ends in the binary
representation of a number n.
Note you cab use this expression to convert decimal to binary:
binary = bin(n)[2:] # Convert number to binary, removing the '0b' prefix
Example Input: 1041 (binary representation: 10000010001)
Expected Output: 5 (the longest sequence of zeros surrounded by ones)

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()

Example Input: "A man, a plan, a canal, Panama"


Expected Output: True (it is a palindrome when ignoring punctuation and case)

Part 3: File Operations


• algorithms.py: Write functions write_to_file(file_path, content) and read_from_file(
file_path) for handling file writing and reading respectively, with proper error
management.
Example for Write: Save "Test content for file." to test.txt
Example for Read: Read from test.txt should return "Test content for file."

Part 4: Integration and Execution


• main_script.py: Utilize the functions from algorithms.py to:
1. Calculate the longest binary gap of a given number.
2. Check if a given string is a palindrome.
3. Write results to a file named results.txt.
4. Read back and print the contents of results.txt.
Expected Output in results.txt:
Longest Binary Gap: 5
Is Palindrome: Yes

Completion and Packaging


Ensure your code is well-commented, handles all exceptions gracefully, and test
each part to verify functionality. Package your algorithms.py and main_script.py files
appropriately for submission.

You might also like