Skip to content

Instantly share code, notes, and snippets.

@andy922200
Created January 31, 2019 04:33
Show Gist options
  • Save andy922200/df8bba3adf0153b6f796f26b1e56a42c to your computer and use it in GitHub Desktop.
Save andy922200/df8bba3adf0153b6f796f26b1e56a42c to your computer and use it in GitHub Desktop.
Alpha Camp Semester 1 Week 3
#以下為 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