Parsing roman numerals with Python

Or… how I managed to make some of Grace’s maths homework into another Code Club session

IMG_0355 Grace had some maths homework to do this weekend, converting a bunch of roman numerals into normal numbers.

Being an interfering sort of parent, I got her to show me what she’d done when she finished.

I could see that she’d gotten a lot of them wrong. She had missed the subtraction you’re supposed to do when a large value follows a smaller value.

I’m a big fan of rubber duck problem solving, but she hadn’t spotted that she’d gone wrong. I decided to try something similar.

First, I got her to try and explain how to convert roman numerals to her sister and me. Having to explain it to someone else often helps to make it clearer to yourself.

IMG_0350

Once she sounded like she’d gotten a better idea of it, I got her to try and describe how to do it as a set of written step by step instructions.

It took several drafts. With each set of instructions she came up with, I gave her an edge case that would break it and she had another go.

IMG_0352

After she’d come up with a set of steps that she was happy with, I reminded her what she’d done in Code Club, that programming was just giving a set of step by step instructions to a computer. So together we turned her instructions into a Python script.

I wrote a few helper functions to give her friendly, readable function names for some of the simple array handling she’s not familiar with. But otherwise, it was pair programming to turn her pseudocode into a Python program.

It seemed to work! (Good enough for the sorts of problems in her homework, at any rate).

She used our program to check her homework.

IMG_0358

It came up with different answers to what she’d written in her homework book for a lot of the questions. And looking at them again, having gone through explaining how to do it a bunch of times, she instantly saw where she’d gone wrong with her homework, and set about fixing it.

So I got her to find for herself that she’d gone wrong and what she needed to do to fix it. But even better, found an excuse to turn it into a Code Club-style afternoon and get her to play with some Python. 🙂

For what it’s worth, this is the script we came up with. But, remember it was written as a quick homework checker by a ten-year old… so it’s not going to be the most robust or comprehensive solution for handling roman numerals you’ve ever seen!

Tags: ,

Comments are closed.