0

New to ruby. I have an array created by nokogiri like this :

Array = ["10:31 Main title", ...]

It is a schedule in the format hour:minute title. Now I have a time, say 10:35 and I want to find the entry in the array with the nearest lower number (time and title). It is like what is playing now?

How can I do this in ruby? I am at a blank here...

Thank you

4
  • 1
    It means with the nearest lower number (time and title) ? Feb 3, 2014 at 22:27
  • Sorry. Output should be time (10:31) and title (main title) of the item with the nearest earlier time. And yes it would be a 24 time format and would already be sorted
    – Macrumpel
    Feb 4, 2014 at 13:28
  • @Macrumpel Why is the answer I provided not correct? With the var t1 set to 10:35, you get this: "#{a2.first} #{h1[a2.first].sort.first}" # => "10:36 C.H.U.D." Feb 4, 2014 at 18:23
  • Sorry. Your answer is very well. Thank you very much. It did solve my problem
    – Macrumpel
    Feb 5, 2014 at 12:37

4 Answers 4

4

You can achieve this using bsearch like below

a = [1, 4, 8, 11, 97]

a.bsearch {|x| x >= 7} # which results 8

1
  • +1 for pointing out Array#bsearch. Note that this method is available since Ruby 2.0
    – mikezter
    Feb 26, 2014 at 12:32
1

You're going to have to walk the array and parse each entry. You'll also have to take into consideration whether the times are 12-hour or 24-hour, e.g. "10:31 Main Title" does that mean 10:31 AM or PM (in 12 hour clock). If its a 24-hour clock then 10:31 is 10:31 [am] and you'll also have 22:31 to reflect 10:31 [pm].

So you could walk the array, parsing each entry and then building a new structure which you can sort by. Ultimately you can get the lowest value and then just find the index of that entry in the original array.

2
  • Very good points, you have mentioned. although did you get with the nearest lower number (time and title) ? Feb 3, 2014 at 22:31
  • I'm not sure what that means: with the nearest lower number (time and title) Feb 3, 2014 at 22:32
0
require 'date'
a1 = ["10:31 The Beastmaster", "10:36 C.H.U.D.", "11:30 Goonies", "11:30 Krull", "11:59 Batteries Not Included"]
#=> ["10:31 The Beastmaster", "10:36 C.H.U.D.", "11:30 Goonies", "11:30 Krull", "11:59 Batteries Not Included"]
h1 = {}; a1.each {|x| m = x.match(/(\d{1,2}:\d{2})\s+(\w.*)/); h1[m[1]] ||= []; h1[m[1]] << m[2]}; h1 # => hash with times as keys and array of titles as corresponding values
#=> {"10:31"=>["The Beastmaster"], "10:36"=>["C.H.U.D."], "11:30"=>["Goonies", "Krull"], "11:59"=>["Batteries Not Included"]}
t1 = DateTime.rfc3339('2014-02-03T10:35:00-08:00').to_time.to_i
#=> 1391452500
within_an_hour = 60 * 60
#=> 3600
t2 = t1 + within_an_hour
#=> 1391456100
a2 = h1.keys.partition {|x| x > Time.at(t1).strftime("%I:%M")}[0] # => all upcoming times
#=> ["10:36", "11:30", "11:59"]
h2 = {}; a2.each {|x| h2[x] = h1[x]}; h2 # => all upcoming show times with corresponding titles
#=> {"10:36"=>["C.H.U.D."], "11:30"=>["Goonies", "Krull"], "11:59"=>["Batteries Not Included"]}
a3 = a2.partition {|x| x < Time.at(t2).strftime("%I:%M")}[0] # => upcoming times within an hour
#=> ["10:36", "11:30"]
h3 = {}; a3.each {|x| h3[x] = h1[x]}; h3 # => upcoming show times with corresponding titles within an hour
#=> {"10:36"=>["C.H.U.D."], "11:30"=>["Goonies", "Krull"]}

using the above code in a method:

require 'date'
def what_is_playing_now(time, a1=["10:31 The Beastmaster", "10:36 C.H.U.D.", "11:30 Goonies", "11:30 Krull", "11:59 Batteries Not Included"])
  h1 = {}; a1.each {|x| m = x.match(/(\d{1,2}:\d{2})\s+(\w.*)/); h1[m[1]] ||= []; h1[m[1]] << m[2]}; h1 # => hash with times as keys and array of titles as corresponding values
  t1 = DateTime.rfc3339("2014-02-03T#{time}:00-08:00").to_time.to_i
  a2 = h1.keys.partition {|x| x > Time.at(t1).strftime("%I:%M")}[0] # => all upcoming times
  h2 = {}; a2.each {|x| h2[x] = h1[x]}; h2 # => all upcoming show times with corresponding titles
  "#{a2.first} #{h2[a2.first].sort.first}"
end
what_is_playing_now("10:35")
#=> "10:36 C.H.U.D."

sources:

0

Since your array contains strings starting with numbers, these can be nicely sorted naturally.

my_array.sort.reverse.find{ |i| i < "10:35" }

This will sort your array in ascending order, then reverse it, and finally return the first item for which the block returns true.

If you are at Ruby version > 2.0, you can also use Array#bsearch:

my_array.sort.bsearch{ |i| i < "10:35" }

This will sort your array and will then use a binary search approach to finding the desired item (Thanks @ala for pointing this out).

These simple lines of code expect the time to be in 24h format with leading zeros (i.e. hh:mm), since it depends on comparing the lines lexicographically.

3
  • Mike, you should perhaps note that this requires a 24-hour clock and leading zeros on times before 10:00. I think you mean it works because the time is at the beginning of the string, not because the strings start with numbers. Also, you don't need to sort; select, then max would probably be faster. Feb 4, 2014 at 0:15
  • Thanks @CarySwoveland i added a note about 24h format. the performance characteristics of max and sort are probably out of scope for this question.
    – mikezter
    Feb 24, 2014 at 17:04
  • for that to work, one would have the hh:mm strings to be actual numbers.
    – mikezter
    Feb 26, 2014 at 12:24

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.