ruby入门小case

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
[code]
print 'Please input the temperature in degrees Fahrenheit :'
STDOUT.flush
temp = gets.chomp.to_f
if temp=~/^%d+\.%d*$/
puts temp
end
[/code]


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.
[code]
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
[/code]


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.
[code]
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
[/code]


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

[code]
#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
[/code]


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.
[code]
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
[/code]


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.
[code]
#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
[/code]

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.
[code]
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
[/code]


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.
[code]
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
[/code]
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值