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

Programming pop quiz

This is a blue board which means that it's for everybody (Safe For Work content only). If you see any adult content, please report it.

Thread replies: 11
Thread images: 2

File: 1423254418356.jpg (562KB, 1980x1080px) Image search: [Google]
1423254418356.jpg
562KB, 1980x1080px
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 (76KB, 270x333px) Image search: [Google]
Sexy-marissa-mayer.jpg
76KB, 270x333px
>>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 posts: 11
Thread images: 2


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

I'm aware that Imgur.com will stop allowing adult images since 15th of May. I'm taking actions to backup as much data as possible.
Read more on this topic here - https://archived.moe/talk/thread/1694/


If you need a post removed click on it's [Report] button and follow the instruction.
DMCA Content Takedown via dmca.com
All images are hosted on imgur.com.
If you like this website please support us by donating with Bitcoins at 16mKtbZiwW52BLkibtCr8jUg2KVUMTxVQ5
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.
This is a 4chan archive - all of the content originated from that site.
This means that RandomArchive shows their content, archived.
If you need information for a Poster - contact them.