Validate International Securities Identification Number: Difference between revisions

→‎{{header|Haskell}}: added missing test case to 1st version (+ minor linting)
(→‎{{header|Haskell}}: Added alternative approach)
(→‎{{header|Haskell}}: added missing test case to 1st version (+ minor linting))
Line 826:
 
=={{header|Haskell}}==
<lang Haskell>module ISINVerification2 where
 
where
import Data.Char ( isUpper , isDigit , digitToInt )
 
verifyISIN :: String -> Bool
verifyISIN isin = correctFormat isin && mod (oddsum + multiplied_even_sum) 10 == 0
correctFormat isin && mod (oddsum + multiplied_even_sum) 10 == 0
where
where
reverted = reverse $ convertToNumber isin
theOddsreverted = fstreverse $ collectOddandEvenconvertToNumber revertedisin
theEvenstheOdds = sndfst $ collectOddandEven reverted
oddsumtheEvens = sumsnd $ map digitToIntcollectOddandEven theOddsreverted
multiplied_even_sumoddsum = addUpDigitssum $ map ( (* 2 ) . digitToInt ) theEvenstheOdds
multiplied_even_sum = addUpDigits $ map ((* 2) . digitToInt) theEvens
 
capitalLetters :: [Char]String
capitalLetters = ['A' , 'B' .. 'Z']
 
numbers :: [Char]String
numbers = ['0' , '1' , '2', '3' , '4' , '5', '6' , '7' , '8' , '9' ]
 
correctFormat :: String -> Bool
correctFormat isin = (length isin == 12 ) && ( all (\b -> elem b capitalLetters ) $ take 2 isin)
(length isin == 12) &&
&& (all (\c -> elem c capitalLetters || elem c numbers) $ drop 2 $ take 11 isin)
&&all (`elem` capitalLetters) (take last2 isin ) numbers) &&
&& (all (\c -> elem c capitalLetters || elem c numbers) $ (drop 2 $ take 11 isin) &&
elem (last isin) numbers
 
convertToNumber :: String -> String
convertToNumber str = concat $ mapconcatMap convert str
where
convert :: Char -> String
convert c =
convert c = if isDigit c then show $ digitToInt c else show ( fromEnum c - 55 )
if isDigit c
then show $ digitToInt c
collectOddandEven :: String -> (String , String )
convert c = if isDigit c then show $ digitToInt c else show ( fromEnum c - 55 )
collectOddandEven term
|odd $ length term = (concat [take 1 $ drop n term | n <- [0,2..length term - 1]] ,
concat [take 1 $ drop d term | d <- [1,3..length term - 2]] )
|otherwise = (concat [take 1 $ drop n term | n <- [0,2..length term -2]] ,
concat [take 1 $ drop d term | d <- [1,3..length term - 1]] )
 
collectOddandEven :: String -> (String , String )
collectOddandEven term
| odd $ length term =
( concat
[ take 1 $ drop n term
|odd $ length term = (concat [take 1 $ drop n term | n <- [0,2 .. length term - 1] ] ,
, concat
[ take 1 $ drop d term
concat [take 1 $ drop d term | d <- [1,3 .. length term - 2]] ])
| otherwise =
( concat
[ take 1 $ drop n term
|otherwise = (concat [take 1 $ drop n term | n <- [0,2 .. length term - 2] ] ,
, concat
[ take 1 $ drop d term
concat [take 1 $ drop d term | d <- [1,3 .. length term - 1]] ])
 
addUpDigits :: [Int] -> Int
addUpDigits list =
addUpDigits list = sum $ map (\d -> if d > 9 then sum $ map digitToInt $ show d else d ) list
sum $
map
(\d ->
if d > 9
addUpDigits list = sum $ map (\d -> if d > 9 then sum $ map digitToInt $ show d else d ) list
else d)
list
 
printSolution :: String -> IO ( )
printSolution str = do
putStr $ str ++ " is"
if verifyISIN str == True then putStrLn " valid" else putStrLn " not valid"
then putStrLn " valid"
else putStrLn " not valid"
 
main :: IO ( )
main = do
let isinnumbers = ["US0378331005" , "US0373831005" , "US03378331005" , "AU0000XVGZA3" ,
[ "US0378331005"
"AU0000VXGZA3" , "FR0000988040"]
, "US0373831005"
mapM_ printSolution isinnumbers</lang>
, "U50378331005"
, "US03378331005"
, "AU0000XVGZA3"
, "AU0000VXGZA3"
"AU0000VXGZA3" , "FR0000988040"]
]
mapM_ printSolution isinnumbers</lang>
 
{{out}}
<pre>US0378331005 is valid
US0373831005 is not valid
U50378331005 is not valid
US03378331005 is not valid
AU0000XVGZA3 is valid
AU0000VXGZA3 is valid
FR0000988040 is valid</pre>
</pre>
 
Or, making alternative choices from standard libraries:
Line 914 ⟶ 945:
(show =<<) . fromMaybe [] . sequence . fmap (`M.lookup` charMap)
 
capitals, digits :: String
capitals = ['A' .. 'Z']
 
digits :: String
digits = ['0' .. '9']
 
9,659

edits