This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Junior Division 2001-2002 Contest 1 Programming Problem Wrap Around Code.
ACSL Junior 2001-2002 Contest 1 Wrap Around Code problem
Wrap Around Code
The ACSL Junior 2001-2002 Contest 1 Wrap Around Code problem problem was the programming problem in ACSL Junior Division 2001-2002 Contest 1.
First, read the Wrap Around Code problem. Make sure you understand the problem description. Read it many times, if necessary. Then, write your algorithm on paper before coding anything. For your algorithm, you can write either pseudocode or a diagrammatical flowchart.
Programming Solution Logic
First, find the numerical value of each letter
From the problem description, we have to find the numberical value for each letter. Let us refer to the letter with the variable letter
.
For this, we can find the ASCII value of each letter. The ASCII value of the letter A
is 65. We can use Python to find the ASCII values of A
, B
, and Z
by running calling the ord()
function on the Python shell.
>>> ord('A')
65
>>> ord('B')
66
>>> ord('Z')
90
So, to assign a numeric value of 1 to A
, we find the ASCII value and subtract 64 from it. Let us represent it with variable lvalue
.
lvalue = ord(letter) - 64
Next, let us review the five rules.
Five Rules
Rule 1: If letter
is between A and E, we multiply its numerical value by 2.
Rule 2: If letter
is between F and J, we find the mod of 3 with its numerical value and multiply the result with 5.
Rule 3: If letter
is between K and O, we divide the numerical value by 4 and multiply the int value with 8.
Rule 4: If letter
is between P and T, we find the sum of the digits of the numerical value and multiply the result with 10.
Rule 5: If letter
is between U and Z, we find the largest factor of the numerical value < lvalue
and multiply the result with 12.
Wrap around the alphabet
If the result is greater than 26, then wrap around the alphabet.
Now, let us write a Python-centric algorithm.
Algorithm
- Read input and store the letter in variable
letter
. - Find the ASCII value of
letter
and store in variablelvalue
. - if 'A' <= letter <= 'E' then
call rule_1() and store value in
lvalue
. - else if 'F' <= letter <= 'J' then
call rule_2() and store value in
lvalue
. - else if 'K' <= letter <= 'O' then
call rule_3() and store value in
lvalue
. - else if 'P' <= letter <= 'T' then
call rule_4() and store value in
lvalue
. - else if 'U' <= letter <= 'Z' then
call rule_5() and store value in
lvalue
. - if
lvalue
is greater than or equal to 26, wrap around it by subtracting from it the highest positive multiple of 26. - Find the ASCII character corresponding to
lvalue
. That is what we need.
My Python solution
This is my Python solution:
'''
ACSL Junior 2001-2002 Contest 1 WRAP AROUND CODE
Blog Post: https://aruljohn.com/blog/acsl-wrap-around-code/
'''
# Rule 1
def rule1(letter):
lvalue = ord(letter) - 64
lvalue *= 2
new_letter = chr(lvalue + 64)
return new_letter
# Rule 2
def rule2(letter):
lvalue = ord(letter) - 64
lvalue = (lvalue % 3) * 5
new_letter = chr(lvalue + 64)
return new_letter
# Rule 3
def rule3(letter):
lvalue = ord(letter) - 64
lvalue = lvalue // 4 * 8
new_letter = chr(lvalue + 64)
return new_letter
# Rule 4
def rule4(letter):
lvalue = ord(letter) - 64
lvalue = sum([int(x) for x in list(str(lvalue))]) * 10
lvalue = wrap_around(lvalue)
new_letter = chr(lvalue + 64)
return new_letter
# Rule 5
def rule5(letter):
lvalue = ord(letter) - 64
largest_factor = 1
i = lvalue
while i > 1:
i -= 1
if lvalue % i == 0:
largest_factor = i
break
lvalue = largest_factor * 12 % 26
lvalue = wrap_around(lvalue)
new_letter = chr(lvalue + 64)
return new_letter
# Wrap around
def wrap_around(n):
while n >= 26:
n -= 26
if n == 0:
return 26
return n
# Function to return encoded letter
def encoded_letter(letter):
new_letter = ''
if 'A' <= letter <= 'E':
new_letter = rule1(letter)
elif 'F' <= letter <= 'J':
new_letter = rule2(letter)
elif 'K' <= letter <= 'O':
new_letter = rule3(letter)
elif 'P' <= letter <= 'T':
new_letter = rule4(letter)
elif 'U' <= letter <= 'Z':
new_letter = rule5(letter)
return new_letter
# Main function
if __name__ == '__main__':
input_data = '''
B
G
Z
T
K
E
H
M
P
V
'''
output_data = '''
D
E
Z
T
P
J
J
X
R
B
'''
sample_input = input_data.strip().split('\n')
expected_output = output_data.strip().split('\n')
for i, s in enumerate(sample_input):
print(expected_output[i] == encoded_letter(s))
Decoding the program
In the main function, I created a string input_data
with the test input, one test case per line. Using list comprehension, I split it into a list.
A second string output_data
contains the expected output, one test case output per line.
The function encoded_letter(s)
is called, and passes the letter as parameter.
We write separate functions for each rule. In each of these rule functions, we find the ASCII number of that letter, subtract 64 from it, then find the new value and return the updated ASCII letter.
There is also a separate function wrap_around(n)
that accepts a number n
which may be greater than 26 and returns the wrapped around value. This is done by subtracting the highest multiple of 26 that is lower than n
.
Useful Python code snippets
There are many code snippets that can be used in competitive programming contests. These are a few. We will use the Python shell to run these individual snippets.
If value is between two values
We use this code to check if letter
is between A and E.
if 'A' <= letter <= 'E':
Finding ASCII numeric value of A using ord('A')
To find the ASCII number associated with any character or letter, we use the ord()
function and pass in the letter as parameter.
To do the opposite, that is find the ASCII character corresponding to a number, we use the chr()
function and pass in the number as parameter.
Find the sum of all digits in a number
Using list comprehension, we find the sum of all digits by (1) converting the number to a string, (2) then breaking it up into a list of ints and (3) applying the sum()
function around the resulting list.
Use the Python shell
If you are not sure about the usage of chr()
or ord()
, just run help
in the Python shell.
Do not be afraid to try different code styles.
Combine common code and DRY
Wherever possible, see if you can combine common code that repeats itself. DRY stands for don't repeat yourself. You can either use repeating code in a function with appropriate parameters. If you don't have time, or it's too late to make a clean fix, just leave it as it is, as long as it is working properly.
Running the Python code
Run the Python code from your Terminal. You do not need to use any input file because I added the input as a string in the main function.
When the program runs, this will be the output:
$ python wrap_around_code.py
True
True
True
True
True
True
True
True
True
True
Conclusion
If there is anything you would like me to add to this article, feel free to comment below or contact me. Thanks.
Related Posts
If you have any questions, please contact me at arulbOsutkNiqlzziyties@gNqmaizl.bkcom. You can also post questions in our Facebook group. Thank you.