コーディングチャレンジ(随時更新)
Palindrome
Palindrome は逆さにしても同じ文字列のこと
Using the Ruby language, have the function PalindromeTwo(str) take the str parameter being passed and return the string true if the parameter is a palindrome, (the string is the same forward as it is backward) otherwise return the string false. The parameter entered may have punctuation and symbols but they should not affect whether the string is in fact a palindrome. For example: "Anne, I vote more cars race Rome-to-Vienna" should return true. Use the Parameter Testing feature in the box below to test your code with different arguments..
Palindrome # == 逆さにしても同じ文字列
def PalindromeTwo(str)
arr = str.scan(/\w+/).join.downcase
arr.reverse == arr
end
http://coderbyte.com/CodingArea/Editor.php?ct=Palindrome%20Two&lan=Ruby
Binary digits
2進数から10進数への変換
Using the Ruby language, have the function BinaryConverter(str) return the decimal form of the binary value. For example: if 101 is passed return 5, or if 1000 is passed return 8.
Use the Parameter Testing feature in the box below to test your code with different arguments.
Binary digits # == 2進数 => 10進数
def BinaryConverter(str)
arr = str.split("")
new_arr = []
index = (0..(arr.length-1)).to_a.reverse
arr.each_with_index do |n, i|
new_arr << 2 ** index[i] * n.to_i
end
return new_arr.inject(:+)
end
http://coderbyte.com/CodingArea/Editor.php?ct=Binary%20Converter&lan=Ruby