The local commuter railroad services a number of towns in
Kiwiland. Because of monetary concerns, all of the tracks are
''one-way.''
That is, a route from Kaitaia to Invercargill does not imply the
existence
of a route from Invercargill to Kaitaia. In fact, even if both of these
routes do happen to exist, they are distinct and are not necessarily the
same distance!
My problem is how to computer the number of trips between two station
with exactly some stops,for example ,A to C with exactly 4stops. In the
sample data below, there are three such trips: A to C (via B,C,D); A
to C (via D,C,D); and A to C (via D,E,B).
For the test ,the towns are named using the first few letters of the
alphabet from A to D. A route between two towns (A to B) with a
distance of 5 is represented as AB5.
Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7
Expected Output:
Output #7: 3
My controller method is below,but i know it is wrong,how do i write it
to make it work?Thank you!
def exa_with_stops(s1,s2,esn)
pair_group=find_route(s1,s2)
for p in pair_group
if p[1,1]==[2,1]
$i=2+$n*2
elsif Route.find_by_name(p[1,1]+p[2,1])
$i=3+$n*2
else
$i=2+$n*2
end
end
$n+=1
if $i<esn
for p in pair_group
exa_with_stops(s1,s2,esn)
end
else $i==esn
end
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Looks suspiciously like an instance of the pattern "do my thoughtworks code review for me" Almost word for word.... ;-) --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
hello:
I do not know the pattern you referenced.Can you help me to complete
that method?
def exa_with_stops(s1,s2,esn)
pair_group=find_route(s1,s2)
for p in pair_group
if p[1,1]==[2,1]
$i=2+$n*2
elsif Route.find_by_name(p[1,1]+p[2,1])
$i=3+$n*2
else
$i=2+$n*2
end
end
$n+=1
if $i<esn
for p in pair_group
exa_with_stops(s1,s2,esn)
end
else $i==esn
end
end
dafydd wrote:> Looks suspiciously like an instance of the pattern "do my thoughtworks
> code review for me"
>
> Almost word for word....
>
>
> ;-)
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Jan-21 07:43 UTC
how to find exactly number of diffrent route for two station
hello:
The following is my task:
The local commuter railroad services a number of towns in
Kiwiland. Because of monetary concerns, all of the tracks are
''one-way.''That is, a route from Kaitaia to Invercargill does
not imply
the
existence of a route from Invercargill to Kaitaia. In fact, even if
both of these routes do happen to exist, they are distinct and are not
necessarily the
same distance!
My task is how to computer the number of trips between two station
with exactly some stops,for example ,A to C with exactly 4stops. In the
sample data below, there are three such trips: A to C (via B,C,D); A
to C (via D,C,D); and A to C (via D,E,B).
For the test ,the towns are named using the first few letters of the
alphabet from A to D. A route between two towns (A to B) with a
distance of 5 is represented as AB5.
Graph: AB5, BC4, CD8, DC8, DE6, AD5, CE2, EB3, AE7
I have design my method,but it works only for some situation.For
example,the route number for A to E with exactly x stops:the result for
1 or 2 or 3 or 4 stops is right,but for 5 or 6 or...,the result is
wrong,they all are zero:0.
The following are my methods.By the way,my model is Route in which i
can get the route_name,distance,start_station,end_station.For example,AE
route have the
attribute:route_name="AE",distance=5,start_station="A",end_station="B".The
Route model works fine for my other method,so i did not paste the model
code here.Please help me to find where are wrong?Thank you very much!
def exa_stops #this
@stops=params[:exastops].to_i
if @stops==1
onestop_route=route_with_onestop(params[:ss1],params[:es1])
if onestop_route
@exa_stops_number=1
else
@exa_stops_number=0
end
else
l=exa_with_stops(params[:ss1],params[:es1],@stops).length
@exa_stops_number=l
end
end
private
def find_route(s1,s2)
start_routes=Route.find_all_by_start_station(s1)
end_routes=Route.find_all_by_end_station(s2)
middle_array=Array.new
for s in start_routes
for e in end_routes
middle_array<<s.name+e.name
end
end
pair_group=middle_array
if pair_group.length !=0
for p in pair_group
pair_group.delete(p) if p[2,1]=="A"
end
return pair_group
end
end
def route_with_onestop(s1,s2)
Route.find_by_name(s1+s2)
end
def exa_with_stops(s1,s2,esn)
if esn==2 || esn==3
@exastops_array=Array.new
@pair_group=find_route(s1,s2)
for p in @pair_group
if p[1,1]==p[2,1]
@exastops_array<<2
end
if Route.find_by_name(p[1,1]+p[2,1])
@exastops_array<<3
end
@part_of_exastops_array=Array.new
@exastops_array.each {|e| @part_of_exastops_array<<e if e==esn}
end
else
@n=1
@exastops_array=Array.new
@pair_group=find_route(s1,s2)
@middle_array=@pair_group
@exaroutes_array=Array.new
if (esn%2)==0
@i=esn/2
else
@i=(esn-1)/2
end
while(@n<@i)
for m in @middle_array
@exaroutes_array=@exaroutes_array+find_route(m[1,1],m[2,1])
end
for e in @exaroutes_array
if e[1,1]==e[2,1]
s=2+2*@n
@exastops_array<<s
end
if Route.find_by_name(e[1,1]+e[2,1])
s=3+2*@n
@exastops_array<<s
end
end
@middle_array=@exaroutes_array
@exaroutes_array.clear
@n+=1
end
if @n==@i
@part_of_exastops_array=Array.new
@exastops_array.each {|e| @part_of_exastops_array<<e if e==esn}
end
end
return @part_of_exastops_array
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Jan-21 11:00 UTC
Re: how to find exactly number of diffrent route for two station
I manage it with another method ,but i is still wrong.The code is:
def exa_stops
exa_with_stops(params[:ss1],params[:es1],params[:stops],0)
end
private
def find_route(s1,s2)
start_routes=Route.find_all_by_start_station(s1)
end_routes=Route.find_all_by_end_station(s2)
middle_array=Array.new
for s in start_routes
for e in end_routes
middle_array<<s.name+e.name
end
end
pair_group=middle_array
if pair_group.length !=0
for p in pair_group
pair_group.delete(p) if p[2,1]=="A"
end
return pair_group
end
end
def route_with_onestop(s1,s2)
Route.find_by_name(s1+s2)
end
def exa_with_stops(s1,s2,esn,n)
if $exastops_array.include?(esn)
@part_of_exastops_array=Array.new
$exastops_array.each{|e|@part_of_exastops_array<<e if e==esn}
return @part_of_exastops_array.length
else
pair_group=find_route(s1,s2)
@n=n
for p in pair_group
if p[1,1]==p[2,1]
s=2+@n*2
$exastops_array<<s
exa_with_stops(p[1,1],p[2,1],esn,@n+1)
elsif Route.find_by_with_name(p[1,1]+p[2,1])
s=3+@n*2
$exastops_array<<s
exa_with_stops(p[1,1],p[2,1],esn,@n+1)
else
exa_with_stops(p[1,1],p[2,1],esn,@n+1)
end
end
end
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Keynan Pratt
2008-Jan-21 12:15 UTC
Re: how to find exactly number of diffrent route for two sta
You may want to start over with some heavy BDD. -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Jan-21 12:42 UTC
Re: how to find exactly number of diffrent route for two sta
I do not know your means.Anyone can help me? Keynan Pratt wrote:> You may want to start over with some heavy BDD.-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Keynan Pratt
2008-Jan-21 13:00 UTC
Re: how to find exactly number of diffrent route for two sta
http://www.youtube.com/watch?v=oOFfHzrIDPk&feature=related -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Keynan Pratt
2008-Jan-21 13:08 UTC
Re: how to find exactly number of diffrent route for two sta
You should be able to modify this pretty easily to get you the routes
from any given starting point to any given ending point at a set
distance.
Lets assume this is rails and not just ruby Were working with.
#
class Station < ActiveRecord::Base
has_and_belongs_to_many :stations_ahead,
:class_name => ''Station'',
:foreign_key => :beginning_station_id,
:association_foreign_key => :ending_station_id
def find_routes(destination, distance)
return false if self == destination and distance > 0
return [[ self ]] if self == destination
return false if distance <= 0
result = false
stations_ahead.each do |station|
sub_routes = station.find_routes(destination, distance-1)
if sub_routes
sub_routes.each do |sub_route|
sub_route.push self
end
result = sub_ruoutes
end
end
return result
end
end
class TrainTrack < ActiveRecord::Base
#
# Has Feilds:
# beginning_station_id
# ending_station_id
#
end
--
Posted via http://www.ruby-forum.com/.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google Groups
"Ruby on Rails: Talk" group.
To post to this group, send email to
rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
To unsubscribe from this group, send email to
rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org
For more options, visit this group at
http://groups.google.com/group/rubyonrails-talk?hl=en
-~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Jan-22 04:16 UTC
Re: how to find exactly number of diffrent route for two sta
hello: Thank you!But i want to resolve this problem:the number of different route from one station to another station with exactly some stops. My model is Route in which i can get the route_name,distance,start_station,end_station.For example,AE route have the attribute:route_name="AE",distance=5,start_station="A",end_station="B". -- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Ryan Bigg
2008-Jan-22 04:22 UTC
Re: how to find exactly number of diffrent route for two sta
Guo, How about setting up a table of the stations and then a table of the routes that has a serialized field containing the ids of the stops along the way (something like stops_id)? --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---
Guo Yangguang
2008-Jan-23 12:30 UTC
Re: how to find exactly number of diffrent route for two sta
hello: Thank you Ryan Bigg and Keynan Pratt ,i have resolve my question.Maby if i modify my database table ,i can do it .I will try it.I have managed it in my method design in another way.Thank you!Good luck! Ryan Bigg wrote:> Guo, > > How about setting up a table of the stations and then a table of the > routes > that has a serialized field containing the ids of the stops along the > way > (something like stops_id)?-- Posted via http://www.ruby-forum.com/. --~--~---------~--~----~------------~-------~--~----~ You received this message because you are subscribed to the Google Groups "Ruby on Rails: Talk" group. To post to this group, send email to rubyonrails-talk-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org To unsubscribe from this group, send email to rubyonrails-talk-unsubscribe-/JYPxA39Uh5TLH3MbocFFw@public.gmane.org For more options, visit this group at http://groups.google.com/group/rubyonrails-talk?hl=en -~----------~----~----~----~------~----~------~--~---