summaryrefslogtreecommitdiff
path: root/kattis-kth
diff options
context:
space:
mode:
Diffstat (limited to 'kattis-kth')
-rw-r--r--kattis-kth/alginda-quicksort/Cargo.lock85
-rw-r--r--kattis-kth/tilda-enkelsyntax/EnkelSyntax.hs42
2 files changed, 127 insertions, 0 deletions
diff --git a/kattis-kth/alginda-quicksort/Cargo.lock b/kattis-kth/alginda-quicksort/Cargo.lock
new file mode 100644
index 0000000..d1c58ad
--- /dev/null
+++ b/kattis-kth/alginda-quicksort/Cargo.lock
@@ -0,0 +1,85 @@
+# This file is automatically @generated by Cargo.
+# It is not intended for manual editing.
+version = 3
+
+[[package]]
+name = "alginda-quicksort"
+version = "0.1.0"
+dependencies = [
+ "rand",
+ "rand_pcg",
+]
+
+[[package]]
+name = "cfg-if"
+version = "1.0.0"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
+
+[[package]]
+name = "getrandom"
+version = "0.2.8"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "c05aeb6a22b8f62540c194aac980f2115af067bfe15a0734d7277a768d396b31"
+dependencies = [
+ "cfg-if",
+ "libc",
+ "wasi",
+]
+
+[[package]]
+name = "libc"
+version = "0.2.139"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "201de327520df007757c1f0adce6e827fe8562fbc28bfd9c15571c66ca1f5f79"
+
+[[package]]
+name = "ppv-lite86"
+version = "0.2.17"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "5b40af805b3121feab8a3c29f04d8ad262fa8e0561883e7653e024ae4479e6de"
+
+[[package]]
+name = "rand"
+version = "0.8.5"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "34af8d1a0e25924bc5b7c43c079c942339d8f0a8b57c39049bef581b46327404"
+dependencies = [
+ "libc",
+ "rand_chacha",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_chacha"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "e6c10a63a0fa32252be49d21e7709d4d4baf8d231c2dbce1eaa8141b9b127d88"
+dependencies = [
+ "ppv-lite86",
+ "rand_core",
+]
+
+[[package]]
+name = "rand_core"
+version = "0.6.4"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "ec0be4795e2f6a28069bec0b5ff3e2ac9bafc99e6a9a7dc3547996c5c816922c"
+dependencies = [
+ "getrandom",
+]
+
+[[package]]
+name = "rand_pcg"
+version = "0.3.1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "59cad018caf63deb318e5a4586d99a24424a364f40f1e5778c29aca23f4fc73e"
+dependencies = [
+ "rand_core",
+]
+
+[[package]]
+name = "wasi"
+version = "0.11.0+wasi-snapshot-preview1"
+source = "registry+https://github.com/rust-lang/crates.io-index"
+checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423"
diff --git a/kattis-kth/tilda-enkelsyntax/EnkelSyntax.hs b/kattis-kth/tilda-enkelsyntax/EnkelSyntax.hs
new file mode 100644
index 0000000..eeaa82e
--- /dev/null
+++ b/kattis-kth/tilda-enkelsyntax/EnkelSyntax.hs
@@ -0,0 +1,42 @@
+import Data.List (elemIndex)
+
+type Parser = String -> Either String String
+
+parseMolekyl :: Parser
+parseMolekyl cs = parseAtom cs >>= parseNum
+
+parseAtom :: Parser
+parseAtom cs = parseBig cs >>= parseSmall
+
+parseBig :: Parser
+parseBig (c : cs) =
+ if c `elem` "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ then Right cs
+ else Left $ "Saknad stor bokstav vid radslutet " ++ (c : cs)
+parseBig "" = Left "Saknad stor bokstav vid radslutet "
+
+parseSmall :: Parser
+parseSmall (c : cs) =
+ Right $
+ if c `elem` "abcdefghijklmnopqrstuvwxyz"
+ then cs
+ else c : cs
+parseSmall "" = Right ""
+
+parseNum :: Parser
+parseNum "" = Right ""
+parseNum cs =
+ let (x, cs') = parseNum' 0 cs
+ in if x >= 2
+ then Right cs'
+ else Left ("För litet tal vid radslutet " ++ cs')
+ where
+ parseNum' n (c : cs) = case c `elemIndex` "0123456789" of
+ Just x -> parseNum' (n * 10 + x) cs
+ Nothing -> (n, c : cs)
+ parseNum' n "" = (n, "")
+
+output (Right _) = "Formeln är syntaktiskt korrekt"
+output (Left s) = s
+
+main = interact (unlines . map (output . parseMolekyl) . takeWhile (/= "#") . lines)