Move History

Fork Selected
  • Code
    fn morse_code(msg: &str) -> String {
        msg.chars().map(|c| MORSE[c as usize - 32]).collect::<Vec<&str>>().join(" ")
    }
    
    const MORSE: [&str; 91] = [
        "/", // 
        "-.-.--", // !
        ".-..-.", // "
        "", // #
        "", // $
        "", // %
        ".-...", // &
        ".----.", // '
        "-.--.",  // (
        "-.--.-", // )
        "", // *
        ".-.-.",  // +
        "--..--", // ,
        "-....-", // -
        ".-.-.-", // .
        "-..-.", // /
        
        "-----", // 0
        ".----", // 1
        "..---", // 2
        "...--", // 3
        "....-", // 4
        ".....", // 5
        "-....", // 6
        "--...", // 7
        "---..", // 8
        "----.", // 9
        
        "---...", // :
        "", // ;
        "", // <
        "-...-", // =
        "", // >
        "..--..", // ?
        ".--.-.", // @
        
        ".-",   // A
        "-...", // B
        "-.-.", // C
        "-..",  // D
        ".",    // E
        "..-.", // F
        "--.",  // G
        "....", // H
        "..",   // I
        ".---", // J
        "-.-",  // K
        ".-..", // L
        "--",   // M
        "-.",   // N
        "---",  // O
        ".--.", // P
        "--.-", // Q
        ".-.",  // R
        "...",  // S
        "-",    // T
        "..-",  // U
        "...-", // V
        ".--",  // W
        "-..-", // X
        "-.--", // Y
        "-..",  // Z
        
        "-.--.", // [
        "", // \
        "-.--.-", // ]
        "", // ^
        "..--.-", // _
        "", // `
        
        ".-",   // a
        "-...", // b
        "-.-.", // c
        "-..",  // d
        ".",    // e
        "..-.", // f
        "--.",  // g
        "....", // h
        "..",   // i
        ".---", // j
        "-.-",  // k
        ".-..", // l
        "--",   // m
        "-.",   // n
        "---",  // o
        ".--.", // p
        "--.-", // q
        ".-.",  // r
        "...",  // s
        "-",    // t
        "..-",  // u
        "...-", // v
        ".--",  // w
        "-..-", // x
        "-.--", // y
        "-..",  // z
    ];
    
    Test Cases
    #[cfg(test)]
    mod tests {
        use super::*;
        
        #[test]
        fn test_alphabetic() {
            assert_eq!(morse_code("sos"), "... --- ...");
            assert_eq!(morse_code("hello world"), ".... . .-.. .-.. --- / .-- --- .-. .-.. -..");
            assert_eq!(morse_code("you passed!"), "-.-- --- ..- / .--. .- ... ... . -.. -.-.--");
            assert_eq!(morse_code("lorem ipsum is simply dummy text of the printing and typesetting industry."), ".-.. --- .-. . -- / .. .--. ... ..- -- / .. ... / ... .. -- .--. .-.. -.-- / -.. ..- -- -- -.-- / - . -..- - / --- ..-. / - .... . / .--. .-. .. -. - .. -. --. / .- -. -.. / - -.-- .--. . ... . - - .. -. --. / .. -. -.. ..- ... - .-. -.-- .-.-.-");
        }
        
        #[test]
        fn test_numeric() {
            assert_eq!(morse_code("0"), "-----");
            assert_eq!(morse_code("1+1=2"), ".---- .-.-. .---- -...- ..---");
            assert_eq!(morse_code("(1+2)=5?"), "-.--. .---- .-.-. ..--- -.--.- -...- ..... ..--..");
        }
        
        #[test]
        fn test_mixed() {
            assert_eq!(morse_code("seraph776@codewarz.com"), "... . .-. .- .--. .... --... --... -.... .--.-. -.-. --- -.. . .-- .- .-. -.. .-.-.- -.-. --- --");
        }
    }
  • Code
    • dot='.'
    • dash='-'
    • morse = {'a':[dot,dash],'b':[dash,dot,dot,dot],'c':[dash,dot,dash,dot],'d':[dash,dot,dot],'e':[dot],'f':[dot,dot,dash,dot],'g':[dash,dash,dot],'h':[dot,dot,dot,dot],""
    • 'i':[dot,dot],'j':[dot,dash,dash,dash],'k':[dash,dot,dash],'l':[dot,dash,dot,dot],'m':[dash,dash],'n':[dash,dot],'o':[dash,dash,dash],'p':[dot,dash,dash],'q':[dash,dash,dot,dash],""
    • 'r':[dot,dash,dot],'s':[dot,dot,dot],'t':[dash],'u':[dot,dot,dash],'v':[dot,dot,dot,dash],'w':[dot,dash,dash],'x':[dash,dot,dot,dash],'y':[dash,dot,dash,dash],'z':[dash,dot,dot],""
    • '0':[dash,dash,dash,dash,dash],'1':[dot,dash,dash,dash,dash],'2':[dot,dot,dash,dash,dash],'3':[dot,dot,dot,dash,dash],'4':[dot,dot,dot,dot,dash],'5':[dot,dot,dot,dot,dot],""
    • '6':[dash,dot,dot,dot,dot],'7':[dash,dash,dot,dot,dot],'8':[dash,dash,dash,dot,dot],'9':[dash,dash,dash,dash,dot]}
    • fn morse_code(msg: &str) -> String {
    • msg.chars().map(|c| MORSE[c as usize - 32]).collect::<Vec<&str>>().join(" ")
    • }
    • def morse_code(msg):
    • code=''
    • for i in msg:
    • if i==' ':
    • code+=' '
    • else:
    • for j in morse[i.lower()]:
    • code+=j
    • return code
    • print(morse_code('1122'))
    • const MORSE: [&str; 91] = [
    • "/", //
    • "-.-.--", // !
    • ".-..-.", // "
    • "�", // #
    • "�", // $
    • "�", // %
    • ".-...", // &
    • ".----.", // '
    • "-.--.", // (
    • "-.--.-", // )
    • "�", // *
    • ".-.-.", // +
    • "--..--", // ,
    • "-....-", // -
    • ".-.-.-", // .
    • "-..-.", // /
    • "-----", // 0
    • ".----", // 1
    • "..---", // 2
    • "...--", // 3
    • "....-", // 4
    • ".....", // 5
    • "-....", // 6
    • "--...", // 7
    • "---..", // 8
    • "----.", // 9
    • "---...", // :
    • "�", // ;
    • "�", // <
    • "-...-", // =
    • "�", // >
    • "..--..", // ?
    • ".--.-.", // @
    • ".-", // A
    • "-...", // B
    • "-.-.", // C
    • "-..", // D
    • ".", // E
    • "..-.", // F
    • "--.", // G
    • "....", // H
    • "..", // I
    • ".---", // J
    • "-.-", // K
    • ".-..", // L
    • "--", // M
    • "-.", // N
    • "---", // O
    • ".--.", // P
    • "--.-", // Q
    • ".-.", // R
    • "...", // S
    • "-", // T
    • "..-", // U
    • "...-", // V
    • ".--", // W
    • "-..-", // X
    • "-.--", // Y
    • "-..", // Z
    • "-.--.", // [
    • "�", // \
    • "-.--.-", // ]
    • "�", // ^
    • "..--.-", // _
    • "�", // `
    • ".-", // a
    • "-...", // b
    • "-.-.", // c
    • "-..", // d
    • ".", // e
    • "..-.", // f
    • "--.", // g
    • "....", // h
    • "..", // i
    • ".---", // j
    • "-.-", // k
    • ".-..", // l
    • "--", // m
    • "-.", // n
    • "---", // o
    • ".--.", // p
    • "--.-", // q
    • ".-.", // r
    • "...", // s
    • "-", // t
    • "..-", // u
    • "...-", // v
    • ".--", // w
    • "-..-", // x
    • "-.--", // y
    • "-..", // z
    • ];
    Test Cases
    • # TODO: Replace examples and use TDD development by writing your own tests
    • # These are some of the methods available:
    • # test.expect(boolean, [optional] message)
    • # test.assert_equals(actual, expected, [optional] message)
    • # test.assert_not_equals(actual, expected, [optional] message)
    • # You can use Test.describe and Test.it to write BDD style test groupings
    • test.assert_equals(morse_code('sos'),'...---...')
    • test.assert_equals(morse_code('hello world'),'......-...-..--- .-----.-..-..-..')
    • test.assert_equals(morse_code('you passed'),'-.-----..- .--.-.......-..')
    • test.assert_equals(morse_code('is a good day'),'..... .- --.-------.. -...--.--')
    • #[cfg(test)]
    • mod tests {
    • use super::*;
    • #[test]
    • fn test_alphabetic() {
    • assert_eq!(morse_code("sos"), "... --- ...");
    • assert_eq!(morse_code("hello world"), ".... . .-.. .-.. --- / .-- --- .-. .-.. -..");
    • assert_eq!(morse_code("you passed!"), "-.-- --- ..- / .--. .- ... ... . -.. -.-.--");
    • assert_eq!(morse_code("lorem ipsum is simply dummy text of the printing and typesetting industry."), ".-.. --- .-. . -- / .. .--. ... ..- -- / .. ... / ... .. -- .--. .-.. -.-- / -.. ..- -- -- -.-- / - . -..- - / --- ..-. / - .... . / .--. .-. .. -. - .. -. --. / .- -. -.. / - -.-- .--. . ... . - - .. -. --. / .. -. -.. ..- ... - .-. -.-- .-.-.-");
    • }
    • #[test]
    • fn test_numeric() {
    • assert_eq!(morse_code("0"), "-----");
    • assert_eq!(morse_code("1+1=2"), ".---- .-.-. .---- -...- ..---");
    • assert_eq!(morse_code("(1+2)=5?"), "-.--. .---- .-.-. ..--- -.--.- -...- ..... ..--..");
    • }
    • #[test]
    • fn test_mixed() {
    • assert_eq!(morse_code("seraph776@codewarz.com"), "... . .-. .- .--. .... --... --... -.... .--.-. -.-. --- -.. . .-- .- .-. -.. .-.-.- -.-. --- --");
    • }
    • }