Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upload homework #2

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
49 changes: 49 additions & 0 deletions exercises/md-table/MdTable.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{-# OPTIONS_GHC -Wall #-}

module MdTable where

import Data.List (intercalate)

getMaxColumns :: [[String]] -> Int
getMaxColumns [] = 0
getMaxColumns xs = maximum [length x | x <- xs]

getMaxColumnLength :: [[String]] -> Int
getMaxColumnLength [] = 0
getMaxColumnLength xs = maximum [length x | x <- concat xs]

addEmptyStrings :: [String] -> Int -> [String]
addEmptyStrings xs 0 = xs
addEmptyStrings [] n = "" : (addEmptyStrings [] (n-1))
addEmptyStrings (x:xs) n = x : (addEmptyStrings xs (n))

equalizeColumnCount :: [[String]] -> [[String]]
equalizeColumnCount xs = [addEmptyStrings x (getMaxColumns xs - length x) | x <- xs]

equalizeColumnSize :: [[String]] -> [[String]]
equalizeColumnSize [] = []
equalizeColumnSize (x:xs) = [pad y | y <- x] : (equalizeColumnSize xs)
where pad z = padWith ' ' (getMaxColumnLength (x:xs)) z

toLine :: [String] -> String
toLine [] = "|"
toLine (x:xs) = "| " ++ x ++ " " ++ toLine xs

padWith :: (Eq a) => a -> Int -> [a] -> [a]
padWith _ 0 x | x == [] = []
| otherwise = x
padWith pad n [] = pad : padWith pad (n-1) []
padWith pad n (x:xs) = x : padWith pad (n-1) xs


layoutTable :: [[String]] -> String
layoutTable xs = intercalate "\n" [toLine x | x <- ys]
where ys = equalizeColumnSize (equalizeColumnCount xs)

table2 :: [[String]]
table2 = [
[ "Item", "Price" ],
[ "iPhone", "$1", "Not in stock" ],
[ "iPad", "$599" ],
[ "CAUTION: It's a scam!"]
]
4 changes: 2 additions & 2 deletions exercises/permutations/perm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ module Permutations where
import Data.List hiding (permutations)

permutations :: [Int] -> [[Int]]
permutations = undefined

permutations [] = [[]]
permutations xs = [ y:ys | y <- xs, ys <- permutations (delete y xs)]
44 changes: 38 additions & 6 deletions exercises/secret-lang/Secret-lang.hs
Original file line number Diff line number Diff line change
@@ -1,32 +1,64 @@
import Data.Char(toLower)

<<<<<<< HEAD
vowels = "aeiou"

=======
>>>>>>> c48c6b3660bd9b1429acba58cb5198a8ec08411d
isChar :: Char -> Bool
isChar = undefined
isChar char
| toLower char >= 'a' && toLower char <= 'z' = True
isChar _ = False

isInString :: Char -> String -> Bool
isInString char [] = False
isInString char (first:rest)
| char == first = True
| char /= first = isInString char rest


isVowel :: Char -> Bool
isVowel = undefined
isVowel char = isInString (toLower char) vowels


isConsonant :: Char -> Bool
isConsonant = undefined
isConsonant char
| not (isVowel char) && isChar char = True
| otherwise = False


encode :: String -> String
encode = undefined
encode [] = []
encode (first:rest)
| isConsonant first = first : 'o' : first : (encode rest)
| otherwise = first:(encode rest)


-- Bonus #1
encode' :: String -> String
encode' = undefined
encode' [] = []
encode' (first:rest)
| isConsonant first = first : 'o' : toLower first : (encode' rest)
| otherwise = first:(encode' rest)


-- Bonus #2
dropN :: Int -> String -> String
dropN = undefined
dropN number [] = []
dropN 0 string = string
dropN number (first:rest) = dropN (number-1) rest



-- Assume we'll be decoding only valid words
decode :: String -> String
<<<<<<< HEAD
decode [] = []
decode (x : 'o' : y : rest)
| x == y && isConsonant x = x : decode (dropN 2 ('o':y:rest))
| otherwise = x : 'o' : y : (decode (rest))
decode (first : rest) = first : decode (rest)
=======
decode = undefined

>>>>>>> c48c6b3660bd9b1429acba58cb5198a8ec08411d