Coding challenge: Arabic Numbers to Roman Numbers

download

Goals

  • Programmatically evaluate Arabic numbers into sets
  • Encode Arabic numbers into roman equiliventl
  • (bonus) interactively using the terminal framework in the code

Starting point

<main>
  <section id="stdout"></section>
  <form onsubmit="javascript:checkForm();">
    <input type="text" name="stdin">
  </form>
</main>

<style>
.color0,  .black      { color: rgb(0, 0, 0); }
.color1,  .red        { color: rgb(227, 30, 96); }
.color2,  .darkblue   { color: rgb(96, 78, 189); }
.color3,  .purple     { color: rgb(255, 68, 253); }
.color4,  .darkgreen  { color: rgb(0, 163, 96); }
.color5,  .grey       { color: rgb(156, 156, 156); }
.color6,  .medblue    { color: rgb(20, 207, 253); }
.color7,  .lightblue  { color: rgb(208, 195, 255); }
.color8,  .brown      { color: rgb(96, 114, 3); }
.color9,  .orange     { color: rgb(255, 106, 60); }
.color10, .lightgrey  { color: rgb(156, 156, 156); }
.color11, .pink       { color: rgb(255, 160, 208); }
.color12, .lightgreen { color: rgb(20, 245, 60); }
.color13, .yellow     { color: rgb(208, 221, 141); }
.color14, .aqua       { color: rgb(114, 255, 208); }
.color15, .white      { color: rgb(255, 255, 255); }

.bgcolor0,  .bgblack      { color: rgb(0, 0, 0); }
.bgcolor1,  .bgred        { color: rgb(227, 30, 96); }
.bgcolor2,  .bgdarkblue   { color: rgb(96, 78, 189); }
.bgcolor3,  .bgpurple     { color: rgb(255, 68, 253); }
.bgcolor4,  .bgdarkgreen  { color: rgb(0, 163, 96); }
.bgcolor5,  .bggrey       { color: rgb(156, 156, 156); }
.bgcolor6,  .bgmedblue    { color: rgb(20, 207, 253); }
.bgcolor7,  .bglightblue  { color: rgb(208, 195, 255); }
.bgcolor8,  .bgbrown      { color: rgb(96, 114, 3); }
.bgcolor9,  .bgorange     { color: rgb(255, 106, 60); }
.bgcolor10, .bglightgrey  { color: rgb(156, 156, 156); }
.bgcolor11, .bgpink       { color: rgb(255, 160, 208); }
.bgcolor12, .bglightgreen { color: rgb(20, 245, 60); }
.bgcolor13, .bgyellow     { color: rgb(208, 221, 141); }
.bgcolor14, .bgaqua       { color: rgb(114, 255, 208); }
.bgcolor15, .bgwhite      { color: rgb(255, 255, 255); }

html, body, div, span, applet, object, iframe, h1, h2, h3, h4, h5, h6, p, blockquote, pre, a, abbr, acronym, address, big, cite, code, del, dfn, em, img, ins, kbd, q, s, samp, small, strike, strong, sub, sup, tt, var, b, u, i, center, dl, dt, dd, ol, ul, li, fieldset, form, label, legend, table, caption, tbody, tfoot, thead, tr, th, td, article, aside, canvas, details, embed,  figure, figcaption, footer, header, hgroup,  menu, nav, output, ruby, section, summary, time, mark, audio, video {
   margin: 0;
   padding: 0;
   border: 0;
   font: inherit;
   font-size: 100%;
   vertical-align: baseline;
   background-color: black;
   color: rgb(107, 107, 107);
}

main {
  font-weight: normal;
  font-family: monospace;
  font-size: 18px;
}

input {
  width: 640px;
  color: rgb(255, 68, 90);
  background-color: black;
}

#stdout {
  height: 480px;
  width: 680px;
  color: rgb(107, 107, 107);
  background: black;
}
</style>

<script>
// libc

let stdio = {
  out: () => document.querySelector('#stdout'),
  in: () => document.querySelector('input[name="stdin"]'),
  put: (str) => stdio.out().innerHTML += str,
  putln: (str) => stdio.out().innerHTML += (str + stdio.newline()),
  get: (str) => stdio.in().value,
  newline: () => "<br>"
};

// main

let M = (x) => Math.floor(x % 10000 / 1000),
    C = (x) => Math.floor(x / 100 % 10),
    V = (x) => Math.floor(x % 10 / 5),
    I = (x) => Math.floor(x % 10),
    L = (x) => Math.floor(x % 50 / 10),
    X = (x) => Math.floor(x / 10 % 10);

let f = (x) => {
  if (x == 0) return ".";
  if (M(x)) return "M".repeat(M(x));
  if (C(x)) return "C".repeat(C(x));
  if (V(x)) return "V";
  if (I(x)) return "I".repeat(I(x));
  if (L(x)) return "L".repeat(L(x));
  if (X(x)) return "X".repeat(X(x));
};

let setup = () => {
  stdio.out().classList.add("color8");
  stdio.out().classList.add("bgblack");
  stdio.in().classList.add("color14");
  stdio.in().classList.add("bgcolor0");
  stdio.in().value = "128";
}

function checkForm(n) {
	let input = n || stdio.in().value;
  stdio.putln(`${input}: ${f(input)}`);
}

setup();

stdio.putln("Roman numeral converter challenge by Dwight [email protected]:124/5017");
stdio.putln("");

checkForm();
checkForm(7);
checkForm(33);
checkForm(6);
</script>

Reward

Name entered into the quarterly drawing for prizes that include gift cards, tees, and more.

There is a problem with the code. Many clocks with Roman Numerals use “IIII” for the digit 4 instead of “IV”.
Clock

maybe there’s room for both?

That’s often the case with raised numbers because it allow the manufacturers to make a small set of digits(?) with five I’s, one V, and one X, using four sets of these to populate the face of the clock.

Using IIiI for four also helps in that IV and VI can be confusing when shown inverted at the bottom of the clock face.

1 Like

Ok guys, clocks are fun and all though this is a challenge about encoding. Feel free to post jsfiddle links below with one’s entry.

i didn’t want to do this in java. I wanted to brush up on some C# for a possible interview soon
aaaand caught a bug already! it was burning through the array too fast not allowing for repeat roman numerals. added an i++; to slow it down

1 Like

Nice job! Adding you into the hat for the drawing.

this is easy

So solve it in a weird way then :wink: make a rope and pulley computer that solves it or something~

This could be something to work on at the first DMS Coder Night this Saturday.

1 Like

cool ! I want to attend.

1 Like

Great! Instructions are Here: DMS Coder Night

Brute force ugly solution: Look up table as in array.
Index into array is number to be converted.
“Efficient” No coding bs needed.
Now what range did you want to cover?

1 Like

Nice try but 399 yields CCCLCIX and it SHOULD yield CCCXCIX…

1 Like

agh! good catch! thank you! that was a typo when i was building the array on line 12. fixed!

I want to express up to the 5th decimal point and include a 4 dimensional vector to allow for the i axis and irrational pi values… let’s make a galaxy of roman numerals! yeehaw!

Roman numerals to the right of the dp - how quaint…:grimacing:
KISS says Integers only.
But any input can be processed to such.
BTW just realized my solution is program language agnostic.
I win!

1 Like

Painting%20Blog%20Banner

Next Quarter’s Challenge will be based on IPFS and BadUSB so get to coding and brush up on WiFi Duck - DSTIKE Update Tutorial

1 Like