I have a question in my work,please help me!

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+$n2
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

Looks suspiciously like an instance of the pattern “do my thoughtworks
code review for me”

Almost word for word…

:wink:

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+$n2
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…

:wink:

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§ 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+@n2
$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

You may want to start over with some heavy BDD.

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§ 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

I do not know your means.Anyone can help me?

Keynan P. wrote:

You may want to start over with some heavy BDD.

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”.

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 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

hello:
Thank you Ryan B. and Keynan P. ,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 B. 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)?