Created
January 31, 2019 04:33
Alpha Camp Semester 1 Week 3
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#以下為 Array 應用 | |
#使用者輸入 | |
puts "Hi, please enter a sentence." | |
sentence = gets.chomp | |
#分拆句子結構 (拆字串&序號) | |
sentence_split = sentence.split(" ") | |
sentence_index = (0..sentence_split.length-1).to_a | |
#取得每項字母長度 | |
word_length = [] | |
sentence_index.each do |i| | |
word_length.push (sentence_split[i].length) | |
end | |
#若字母數為奇數,則印出字母數;否則,顛倒字序 | |
reverse = [] | |
sentence_index.each do |i| | |
if word_length[i] % 2 == 1 | |
reverse.push(word_length[i]) | |
else | |
reverse.push(sentence_split[i].reverse) | |
end | |
end | |
p reverse |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment