ichko / fmi-fp-2020-21

Functional Programming Practicum at FMI 2020-21
https://ichko.github.io/fmi-fp-2020-21
5 stars 1 forks source link

Exercises week 2 #5

Open ichko opened 4 years ago

ichko commented 4 years ago

Тук може да споделяте решенията си на задачите от седмица 2. Самите задачи са дефинирани тук, а тук може да намерите нашите решения.

Публикувайте решенията като коментари в това issue или като линкове към ваши хранилища/gist. Заградете кода на решението си по следния начин за да се highlight-не правилно (ако поствате коментар с кода тук):

```hs
-- solution
```
Ivaylo-Valentinov commented 3 years ago
transpose matrix = tail (iter ([], matrix) [])
  where
    iter (newRow, curMatrix) result
      | curMatrix == [] = result
      | otherwise = iter (helper curMatrix [] []) (result ++ [newRow])
      where
        helper matrix row newMatrix =
          case matrix of
            [] -> (row, newMatrix)
            ([] : _) -> ([], [])
            ((ah : at) : t) -> helper t (row ++ [ah]) (newMatrix ++ [at])

contains [] _ = False
contains (h : t) a
  | h == a = True
  | otherwise = contains t a

union s1 s2 = iter s1 s2 []
  where
    iter s1 s2 res
      | s1 == [] && s2 == [] = res
      | s1 /= [] && not (contains res h1) = iter t1 s2 (h1 : res)
      | s2 /= [] && not (contains res h2) = iter s1 t2 (h2 : res)
      | s1 /= [] = iter t1 s2 res
      | s2 /= [] = iter s1 t2 res
      where
        (h1 : t1) = s1
        (h2 : t2) = s2

intersect s1 s2 = iter s1 s2 []
  where
    iter s1 s2 res
      | s1 == [] || s2 == [] = res
      | s2 /= [] && not (contains res h2) && (contains s1 h2) = iter s1 t2 (h2 : res)
      | s2 /= [] = iter s1 t2 res
      where
        (h2 : t2) = s2

intToString n = if n == 0 then "0" else iter n []
  where
    iter num str
      | num == 0 = str
      | otherwise = iter (div num 10) ((helper (mod num 10)) : str)
      where
        helper digit
          | digit == 0 = '0'
          | digit == 1 = '1'
          | digit == 2 = '2'
          | digit == 3 = '3'
          | digit == 4 = '4'
          | digit == 5 = '5'
          | digit == 6 = '6'
          | digit == 7 = '7'
          | digit == 8 = '8'
          | digit == 9 = '9'

stringToInt str = iter str 0
  where
    iter str res =
      case str of
        "" -> res
        (h : t) -> iter t (res * 10 + (helper h))
      where
        helper digit
          | digit == '0' = 0
          | digit == '1' = 1
          | digit == '2' = 2
          | digit == '3' = 3
          | digit == '4' = 4
          | digit == '5' = 5
          | digit == '6' = 6
          | digit == '7' = 7
          | digit == '8' = 8
          | digit == '9' = 9

generateListToPrint = iter 100 1 []
  where
    iter cur final res
      | cur < final = res
      | (mod cur 3 == 0) && (mod cur 5 == 0) = iter (cur - 1) final ("FooBar" : res)
      | mod cur 3 == 0 = iter (cur - 1) final ("Foo" : res)
      | mod cur 5 == 0 = iter (cur - 1) final ("Bar" : res)
      | otherwise = iter (cur - 1) final ((intToString cur) : res)
ichko commented 3 years ago

@Ivaylo-Valentinov

Благодаря че събмитна :star2:

dimitroffangel commented 3 years ago
import Data.Char

fooBar = 
    iterateHelperFooBar 1 []
    where
    helperFooBar n 
        | n `mod` 15 == 0 = "FooBar"
        | n `mod` 3 == 0 = "Foo"
        | n `mod` 5 == 0 = "Bar"
        | otherwise = show n
    iterateHelperFooBar index result 
        | index == 101 = result
        | otherwise = iterateHelperFooBar (index + 1) (result ++ [helperFooBar index ])

quicksort [] = []
quicksort (xHead : xTail) = 
    quicksort lesser ++ (xHead : quicksort greater)
    where 
        lesser = filter (< xHead) xTail
        greater = filter (>= xHead) xTail 

createIthElementList (head: tail) index result
    | null tail = result ++ [head !! index]
    | otherwise = createIthElementList tail index (result ++ [ head !! index ])

assertElementLength [] = False
assertElementLength list = 
    assertElementLengthHelper list 0 (length (head list))
    where
        assertElementLengthHelper list index currentLength 
            | index == length list = True
            | length (list !! index) /= currentLength = False
            | otherwise = assertElementLengthHelper list (index + 1) currentLength

transposeList list = 
    transposeListHelper list 0 []
    where 
        transposeListHelper list index result
            | not (assertElementLength list) = []
            | index == length (head list) = result
            | otherwise = transposeListHelper list (index + 1) (result ++ [createIthElementList list index []])

mergeReversed [] [] result = result
mergeReversed (xHead : xTail) [] result
    | result /= [] && (head result == xHead) = mergeReversed xTail [] result
    | otherwise = mergeReversed xTail [] (xHead : result)
mergeReversed [] (xHead : xTail) result 
    | result /= [] && (head result == xHead) = mergeReversed [] xTail result
    | otherwise = mergeReversed [] xTail (xHead : result)
mergeReversed (lhsHead : lhsRest) (rhsHead : rhsRest) result
    | lhsHead < rhsHead && result /= [] && (head result == lhsHead) = mergeReversed lhsRest rhsRest result 
    | lhsHead >= rhsHead && result /= [] && (head result == rhsHead) = mergeReversed lhsRest rhsRest result
    | lhsHead < rhsHead= mergeReversed lhsRest (rhsHead : rhsRest) (lhsHead : result)
    | otherwise = mergeReversed (lhsHead : lhsRest) rhsRest (rhsHead : result)

mergeIntersect _ [] result = result
mergeIntersect [] _ result = result
mergeIntersect (lhsHead : lhsRest) (rhsHead : rhsRest) result
    | lhsHead < rhsHead = mergeIntersect lhsRest (rhsHead : rhsRest) result 
    | lhsHead > rhsHead = mergeIntersect (lhsHead : lhsRest) rhsRest result
    | lhsHead == rhsHead && result /= [] && (head result == lhsHead)= mergeIntersect lhsRest rhsRest result
    | otherwise = mergeIntersect lhsRest rhsRest (lhsHead : result)

intersectionOfTwoLists listLHS listRHS = 
    let sortedLHS = quicksort listLHS
        sortedRHS = quicksort listRHS
        in  reverse (mergeIntersect sortedLHS sortedRHS [])

unionOfTwoLists listLHS listRHS = 
    let sortedLHS = quicksort listLHS
        sortedRHS = quicksort listRHS
        in reverse (mergeReversed sortedLHS sortedRHS [])

asciiCodeForZero = 48

getDigits n result 
    | n == 0 = result
    | otherwise = getDigits (div n 10)  (chr (asciiCodeForZero + (n `mod` 10)) : result)

convertIntToString 0 = "0"
convertIntToString number =
    convertIntToStringHelper number []
    where 
        convertIntToStringHelper number result 
            | number == 0 = result
            | otherwise = convertIntToStringHelper (number `div` 10) (intToDigit (number `mod` 10) : result)  

stringToInt (head: tail) = 
    stringToIntHelper tail $ digitToInt head
    where 
        stringToIntHelper (head: tail) result
            | tail == "" = result * 10 + digitToInt head
            | otherwise = stringToIntHelper tail (result * 10 + digitToInt head)