From ff6a7012a4baf3cec132a17a7d1ea8a3abee9337 Mon Sep 17 00:00:00 2001 From: belliash Date: Sun, 5 Aug 2018 22:25:15 +0200 Subject: [PATCH] Another test, converting arab numbers to roman --- tests/arab_to_roman.aer | 32 ++++++++++++++++++++++++++++++++ tests/arab_to_roman.exp | 9 +++++++++ 2 files changed, 41 insertions(+) create mode 100644 tests/arab_to_roman.aer create mode 100644 tests/arab_to_roman.exp diff --git a/tests/arab_to_roman.aer b/tests/arab_to_roman.aer new file mode 100644 index 0000000..837e62d --- /dev/null +++ b/tests/arab_to_roman.aer @@ -0,0 +1,32 @@ +class Main { + + private function num2Roman($num) { + $n = intval($num); + $result = ''; + $lookup = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, + 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, + 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1); + + foreach($lookup as $roman => $value) { + $matches = intval($n / $value); + $result .= str_repeat($roman, $matches); + $n = $n % $value; + } + return $result; + } + + public function __construct() { + print(' 7 => ' . $this->num2Roman(7) . "\n"); + print(' 9 => ' . $this->num2Roman(9) . "\n"); + print(' 11 => ' . $this->num2Roman(11) . "\n"); + print(' 42 => ' . $this->num2Roman(42) . "\n"); + print(' 105 => ' . $this->num2Roman(105) . "\n"); + print('1984 => ' . $this->num2Roman(1984) . "\n"); + print('1999 => ' . $this->num2Roman(1999) . "\n"); + print('2018 => ' . $this->num2Roman(2018) . "\n"); + print('2144 => ' . $this->num2Roman(2144) . "\n"); + } + +} + +new Main(); diff --git a/tests/arab_to_roman.exp b/tests/arab_to_roman.exp new file mode 100644 index 0000000..9d951f6 --- /dev/null +++ b/tests/arab_to_roman.exp @@ -0,0 +1,9 @@ + 7 => VII + 9 => IX + 11 => XI + 42 => XLII + 105 => CV +1984 => MCMLXXXIV +1999 => MCMXCIX +2018 => MMXVIII +2144 => MMCXLIV