Created
January 2, 2025 17:26
-
-
Save evgenii-malov/ad57a00c7fbb86a9825b1ab30315a011 to your computer and use it in GitHub Desktop.
hackerrank random-number-generator (with desmos visualisation)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
{- | |
random-number-generator: https://www.hackerrank.com/challenges/random-number-generator | |
Author: Evgeniy Malov <[email protected]> | |
Date: Jan 1, 2025 | |
join me : https://www.youtube.com/@EvgeniyMalov | |
https://ru.linkedin.com/in/deepwalk | |
-} | |
{-# LANGUAGE DuplicateRecordFields, FlexibleInstances, UndecidableInstances #-} | |
module Main where | |
import Control.Monad | |
import Data.Array | |
import Data.Bits | |
import Data.List | |
import Data.List.Split | |
import Data.Set | |
import Data.Text | |
import Debug.Trace | |
import System.Environment | |
import System.IO | |
import System.IO.Unsafe | |
-- | |
-- Complete the 'solve' function below. | |
-- | |
-- The function is expected to return a STRING. | |
-- The function accepts following parameters: | |
-- 1. INTEGER a | |
-- 2. INTEGER b | |
-- 3. INTEGER c | |
-- | |
solve a b c | |
| a < b = solve' a b c | |
| otherwise = solve' b a c | |
solve' a b c | |
| (c <= a) = f (c*c) (2*total_area) | |
| (a < c) && (c <= b ) = let | |
tb = c | |
ta = c - a | |
h = a | |
in f ((ta+tb)*h) (2*total_area) -- trapezoid area | |
| (b < c) && (c <= a + b) = let | |
dy = c - b | |
dx = (c-a) | |
dx' = b - dx | |
dy' = a - dy | |
trng_sqr_area = dx'*dy' | |
in f (2*total_area - trng_sqr_area) (2*total_area) | |
| otherwise = f 1 1 | |
where | |
f :: Int -> Int -> String | |
f numerator denominator = let g = gcd numerator denominator | |
in (show $ numerator `div` g) ++ "/" ++ (show $ denominator `div` g) | |
total_area = a*b | |
-- Write your code here | |
lstrip = Data.Text.unpack . Data.Text.stripStart . Data.Text.pack | |
rstrip = Data.Text.unpack . Data.Text.stripEnd . Data.Text.pack | |
main :: IO() | |
main = do | |
stdout <- getEnv "OUTPUT_PATH" | |
fptr <- openFile stdout WriteMode | |
nTemp <- getLine | |
let n = read $ lstrip $ rstrip nTemp :: Int | |
forM_ [1..n] $ \n_itr -> do | |
firstMultipleInputTemp <- getLine | |
let firstMultipleInput = Data.List.words $ rstrip firstMultipleInputTemp | |
let a = read (firstMultipleInput !! 0) :: Int | |
let b = read (firstMultipleInput !! 1) :: Int | |
let c = read (firstMultipleInput !! 2) :: Int | |
let result = solve a b c | |
hPutStrLn fptr result | |
hFlush fptr | |
hClose fptr | |
-- some helpfull drawings of probability cases | |
-- https://www.desmos.com/calculator/ef0bqflcbr?lang=ru | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment