The New “Handmade”
-
[image: Screen Shot 2013-05-19 at 5.39.54 PM]Amid grumblings of a “general
fatigue” when it comes to software-based startups, a potentially
transformative ...
13 minutes ago
Random commentary about C++, STL, Boost, Perl, Python, Algorithms, Problem Solving and Web Search
const unsigned MULTIPLY_BASE = 10;
void multiplyLittleEndian(const vector < unsigned > &a, const vector < unsigned > &b, vector < unsigned > &r)
{
r.assign(a.size()+b.size(), 0);
unsigned ri=0;
for(unsigned ai=0; ai < a.size(); ++ai)
{
unsigned cri = ri++, carry = 0, am = a[ai], cr;
for(unsigned bi=0; bi < b.size(); ++bi, ++cri)
{
cr = carry + am*b[bi] + r[cri];
r[cri] = cr % MULTIPLY_BASE;
carry = cr / MULTIPLY_BASE;
}
while(carry)
{
cr = carry + r[cri];
r[cri++] = cr % MULTIPLY_BASE;
carry = cr / MULTIPLY_BASE;
}
}
while((!r.empty()) && r.back() == 0) r.pop_back(); // trim result
}
mapping = {
'I' : 1,
'V' : 5,
'X' : 10,
'L' : 50,
'C' : 100,
'D' : 500,
'M' : 1000,
}
def roman(string):
prev = 0
totalsum = 0
for c in string:
if mapping[c] <= prev:
totalsum = totalsum + mapping[c]
else:
totalsum = totalsum - prev
totalsum = totalsum + mapping[c] - prev
prev = mapping[c]
print(totalsum)
roman("XCIX")