Re: Morse

new topic     » goto parent     » topic index » view thread      » older message » newer message

Well, it's not smaller, but it is probably more efficient and more Euphoria-like. Storing the codes in a table indexed by ASCII character should be more efficient than using vslice() and lookup(). I'm a bit rusty on my time complexity theory, but I believe it's the difference of O(1) vs O(n) or O(log n). I haven't examined the underlying code, so I don't know if lookup() uses a binary search or linear search. I would imagine it's binary, which is O(log n) complex.

-- morse.e 
 
public constant 
    STARTING_SIGNAL         =  1, 
    END_OF_MESSAGE          =  4, 
    INVITATION_TO_TRANSMIT  =  5, 
    UNDERSTOOD              =  6, 
    ERROR                   = 21, 
    WAIT                    = 22, 
    END_OF_WORK             = 23, 
$ 
 
public sequence MORSE = repeat( "", 127 ) 
 
MORSE[ERROR]                    = "........" 
MORSE[END_OF_MESSAGE]           = ".-.-." 
MORSE[END_OF_WORK]              = "...-.-" 
MORSE[INVITATION_TO_TRANSMIT]   = "-.-" 
MORSE[STARTING_SIGNAL]          = "-.-.-" 
MORSE[UNDERSTOOD]               = "...-." 
MORSE[WAIT]                     = ".-..." 
MORSE['!']  = "-.-.--" 
MORSE['\"'] = ".-..-." 
MORSE['$']  = "...-..-" 
MORSE['&']  = ".-..." 
MORSE['\''] = ".----." 
MORSE['(']  = "-.--." 
MORSE[')']  = "-.--.-" 
MORSE['+']  = ".-.-." 
MORSE[',']  = "--..--" 
MORSE['-']  = "-....-" 
MORSE['.']  = ".-.-.-" 
MORSE['/']  = "-..-." 
MORSE['0']  = "-----" 
MORSE['1']  = ".----" 
MORSE['2']  = "..---" 
MORSE['3']  = "...--" 
MORSE['4']  = "....-" 
MORSE['5']  = "....." 
MORSE['6']  = "-...." 
MORSE['7']  = "--..." 
MORSE['8']  = "---.." 
MORSE['9']  = "----." 
MORSE[':']  = "---..." 
MORSE[';']  = "-.-.-." 
MORSE['=']  = "-...-" 
MORSE['?']  = "..--.." 
MORSE['@']  = ".--.-." 
MORSE['A']  = ".-" 
MORSE['B']  = "-..." 
MORSE['C']  = "-.-." 
MORSE['D']  = "-.." 
MORSE['E']  = "." 
MORSE['F']  = "..-." 
MORSE['G']  = "--." 
MORSE['H']  = "...." 
MORSE['I']  = ".." 
MORSE['J']  = ".---" 
MORSE['K']  = "-.-" 
MORSE['L']  = ".-.." 
MORSE['M']  = "--" 
MORSE['N']  = "-." 
MORSE['O']  = "---" 
MORSE['P']  = ".--." 
MORSE['Q']  = "--.-" 
MORSE['R']  = ".-." 
MORSE['S']  = "..." 
MORSE['T']  = "-" 
MORSE['U']  = "..-" 
MORSE['V']  = "...-" 
MORSE['W']  = ".--" 
MORSE['X']  = "-..-" 
MORSE['Y']  = "-.--" 
MORSE['Z']  = "--.." 
MORSE['_']  = "..--.-" 
MORSE['a']  = ".-" 
MORSE['b']  = "-..." 
MORSE['c']  = "-.-." 
MORSE['d']  = "-.." 
MORSE['e']  = "." 
MORSE['f']  = "..-." 
MORSE['g']  = "--." 
MORSE['h']  = "...." 
MORSE['i']  = ".." 
MORSE['j']  = ".---" 
MORSE['k']  = "-.-" 
MORSE['l']  = ".-.." 
MORSE['m']  = "--" 
MORSE['n']  = "-." 
MORSE['o']  = "---" 
MORSE['p']  = ".--." 
MORSE['q']  = "--.-" 
MORSE['r']  = ".-." 
MORSE['s']  = "..." 
MORSE['t']  = "-" 
MORSE['u']  = "..-" 
MORSE['v']  = "...-" 
MORSE['w']  = ".--" 
MORSE['x']  = "-..-" 
MORSE['y']  = "-.--" 
MORSE['z']  = "--.." 
 
public function morse( object s ) -- s = string 
     
    if atom( s ) then 
         
        s = and_bits( s, #FF ) 
        return MORSE[s] 
         
    end if 
     
    sequence m = "" -- m = message 
     
    for i = 1 to length( s ) do 
         
        sequence c = morse( s[i] ) -- c = code 
         
        if length( c ) > 0 and length( m ) > 0 then 
            m &= " " 
        end if 
         
        m &= c 
         
    end for 
     
    return m 
end function 
-- morse.ex 
 
include std/console.e 
include morse.e 
 
sequence cmd = command_line() 
 
for i = 3 to length( cmd ) do 
    sequence code = morse( cmd[i] & END_OF_MESSAGE ) 
    printf( 1, "%s\n", {code} ) 
end for 

> eui morse.ex "Hello, world!" 
.... . .-.. .-.. --- --..-- .-- --- .-. .-.. -.. -.-.-- .-.-. 
 
> eui morse.ex "The quick brown fox jumps over the lazy dog." 
- .... . --.- ..- .. -.-. -.- -... .-. --- .-- -. ..-. --- -..- .--- ..- -- .--. ... --- ...- . .-. - .... . .-.. .- --.. -.-- -.. --- --. .-.-.- .-.-. 

-Greg

new topic     » goto parent     » topic index » view thread      » older message » newer message

Search



Quick Links

User menu

Not signed in.

Misc Menu