From: Markus Schirp Date: 2013-01-14T22:09:15+09:00 Subject: Re: Conversion of Ruby-code to c/c++ code :: URGENT Plz help Homework? On Mon, Jan 14, 2013 at 10:04:22PM +0900, Nilesh S. wrote: > Hi.. I urgent require to convert the following ruby-code to c/c++ code. > If some-one have both the knowledge ..plz help me.. At least give me > some algorithm to it.. IT's ABOUT THE MORSE-CODE CONVERSION TO STRINGS > IN ALPHABETICAL ORDER > > ---------------------------------------------------------------------- > > #!/usr/bin/env ruby -wKU > require "set" > MORSE_LETTERS = %w[.- -... -.-. -.. . ..-. --. .... .. .--- -.- .-.. > -- > -. --- .--. --.- .-. ... - ..- ...- .-- -..- -.-- --..] > ENCODINGS = Hash[*MORSE_LETTERS.zip(('A'..'Z').to_a).flatten] > > def morse_decodings(word) > # iterate through matching prefixes > ENCODINGS.select { |p,l| p == word[0,p.size] }.map do | > prefix,letter| > > # gather decoded suffixes for the current prefix > suffixes = morse_decodings( word[prefix.size,word.size] ) > > # append decoded suffixes to decoded letter > suffixes.empty? ? letter : suffixes.map { |s| letter + s } > > end.flatten > end > > decodings = morse_decodings(readline.chomp).sort > > puts "All Possible Decodings:" > decodings.each { |e| puts e } > > -- > Posted via http://www.ruby-forum.com/. >