Skip to content

Instantly share code, notes, and snippets.

@LukasPietzschmann
Last active July 26, 2024 21:54
Show Gist options
  • Save LukasPietzschmann/0ef02344ac83500f48fa2505de7d5f27 to your computer and use it in GitHub Desktop.
Save LukasPietzschmann/0ef02344ac83500f48fa2505de7d5f27 to your computer and use it in GitHub Desktop.
A small script to determine your final grade :)
{-# LANGUAGE LambdaCase #-}
import Data.List (sort)
import Control.Monad (unless)
data Grade = Grade {
grade :: Float,
credits :: Int
}
instance Show Grade where
show (Grade g c) = show g ++ " (" ++ show c ++ "CP)"
getBestGradesWithNCPs :: Int -> [Grade] -> [Float]
getBestGradesWithNCPs n = take n . sort . concatMap normalize
normalize :: Grade -> [Float]
normalize (Grade g c) = replicate c g
readGrade :: IO (Maybe Grade)
readGrade = parseGrade <$> getLine
parseGrade :: String -> Maybe Grade
parseGrade s = case words s of
[g, c] -> Just $ Grade (read g) (read c)
_ -> Nothing
readGrades :: IO [Grade]
readGrades = do
line <- getLine
case parseGrade line of
Just grade -> (:) grade <$> readGrades
Nothing -> return []
changeNth :: Int -> a -> [a] -> [a]
changeNth n x xs = take n xs ++ (x : drop (n + 1) xs)
removeNth :: Int -> [a] -> [a]
removeNth _ [] = []
removeNth 0 (_:xs) = xs
removeNth i (x:xs) = x:removeNth (i-1) xs
calc :: [Grade] -> Float
calc grades = do
let ma = filter ((==30) . credits) grades
let bestGrades = getBestGradesWithNCPs 74 grades
if length ma == 1 then do
let normalizedMa = normalize $ head ma
sum (bestGrades ++ normalizedMa) / fromIntegral (length bestGrades + length normalizedMa)
else sum bestGrades / fromIntegral (length bestGrades)
loop grades = do
print "What do you want to do? r: print result, q: quit, i: insert grade, d: delete grade, c: change grade, l: list grades"
mode :: Char <- getChar
putStrLn ""
case mode of
'q' -> return ()
'l' -> print grades >> loop grades
'r' -> print (calc grades) >> loop grades
'i' -> do
print "Enter the grade in the format 'grade credits' (e.g. '1.0 6'):"
readGrade >>= \case
Just grade -> loop $ grades ++ [grade]
Nothing -> print "Invalid input" >> loop grades
'd' -> do
print "Enter the index you want to delete:"
i :: Int <- read <$> getLine
loop $ removeNth i grades
'c' -> do
print "Enter the index you want to change:"
i :: Int <- read <$> getLine
print "Enter the new grade in the format 'grade credits' (e.g. '1.0 6'):"
readGrade >>= \case
Just grade -> loop $ changeNth i grade grades
Nothing -> print "Invalid input" >> loop grades
main = do
print "Enter your grades line by line in the format 'grade credits' (e.g. '1.0 6'):"
grades <- readGrades
print "Thanks!"
loop grades
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment