-
-
Notifications
You must be signed in to change notification settings - Fork 40
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[Roman Numerals] add practice exercise (v2) (#311)
* [Roman Numerals] add practice exercise (v2) * Update config.json --------- Co-authored-by: Jon Calder <[email protected]>
- Loading branch information
1 parent
a6088bd
commit 146a088
Showing
7 changed files
with
281 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,41 @@ | ||
# Instructions | ||
|
||
Write a function to convert from normal numbers to Roman Numerals. | ||
|
||
The Romans were a clever bunch. | ||
They conquered most of Europe and ruled it for hundreds of years. | ||
They invented concrete and straight roads and even bikinis. | ||
One thing they never discovered though was the number zero. | ||
This made writing and dating extensive histories of their exploits slightly more challenging, but the system of numbers they came up with is still in use today. | ||
For example the BBC uses Roman numerals to date their programs. | ||
|
||
The Romans wrote numbers using letters - I, V, X, L, C, D, M. | ||
(notice these letters have lots of straight lines and are hence easy to hack into stone tablets). | ||
|
||
```text | ||
1 => I | ||
10 => X | ||
7 => VII | ||
``` | ||
|
||
The maximum number supported by this notation is 3,999. | ||
(The Romans themselves didn't tend to go any higher) | ||
|
||
Wikipedia says: Modern Roman numerals ... are written by expressing each digit separately starting with the left most digit and skipping any digit with a value of zero. | ||
|
||
To see this in practice, consider the example of 1990. | ||
|
||
In Roman numerals 1990 is MCMXC: | ||
|
||
1000=M | ||
900=CM | ||
90=XC | ||
|
||
2008 is written as MMVIII: | ||
|
||
2000=MM | ||
8=VIII | ||
|
||
Learn more about [Roman numerals on Wikipedia][roman-numerals]. | ||
|
||
[roman-numerals]: https://wiki.imperivm-romanvm.com/wiki/Roman_Numerals |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"authors": [ | ||
"colinleach" | ||
], | ||
"files": { | ||
"solution": [ | ||
"roman-numerals.R" | ||
], | ||
"test": [ | ||
"test_roman-numerals.R" | ||
], | ||
"example": [ | ||
".meta/example.R" | ||
] | ||
}, | ||
"blurb": "Write a function to convert from normal numbers to Roman Numerals.", | ||
"source": "The Roman Numeral Kata", | ||
"source_url": "https://codingdojo.org/kata/RomanNumerals/" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
roman <- function(arabic) { | ||
nums <- c(1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1) | ||
romans <- c("M", "CM", "D", "CD", "C", "XC", | ||
"L", "XL", "X", "IX", "V", "IV", "I") | ||
result <- c() | ||
|
||
for (i in seq_along(nums)) { | ||
num <- nums[i] | ||
while (num <= arabic) { | ||
result <- append(result, romans[i]) | ||
arabic <- arabic - num | ||
} | ||
} | ||
paste(result, sep = "", collapse = "") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
# This is an auto-generated file. | ||
# | ||
# Regenerating this file via `configlet sync` will: | ||
# - Recreate every `description` key/value pair | ||
# - Recreate every `reimplements` key/value pair, where they exist in problem-specifications | ||
# - Remove any `include = true` key/value pair (an omitted `include` key implies inclusion) | ||
# - Preserve any other key/value pair | ||
# | ||
# As user-added comments (using the # character) will be removed when this file | ||
# is regenerated, comments can be added via a `comment` key. | ||
|
||
[19828a3a-fbf7-4661-8ddd-cbaeee0e2178] | ||
description = "1 is I" | ||
|
||
[f088f064-2d35-4476-9a41-f576da3f7b03] | ||
description = "2 is II" | ||
|
||
[b374a79c-3bea-43e6-8db8-1286f79c7106] | ||
description = "3 is III" | ||
|
||
[05a0a1d4-a140-4db1-82e8-fcc21fdb49bb] | ||
description = "4 is IV" | ||
|
||
[57c0f9ad-5024-46ab-975d-de18c430b290] | ||
description = "5 is V" | ||
|
||
[20a2b47f-e57f-4797-a541-0b3825d7f249] | ||
description = "6 is VI" | ||
|
||
[ff3fb08c-4917-4aab-9f4e-d663491d083d] | ||
description = "9 is IX" | ||
|
||
[6d1d82d5-bf3e-48af-9139-87d7165ed509] | ||
description = "16 is XVI" | ||
|
||
[2bda64ca-7d28-4c56-b08d-16ce65716cf6] | ||
description = "27 is XXVII" | ||
|
||
[a1f812ef-84da-4e02-b4f0-89c907d0962c] | ||
description = "48 is XLVIII" | ||
|
||
[607ead62-23d6-4c11-a396-ef821e2e5f75] | ||
description = "49 is XLIX" | ||
|
||
[d5b283d4-455d-4e68-aacf-add6c4b51915] | ||
description = "59 is LIX" | ||
|
||
[4465ffd5-34dc-44f3-ada5-56f5007b6dad] | ||
description = "66 is LXVI" | ||
|
||
[46b46e5b-24da-4180-bfe2-2ef30b39d0d0] | ||
description = "93 is XCIII" | ||
|
||
[30494be1-9afb-4f84-9d71-db9df18b55e3] | ||
description = "141 is CXLI" | ||
|
||
[267f0207-3c55-459a-b81d-67cec7a46ed9] | ||
description = "163 is CLXIII" | ||
|
||
[902ad132-0b4d-40e3-8597-ba5ed611dd8d] | ||
description = "166 is CLXVI" | ||
|
||
[cdb06885-4485-4d71-8bfb-c9d0f496b404] | ||
description = "402 is CDII" | ||
|
||
[6b71841d-13b2-46b4-ba97-dec28133ea80] | ||
description = "575 is DLXXV" | ||
|
||
[dacb84b9-ea1c-4a61-acbb-ce6b36674906] | ||
description = "666 is DCLXVI" | ||
|
||
[432de891-7fd6-4748-a7f6-156082eeca2f] | ||
description = "911 is CMXI" | ||
|
||
[e6de6d24-f668-41c0-88d7-889c0254d173] | ||
description = "1024 is MXXIV" | ||
|
||
[efbe1d6a-9f98-4eb5-82bc-72753e3ac328] | ||
description = "1666 is MDCLXVI" | ||
|
||
[bb550038-d4eb-4be2-a9ce-f21961ac3bc6] | ||
description = "3000 is MMM" | ||
|
||
[3bc4b41c-c2e6-49d9-9142-420691504336] | ||
description = "3001 is MMMI" | ||
|
||
[4e18e96b-5fbb-43df-a91b-9cb511fe0856] | ||
description = "3999 is MMMCMXCIX" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,3 @@ | ||
roman <- function(arabic) { | ||
|
||
} |
107 changes: 107 additions & 0 deletions
107
exercises/practice/roman-numerals/test_roman-numerals.R
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,107 @@ | ||
source("./roman-numerals.R") | ||
library(testthat) | ||
|
||
test_that("1 is I", { | ||
expect_equal(roman(1), "I") | ||
}) | ||
|
||
test_that("2 is II", { | ||
expect_equal(roman(2), "II") | ||
}) | ||
|
||
test_that("3 is III", { | ||
expect_equal(roman(3), "III") | ||
}) | ||
|
||
test_that("4 is IV", { | ||
expect_equal(roman(4), "IV") | ||
}) | ||
|
||
test_that("5 is V", { | ||
expect_equal(roman(5), "V") | ||
}) | ||
|
||
test_that("6 is VI", { | ||
expect_equal(roman(6), "VI") | ||
}) | ||
|
||
test_that("9 is IX", { | ||
expect_equal(roman(9), "IX") | ||
}) | ||
|
||
test_that("16 is XVI", { | ||
expect_equal(roman(16), "XVI") | ||
}) | ||
|
||
test_that("27 is XXVII", { | ||
expect_equal(roman(27), "XXVII") | ||
}) | ||
|
||
test_that("48 is XLVIII", { | ||
expect_equal(roman(48), "XLVIII") | ||
}) | ||
|
||
test_that("49 is XLIX", { | ||
expect_equal(roman(49), "XLIX") | ||
}) | ||
|
||
test_that("59 is LIX", { | ||
expect_equal(roman(59), "LIX") | ||
}) | ||
|
||
test_that("66 is LXVI", { | ||
expect_equal(roman(66), "LXVI") | ||
}) | ||
|
||
test_that("93 is XCIII", { | ||
expect_equal(roman(93), "XCIII") | ||
}) | ||
|
||
test_that("141 is CXLI", { | ||
expect_equal(roman(141), "CXLI") | ||
}) | ||
|
||
test_that("163 is CLXIII", { | ||
expect_equal(roman(163), "CLXIII") | ||
}) | ||
|
||
test_that("166 is CLXVI", { | ||
expect_equal(roman(166), "CLXVI") | ||
}) | ||
|
||
test_that("402 is CDII", { | ||
expect_equal(roman(402), "CDII") | ||
}) | ||
|
||
test_that("575 is DLXXV", { | ||
expect_equal(roman(575), "DLXXV") | ||
}) | ||
|
||
test_that("666 is DCLXVI", { | ||
expect_equal(roman(666), "DCLXVI") | ||
}) | ||
|
||
test_that("911 is CMXI", { | ||
expect_equal(roman(911), "CMXI") | ||
}) | ||
|
||
test_that("1024 is MXXIV", { | ||
expect_equal(roman(1024), "MXXIV") | ||
}) | ||
|
||
test_that("1666 is MDCLXVI", { | ||
expect_equal(roman(1666), "MDCLXVI") | ||
}) | ||
|
||
test_that("3000 is MMM", { | ||
expect_equal(roman(3000), "MMM") | ||
}) | ||
|
||
test_that("3001 is MMMI", { | ||
expect_equal(roman(3001), "MMMI") | ||
}) | ||
|
||
test_that("3999 is MMMCMXCIX", { | ||
expect_equal(roman(3999), "MMMCMXCIX") | ||
}) | ||
|