Code: Select all
Create a class called RomanConverter that will take in a regular, Arabic number, and output it in Roman numeral form. Input will be an integer greater than 0 and less than 2500. Hopefully this will help:
RN Number
I 1
V 5
X 10
L 50
C 100
D 500
M 1000
Sample input/output:
Enter Arabic number: 152
Equivalent Roman numeral: CLII Code: Select all
<?php
function read() {
$fp = fopen('php://stdin','r');
$line = fgets($fp, 4);
fclose($fp);
return trim($line);
}
function numeralizer($int) {
if (!preg_match('/^\d+$/', $int) || $int <= 0 || $int > 2500) return false;
$int_str = '';
while ($int > 0) {
if ($int >= 1000) {
$int_str .= 'M';
$int -= 1000;
} elseif ($int >= 500) {
$int_str .= 'D';
$int -= 500;
} elseif ($int >= 100) {
$int_str .= 'C';
$int -= 100;
} elseif ($int >= 50) {
$int_str .= 'L';
$int -= 50;
} elseif ($int >= 10) {
$int_str .= 'X';
$int -= 10;
} elseif ($int >= 5) {
$int_str .= 'V';
$int -= 5;
} elseif ($int >= 1) {
$int_str .= 'I';
$int -= 1;
}
}
return $int_str;
}
$output = numeralizer(read());
echo (($output === false || $output == '') ? 'Input invalid or an error occured' : $output)."\n";
?>Here is user peedek's solution. He used Java's maps, which was a good idea I never even considered. Input is done via the String[] argument of the main method. A bit long, but it's a neat trick:
Code: Select all
import java.util.Map;
import java.util.HashMap;
/**
* RomanConverter class.
* Converts a given positive integer into its roman numeral equivalent.
* @author peedekk - Paul Dekker
*/
public class RomanConverter {
private static Map<Integer, Character> romanMap;
static {
// Initialise roman numeral map
romanMap = new HashMap<Integer, Character>();
romanMap.put(1000, 'M');
romanMap.put(500, 'D');
romanMap.put(100, 'C');
romanMap.put(50, 'L');
romanMap.put(10, 'X');
romanMap.put(5, 'V');
romanMap.put(1, 'I');
}
public static String convert(int number) {
StringBuffer romanNumeral = new StringBuffer();
int theOneNum = 1000;
for (int i = 0; i < 4; i++) { // run through four times (1000, 100, 10, 1)
for (int j = 0; j < number/theOneNum; j++) {
romanNumeral.append(romanMap.get(theOneNum));
}
number = number % theOneNum;
if (theOneNum == 1) break; // check if just added the I's, & break out if so
int theNineNum = theOneNum - (theOneNum/10); //900, 90, 9
int theFiveNum = theOneNum / 2; //500, 50, 5
int theFourNum = theFiveNum - (theOneNum/10); //400, 40, 4
if (number >= theNineNum) {
romanNumeral.append(romanMap.get(theOneNum/10)).append(romanMap.get(theOneNum));
number -= theNineNum;
} else if (number >= theFiveNum) {
romanNumeral.append(romanMap.get(theFiveNum));
number -= theFiveNum;
} else if (number >= theFourNum) {
romanNumeral.append(romanMap.get(theOneNum/10)).append(romanMap.get(theFiveNum));
number -= theFourNum;
}
theOneNum /= 10; // divide theOneNum by 10 - move to next number (1000, 100, 10, 1)
}
return romanNumeral.toString();
}
/**
* After compiling the program (javac RomanConverter.java)
* Run the program with the positive integer as an argument
* (java RomanConverter [positive_integer])
* e.g. java RomanConverter 152
*
* @param args The positive integer to convert into Roman Numerals.
*/
public static void main(String[] args) {
if (args.length != 1) {
System.out.println("Invalid syntax.\njava RomanConverter arabic-number\nExample: java RomanConverter 152");
System.exit(1);
}
if (!args[0].matches("\\d+")) {
System.out.println("Error: Data entered is not a positive integer. '" + args[0] + "'");
System.exit(1);
}
int arabicNumber = Integer.parseInt(args[0]);
String romanNumeral = RomanConverter.convert(arabicNumber);
System.out.println("Resulting Roman Numeral: " + romanNumeral);
System.exit(0);
}
}Code: Select all
import java.util.Scanner;
public class RomanConverter
{
public static void main(String[] args)
{
System.out.println("Equivalent Roman numeral: "+convertToRoman(readInput()));
}
public static int[] readInput()
{
Scanner scan = new Scanner(System.in);
System.out.print("Enter Arabic number: ");
String line = scan.nextLine();
if (Integer.parseInt(line) >= 4000 || Integer.parseInt(line) <= 0)
{
System.out.println("Invalid input");
return new int[]{};
}
int[] output = new int[line.length()];
for (int i = 0; i < output.length; i++)
output[i] = Character.getNumericValue(line.charAt(line.length()-1-i));
return output;
}
public static String convertToRoman(int[] nums)
{
String[] romanTenValues = {"I", "X", "C", "M"};
String output = "";
for (int i = 0; i < nums.length; i++)
{
for (int j = 0; j < nums[i]; j++)
output += romanTenValues[i];
}
output = flipString(output);
output = output.replaceAll("IIIIIIIII", "IX");
output = output.replaceAll("IIIII", "V");
output = output.replaceAll("IIII", "IV");
output = output.replaceAll("XXXXXXXXX", "XC");
output = output.replaceAll("XXXXX", "L");
output = output.replaceAll("XXXX", "XL");
output = output.replaceAll("CCCCCCCCC", "CM");
output = output.replaceAll("CCCCC", "D");
output = output.replaceAll("CCCC", "CD");
return output;
}
public static String flipString(String in)
{
String out = "";
for (int i = in.length()-1; i >= 0; i--)
out += in.charAt(i);
return out;
}
}
And that's it for last week. Now for the current contest:
Code: Select all
Programming Challenge: Challenge 1 - Monday, May 14, 2007
Calendar Generator
Many times you will need to check what day of the week a certain date lies on, and you find yourself without a calendar. You think to yourself, man I knew I should've coded a calendar generator last week, and you end up having to get off your ass and look through every drawer in the house to find one.
Today we will put an end to this extremely widespread problem. Here is this week's challenge:
Given a MM/DD/YYYY input, generate a calendar of the month given, the previous month, and the following month. Also, print out what day of the week the given date is on. Here is some sample in/output.
Enter the date (MM/DD/YYYY): 05/14/1887
May 14, 1887 is a Saturday. Here is a very handy calendar:
April May June
S M T W T F S S M T W T F S S M T W T F S
1 2 1 2 3 4 5 6 7 1 2 3 4
3 4 5 6 7 8 9 8 9 10 11 12 13 14 5 6 7 8 9 10 11
10 11 12 13 14 15 16 15 16 17 18 19 20 21 12 13 14 15 16 17 18
17 18 19 20 21 22 23 22 23 24 25 26 27 28 19 20 21 22 23 24 25
24 25 26 27 28 29 30 29 30 31 26 27 28 29 30
Good luck
-Joe
