Overclock.net - An Overclocking Community - View Single Post - Programming Challenges!
View Single Post
post #93 of (permalink) Old 12-26-2008, 05:48 PM
mentholmoose
New to Overclock.net
 
mentholmoose's Avatar
 
Join Date: Jan 2007
Posts: 1,125
Rep: 130 (Unique: 117)
Here's the roman numerals one, in PHP:

Code:

<?php

$handle = fopen("roman.txt", "r");
$numbers_file = fread($handle, filesize("roman.txt"));
fclose($handle);

$numbers_array = explode("\
", $numbers_file);

foreach($numbers_array as $number) {
$number = preg_replace("/[\
\ ]/", "", $number);
print($number . ": " . strlen($number) . " characters beforehand.\
");
$old = strlen($number);
$number = str_replace("IIIII", "V", $number);
$number = str_replace("IIII", "IV", $number);
$number = str_replace("VIV", "IX", $number);
$number = str_replace("VV", "X", $number);
$number = str_replace("XXXXX", "L", $number);
$number = str_replace("XXXX", "XL", $number);
$number = str_replace("LXL", "XC", $number);
$number = str_replace("LL", "C", $number);
$number = str_replace("CCCCC", "D", $number);
$number = str_replace("CCCC", "CD", $number);
$number = str_replace("DCD", "CM", $number);
$number = str_replace("DD", "M", $number);
print($number . ": ". strlen($number) . " characters afterward.\
");
print("There were " . ($old - strlen($number)) . " characters saved.\
\
");
}
?>
It's verified working with the txt you provided. I think there are a couple of small cases where it won't, but none in your example file.

Jimmy Rollins, presented at your left, is impersonating the New York Mets.
mentholmoose is offline