week 1 commit

This commit is contained in:
Dimitar Byalkov
2024-03-01 15:33:07 +01:00
parent 0ff6b73ce2
commit a505e41504
6 changed files with 165 additions and 0 deletions

BIN
wk1/caesar1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 32 KiB

24
wk1/elm.json Normal file
View 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": {}
}
}

BIN
wk1/pythagoras1.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 43 KiB

48
wk1/src/Caesar1.elm Normal file
View File

@@ -0,0 +1,48 @@
module Caesar1 exposing (..)
{-
Caesar cipher function
Dimitar Byalkov and Kaloyan Stoykov
-}
encode: Int -> Char -> Char
encode offset character =
-- 65 A 90 Z
if Char.toCode(character) > 64 && Char.toCode(character) < 91 then
Char.fromCode(65 + modBy 26 (Char.toCode(character) - 65 + offset - 26))
-- 97 a 122 z
else if Char.toCode(character) > 96 && Char.toCode(character) < 123 then
Char.fromCode(97 + modBy 26 (Char.toCode(character) - 97 + offset - 26))
else Char.fromCode(32)
decode: Int -> Char -> Char
decode offset character =
encode (0 - offset) character
-- Tests
testEncodeUpperSmallOffsetA: Bool
testEncodeUpperSmallOffsetA = (encode 5 'A' == 'F')
testEncodeUpperBigOffsetF: Bool
testEncodeUpperBigOffsetF = (encode 348 'F' == 'P')
testEncodeLowerSmallOffsetQ: Bool
testEncodeLowerSmallOffsetQ = (encode 9 'q' == 'z')
testEncodeLowerBigOffsetG: Bool
testEncodeLowerBigOffsetG = (encode 215 'g' == 'n')
testDecodeUpperSmallOffsetA: Bool
testDecodeUpperSmallOffsetA = (decode 5 'F' == 'A')
testDecodeUpperBigOffsetF: Bool
testDecodeUpperBigOffsetF = (decode 348 'P' == 'F')
testDecodeLowerSmallOffsetQ: Bool
testDecodeLowerSmallOffsetQ = (decode 9 'z' == 'q')
testDecodeLowerBigOffsetG: Bool
testDecodeLowerBigOffsetG = (decode 215 'n' == 'g')
allTests: List Bool
allTests = [testEncodeUpperSmallOffsetA, testEncodeUpperBigOffsetF, testEncodeLowerSmallOffsetQ, testEncodeLowerBigOffsetG, testDecodeUpperSmallOffsetA, testDecodeUpperBigOffsetF, testDecodeLowerSmallOffsetQ, testDecodeLowerBigOffsetG]

93
wk1/src/Pythagoras1.elm Normal file
View File

@@ -0,0 +1,93 @@
module Pythagoras1 exposing (..)
{-
Pythagoraen triples
Dimitar Byalkov and Kaloyan Stoykov
-}
sqr: Int -> Int
sqr n =
n * n
isTriple: Int -> Int -> Int -> Bool
isTriple a b c =
if a <= 0 || b <= 0 || c <= 0 then
False
else if (sqr a + sqr b) /= sqr c then
False
else True
leg1 : Int -> Int -> Int
leg1 x y =
sqr x - sqr y
leg2 : Int -> Int -> Int
leg2 x y =
2 * y * x
hyp : Int -> Int -> Int
hyp x y =
sqr x + sqr y
pythTriple : (Int, Int) -> (Int, Int, Int)
pythTriple inputTuple =
if Tuple.first inputTuple < 0 || Tuple.second inputTuple < 0 then
(0, 0, 0)
else
(leg1 (Tuple.first inputTuple) (Tuple.second inputTuple), leg2 (Tuple.first inputTuple) (Tuple.second inputTuple), hyp (Tuple.first inputTuple) (Tuple.second inputTuple))
isTripleTuple : (Int, Int, Int) -> Bool
isTripleTuple (a, b, c) =
isTriple a b c
-- Tests
testSqr: Bool
testSqr = (sqr 2 == 4)
testSqrNegaviteNumber: Bool
testSqrNegaviteNumber = (sqr -1 == 1)
testIsTripleZeros: Bool
testIsTripleZeros = (isTriple 0 0 0 == False)
testIsTripleNotEqual: Bool
testIsTripleNotEqual = (isTriple 6 5 4 == False)
testIsTripleNotEqual2: Bool
testIsTripleNotEqual2 = (isTriple 4 6 5 == False)
testIsTripleEqual: Bool
testIsTripleEqual =(isTriple 3 4 5 == True)
testLeg1: Bool
testLeg1 = (leg1 5 6 == -11)
testLeg1B: Bool
testLeg1B = (leg1 7 3 == 40)
testLeg2: Bool
testLeg2 = (leg2 3 9 == 54)
testLeg2B: Bool
testLeg2B = (leg2 2 7 == 28)
testHyp: Bool
testHyp = (hyp 5 6 == 61)
testHypB: Bool
testHypB = (hyp 7 3 == 58)
testInvalidPythTriple: Bool
testInvalidPythTriple = (pythTriple (-5, -9) == (0, 0, 0))
testPythTriple: Bool
testPythTriple = (pythTriple (4, 5) == (-9, 40, 41))
testPythTripleB: Bool
testPythTripleB = (pythTriple (7, 3) == (40, 42, 58))
testisTripleTuple: Bool
testisTripleTuple = (isTripleTuple (3, 4, 5) == True)
allPythTests: List Bool
allPythTests = [testSqr, testSqrNegaviteNumber, testIsTripleZeros, testIsTripleNotEqual, testIsTripleNotEqual2, testIsTripleEqual, testLeg1, testLeg1B, testLeg2, testLeg2B, testHyp, testHypB, testInvalidPythTriple, testPythTriple, testPythTripleB, testisTripleTuple]