<commit_before>-- https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
-- How to run: stack runhaskell ClimbingTheLaderboard.hs
import Control.Exception.Base(assert)
import qualified Data.Set as Set

readScores :: Int -> IO([Int])
readScores size = do
  line <- getLine
  let leaderboard = map (read :: String -> Int) $ words line
  assert (size == length leaderboard) (return leaderboard)

rankScore :: [Int] -> [Int] -> Int -> ([Int], Int)
rankScore initial [] score = (initial ++ [score], length initial)
rankScore initial (x:xs) score
  | score > x = (initial ++ score:x:xs, length initial)
  | score == x = (initial ++ x:xs, length initial)
  | score < x = rankScore (initial ++ [x]) xs score

getRanks :: [Int] -> [Int] -> [Int] -> [Int]
getRanks leaderboard [] ranks  = ranks
getRanks leaderboard (score:scores) ranks =
  let (newLeaderboard, rank) = rankScore [] leaderboard score
   in getRanks newLeaderboard scores (ranks ++ [rank])

main = do
  leaderboardSize <- readLn :: IO Int
  leaderboard <- readScores leaderboardSize
  aliceGamesSize <- readLn :: IO Int
  aliceGames <- readScores aliceGamesSize
  let leaderboardNoDuplicates = (Set.toDescList . Set.fromDescList) leaderboard
  let aliceRanks = getRanks leaderboardNoDuplicates aliceGames []
  let correctedIndicesRanks = map (+1) aliceRanks
  mapM_ print correctedIndicesRanks<commit_msg>Improve solution to O(n) complexity.<commit_after>-- https://www.hackerrank.com/challenges/climbing-the-leaderboard/problem
-- How to run: stack runhaskell ClimbingTheLaderboard.hs
import Control.Exception.Base(assert)
import qualified Data.Set as Set

readScores :: Int -> IO([Int])
readScores size = do
  line <- getLine
  let leaderboard = map (read :: String -> Int) $ words line
  assert (size == length leaderboard) (return leaderboard)

getRanks :: [Int] -> [Int] -> Int -> [Int]
getRanks [] scores index = replicate (length scores) index
getRanks leaderboard [] index = []
getRanks (x:xs) (score:scores) index
  | score < x = getRanks xs (score:scores) (index + 1)
  | score == x = index:getRanks (x:xs) scores index
  | score > x = index:getRanks (x:xs) scores index

main = do
  leaderboardSize <- readLn :: IO Int
  leaderboard <- readScores leaderboardSize
  let leaderboardSet = (Set.toDescList . Set.fromDescList) leaderboard
  aliceGamesSize <- readLn :: IO Int
  aliceGames <- readScores aliceGamesSize
  let aliceGamesDesc = reverse aliceGames
  let aliceRanks = getRanks leaderboardSet aliceGamesDesc 0
  let correctedIndicesRanks = map (+1) $ reverse aliceRanks
  mapM_ print correctedIndicesRanks
{'fraction_non_alphanumeric': 0.11608175858079445, 'fraction_numerical': 0.0015426147319706903, 'mean_word_length': 4.426778242677824, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 18, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4022724', 'n_tokens_mistral': 869, 'n_tokens_neox': 834, 'n_words': 250}