Strings and Regular Expressions(homework for 02)
来源:互联网 发布:淘宝玻璃茶具套装 编辑:程序博客网 时间:2024/06/05 11:36
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, "+nameenddef 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 endenddef 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 endend
- Strings and Regular Expressions(homework for 02)
- Strings and Regular Expressions
- <<High Performance JavaScript>>读书笔记-5.Strings and Regular Expressions
- XPath and Regular Expressions
- JHTP小结_第十四章_字符串、字符及正则表达式(Strings, Characters and Regular Expressions)
- JHTP自测题_第十四章_字符串、字符及正则表达式(Strings, Characters and Regular Expressions)
- Regular Expression Pocket Reference: Regular Expressions for Perl, Ruby, PHP, Python, C, Java and .N
- Regular Expression Pocket Reference: Regular Expressions for Perl, Ruby, PHP, Python, C, Java and .NET, 2nd edition
- Regular Expressions and the Java Programming Language
- 正则表达式(Regular Expressions)
- 正则表达式(Regular Expressions)
- 正则表达式(regular expressions)
- SAS Perl Regular Expression and SAS Regular Expressions (2)
- Regular Expressions
- Regular Expressions
- Regular Expressions
- Regular Expressions
- Regular Expressions
- 我的Web学习之路2——密码框的提示与表单验证
- swapPairs
- TrueType和Bitmap字体的区别
- 第六周--后缀表达式
- Guava 学习笔记 02
- Strings and Regular Expressions(homework for 02)
- BestCoder Round #58
- C++中引用(&)的用法和应用实例
- 文章标题
- 4.19**
- java——练习题4.16
- [hiho]#1069 : 最近公共祖先·三 线段树|树转数组
- linux中cat、more、less、tail、head命令的区别
- java记——关于溢出