Created
April 21, 2018 15:26
-
-
Save p0358/0134686b9cedb80ddda045d0feb4b4ce to your computer and use it in GitHub Desktop.
Check which of the roman numbers are valid Polish words
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
// https://sjp.pl/slownik/growy/ | |
// Firstly, put into separate file only those words that contain only letters making up roman numbers | |
$handle = fopen("slowa.txt", "r"); | |
$handle_write = fopen("jakby_rzymskie.txt", "w"); | |
if ($handle) { | |
while (($line = fgets($handle)) !== false) { | |
// process the line read. | |
// ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] | |
if (trim(str_replace(['m', 'cm', 'd', 'cd', 'c', 'xc', 'l', 'xl', 'x', 'ix', 'v', 'iv', 'i'], '', $line)) == '') { | |
fputs($handle_write, $line); | |
echo $line; | |
} | |
} | |
fclose($handle); | |
} else { | |
// error opening the file. | |
} | |
fclose($handle_write); | |
///////////////////////////////////////////////////////////////////// | |
function numberToRomanRepresentation($number) { | |
$map = array('M' => 1000, 'CM' => 900, 'D' => 500, 'CD' => 400, 'C' => 100, 'XC' => 90, 'L' => 50, 'XL' => 40, 'X' => 10, 'IX' => 9, 'V' => 5, 'IV' => 4, 'I' => 1); | |
$returnValue = ''; | |
while ($number > 0) { | |
foreach ($map as $roman => $int) { | |
if($number >= $int) { | |
$number -= $int; | |
$returnValue .= $roman; | |
break; | |
} | |
} | |
} | |
return $returnValue; | |
} | |
$rzymskie = []; | |
foreach (range(1, 200000) as $liczba) { | |
$rzymskie[$liczba] = strtolower(numberToRomanRepresentation($liczba)); | |
} | |
$handle = fopen("jakby_rzymskie.txt", "r"); | |
if ($handle) { | |
while (($line = fgets($handle)) !== false) { | |
// process the line read. | |
// ['M', 'CM', 'D', 'CD', 'C', 'XC', 'L', 'XL', 'X', 'IX', 'V', 'IV', 'I'] | |
foreach ($rzymskie as $liczba => $rzym) { | |
if ($rzym == trim($line)) { | |
echo $liczba . ' ' . $rzym . PHP_EOL; | |
} | |
} | |
} | |
fclose($handle); | |
} else { | |
// error opening the file. | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment