Holes (Easy)

One fateful day, 6 people numbered 1,2,3,4,5 and 6 are trying to cross a road that contains several deep holes. To pass through a hole with depth 3, the 3 leading people have to go into the holes in order so that the other people can safely cross the hole. Then, the last people who crossed the hole will pull the highest people from the hole up, and so on. For clarity, look at the diagram below where 4 people are crossing a hole with depth 3:

If the entering sequence is 1 , 2 , 3 , 4 , 5 , 6 1,2,3,4,5,6 and the holes they must pass through in order has depth of 3 , 5 , 2 3,5,2 respectively, what is the output sequence ?

If you think the output sequence is 6 , 5 , 4 , 3 , 2 , 1 6,5,4,3,2,1 , input your answer as 654321 .


The answer is 365421.

This section requires Javascript.
You are seeing this because something didn't load right. We suggest you, (a) try refreshing the page, (b) enabling javascript if it is disabled on your browser and, finally, (c) loading the non-javascript version of this page . We're sorry about the hassle.

3 solutions

Snehal Tekale
May 15, 2016

Let us make it easy.... Initial order: 123456 First hole depth: 3 People who go in the first hole: 123 Order in which the three people come out: 321 Order after all the people have crossed the first hole: 456321 Second hole depth: 5 People who go in the second hole: 45632 Order in which the three people come out: 23654 Order after all the people have crossed the first hole: 123654 Third hole depth: 2 People who go in the third hole: 12 Order in which the three people come out: 21 Order after all the people have crossed the third hole: 365421

So the answer is 365421

Mayank Singh
Jun 9, 2016

I've made a general solution on python

https://brilliant.org/problems/holes-easy/

def hole(arr,siz) :

newarr=[]

for i in range(siz,len(arr)):

    newarr.append(arr[i])

for i in range((siz-1),-1,-1):

    newarr.append(arr[i])

return newarr

print "Holes general version"

lis=list(raw_input("Enter space seperated initial sequence : ").split())

while True:

ch=input("Enter the size of hole (Enter -1 to stop)")

if ch==-1:

    break

lis=list(hole(lis,ch))

print lis

Daniel Venable
Oct 8, 2018

Haskell solution:

solve (hole:holes) guys = solve holes $ (drop hole guys) ++ (reverse $ take hole guys)
solve [] guys = guys

Only two lines!

0 pending reports

×

Problem Loading...

Note Loading...

Set Loading...