Skip to content

Instantly share code, notes, and snippets.

@maymax777
Last active January 18, 2023 15:12
Show Gist options
  • Save maymax777/511c9f978e0ca5e6ab7e4ed17a59954a to your computer and use it in GitHub Desktop.
Save maymax777/511c9f978e0ca5e6ab7e4ed17a59954a to your computer and use it in GitHub Desktop.
pragma solidity ^0.4.19;
contract Roman {
uint[] key = [1000, 900, 500, 400, 100, 90, 50, 40, 10, 9, 5, 4, 1];
string[] numerals = ["M", "CM", "D", "CD", "C", "XC", "L", "XL", "X", "IX", "V", "IV", "I"];
function solution(uint n) public view returns (string) {
// Convert the positive integer to a Roman Numeral
string memory res = "";
for (uint i = 0; i < key.length; i++) {
while (n >= key[i]) {
n -= key[i];
res = strConcat(res, numerals[i]);
}
}
return res;
}
function strConcat(string _a, string _b) internal pure returns (string){
bytes memory _ba = bytes(_a);
bytes memory _bb = bytes(_b);
string memory ret = new string(_ba.length + _bb.length);
bytes memory bret = bytes(ret);
uint k = 0;
for (uint i = 0; i < _ba.length; i++)bret[k++] = _ba[i];
for (i = 0; i < _bb.length; i++) bret[k++] = _bb[i];
return string(bret);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment