Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions exercises/practice/roman-numerals/.approaches/config.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
"authors": [
"colinleach",
"BethanyG"
],
"contributors": [
"yrahcaz7"
]
},
"approaches": [
Expand All @@ -14,16 +17,22 @@
"authors": [
"BethanyG",
"colinleach"
],
"contributors": [
"yrahcaz7"
]
},
{
"uuid": "4ed8396c-f2c4-4072-abc9-cc8fe2780a5a",
"slug": "loop-over-romans",
"title": "Loop Over Romans",
"title": "Loop Over Roman Numerals",
"blurb": "Test Roman numerals from the largest down and eat the maximum possible at each step.",
"authors": [
"BethanyG",
"colinleach"
],
"contributors": [
"yrahcaz7"
]
},
{
Expand All @@ -34,6 +43,9 @@
"authors": [
"BethanyG",
"colinleach"
],
"contributors": [
"yrahcaz7"
]
},
{
Expand All @@ -44,16 +56,22 @@
"authors": [
"BethanyG",
"colinleach"
],
"contributors": [
"yrahcaz7"
]
},
{
"uuid": "a492c6b4-3780-473d-a2e6-a1c3e3da4f81",
"slug": "recurse-match",
"title": "Recurse Match",
"blurb": "Combine recursive programming with the recently-introduced structural pattern matching.",
"blurb": "Combine recursive programming with structural pattern matching.",
"authors": [
"BethanyG",
"colinleach"
],
"contributors": [
"yrahcaz7"
]
}
]
Expand Down
66 changes: 30 additions & 36 deletions exercises/practice/roman-numerals/.approaches/if-else/content.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,71 +16,50 @@ def roman(number):
if m > 0:
res += m * 'M'

if 4 > c > 0:
if c < 4:
res += c * 'C'
elif c == 4:
res += 'CD'
elif 9 > c > 4:
elif c < 9:
res += 'D' + ((c - 5) * 'C')
elif c == 9:
res += 'CM'

if 4 > x > 0:
if x < 4:
res += x * 'X'
elif x == 4:
res += 'XL'
elif 9 > x > 4:
elif x < 9:
res += 'L' + ((x - 5) * 'X')
elif x == 9:
res += 'XC'

if 4 > i > 0:
if i < 4:
res += i * 'I'
elif i == 4:
res += 'IV'
elif 9 > i > 4:
elif i < 9:
res += 'V' + ((i - 5) * 'I')
elif i == 9:
res += 'IX'

return res
```

This gets the job done.
Something like it would work in most languages, though Python's range test (`a > x > b`) saves some boolean logic.
Though this approach is rather naive, but it gets the job done.
Something similar would work in most languages, though the usage of the `*` operator for string repetition is fairly Python-specific.

## Refactoring
Here, the first block of code uses the floor division operator (`//`) and the modulo operator (`%`) to extract each digit from the input number.
Then, the following blocks determine the Roman equivalent for each digit and append it to the resulting string.

The code above is quite long and a bit repetitive.
We should explore ways to make it more concise.
A more concise variation of the approach is discussed below.

The first block is just a way to extract the digits from the input number.
This can be done with a list comprehension, left-padding with zeros as necessary:

```python
digits = ([0, 0, 0, 0] + [int(d) for d in str(number)])[-4:]
```

The blocks for hundreds, tens, and units are all essentially the same, so we can put that code in a function.
We just need to pass in the digit, plus a tuple of translations for `(1, 4, 5, 9)` or their 10x and 100x equivalents.

It is also unnecessary to keep retesting the lower bounds within an `elif`, as the code line will only be reached if that is satisfied.

Given that, the code simplifies to:
## Variation #1

```python
def roman(number: int) -> str:
def translate_digit(digit: int, translations: iter) -> str:
units, four, five, nine = translations
if digit < 4:
return digit * units
if digit == 4:
return four
if digit < 9:
return five + (digit - 5) * units
return nine

m, c, x, i = ([0, 0, 0, 0] + [int(d) for d in str(number)])[-4:]
def roman(number):
m, c, x, i = ([0, 0, 0, 0] + [int(digit) for digit in str(number)])[-4:]
res = ''

if m > 0:
Expand All @@ -93,8 +72,23 @@ def roman(number: int) -> str:
res += translate_digit(i, ('I', 'IV', 'V', 'IX'))

return res


def translate_digit(digit, translations):
units, four, five, nine = translations
if digit < 4:
return digit * units
if digit == 4:
return four
if digit < 9:
return five + (digit - 5) * units
return nine
```

(Using `return` instead of `elif` is a matter of personal preference.)
In this variant, a [list comprehension][list-comprehension] is used to extract the digits from the input number, left-padding with zeros as necessary.
For determining the Roman equivalent for each digit, this solution uses a helper function that takes a digit and a tuple of translations for `(1, 4, 5, 9)` (or their 10x and 100x equivalents).

The last few lines are quite similar and it would be possible to refactor them into a loop, but this is enough to illustrate the principle.


[list-comprehension]: https://docs.python.org/3/tutorial/datastructures.html#list-comprehensions
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
def translate_digit(digit: int, translations: iter) -> str:
def translate_digit(digit, translations):
units, four, five, nine = translations
if digit < 4: return digit * units
if digit == 4: return four
Expand Down
Loading
Loading