Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

New exercise: Roman Numerals #520

Merged
merged 1 commit into from
Feb 1, 2024
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
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ doc/

## IDE files/folders
/.idea/
/.vscode/
/.vscode/
58 changes: 35 additions & 23 deletions config.json
Original file line number Diff line number Diff line change
Expand Up @@ -142,9 +142,9 @@
]
},
{
"uuid": "40dd01f4-71be-4b01-827c-fe6d8a33b32a",
"slug": "high-scores",
"name": "High Scores",
"uuid": "40dd01f4-71be-4b01-827c-fe6d8a33b32a",
"practices": [],
"prerequisites": [],
"difficulty": 2
Expand Down Expand Up @@ -179,7 +179,11 @@
"practices": [],
"prerequisites": [],
"difficulty": 8,
"topics": ["domain_specific_languages", "parsing", "stacks"]
"topics": [
"domain_specific_languages",
"parsing",
"stacks"
]
},
{
"slug": "word-count",
Expand Down Expand Up @@ -263,9 +267,9 @@
]
},
{
"uuid": "6b552157-5c08-413e-8667-b9555a1dbba5",
"slug": "sum-of-multiples",
"name": "Sum of Multiples",
"uuid": "6b552157-5c08-413e-8667-b9555a1dbba5",
"practices": [],
"prerequisites": [],
"difficulty": 2
Expand Down Expand Up @@ -622,7 +626,15 @@
"arrays",
"lists"
]
}
},
{
"slug": "roman-numerals",
"name": "Roman Numerals",
"uuid": "8472c6f2-1ede-426e-8234-89b5da766d88",
"practices": [],
"prerequisites": [],
"difficulty": 3
}
]
},
"concepts": [
Expand Down Expand Up @@ -669,50 +681,50 @@
],
"key_features": [
{
"icon": "web",
"title": "Web and Mobile",
"content": "Dart has the Flutter library, which can be used to quickly build web and smartphone applications."
"content": "Dart has the Flutter library, which can be used to quickly build web and smartphone applications.",
"icon": "web"
},
{
"icon": "portable",
"title": "iOS and Android",
"content": "Dart can be converted into Swift and Kotlin, making mobile development fast and efficient."
"content": "Dart can be converted into Swift and Kotlin, making mobile development fast and efficient.",
"icon": "portable"
},
{
"icon": "documentation",
"title": "Full Documentation",
"content": "Dart's learning materials are constantly updated, making it easy to look up information."
"content": "Dart's learning materials are constantly updated, making it easy to look up information.",
"icon": "documentation"
},
{
"icon": "community",
"title": "Countless Packages",
"content": "Easily build and publish packages for public use, which can be accessed from Dart directly."
"content": "Easily build and publish packages for public use, which can be accessed from Dart directly.",
"icon": "community"
},
{
"icon": "functional",
"title": "Highly-organized Code",
"content": "Dart's coding style makes it easy to read and understand."
"content": "Dart's coding style makes it easy to read and understand.",
"icon": "functional"
},
{
"icon": "fun",
"title": "Easy to Learn",
"content": "Dart is easy to understand and great for beginners to programming."
"content": "Dart is easy to understand and great for beginners to programming.",
"icon": "fun"
}
],
"tags": [
"execution_mode/compiled",
"paradigm/declarative",
"paradigm/functional",
"paradigm/imperative",
"paradigm/object_oriented",
"typing/static",
"typing/strong",
"execution_mode/compiled",
"platform/windows",
"platform/mac",
"platform/linux",
"platform/ios",
"platform/android",
"platform/ios",
"platform/linux",
"platform/mac",
"platform/web",
"platform/windows",
"typing/static",
"typing/strong",
"used_for/cross_platform_development",
"used_for/games",
"used_for/guis",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
# ignore

## Dart Extension Methods

We're implementing this exercise with an extention method on the `int` class.
For details, see [the Dart docs][extension-methods].

[extension-methods]: https://dart.dev/language/extension-methods
41 changes: 41 additions & 0 deletions exercises/practice/roman-numerals/.docs/instructions.md
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
17 changes: 17 additions & 0 deletions exercises/practice/roman-numerals/.meta/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
{
"authors": ["glennj"],
"files": {
"solution": [
"lib/roman_numerals.dart"
],
"test": [
"test/roman_numerals_test.dart"
],
"example": [
".meta/lib/example.dart"
]
},
"blurb": "Write a function to convert from normal numbers to Roman Numerals.",
"source": "The Roman Numeral Kata",
"source_url": "https://codingdojo.org/kata/RomanNumerals/"
}
22 changes: 22 additions & 0 deletions exercises/practice/roman-numerals/.meta/lib/example.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
extension ConvertToRomanNumerals on int {
String toRoman() {
var n = this;
var roman = '';
while (n > 0) {
if (n >= 1000) { roman += 'M'; n -= 1000; }
else if (n >= 900) { roman += 'CM'; n -= 900; }
else if (n >= 500) { roman += 'D'; n -= 500; }
else if (n >= 400) { roman += 'CD'; n -= 400; }
else if (n >= 100) { roman += 'C'; n -= 100; }
else if (n >= 90) { roman += 'XC'; n -= 90; }
else if (n >= 50) { roman += 'L'; n -= 50; }
else if (n >= 40) { roman += 'XL'; n -= 40; }
else if (n >= 10) { roman += 'X'; n -= 10; }
else if (n >= 9) { roman += 'IX'; n -= 9; }
else if (n >= 5) { roman += 'V'; n -= 5; }
else if (n >= 4) { roman += 'IV'; n -= 4; }
else if (n >= 1) { roman += 'I'; n -= 1; }
}
return roman;
}
}
88 changes: 88 additions & 0 deletions exercises/practice/roman-numerals/.meta/tests.toml
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"
18 changes: 18 additions & 0 deletions exercises/practice/roman-numerals/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
analyzer:
strong-mode:
implicit-casts: false
implicit-dynamic: false
errors:
unused_element: error
unused_import: error
unused_local_variable: error
dead_code: error

linter:
rules:
# Error Rules
- avoid_relative_lib_imports
- avoid_types_as_parameter_names
- literal_only_boolean_expressions
- no_adjacent_strings_in_list
- valid_regexps
5 changes: 5 additions & 0 deletions exercises/practice/roman-numerals/lib/roman_numerals.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
extension ConvertToRomanNumerals on int {
String toRoman() {
// Put your code here
}
}
5 changes: 5 additions & 0 deletions exercises/practice/roman-numerals/pubspec.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
name: 'roman_numerals'
environment:
sdk: '>=2.18.0 <3.0.0'
dev_dependencies:
test: '<2.0.0'
Loading