论坛首页 编程语言技术论坛

ruby入门小case

浏览 2454 次
精华帖 (0) :: 良好帖 (0) :: 新手帖 (0) :: 隐藏帖 (0)
作者 正文
   发表时间:2007-11-14  
Q1. Write a Ruby program that asks for a numeric value of the temperature in degrees Fahrenheit. Finally, the program displays the equivalent value in degrees Centigrade. To format the output to say 2 decimal places, we can use the Kernel's format method. For example, if x = 45.5678 then format("%.2f", x) will return a string 45.57. Another way is to use the round function as follows: puts (x*100).round/100.0
print 'Please input the temperature in degrees Fahrenheit :'
STDOUT.flush
temp = gets.chomp.to_f
if temp=~/^%d+\.%d*$/
  puts temp
end




Q2. Write a Ruby program that asks for a year and then displays to the user whether the year entered by him/her is a leap year or not.
print 'Please input a year : '
STDOUT.flush
year = gets.chomp.to_i
if (year%4==0&&year%100>0)||year%400==0
  puts "#{year} is a leap-year!!"
else
  puts "#{year} is not a leap-year!!"
end



Q3. Write a method leap_year. Accept a year value from the user, check whether it's a leap year and then display the number of minutes in that year.
def if_leap_year(year)
  (year%4==0&&year%100>0)||year%400==0
end
print 'Please input a year : '
STDOUT.flush
year = gets.chomp.to_i
if if_leap_year(year)
  puts "#{year} is a leap-year!! it has #{366*24*60} minutes"
else
  puts "#{year} is a leap-year!! it has #{365*24*60} minutes"
end



Q4. Write a Ruby program that, when given an array as collection = [1, 2, 3, 4, 5] it calculates the sum of its elements.

#solution I
coll = [1,2,3,4,5]
sum = 0
coll.inject{|sum,each| sum+=each}
puts sum

#solution II
coll = [1,2,3,4,5]
sum = 0
coll.each {|i| sum+=i}
puts sum



Q5. Write a Ruby program that, when given an array as collection = [12, 23, 456, 123, 4579] it displays for each number, whether it is odd or even.
coll = [12, 23, 456, 123, 4579]
coll.each do |i|
  if i%2==0
    puts "#{i} is odd"
  else
    puts "#{i} is even"
  end
end



Q6.Given a string s = 'key=value', create two strings s1 and s2 such that s1 contains key and s2 contains value. Hint: Use some of the String functions.
#solution I
s = 'key=value'
s1 = s.split(/=/)[0]
s2 = s.split(/=/)[1]
puts s1,s2

#solution II
s = 'key=value'
posion = s.index('=')
s1 = s[0...posion]
s2 = s[posion+1..(s.size)]
puts s1,s2


Q7. Write a Deaf Grandma program. Whatever you say to grandma (whatever you type in), she should respond with HUH?!  SPEAK UP, SONNY!, unless you shout it (type in all capitals). If you shout, she can hear you (or at least she thinks so) and yells back, NO, NOT SINCE 1938! To make your program really believable, have grandma shout a different year each time; maybe any year at random between 1930 and 1950. You can't stop talking to grandma until you shout BYE.
year = (1930..1950).to_a
puts 'you say :'
STDOUT.flush
until (say=gets.chomp).eql?('Bye')
  if say.eql?(say.upcase)
    puts "I got what u say in #{year[rand(year.size)]}"
  else
    puts "huh ?? speak louder!!!"
  end
  puts 'you say :'
  STDOUT.flush
end



Q8. Extend your Deaf Grandma program: What if grandma doesn't want you to leave? When you shout BYE, she could pretend not to hear you. Change your previous program so that you have to shout BYE three times in a row. Make sure to test your program: if you shout BYE three times, but not in a row, you should still be talking to grandma.
year = (1930..1950).to_a
puts 'you say :'
STDOUT.flush
until (say=gets.chomp.to_s)=~/^ByeByeBye$/
  if say.eql?(say.upcase)
    puts "I got what u say in #{year[rand(year.size)]}"
  else
    puts "huh ?? speak louder!!!"
  end
  puts 'you say :'
  STDOUT.flush
end
论坛首页 编程语言技术版

跳转论坛:
Global site tag (gtag.js) - Google Analytics