###前提・実現したいこと
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")を与え,その実行結果を確認せよ.
(引用元:http://www.cl.ecei.tohoku.ac.jp/nlp100/)
色々調べたのですがわかりませんでした。よろしくお願いします。
###発生している問題・エラーメッセージ
09.rb:5:in `block in Typoglycemia': no implicit conversion of String into Array (TypeError) from 09.rb:3:in `each' from 09.rb:3:in `Typoglycemia' from 09.rb:17:in `<main>'
###該当のソースコード
Ruby
1def Typoglycemia(str) 2 result = [] 3 str.split.each {|words| 4 if words.length <= 4 5 result += words 6 else 7 list = words.slice!(1..-2) 8 mid = rand(list) 9 shuffle_list = words[0] + mid + words[-1] 10 result += shuffle_list 11 end 12 } 13 return result.join(" ") 14end 15 16str = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ." 17print Typoglycemia(str)
###補足情報(言語/FW/ツール等のバージョンなど)
Ruby2.0.0
回答3件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
退会済みユーザー
2016/10/24 14:01