Compare commits
5 commits
2de1f283bd
...
2fb615473c
Author | SHA1 | Date | |
---|---|---|---|
2fb615473c | |||
8fa41c4e4c | |||
2a6c53e075 | |||
a7f05aa3c7 | |||
11b969d169 |
6 changed files with 121 additions and 1 deletions
8
haskell/10_split_strings.hs
Normal file
8
haskell/10_split_strings.hs
Normal file
|
@ -0,0 +1,8 @@
|
||||||
|
module Codewars.Kata.SplitStrings (solution) where
|
||||||
|
|
||||||
|
import qualified Data.Char as Char
|
||||||
|
|
||||||
|
solution :: String -> [String]
|
||||||
|
solution [] = []
|
||||||
|
solution [c1] = [c1 : "_"]
|
||||||
|
solution (c1:c2:rest) = (c1 : c2 : "") : solution rest
|
23
haskell/11_roman_numerals_decoder.hs
Normal file
23
haskell/11_roman_numerals_decoder.hs
Normal file
|
@ -0,0 +1,23 @@
|
||||||
|
{-# LANGUAGE NumericUnderscores #-}
|
||||||
|
module Roman (solution) where
|
||||||
|
|
||||||
|
solution :: String -> Int
|
||||||
|
solution = sum . mergeParts . map fromRoman
|
||||||
|
|
||||||
|
mergeParts :: [Int] -> [Int]
|
||||||
|
mergeParts [] = []
|
||||||
|
mergeParts [n1] = [n1]
|
||||||
|
mergeParts (n1:n2:rest)
|
||||||
|
| n1 < n2 = n2 - n1 : mergeParts rest
|
||||||
|
| otherwise = n1 : mergeParts (n2:rest)
|
||||||
|
|
||||||
|
fromRoman :: Char -> Int
|
||||||
|
fromRoman c = case c of
|
||||||
|
'I' -> 1
|
||||||
|
'V' -> 5
|
||||||
|
'X' -> 10
|
||||||
|
'L' -> 50
|
||||||
|
'C' -> 100
|
||||||
|
'D' -> 500
|
||||||
|
'M' -> 1_000
|
||||||
|
_ -> 0
|
25
haskell/9_title_case.hs
Normal file
25
haskell/9_title_case.hs
Normal file
|
@ -0,0 +1,25 @@
|
||||||
|
module TitleCase (titleCase) where
|
||||||
|
|
||||||
|
import qualified Data.Char as Char
|
||||||
|
|
||||||
|
titleCase :: String -> String -> String
|
||||||
|
titleCase minor = unwords . process . words . toLower
|
||||||
|
where
|
||||||
|
process :: [String] -> [String]
|
||||||
|
process [] = []
|
||||||
|
process (head:tail) = toTitle head : map maybeToTitle tail
|
||||||
|
|
||||||
|
lowerMinorWords :: [String]
|
||||||
|
lowerMinorWords = words $ toLower minor
|
||||||
|
|
||||||
|
maybeToTitle :: String -> String
|
||||||
|
maybeToTitle word
|
||||||
|
| word `elem` lowerMinorWords = word
|
||||||
|
| otherwise = toTitle word
|
||||||
|
|
||||||
|
toTitle :: String -> String
|
||||||
|
toTitle [] = []
|
||||||
|
toTitle (head:tail) = Char.toUpper head : toLower tail
|
||||||
|
|
||||||
|
toLower :: String -> String
|
||||||
|
toLower = map Char.toLower
|
9
rust/Cargo.lock
generated
9
rust/Cargo.lock
generated
|
@ -2,6 +2,15 @@
|
||||||
# It is not intended for manual editing.
|
# It is not intended for manual editing.
|
||||||
version = 3
|
version = 3
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "once_cell"
|
||||||
|
version = "1.10.0"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "87f3e037eac156d1775da914196f0f37741a274155e34a0b7e427c35d2a2ecb9"
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "rust"
|
name = "rust"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
|
dependencies = [
|
||||||
|
"once_cell",
|
||||||
|
]
|
||||||
|
|
|
@ -5,4 +5,5 @@ edition = "2021"
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
once_cell = "1.10.0"
|
||||||
|
|
54
rust/tests/9_if_you_can_reed_this.rs
Normal file
54
rust/tests/9_if_you_can_reed_this.rs
Normal file
|
@ -0,0 +1,54 @@
|
||||||
|
use once_cell::sync::Lazy;
|
||||||
|
use std::collections::HashMap;
|
||||||
|
|
||||||
|
#[rustfmt::skip]
|
||||||
|
pub static NATO: Lazy<HashMap<char, &'static str>> = Lazy::new(|| {
|
||||||
|
[
|
||||||
|
('A', "Alfa"), ('B', "Bravo"), ('C', "Charlie"), ('D', "Delta"),
|
||||||
|
('E', "Echo"), ('F', "Foxtrot"), ('G', "Golf"), ('H', "Hotel"),
|
||||||
|
('I', "India"), ('J', "Juliett"), ('K', "Kilo"), ('L', "Lima"),
|
||||||
|
('M', "Mike"), ('N', "November"), ('O', "Oscar"), ('P', "Papa"),
|
||||||
|
('Q', "Quebec"), ('R', "Romeo"), ('S', "Sierra"), ('T', "Tango"),
|
||||||
|
('U', "Uniform"), ('V', "Victor"), ('W', "Whiskey"), ('X', "Xray"),
|
||||||
|
('Y', "Yankee"), ('Z', "Zulu"),
|
||||||
|
]
|
||||||
|
.iter()
|
||||||
|
.copied()
|
||||||
|
.collect()
|
||||||
|
});
|
||||||
|
|
||||||
|
fn to_nato(words: &str) -> String {
|
||||||
|
words
|
||||||
|
.to_uppercase()
|
||||||
|
.chars()
|
||||||
|
.filter(|c| !c.is_whitespace())
|
||||||
|
.map(|c| match NATO.get(&c) {
|
||||||
|
Some(w) => w.to_string(),
|
||||||
|
None => c.to_string(),
|
||||||
|
})
|
||||||
|
.collect::<Vec<_>>()
|
||||||
|
.join(" ")
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::to_nato;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn base_to_nato() {
|
||||||
|
assert_eq!(
|
||||||
|
to_nato("If you can read"),
|
||||||
|
"India Foxtrot Yankee Oscar Uniform Charlie Alfa November Romeo Echo Alfa Delta"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
to_nato("Did not see that coming",),
|
||||||
|
"Delta India Delta November Oscar Tango Sierra Echo Echo Tango Hotel Alfa Tango Charlie Oscar Mike India November Golf"
|
||||||
|
);
|
||||||
|
|
||||||
|
assert_eq!(
|
||||||
|
to_nato("go for it!"),
|
||||||
|
"Golf Oscar Foxtrot Oscar Romeo India Tango !"
|
||||||
|
);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Add table
Reference in a new issue