caesar 2 and pythagoras 2 (no tests)
Co-authored-by: KaloyanStoykov <KaloyanStoykov@users.noreply.github.com>
This commit is contained in:
24
wk2/elm.json
Normal file
24
wk2/elm.json
Normal file
@@ -0,0 +1,24 @@
|
||||
{
|
||||
"type": "application",
|
||||
"source-directories": [
|
||||
"src"
|
||||
],
|
||||
"elm-version": "0.19.1",
|
||||
"dependencies": {
|
||||
"direct": {
|
||||
"elm/browser": "1.0.2",
|
||||
"elm/core": "1.0.5",
|
||||
"elm/html": "1.0.0"
|
||||
},
|
||||
"indirect": {
|
||||
"elm/json": "1.1.3",
|
||||
"elm/time": "1.0.0",
|
||||
"elm/url": "1.0.0",
|
||||
"elm/virtual-dom": "1.0.3"
|
||||
}
|
||||
},
|
||||
"test-dependencies": {
|
||||
"direct": {},
|
||||
"indirect": {}
|
||||
}
|
||||
}
|
45
wk2/src/Caesar2.elm
Normal file
45
wk2/src/Caesar2.elm
Normal file
@@ -0,0 +1,45 @@
|
||||
module Caesar2 exposing (..)
|
||||
|
||||
{-
|
||||
Caesar cipher (Part 2)
|
||||
Dimitar Byalkov and Kaloyan Stoykov
|
||||
-}
|
||||
|
||||
encode: Int -> Char -> Char
|
||||
encode offset character =
|
||||
-- Char.isUpper replaces the check for the ASCII code range of uppercase letters
|
||||
if Char.isUpper character then
|
||||
shiftChar (Char.toCode 'A') offset character
|
||||
-- Char.isLowe replaces the check for the ASCII code range of lowercase letters
|
||||
else if Char.isLower character then
|
||||
shiftChar (Char.toCode 'a') offset character
|
||||
else ' '
|
||||
|
||||
-- shiftChar takes the common arithmetic actions from the if-statement in 'encode'
|
||||
shiftChar: Int -> Int -> Char -> Char
|
||||
shiftChar baseCode offset character =
|
||||
Char.fromCode(baseCode + modBy 26 ((Char.toCode character) - baseCode + offset - 26))
|
||||
|
||||
decode: Int -> Char -> Char
|
||||
decode offset character =
|
||||
encode (0 - offset) character
|
||||
|
||||
normalize: String -> String
|
||||
normalize input =
|
||||
String.fromList (List.filter Char.isAlpha (String.toList input))
|
||||
|
||||
encrypt: Int -> String -> String
|
||||
encrypt offset input =
|
||||
case String.uncons input of
|
||||
Nothing ->
|
||||
""
|
||||
Just (head, tail) ->
|
||||
String.cons (encode offset head) (encrypt offset tail)
|
||||
|
||||
decrypt: Int -> String -> String
|
||||
decrypt offset input =
|
||||
case String.uncons input of
|
||||
Nothing ->
|
||||
""
|
||||
Just (head, tail) ->
|
||||
String.cons (decode offset head) (decrypt offset tail)
|
62
wk2/src/Pythagoras2.elm
Normal file
62
wk2/src/Pythagoras2.elm
Normal file
@@ -0,0 +1,62 @@
|
||||
module Pythagoras2 exposing (..)
|
||||
|
||||
{-
|
||||
Pythagoraen triples (Part 2)
|
||||
Dimitar Byalkov and Kaloyan Stoykov
|
||||
-}
|
||||
|
||||
pythTriplesMap: List (Int, Int) -> List (Int, Int, Int)
|
||||
pythTriplesMap input =
|
||||
List.map pythTriple input
|
||||
|
||||
pythTriplesRec: List (Int, Int) -> List (Int, Int, Int)
|
||||
pythTriplesRec input =
|
||||
-- We have a base case for an empty list which stops the recursion.
|
||||
case input of
|
||||
[] ->
|
||||
[]
|
||||
{- We take out the first element x of the list and
|
||||
we are left with the rest of the list - xs, which we use to recursively call the function -}
|
||||
x :: xs ->
|
||||
List.singleton (pythTriple x) ++ pythTriplesRec xs
|
||||
|
||||
|
||||
-- Taken and condensed from Pythagoras, part 1
|
||||
pythTriple: (Int, Int) -> (Int, Int, Int)
|
||||
pythTriple (x, y) =
|
||||
if x < 0 || y < 0 then
|
||||
(0, 0, 0)
|
||||
else
|
||||
((x ^ 2 - y ^ 2), (2 * y * x), (x ^ 2 + y ^ 2))
|
||||
|
||||
{-
|
||||
Taken from Pythagoras, part 1
|
||||
Checks if the three numbers form a Pythagorean triple
|
||||
-}
|
||||
isTriple: Int -> Int -> Int -> Bool
|
||||
isTriple a b c =
|
||||
if a <= 0 || b <= 0 || c <= 0 then
|
||||
False
|
||||
else if (a ^ 2 + b ^ 2) /= c ^ 2 then
|
||||
False
|
||||
else True
|
||||
|
||||
-- check a give triple tuple forms a Pythagorean triple
|
||||
isTripleTuple : (Int, Int, Int) -> Bool
|
||||
isTripleTuple (a, b, c) =
|
||||
isTriple a b c
|
||||
|
||||
arePythTriplesFilter: List (Int, Int, Int) -> List (Int, Int, Int)
|
||||
arePythTriplesFilter tripleList =
|
||||
List.filter isTripleTuple tripleList
|
||||
|
||||
arePythTriplesRec: List (Int, Int, Int) -> List (Int, Int, Int)
|
||||
arePythTriplesRec tripleList =
|
||||
case tripleList of
|
||||
[] ->
|
||||
[]
|
||||
x :: xs ->
|
||||
if isTripleTuple x then
|
||||
List.singleton x ++ arePythTriplesRec xs
|
||||
else
|
||||
arePythTriplesRec xs
|
Reference in New Issue
Block a user