iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐣

Teaching Programming to a Junior High School Student via AtCoder Part 4: First Submission (Part 2) - ABC086A - Product

に公開

Content of this Article

This is a record of a software engineer teaching programming to a middle schooler. This is the 4th installment. The child operates their own machine and submits their first solution. In the first half, they learned to write and run a single statement. Now, in the second half, we will cover writing code according to the problem statement and submitting it.

【Previous Article】
The preamble was long, but in the 3rd installment, the child finally started writing code themselves.

In 3 lines... or rather, just 1 line

  1. Proceed one step at a time with small increments and reach the final submission.

Recap of the last installment

We started working on ABC 086 A - Product and took small steps.

https://atcoder.jp/contests/abc086/tasks/abc086_a

Our goal was to build it starting from input acquisition, writing one part at a time, and performing debug runs to confirm the behavior, proceeding as follows:

  1. Read the problem carefully first.
  • 📌 Teach the method of writing code while having fun ← Important
  1. Write code to get one line of input and try running it.
  2. Add code to output an arbitrary string unrelated to the input and try running it.
  3. Modify the code to get one line of input and output exactly what was received, then try running it.

Second Half: Writing Code Following the Problem and Reaching the Submission

In this article, we will proceed as follows:

  1. Write code to split the input into an array by spaces and try running it. a = input().split()
  2. Modify the code to convert each element of the string array into an integer and assign them, then try running it. a, b = map(int, input().split())
  3. Build the calculations for the logic while testing them step-by-step.
    1. Product of a and b... a*b
    2. To determine if it's even or odd, calculate the remainder when divided by 2... a*b % 2 (calculate the remainder when divided by 2)
    3. Check if the remainder is 0... print(a*b % 2 == 0) → True/False
  4. Use this to write an if statement and try running it.
    1. if a*b % 2 == 0: print('Even') else: print('Odd')

5. Write Code to Split into an Array by Spaces and Run It. a = input().split()

Me: "We've got the input, but it's just one long string, so we can't do multiplication yet."
Kid: "We need to split it by spaces into two and convert them into numbers, right? Like you taught me the other day."
Me: "You remember it so well! Even though I told you that you didn't need to memorize it."
Kid: "Am I being scolded right now? For actually remembering it?"
Me: "Well said. Since you remember it, go ahead and write it."
Kid: "Splitting is like this, right? a = input().split()"
Me: "No way... you really remember it..."
Kid: "Yeah, yeah, I got it. Running it now."

solve_abc086_a.py
#!/usr/bin/env python
a = input().split()
print(a)
Result
["3", "4"]

Me: "Excellent! By the way, could you explain to me what's happening here?"
Kid: "Polite all of a sudden. I took the input string, split it into pieces by spaces, and made it into an array."
Me: "I have nothing left to teach you, can I go home?"
Kid: "Where do you think you're going?"

6. Modify the Code to Convert Each Element of the String Array to an Integer and Assign Them, Then Run It. a, b = map(int, input().split())

Kid: "I need to turn these into numbers. What was it... I use that 'thing'."
Me: "Ah, so there are things you don't remember."
Kid: "Be quiet. Can you go home now?"
Me: "The one using map."
Kid: "That's it, that's the one."

solve_abc086_a.py
#!/usr/bin/env python
a = map(int, input().split())
print(a)
Result
<map object at 0x104f2bee0>

Kid: "Wait, what?"
Me: "I can't explain it well, but the result of map isn't an array."
Kid: "Explain it! (laughs)"
Me: "When you're older."
Kid: "You're dodging it. Treating me like a kid."
Me: "Sorry lol. Seriously, if I explain it now, it'll just get unnecessarily complicated and you'll regret it."
Kid: "Fine then. But I've seen this before. How do I use it again?"
Me: "Try assigning it directly to a and b."
Kid: "Oh, like this?"

solve_abc086_a.py
#!/usr/bin/env python
a, b = map(int, input().split())
print(a, b)
Result
3 4

Kid: "Right, right, this is it."

7. Let's Build the Logic Operations While Testing Them Step-by-Step

  1. Product of a and b... a*b

Me: "Can you multiply these and display the result?"
Kid: "What do I use for multiplication?"
Me: "* this."
Kid: "It reminds me of Pompompurin."
Me: "Don't do him like that."

solve_abc086_a.py
#!/usr/bin/env python
a, b = map(int, input().split())
print(a, b)
print(a*b)
Result
3 4
12

Kid: "Done."
Me: "You're already doing stuff like print debugging without me even teaching you."
Kid: "Isn't it important to run it and check the result?"
Me: "Exactly as you say."

  1. To determine if it's even or odd, calculate the remainder when divided by 2... a*b % 2 (calculate the remainder when divided by 2)

Kid: "Is there no command that determines if it's even or odd?"
Me: "None. If you were asked to judge if a number is even or odd in math, what would you do?"
Kid: "Check if it's divisible by 2?"
Me: "Right. You've learned about quotients and remainders, right? There's something to calculate that: // and %. The remainder when divided by 2 is given by x % 2."
Kid: "Man, I was just about to say that! Can I write it?"
Me: "Please, go ahead."

solve_abc086_a.py
#!/usr/bin/env python
a, b = map(int, input().split())
print(a, b)
print(a*b)
print(a*b%2)
Result
3 4
12
0

Kid: "It was divisible. So that means it's even."

8. Use This to Write an if Statement and Try Running It

Me: "Can you write an if statement using this?"
Kid: "Isn't the size of these 'small steps' getting a bit large?"

solve_abc086_a.py
#!/usr/bin/env python
a, b = map(int, input().split())
print(a, b)
print(a*b)
print(a*b%2)
if a*b % 2 == 0:
  print('Even')
else:
  print('Odd')
Result
3 4
12
0
Even

Kid: "I did it! I'll try making it odd."

Result
3 5
15
1
Odd

Me: "You did it! The debug outputs are still there, so you should probably remove them."
Kid: "Okay."

solve_abc086_a.py
#!/usr/bin/env python
a, b = map(int, input().split())
if a*b % 2 == 0:
  print('Even')
else:
  print('Odd')

Kid: "It's cleaner now. Hey, I want to try that thing that tests against sample inputs."
Me: "Press here and select oj test."

Result
(.venv) a% oj test -c ./solve_abc086_a.py -d ./tests   
[INFO] online-judge-tools 11.5.1 (+ online-judge-api-client 10.10.0)
[INFO] 2 cases found

[INFO] sample-1
[INFO] time: 0.017030 sec
[SUCCESS] AC

[INFO] sample-2
[INFO] time: 0.028136 sec
[SUCCESS] AC

[INFO] slowest: 0.028136 sec  (for sample-2)
[INFO] max memory: 6.784000 MB  (for sample-2)
[SUCCESS] test success: 2 cases
(.venv) a% 

Kid: "I did it!"

Me: "Want to try submitting this?"
Kid: "I do!!"
Me: "Press here and select acc submit."
Kid: "Whoa... it became AC!"

Me: "Congratulations! Your first submission was a huge success!"
Kid: "Yay!"

【Next Article】 We will solve the next Problem A, ABC081A - Placing Marbles.

Discussion