Task 1: Roman Calculator
Write a script to implement a Roman Calculator that performs arithmetic operations on Roman numerals and outputs the result in Roman numerals.
Example Output
Input: "V", "+", "VI"
Output: "XI"
Logic
Convert the input Roman numerals to Arabic (decimal) numbers, perform the required math operation, and then convert the result back to Roman numerals.
Perl Solution
ch-1.pl
use Roman;
sub roman_magic {
my ($v1, $op, $v2) = @_;
my $rom_v1 = arabic($v1);
my $rom_v2 = arabic($v2);
return uc(roman(eval "$rom_v1 $op $rom_v2"))
}
Python Solution
ch-1.py
from roman import toRoman, fromRoman
class RomanConvertor:
def roman_magic(self, v1, op, v2):
rom_v1 = str(fromRoman(v1))
rom_v2 = str(fromRoman(v2))
operation = rom_v1+op+rom_v2
return toRoman(eval(operation))
Task 2: Gapful Numbers
Write a script to generate the first 20 Gapful Numbers. A gapful number is a number of at least 3 digits that is divisible by the number formed by its first and last digit.
Example Output
100, 105, 108, 110, 120, 121, 130, 132, 135, 140, 143, 150, 154, 160, 165, 170, 176, 180, 187, 190
Logic
Start from 100, parse the first and last digits, concatenate them to form a two-digit divisor, and check if the original number is divisible by this divisor. Keep track of the count and stop when 20 numbers are found.
Perl Solution
ch-2.pl
my $count = 0;
for (100..10000) {
if ($_ % int(substr($_,0,1).substr($_,2,1)) == 0) {
say;
$count++;
last if $count == 20;
}
}
Python Solution
ch-2.py
count = 0
for i in range(100, 10001):
if i % int(str(i)[0]+str(i)[2]) == 0:
print(i)
count += 1
if count == 20:
break