This article discusses and contains the Python code solution for the American Computer Science League (ACSL) Junior Division 2003-2004 Contest 1 Programming Problem Change Digits.
ACSL Junior 2003-2004 Contest 1 Change Digits problem and solution
Change Digits
The ACSL Junior Change Digits problem was the programming problem in ACSL Intermediate Division 2003-2004 Contest 1.
First, read the Change Digits 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 flowchart.
There are 3 versions of this, Junior, Intermediate and Senior. This is relatively the easiest version.
Algorithm
- We will create a list of integers, where each digit in the number is saved as an individual list item. In short, we will split the number into an integer list of digits.
- Next, we will find the largest digit in this list.
- Loop through the digits, and check if the current digit is the largest digit. If yes, then:
(i) if the digit is odd, change that corresponding list item to 0
(ii) if the digit is even, add 4 to it and extract the ones place - Create a new integer with the current digits of the list and return it for printing in the main function.
Sample Input
Save this sample input as a file junior_change_digits_1.in
132
1421
18234
923
This file has 4 lines with integers, each less than 100,000. Each line is a test case.
Sample Output
Save this sample output as a file junior_change_digits_1.out
102
1821
12234
23
There are 3 output lines, one for each test case.
My Python solution
This is my working Python solution. Save it as a file junior_change_digits.py.
'''
ACSL Junior Division 2003-2004 Contest 1 - CHANGE DIGITS
Blog Post: https://aruljohn.com/blog/acsl-junior-change-digits/
'''
def change_digits(n):
answer = n
digits = [int(digit) for digit in list(n)]
largest_digit = max(digits)
for i, digit in enumerate(digits):
if largest_digit == digit:
if digit % 2 == 1:
digits[i] = 0
else:
newdigit = digits[i] + 4
if newdigit > 9:
digits[i] = newdigit - 10
else:
digits[i] = newdigit
answer = ''.join(str(digit) for digit in digits)
return int(answer)
if __name__ == "__main__":
for _ in range(4):
n = input()
print(change_digits(n))
Method 1: How to run the program with diff and cat
If you are running this program on Linux or macOS, you can use regular Unix/Linux commands.
$ diff <(cat junior_change_digits_1.in | python junior_change_digits.py) <(cat junior_change_digits_1.out)
Method 2: How to run the program the regular way
The regular way to run this program is:
python junior_change_digits.py
Copy/paste the input lines and press ENTER or return.
132 1421 18234 923
Screenshot
$ python junior_change_digits.py
132
1421
18234
923
102
1821
12234
23
Method 3: How to run the program using cat on the input file
On my Mac, I personally prefer running cat on the input file and pipe the output to the Python program.
cat junior_change_digits_1.in | python junior_change_digits.py
Screenshot
$ cat junior_change_digits_1.in | python junior_change_digits.py
102
1821
12234
23
Compare this output with the contents of junior_change_digits_1.out. They should be identical.
Decoding the program
In the change_digits(n) function, we use list comprehension to create a list of digits of the the number n.
We use the list max() function to find the largest digit in the number n.
We iterate through the list containing the digits of n. In the for block, we check if the digit is same as the largest digit. If it is, then replace the corresponding list item with the appropriate digit.
At the end, convert each digit to string, do a join of the string digits and
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 test these individual snippets.
List comprehension
We take an integer in string form and create a list of integers of each digit.
digits = [int(digit) for digit in list(n)]
join()
You can join the list of integers using the join() function. Prior to join() being called, we convert each item to a string using str().
answer = ''.join(str(digit) for digit in digits)
Use the Python shell
Feel free to test all the individual keywords and functions in the Python shell.
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.
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.