diff options
| author | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-07-31 18:31:44 +0100 |
|---|---|---|
| committer | Mohammad Sajid Anwar <mohammad.anwar@yahoo.com> | 2024-07-31 18:31:44 +0100 |
| commit | ff14617cc413ec1f81cb77b1db8e28310146c36d (patch) | |
| tree | a0d6e742b53d76d968c103d6cc4b1da7703a4b94 | |
| parent | 67ea9651b2a0cd0f1928bfa02c73d01830973808 (diff) | |
| download | perlweeklychallenge-club-ff14617cc413ec1f81cb77b1db8e28310146c36d.tar.gz perlweeklychallenge-club-ff14617cc413ec1f81cb77b1db8e28310146c36d.tar.bz2 perlweeklychallenge-club-ff14617cc413ec1f81cb77b1db8e28310146c36d.zip | |
- Updated solution by Ulrich Rieke.
| -rwxr-xr-x | challenge-280/ulrich-rieke/haskell/ch-1.hs | 45 |
1 files changed, 26 insertions, 19 deletions
diff --git a/challenge-280/ulrich-rieke/haskell/ch-1.hs b/challenge-280/ulrich-rieke/haskell/ch-1.hs index 6c48b547ee..346e604a88 100755 --- a/challenge-280/ulrich-rieke/haskell/ch-1.hs +++ b/challenge-280/ulrich-rieke/haskell/ch-1.hs @@ -1,19 +1,26 @@ -module Challenge280
- where
-import qualified Data.Set as S
-import Data.List ( findIndices , sortOn )
-
-solution :: String -> Char
-solution s = if any ( (>= 2) . length . snd ) indices then fst $ head $
- sortOn ( last . snd ) $ filter ( (> 1 ) . length . snd ) indices else ' '
- where
- indices = map (\c -> (c , findIndices (== c ) s ) ) $ S.toList $
- S.fromList s
-
-main :: IO ( )
-main = do
- putStrLn "Enter a word of lowercase letters only!"
- line <- getLine
- print $ solution line
-
-
+module Challenge280 + where +import qualified Data.Set as S +import Data.List ( findIndices , sortOn , (!!) ) + +--the solution is as follows : pair up every character with the list +--of indices where it can be found in the string. We use unique +--characters for that. Then , we look for the character that occurs +--more than once in the string by filtering all pairs that have more +--than one element in the associated list. The character we look for +--is the one with the smallest index number at position 2 corresponding +--to index 1 , so we sort on that index + +solution :: String -> Char +solution s = if any ( (>= 2) . length . snd ) indices then fst $ head $ + sortOn (\p -> (snd p) !! 1 ) $ filter ( (> 1 ) . length . snd ) indices + else ' ' + where + indices = map (\c -> (c , findIndices (== c ) s ) ) $ S.toList $ + S.fromList s + +main :: IO ( ) +main = do + putStrLn "Enter a word of lowercase letters only!" + line <- getLine + print $ solution line |
