Skip to content

Instantly share code, notes, and snippets.

@ostapenko-me
Last active February 25, 2017 11:03
Show Gist options
  • Select an option

  • Save ostapenko-me/dce032d74d08586a5c2c3433bcc43c2c to your computer and use it in GitHub Desktop.

Select an option

Save ostapenko-me/dce032d74d08586a5c2c3433bcc43c2c to your computer and use it in GitHub Desktop.

Revisions

  1. @kopte3 kopte3 revised this gist Feb 25, 2017. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fromRoman-one-line.js
    Original file line number Diff line number Diff line change
    @@ -4,7 +4,7 @@ const numbersDict = {
    I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000
    };

    const fromRoman = input => [...input.toUpperCase()].reduce((number, letter, i, source) => number += (numbersDict[letter] >= ( numbersDict[source[i + 1]] | 0)) ? numbersDict[letter] : -numbersDict[letter], 0);
    const fromRoman = input => [...input.toUpperCase()].reduce((number, letter, i, source) => number + (numbersDict[letter] >= ( numbersDict[source[i + 1]] | 0)) ? numbersDict[letter] : -numbersDict[letter], 0);

    assert.equal(fromRoman('I'), 1, 'I=' + fromRoman('I'));
    assert.equal(fromRoman('XI'), 11, 'XI=' + fromRoman('XI'));
  2. @kopte3 kopte3 created this gist Feb 25, 2017.
    19 changes: 19 additions & 0 deletions fromRoman-one-line.js
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,19 @@
    const assert = require('assert');

    const numbersDict = {
    I: 1, V: 5, X: 10, L: 50, C: 100, D: 500, M: 1000
    };

    const fromRoman = input => [...input.toUpperCase()].reduce((number, letter, i, source) => number += (numbersDict[letter] >= ( numbersDict[source[i + 1]] | 0)) ? numbersDict[letter] : -numbersDict[letter], 0);

    assert.equal(fromRoman('I'), 1, 'I=' + fromRoman('I'));
    assert.equal(fromRoman('XI'), 11, 'XI=' + fromRoman('XI'));
    assert.equal(fromRoman('XXI'), 21, 'XXI=' + fromRoman('XXI'));
    assert.equal(fromRoman('XXVI'), 26, 'XXVI=' + fromRoman('XXVI'));
    assert.equal(fromRoman('MMMD'), 3500, 'MMMD=' + fromRoman('MMMD'));

    assert.equal(fromRoman('XIX'), 19, 'XIX=' + fromRoman('XIX'));
    assert.equal(fromRoman('IV'), 4, 'IV=' + fromRoman('IV'));
    assert.equal(fromRoman('IX'), 9, 'IX=' + fromRoman('IX'));
    assert.equal(fromRoman('MMCD'), 2400, 'MMCD=' + fromRoman('MMCD'));
    assert.equal(fromRoman('MMCM'), 2900, 'MMCM=' + fromRoman('MMCM'));