Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Roman number and a root of a number in Haskell #507

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions binary_search/root_of_a_number.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@

sqrtInt :: Int -> Int
sqrtInt x = sqrtInt' x 0 x
where sqrtInt' :: Int -> Int -> Int -> Int
sqrtInt' x low high
| low >= high = low
| middSq < x = sqrtInt' x (midd+1) high
| middSq > x = sqrtInt' x (low) (midd-1)
| middSq == x = midd
where midd = (high + low) `quot` 2
middSq = midd*midd

40 changes: 40 additions & 0 deletions roman/roman.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
module Main where

import Data.Bool

romanToInt :: Char -> Integer
romanToInt 'I' = 1
romanToInt 'V' = 5
romanToInt 'X' = 10
romanToInt 'L' = 50
romanToInt 'C' = 100
romanToInt 'D' = 500
romanToInt 'M' = 1000
romanToInt _ = 0


romanToNum :: [Char] -> Integer
romanToNum xs
| length xs == 0 = 0
| length xs == 1 = romanToInt (head xs)
| otherwise = let fst = romanToInt (head xs)
snd = romanToInt (xs !! 1)
t1 = romanToNum (tail xs)
in if fst < snd then t1 - fst
else t1 + fst

main :: IO()
main = putStrLn result

result :: [Char]
result = if final == False then "Your program is wrong"
else "Your program is working!!! "
where final = foldl (&&) True expr
expr :: [Bool]
expr = [e1, e2, e3, e4, e5]
e1 = (romanToNum "XVI") == 16
e2 = (romanToNum "VI") == 6
e3 = (romanToNum "CM") == 900
e4 = (romanToNum "CMXL") == 940
e5 = (romanToNum "XIII") == 13