I am supposed to get a string and make sure that it represents a binary number like "101011".
This question deals with hexadecimal strings, but I do not know how I should replace the letter H in the regex in statement !str[/\H/]. Could you help me?
A straightforward way using regex is:
"101011" !~ /[^01]/
Four ways using String methods:
str1 = '100100110'
str2 = '100100210'
String#delete
str1.delete('01') == '' #=> true
str2.delete('01') == '' #=> false
String#tr
str1.tr('01','') == '' #=> true
str2.tr('01','') == '' #=> false
String#gsub
str1.gsub(/[01]/,'') == '' #=> true
str2.gsub(/[01]/,'') == '' #=> false
String#count
str1.count('01') == str1.size #=> true
str2.count('01') == str2.size #=> false
Here is one way of doing this:
"101011".chars.all? {|x| x =~ /[01]/} # true if binary, else false
Related
# Write a method that takes a string in and returns true if the letter
# "z" appears within three letters **after** an "a". You may assume
# that the string contains only lowercase letters.
I came up with this, which seems logical, but for some reason if "z" comes directly after "a", it returns false. Can someone explain why?
def nearby_az(string)
i = 0
if string[i] == "a" && string[i+1] == "z"
return true
elsif string[i] == "a" && string[i+2] == "z"
return true
elsif string[i] == "a" && string[i+3] == "z"
return true
else return false
end
i += 1
end
#shivram has given the reason for your problem. Here are a couple of ways to do it.
Problem is tailor-made for a regular expression
r = /
a # match "a"
.{,2} # match any n characters where 0 <= n <= 2
z # match "z"
/x # extended/free-spacing regex definition mode
!!("wwwaeezdddddd" =~ r) #=> true
!!("wwwaeeezdddddd" =~ r) #=> false
You would normally see this regular expression written
/a.{0,2}z/
but extended mode allows you to document each of its elements. That's not important here but is useful when the regex is complex.
The Ruby trick !!
!! is used to convert truthy values (all but false and nil) to true and falsy values (false or nil) to false:
!!("wwwaeezdddddd" =~ r)
#=> !(!("wwwaeezdddddd" =~ r))
#=> !(!3)
#=> !false
#=> true
!!("wwwaeezdddddd" =~ r)
#=> !(!("wwwaeeezdddddd" =~ r))
#=> !(!nil)
#=> !true
#=> false
but !! is not really necessary, since
puts "hi" if 3 #=> "hi"
puts "hi" if nil #=>
Some don't like !!, arguing that
<condition> ? true : false
is more clear.
A non-regex solution
def z_within_4_of_a?(str)
(str.size-3).times.find { |i| str[i]=="a" && str[i+1,3].include?("z") } ? true : false
end
z_within_4_of_a?("wwwaeezdddddd")
#=> true
z_within_4_of_a?("wwwaeeezdddddd")
#=> false
This uses the methods Fixnum#times, Enumerable#find and String#include? (and String#size of course).
Your solution is incorrect. You are considering only the case where String starts with a (with i = 0 at the start of your method). I can see you are incrementing i at the end, but its of no use as its not in a loop.
I can think of a solution as to find the index of a in string, then take substring from that index + 3 and look for z. Something like:
s = "wwwaeezdddddd"
s[s.index("a")..s.index("a")+3]
#=> "aeez"
s[s.index("a")..s.index("a")+3] =~ /z/ # checking if z is present
#=> 3
If a can occur more than once in input String, you need to find all indices of a and run the above logic in a loop. Something like:
s = "wwwaesezddddddaz"
indexes = (0 ... s.length).find_all { |i| s[i,1] == 'a' }
#=> [3, 14]
indexes.each { |i| break if #is_present = s[i..i+3] =~ /z/ }
#is_present
#=> 1
Let’s implement the FSM ourselves :)
input = "wwwaeezdddddd"
!(0...input.length).each do |idx|
next unless input[idx] == 'a' # skip unrelated symbols
current = (idx..[idx + 3, input.length - 1].min).any? do |i|
input[i] == 'z' # return true if there is 'z'
end
# since `each` returns truthy (range itself),
# in case of success we return falsey and negate
break false if current
end
#⇒ true
Please note, that the above implementation is O(length(input)) and does not use any built-in ruby helpers, it is just iterating a string char by char.
While the regexp solution is the most elegant, here is one for completion, which is more in spirit to your original attempt:
def nearby_az(string)
!!(apos = string.index('a') and string[apos,3].index('z'))
end
This is the question's prompt:
Write a method that takes a string and returns true if the letter
"z" appears within three letters after an "a". You may assume
that the string contains only lowercase letters.
I'm trying to use the ternary operator, and want to include the match or count methods. Any idea on how I can find the number of characters between "a" and "z" or the simplest way to solve this?
def nearby_az(string)
string.count <= 3 ? true : false
end
Regex would be a good way to solve this problem.
You can use online regex testers to experiment with different regexes, inputs and outputs.
The first solution that comes to my mind is to come up with a pattern for each possible correct input:
az
a[a-z]z
a[a-z][a-z]z
Which means:
Match the string "az"
Match a string with "a" and then a character from "a" to "z" and then a "z" character
Match a string with an "a" and then 2 characters from "a" to "z" and then a "z"
and then combine them with the 'or' operator (|)
az|a[a-z]z|a[a-z][a-z]z
Which means match on all three of those conditions.
A link to this example is here.
Doing it this way is a bit verbose so it can be improved by expressing this in a more compact way:
a[a-z]{0,2}z
This means:
Match an "a" then match a character from "a" to "z" 0, 1 or 2 times and then match a "z"
A link to this example is here
You use the method on ruby strings called match which takes in a regex object and then check the boolean return value.
Edit:
The ruby code would look something like this:
def nearby_az(string)
return string.match(/a[a-z]{0,2}z/) != nil
end
string.match() returns an object that you can query to get information about the match. If there is no match, string.match() will return nil.
!!("fjeioaeiz" =~ /a.{,2}z/) #=> true
!!("fjeioaz" =~ /a.{,2}z/) #=> true
!!("fjeioasbdz" =~ /a.{,2}z/) #=> false
Look, Ma! No regex!
def a_upto_4_z(str)
str.each_char.with_index.any? { |c,i| c == ?a && str[i+1,3].include?(?z) }
end
a_upto_4_z "rvaxxzo" #=> true
a_upto_4_z "rvaxxxzo" #=> false
a_upto_4_z "rvaxzo" #=> true
a_upto_4_z "rvazo" #=> true
a_upto_4_z "rvzao" #=> false
Edit: #Stefan makes a good point. Let's do it this way:
def mind_the_gap(str, max_gap=2)
gap = max_gap + 1 # or larger
str.each_char do |c|
case c
when ?z
return true if gap <= max_gap
when ?a
gap = 0
else
gap += 1
end
end
false
end
mind_the_gap "rvaxxzo" #=> true
mind_the_gap "rvaxxxzo" #=> false
mind_the_gap "rvaxzo" #=> true
mind_the_gap "rvazo" #=> true
mind_the_gap "rvzao" #=> false
Note it is not necessary to increment gap when c == ?z and gap > max_gap.
I have a string like this:
string1 = ",,"
I want to check if there are only commas in my string. Each time string1 changes, it can have any number of commas. How can I check this?
A regex:
Start of string: \A
Comma: , (since it has no special meaning in regexes)
0+ occurrences of the previous matcher: *
End of string: \z
Not \Z! That one's intended to ignore \n at the end, such as the result of readline
The condition is:
/\A,*\z/ =~ your_string
You can find number of ,s in string1 using this:
noc = string1.scan(/,/).size
# => 2
using this, you can verify if the string contains only ,s by doing something like this:
string1=",,"
string1.scan(/,/).size == string1.size
# true
string1=",1,"
string1.scan(/,/).size == string1.size
# false
Use negative range:
",," !~ /[^,]/
# => true
Just out of curiosity:
string1.tr(',', '').empty?
string1.delete(',').empty?
(string1.split('') - [',']).empty?
string1.codepoints.uniq == [44]
def all_commas? str
str.squeeze == ','
end
all_commas? ',' #=> true
all_commas? ',,,' #=> true
all_commas? '3,' #=> false
all_commas? ' ,,,' #=> false
all_commas? ',9,,' #=> false
I'd use this :
string.each_char.all? { |c| c == ',' }
# && !string.empty? if the empty string is not valid
I think it's pretty expressive
try this
string1=",,"
if string1.count(',,') == string1.length
#here is your code
end
The question is very simple and probably have thousand of answers, but i am looking for some magical ruby function.
Problem:
To determine whether a letter is upcase or not i.e belongs to A-Z.
Possible Solution:
array = ["A","B", ....., "Z"]
letter = "A"
is_upcase = array.include? letter
Please note that "1" is not an uppercase letter.
Is there any magical ruby function which solve the problem with less code?
You can use POSIX character classes:
/[[:lower:]]/ - Lowercase alphabetical character
/[[:upper:]]/ - Uppercase alphabetical
Example:
def which_case(letter)
case letter
when /[[:upper:]]/
:uppercase
when /[[:lower:]]/
:lowercase
else
:other
end
end
which_case('a') #=> :lowercase
which_case('ä') #=> :lowercase
which_case('A') #=> :uppercase
which_case('Ä') #=> :uppercase
which_case('1') #=> :other
Or with a simple if statement:
puts 'lowercase' if /[[:lower:]]/ =~ 'a'
#=> lowercase
Use ===
?> ('A'..'Z') === 'C'
=> true
>> ('A'..'Z') === 'c'
=> false
>> ('A'..'Z') === '1'
=> false
>> ('A'..'Z') === '&'
=> false
>> ('A'..'Z') === 'Z'
=> true
Also lacks support for umlauts, diacritcs etc. und needs ActiveSupport, but I like the syntax:
'A'.in?('A'..'Z') # => true
'a'.in?('A'..'Z') # => false
'A' =~ /[A-Z]/ #=> 0 (boolean true)
'a' =~ /[A-Z]/ #=> nil (boolean false)
def is_upcase? x
('A'..'Z').cover? x
end
Edit: .cover? is a new function in 1.9 that checks if value is in range by only checking the endpoints. In that way the computer does not need to convert it into an array and store it in memory, making it faster.
It is basically another way of writing x >= 'A' && x <= 'Z'
x >= 'A' && x <= 'Z'
There are several ways to check if the character is uppercase
# false
c = 'c'
p c=~/[A-Z]/
p c==c.upcase
p /[A-Z]/===c
p (?A..?Z)===c
p ?A<=c&&c<=?Z
p (?A..?Z).cover?c
p c=~/[[:upper:]]/
p /[[:upper:]]/===c
# true
C = 'C'
p C=~/[A-Z]/
p C==C.upcase
p /[A-Z]/===C
p (?A..?Z)===C
p ?A<=C&&C<=?Z
p (?A..?Z).cover?C
p C=~/[[:upper:]]/
p /[[:upper:]]/===C
=~ returns a nil or 0.
!!nil == false; !!0 == true.
P.S. Not all of them works in the same way
'.' == '.'.upcase => true but it's not a capital letter
If you need to check letters with diphthongs use
/[[:upper:]]/==='Ñ' => true as expected
In this case you can to add interested letters manually:
/[A-ZÑ]/==='Ñ' => true
Why isnt that working:
>> s = "hi"
=> "hi"
>> s == ("hi"|"ho")
NoMethodError: undefined method `|' for "hi":String
from (irb):2
>>
I don't get it.. Is there a solution for this kind of syntax? Because
s == ("hi"|"ho")
#is shorther than
s == "hi" || s == "ho"
Yes, the bitwise operator | is not defined in the String class: http://ruby-doc.org/core/classes/String.html
Consider this for expressiveness:
["hi", "ho"].include? myStr
irb(main):001:0> s = "hi"
=> "hi"
irb(main):002:0> ["hi", "ho"]
=> ["hi", "ho"]
irb(main):003:0> ["hi", "ho"].include? s
=> true
irb(main):004:0> s = "foo"
=> "foo"
irb(main):005:0> ["hi", "ho"].include? s
=> false
In most high level languages that syntax will not work, you have to stick to the longer syntax of:
s == "hi" || s == "ho"
Note that | is a bitwise or, whereas || is a regular or
You could use the include? method on array if you've got several == tests to do:
["hi", "ho"].include?(s)
Not shorter for two checks admittedly but it will be shorter for three or more.
This syntax doesn't exist in any language as far as I know.
What you are saying
s == ("hi"|"ho")
Literally translates to 'bitwise OR the strings "hi" and "ho" together and then compare them with s'. If you can't see why this is not what you are looking for, try writing down the ASCII codes for "hi" and "ho" and then bitwise ORing them together. You are going to get complete gibberish.
You could make it work that way:
irb> class Pair
def initialize(strA,strB)
#strA,#strB = strA,strB
end
def ==(string)
string == #strA || string == #strB
end
def |(other)
Pair.new(self,other)
end
end
#=> nil
irb> class String
def |(other)
Pair.new(self,other)
end
alias old_equals :==
def ==(other)
if other.kind_of? Pair
other == self
else
old_equals other
end
end
end
#=> nil
irb> ("one"|"two") == "one"
#=> true
irb> ("one"|"two") == "two"
#=> true
irb> ("one"|"two") == "three"
#=> false
irb> "one" == ("one"|"two")
#=> true
irb> "three" == ("one"|"two"|"three")
#=> true
But since this involves some monkey-patching of a fairly lowlevel class, I wouldn't advise relying on it. Other people will hate reading your code.
Ruby supports binary 'or' and other binary operations on values of type Fixnum and Bignum, meaning any integer. Bitwise operations aren't supported on strings or any other type, as far as I know.
As other people have mentioned, you probably want something other than binary operations altogether. However, you can easily get integer representations of characters, so you can compare characters like so:
a = "Cake"
b = "Pie"
puts a[0] | b[0] # Prints "83" - C is 67 and P is 80.
You can get an array of the comparisons easily with some conversions.
a = "Cake"
b = "Pie " # Strings of uneven length is trivial but more cluttered.
a_arr = a.split(//)
b_arr = b.split(//)
c_arr = []
a.each_with_index { |char, i| c.push(a[i].to_i | b[i].to_i) }
# If you *really* want an ASCII string back...
c = c_arr.collect(&:chr).join
You could use a regex:
Like so:
regex = /hi|ho/
s = "hi"
t = "foo"
s =~ regex
#=> 0
t =~ regex
#=> nil