language: Haskell (ghc-6.8.2)
date: 113 days 19 hours ago
link:
visibility: public
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
module Main where
 
import Control.Applicative 
import Control.Monad
import Data.Char
import Data.List
import Text.ParserCombinators.ReadP
import Text.Printf
 
type Name    = String
type Marks   = [Int] 
type AvgMark = Float
 
data Student = Student Name Marks deriving (Show)
 
nameP :: ReadP String
nameP = many1 $ satisfy isLetter
 
markP :: ReadP Int
markP = read <$> many1 (satisfy isDigit)
 
lexeme :: ReadP a -> ReadP a 
lexeme p = do 
    l <- p
    skipSpaces
    return l
 
studentP :: ReadP Student
studentP = liftM2 Student (lexeme nameP) (count 3 $ lexeme markP)
 
parseStudent :: String -> Maybe Student
parseStudent s = case readP_to_S studentP s of
                    []  -> Nothing
                    a:_ -> Just $ fst a    
   
avg :: Integral a => [a] -> Float
avg xs = (sum $ fromIntegral <$> xs) / (fromIntegral $ length xs)
              
showStudent (Student name marks) = printf "%s %.1f" name $ avg marks 
                    
main = do
    putStrLn "Введите число студентов:" 
    n <- readLn 
    when (n < 0) $ fail "Нужно положительное число!"
    when (n > 7) $ fail "Не больше 7!"
    putStrLn "Введите имена и оценки:"
    students <- sortBy (\(Student _ m1) (Student _ m2) -> compare (avg m2) (avg m1)) <$> replicateM n (ensure =<< parseStudent <$> getLine)
    putStrLn "Достаточно."
    putStrLn "список студентов:"
    forM_ students $ \s -> putStrLn $ showStudent s
    return ()
    where
        ensure s = case s of
            Just a  -> return a
            Nothing -> fail "Нихуя не могу разобрать что ты там написал."
  • upload with new input
  • result: Runtime error     time: 0.01s    memory: 3852 kB     signal: -1

    3
    YOBA 3 3 4
    ETO 2 2 1
    TI? 4 4 5
    2548B5 G8A;> ABC45=B>2:
    2548B5 8<5=0 8 >F5=:8:
    
    prog: user error (8ECO =5 <>3C @07>1@0BL GB> BK B0< =0?8A0;.)