Skip to content

Instantly share code, notes, and snippets.

@aalvesjr
Last active May 25, 2017 17:33
Show Gist options
  • Save aalvesjr/d0800a0cbfe9e6855bf968d5a6f9f844 to your computer and use it in GitHub Desktop.
Save aalvesjr/d0800a0cbfe9e6855bf968d5a6f9f844 to your computer and use it in GitHub Desktop.
Train problem
position_train = 0
def train_leaving_station(station, train)
return if station.empty?
if station.last == train[position_train]
station.pop
position_train++
train_leaving_station(station, train)
end
end
def possible_sequence?(train)
entry = (1..train.size).to_a # [1,2,3,4,5]
station = []
for(i =0 ; i < vagoes ; i++) {
number = entry[i]
station.push(number)
train_leaving_station(station, train)
}
station.empty?
end
puts "[1,2,3,4,5] #{possible_sequence?([1,2,3,4,5])}" # => true
puts "[5,4,3,2,1] #{possible_sequence?([5,4,3,2,1])}" # => true
puts "[5,4,1,2,3] #{possible_sequence?([5,4,1,2,3])}" # => false
puts "[1,3,2,5,4,6] #{possible_sequence?([1,3,2,5,4,6])}" # => true
__END__
5 = vagoes
12345
54321
0
6
123456
132546
0
0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment