[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y ] [Home]
4chanarchives logo
Programming pop quiz

You are currently reading a thread in /g/ - Technology

Thread replies: 11
Thread images: 2
File: 1423254418356.jpg (562 KB, 1980x1080) Image search: [Google]
1423254418356.jpg
562 KB, 1980x1080
Alright fags, time to see how many of you can actually program.

Your challenge:

Write a script that will convert natural numbers to Roman numerals.

Use any language you choose, no time limit.
>>
>>54942769
>hey /g/ please solve my project euler for me
numerals = {
'I': 1,
'V': 5,
'X': 10,
'L': 50,
'C': 100,
'D': 500,
'M': 1000
}

values = {
1: 'I',
4: 'IV',
5: 'V',
9: 'IX',
10: 'X',
40: 'XL',
50: 'L',
90: 'XC',
100: 'C',
400: 'CD',
500: 'D',
900: 'CM',
1000: 'M'
}

def expand(numeral):
i = value = 0
while i < len(numeral):
v = numerals[numeral[i]]

if i < len(numeral)-1 and v < numerals[numeral[i+1]]:
v = numerals[numeral[i+1]] - numerals[numeral[i]]
i += 1

i += 1
value += v

return value

def contract(value):
string = ""
remaining = value

for v in sorted(values.keys(), reverse=True):
while remaining >= v:
string += values[v]
remaining -= v

return string

>>
>>54942769
i'll do your homework for you op here it is:
see if its divisible by the biggest unit
if it is then divide it by that and take its remainder and repeat the process
if not, see if its dvisible by the next biggest unit
... and so on

easy shit just use your brain and think about it
>>
#include <iostream>
#include <string>
using namespace std;

string = "natural numbers";

cout << string << endl;

string = "Roman numerals";
cout << string;
>>
>>54942769
Also adding lines over numbers for multiplying past 10000?
>>
File: Sexy-marissa-mayer.jpg (76 KB, 270x333) Image search: [Google]
Sexy-marissa-mayer.jpg
76 KB, 270x333
>>54942806
>>54942828
OP here

Not homework, I'm not a CS student. It's an interview practice question I was working on earlier, just interested to see how /g/ handles it.
>>
>>54942841
Whatever you like m8
>>
>>54942858
i forgot about the thing where it subtracts depending on what side it's on. thats harder than i thought m8
>>
>>54942947
Yeah it's actually a surprising pain in the ass

I'm not a great programmer, but I'm definitely struggling with it. 0 to 9 is easy, after that it gets complex
>>
>>54943244
i suppose just try to implement the same algorithm you inexplicitly use when you're reading a roman numeral in your head. idk what to tell u maiman
>>
>>54943296
Huh, that's a good approach actually.

Maybe I should read more into algorithmic design, I know nothing about it
Thread replies: 11
Thread images: 2

[Boards: 3 / a / aco / adv / an / asp / b / biz / c / cgl / ck / cm / co / d / diy / e / fa / fit / g / gd / gif / h / hc / his / hm / hr / i / ic / int / jp / k / lgbt / lit / m / mlp / mu / n / news / o / out / p / po / pol / qa / r / r9k / s / s4s / sci / soc / sp / t / tg / toy / trash / trv / tv / u / v / vg / vp / vr / w / wg / wsg / wsr / x / y] [Home]

All trademarks and copyrights on this page are owned by their respective parties. Images uploaded are the responsibility of the Poster. Comments are owned by the Poster.
If a post contains personal/copyrighted/illegal content you can contact me at [email protected] with that post and thread number and it will be removed as soon as possible.
If a post contains illegal content, please click on its [Report] button and follow the instructions.
This is a 4chan archive - all of the content originated from them. If you need information for a Poster - you need to contact them.
This website shows only archived content and is not affiliated with 4chan in any way.
If you like this website please support us by donating with Bitcoin at 1XVgDnu36zCj97gLdeSwHMdiJaBkqhtMK