Skip to main content
Code Review

Return to Question

deleted 8 characters in body; edited tags
Source Link
Jamal
  • 35.2k
  • 13
  • 134
  • 238

We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.

gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false

I have written two functions to solve this problem, but I have some concerns:

  • Of the one with Regex, I don't like all the special cases.
  • Of the one with iteration, I don't like the weird helper method.

Both pass a fair number of tests and work correctly, which. Which one is better?

regex_geppy.rbregex_geppy.rb

def geppy(string)
 return true if ! string.include?('g')
 return false if [0, 1].include?(string.length)
 return false if string.length == 2 && string != 'gg'
 return (not string.match("[^g]g[^g]"))
end

array_geppy.rbarray_geppy.rb

def non_wrap_get(array, index)
 index < 0 ? nil : array[index]
end
def geppy(string)
 string
 .chars
 .each_with_index
 .all? { |curr, i| 
 curr == 'g' ? 
 non_wrap_get(string, i - 1) == 'g' || non_wrap_get(string, i + 1) == 'g' 
 : true
 }
end

geppy_testsgeppy_tests

geppy("xxggxx") #=> true
geppy("xxgxx") #=> false
geppy("xxggyygxx") #=> false
geppy("g") #=> false
geppy("ag") #=> false
geppy("gg") #=> true
geppy("") #=> true
geppy("ab") #=> true
geppy("aaaa") #=> true
geppy('g'*1000) #=> true

We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.

gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false

I have written two functions to solve this problem, but I have some concerns:

  • Of the one with Regex I don't like all the special cases
  • Of the one with iteration, I don't like the weird helper method.

Both pass a fair number of tests and work correctly, which one is better?

regex_geppy.rb

def geppy(string)
 return true if ! string.include?('g')
 return false if [0, 1].include?(string.length)
 return false if string.length == 2 && string != 'gg'
 return (not string.match("[^g]g[^g]"))
end

array_geppy.rb

def non_wrap_get(array, index)
 index < 0 ? nil : array[index]
end
def geppy(string)
 string
 .chars
 .each_with_index
 .all? { |curr, i| 
 curr == 'g' ? 
 non_wrap_get(string, i - 1) == 'g' || non_wrap_get(string, i + 1) == 'g' 
 : true
 }
end

geppy_tests

geppy("xxggxx") #=> true
geppy("xxgxx") #=> false
geppy("xxggyygxx") #=> false
geppy("g") #=> false
geppy("ag") #=> false
geppy("gg") #=> true
geppy("") #=> true
geppy("ab") #=> true
geppy("aaaa") #=> true
geppy('g'*1000) #=> true

We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.

gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false

I have written two functions to solve this problem, but I have some concerns:

  • Of the one with Regex, I don't like all the special cases.
  • Of the one with iteration, I don't like the weird helper method.

Both pass a fair number of tests and work correctly. Which one is better?

regex_geppy.rb

def geppy(string)
 return true if ! string.include?('g')
 return false if [0, 1].include?(string.length)
 return false if string.length == 2 && string != 'gg'
 return (not string.match("[^g]g[^g]"))
end

array_geppy.rb

def non_wrap_get(array, index)
 index < 0 ? nil : array[index]
end
def geppy(string)
 string
 .chars
 .each_with_index
 .all? { |curr, i| 
 curr == 'g' ? 
 non_wrap_get(string, i - 1) == 'g' || non_wrap_get(string, i + 1) == 'g' 
 : true
 }
end

geppy_tests

geppy("xxggxx") #=> true
geppy("xxgxx") #=> false
geppy("xxggyygxx") #=> false
geppy("g") #=> false
geppy("ag") #=> false
geppy("gg") #=> true
geppy("") #=> true
geppy("ab") #=> true
geppy("aaaa") #=> true
geppy('g'*1000) #=> true
Source Link
Caridorc
  • 28.1k
  • 7
  • 54
  • 137

The Geppy String: Regex vs Iteration

We'll say that a lowercase 'g' in a string is "happy" if there is another 'g' immediately to its left or right. Return true if all the g's in the given string are happy.

gHappy("xxggxx") → true
gHappy("xxgxx") → false
gHappy("xxggyygxx") → false

I have written two functions to solve this problem, but I have some concerns:

  • Of the one with Regex I don't like all the special cases
  • Of the one with iteration, I don't like the weird helper method.

Both pass a fair number of tests and work correctly, which one is better?

regex_geppy.rb

def geppy(string)
 return true if ! string.include?('g')
 return false if [0, 1].include?(string.length)
 return false if string.length == 2 && string != 'gg'
 return (not string.match("[^g]g[^g]"))
end

array_geppy.rb

def non_wrap_get(array, index)
 index < 0 ? nil : array[index]
end
def geppy(string)
 string
 .chars
 .each_with_index
 .all? { |curr, i| 
 curr == 'g' ? 
 non_wrap_get(string, i - 1) == 'g' || non_wrap_get(string, i + 1) == 'g' 
 : true
 }
end

geppy_tests

geppy("xxggxx") #=> true
geppy("xxgxx") #=> false
geppy("xxggyygxx") #=> false
geppy("g") #=> false
geppy("ag") #=> false
geppy("gg") #=> true
geppy("") #=> true
geppy("ab") #=> true
geppy("aaaa") #=> true
geppy('g'*1000) #=> true
lang-rb

AltStyle によって変換されたページ (->オリジナル) /