Comparing a value(the current time) to a list of values

Post your questions and help other users.

Moderator: Martin

Post Reply
User avatar
Skoinks
Posts: 6
Joined: 17 Nov 2014 13:12

Comparing a value(the current time) to a list of values

Post by Skoinks » 25 Nov 2014 23:29

Hey guys.

I wasn't sure exactly how to explain it in the thread title, so sorry if it's a little underscriptive.

Right.
I have a text file that contains the timetable for the bus I need to take every morning.

Code: Select all

0800
0815
0825
0830
...
1820
1850
1920
1950
2050
2150
2300
I want my phone to display the time for the next bus every day.
In other words, how can I compare the current system time to the list and determine what is the nearest time in it. Finding the current time in the same format wouldn't be too hard, I think. Something like

Code: Select all

time = "{getDate(),dateformat,kk:mm}";
Comparing that to the values on the list though - that's what I'm trying to figure out.
It would have to display the time of the next bus. So, if it's 18:28 on my clock, I'd want it to display the 18:50 bus. (The text can be shown on a widget perhaps)
I'm absolutely stuck. Any ideas on how to approach this?

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Comparing a value(the current time) to a list of values

Post by Bushmills » 26 Nov 2014 00:49

You' probably start by creating a list with departure times as elements, sorted, so partitioning that list into past and future becomes easier.
The best way for finding the elements around which list gets partitioned will presumably depend on lowest frequency of departure, the number of busses going each day, and the effort you're willing to put into this. A binary search is considered an efficient way for this. It goes like: look at list element in the middle of the list, compare against time now. according the outcome, look again in the middle of the "past" or "future" part. Keep on comparing and dividing list until you got to the point seperating past and future.
This is way more efficient than starting at first element, then the next, and the next until you found the first "future" element. But the latter approach is considerably simpler to code. You could make this much more efficient by having a second list, with one element for each hour, and a value which is the element number of first departure in that hour. This second list you could then use as a starting point, and run one by one exclusively through the busses leaving in that hour.

These solutions are suited for not frequently determining next departure time, sporadically. If you intend to update the display periodically, you could also think of comparing now with next known departure time. Once it matches or is in the past, progress to next future departure time in that list. This is quick and simple, but won't do efficiently when you intend to just throw a time at it, and get the next departure as result.

Organising departure times in a balanced binary tree, with branches going to earlier and later departures, is also efficient, at run time, but Automagic lacks the proper data structures for representing data as such a tree - you'd use a a list to emulate such a tree. As consequence, the binary search is probably preferable, because fitting the available lists data structure better.

There are more solutions possible. Maybe a simpler one can be found, but right now I couldn't think of any (apart from one needing a list of 24x60 elements which is probably too much to handle comfortably - this approach would simply map next departure time (or minutes until next departure) to any minute of the day, again using a list).

A great simplification for this would exist if you could get Martin to add APL like scalar operations to Automagic, but he'd probably throw angry glances at me for merely suggesting this.

User avatar
Skoinks
Posts: 6
Joined: 17 Nov 2014 13:12

Re: Comparing a value(the current time) to a list of values

Post by Skoinks » 26 Nov 2014 12:45

Thank you for the quick reply!

Reading through it, I'm getting the impression that I'm going a bit over my head here. Thankfully, I found what I think is is a good alternative. While trying to find a list of bus-stops in my surrounding area, I stumbled upon this:
http://data.gov.uk/dataset/naptan

Which is the UK government's API for civil transportation related matters. In the CSV file, there is a rather large (200 mb or so) excel file that contains the data for every single bus and train stop in the UK, with its GPS coordinates and its atco code (which is what I need for the next step)

https://developer.transportapi.com/docu ... nformation

I had to register here and use my key, plus the atco code of the bus stop I needed to get to a link which contains a json file with the bus times in the next hour. I still haven't exactly figured out how to get only the time from all of that - an XML file would've been more convenient; alas, there is no option for that.
Anyway, I found a couple of tutorials on how to filter through the json file, so hopefully I'll be able to do it in a rather simpler way.

Again, thank you for the very detailed reply! I'm really sorry it ended up being for nothing - hopefully someone else will benefit from it!

User avatar
Bushmills
Posts: 286
Joined: 23 Sep 2014 21:56

Re: Comparing a value(the current time) to a list of values

Post by Bushmills » 26 Nov 2014 13:22

maybe running a shell command is an easier solution:
Have a file, with one departure time each line. Call it "times".
With current time in environment variable "now", this produces next departure time:

Code: Select all

awk -v now="0700" '$1 > now { print $1; exit }' times
Variable now is here assigned to statically for test purposes, you want the actual time (as integer, same format as departure times) in there. Uncaught condition in that command is if the last bus of the day has already left, as the result will be nothing instead of first bus next day.

mcyber
Posts: 37
Joined: 08 Nov 2013 14:21

Re: Comparing a value(the current time) to a list of values

Post by mcyber » 26 Nov 2014 14:49

As suggested by Bushmills, probably a sorted list (ascending) is the best structure available in AM for the purpose, and the binary search an elegant solution, with the data structures available in AM. The shell solution is even better.

Another strategy, more "brutal", is to cycle through the sorted list, sequentially, until you find an element whose value is greater than the present time. This value is the one you're looking for. When found you can do the corresponding math to calculate the time left, or whatever manipulation you need.

Not the most elegant solution, but very quick and easy to implement and "efficient" enough if you consider that you are expected to compare some tens of values, maximum.

User avatar
Skoinks
Posts: 6
Joined: 17 Nov 2014 13:12

Re: Comparing a value(the current time) to a list of values

Post by Skoinks » 26 Nov 2014 15:24

Image

Here it is, showing the next bus times out and in from where I live (they happen to be the same right now).

Post Reply