Strings and Regular Expressions(homework for 02)

Strings and Regular Expressions

                                                                                                                                                                             ——孙琨SealSun

1、问题


  Check the documentation on String and Regexp as they could help tremendously with these exercises. 

 Define a method hello(name) that takes a string representing a name and returns the string "Hello, " concatenated with the name.

 Define a method starts_with_consonant?(s) that takes a string and returns true if it starts with a consonant and false otherwise. (For our purposes, a consonant is any letter other than A, E, I, O, U.) NOTE: be sure it works for both upper and lower case and for nonletters!

 Define a method binary_multiple_of_4?(s) that takes a string and returns true if the string represents a binary number that is a multiple of 4. NOTE: be sure it returns false if the string is not a valid binary number!


2、解决代码


#-------------------------------
#-----程序名称:homework for 02
#-----编译环境:ruby 2.2.3
#-----作    者:孙琨SealSun
#-----编写地点:UCAS  
#-----编写时间:2015年10月05日  
#--------------------------------

def hello(name)
  # YOUR CODE HERE
  puts "hello, "+name
end

def starts_with_consonant? s
  # YOUR CODE HERE
  reg_1=/[a-z]/ 
  reg_2=/[^aeiou]/
  if(reg_1===s[0].downcase && reg_2===s[0].downcase) #判断首位是否为不为元音的英语字母
    return true
  else
    return false
  end
end

def binary_multiple_of_4? s
  # YOUR CODE HERE
  reg=/[^01]/
  if(reg===s) #判断其是否为二进制数
    return false
  elsif(s.to_i.to_s(10).to_i%4==0) #判断此二进制数是否能被4整除
    return true
  else
    return false
  end
end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值