Skip to content

Commit

Permalink
Roman numbers
Browse files Browse the repository at this point in the history
  • Loading branch information
wolandark committed Sep 29, 2024
1 parent 461e6d6 commit 18568c1
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 0 deletions.
68 changes: 68 additions & 0 deletions roman_to_hindi/roman.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#!/bin/bash
# vim: foldmethod=marker

function romanToArabic {
local input=$1
local result=0
local prevChar=""
local currChar=""
local currValue=0
local prevValue=0

for ((i=0; i<${#input}; i++)); do
currChar="${input:i:1}"

case $currChar in
"I") currValue=1 ;;
"V") currValue=5 ;;
"X") currValue=10 ;;
"L") currValue=50 ;;
"C") currValue=100 ;;
"D") currValue=500 ;;
"M") currValue=1000 ;;
*) continue ;;
esac
# Comment{{{
# For numbers such as IV
# The loop first executes the else block
# since there is no prevValue yet.
# so 1 is added to the result variable
# but in the case of IV and such the second iteration
# executes the if block, and so we have to substract 2
# from the result variable. 1 for the incorrect addition
# and 1 for the current number.
# }}}
if ((prevValue < currValue)); then
result=$((result + currValue - 2 * prevValue))
else
result=$((result + currValue))
fi

prevChar="$currChar"
prevValue="$currValue"
done

echo "$result"
}

if [[ -z "$1" ]]; then
echo "Usage: $0 <inputFile_or_romanNumerals>"
exit 1
fi

if [[ -f "$1" ]]; then
inputFile="$1"

while IFS= read -r line; do
eval "line=$(echo "$line" | sed -E 's/([IVXLCDM]+)/$(romanToArabic "\1")/g')"
echo "$line"
done < "$inputFile" > "$inputFile.tmp"

mv "$inputFile.tmp" "$inputFile"

echo "Roman numerals converted in $inputFile"
else
romanNumerals="$1"
arabicNumber=$(romanToArabic "$romanNumerals")
echo "Roman numerals '$romanNumerals' converted to: $arabicNumber"
fi
10 changes: 10 additions & 0 deletions roman_to_hindi/test.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
I
II
III
IV
V
VI
VII
VIII
IX
X

0 comments on commit 18568c1

Please sign in to comment.