There is a helicopter flying 500 meters from the ground. It can fly up 100 meters, which takes 1.0 secs. Or if it doesn't fly up it will fall 200 meters, which also takes 1.0 secs - This means if the instruction is anything other than "Fly" the helicopter falls. Build a program that takes input of a list of instructions, telling the helicopter when to fly and when not to. When the helicopter falls it should output the time counter.


Fly Dance Fly Golf Fly Fly


8.5 secs


1) Fly == Fly so +100 = 600m

2) Dance != Fly so -200 = 400m

3) Fly == Fly so +100 = 500m

4) Golf != Fly so -200m = 300m

5) Fly == Fly so +100 = 400m

6) Fly == Fly so +100 = 500m

7) NO INSTRUCTIONS so -200 = 300m

8) NO INSTRUCTIONS so -200 = 100m

9) NO INSTRUCTIONS so -100 = 0m (This takes 0.5 seconds)

Each instruction should be separated by a space, and if the instuction isn't fly, then automatically fall. When the program runs out of instructions automatically fall. Your program must output in (float)secs, and if it takes => 60.0 seconds display in mins and seconds eg. 1 mins 32.0 secs

Seconds are float decimal values, minutes are whole interger values. Also note capitalization does not matter your program should work for capitals or no capitals.

This is code-golf so the least NO bytes wins!

  • 3
    \$\begingroup\$ Could you clarify this? Perhaps a step by step walk through of the example. Is the word "fall" being used both for dropping 200 meters, and for reaching 0 meters? Perhaps using a different word for reaching 0 meters would make this clearer. Is the output the time when the helicopter reaches 0 meters? \$\endgroup\$ – trichoplax Nov 15 '16 at 18:47
  • 2
    \$\begingroup\$ Can we assume that if the helicopter is at 100m, and falls, it still takes 1 second to reach 0m, even though it usually takes 1 second to drop by 200m? \$\endgroup\$ – trichoplax Nov 15 '16 at 18:57
  • 6
    \$\begingroup\$ I think this could be a good question is it is asked more clearly maybe with some examples. May I suggest the Sandbox to get some feedback prior to posting next time? \$\endgroup\$ – ElPedro Nov 15 '16 at 18:59
  • 1
    \$\begingroup\$ if it dosen't fly up, it will fall 200m does that mean heli can crash and after crashing it should not fly anymore which means rest of the instructions should be ignored? Example : please tell me output if the input is this : fly fall fall fall fall fly fly fly fly \$\endgroup\$ – Mukul Kumar Nov 15 '16 at 19:26
  • 1
    \$\begingroup\$ The string "M min S secs" specifically, or are normal output formats like a list of [M,S] and other reasonable formats like "M:S" OK? If specifically, then can less than one minute output "0 min S secs"? \$\endgroup\$ – Jonathan Allan Nov 15 '16 at 19:53

Perl, 90 bytes

$_++,$a+=shift=~/Fly/?1:-2 while$a;
say+($=?"$= mins ":"")."$_ secs"

Accepts input as command-line parameters. Run as

perl -E '$a=5;$_++,$a+=shift=~/Fly/?1:-2 while$a>0;$==$_/60;$_%=60;say+($=?"$= mins ":" ")."$_ secs"' Fly Dance Fly Fly Golf

Starts $a at 5, and increases it by 1 every time it finds "Fly", decrements by 2 otherwise. Keeps going until $a is non-positive, and counts how many iterations it took to get there. $= is special and forces its value to be an integer, so $==$_/60 only takes the integer portion of the division, ignoring the rest. Everything else is self-explanatory, I feel.


Python, 100 92 89 bytes

lambda I:sum(reduce(lambda(a,s),i:(a+(a>0)*(-1,.5)[i=='Fly'],s+(a>0)),I.split(),(2.5,0)))

try it online


Burlesque, 64 bytes


Try it online!

wd         # Split into words
m{         # Map
 "Fly"==   # word is "Fly"?
 {1}{-2}IE # If fly 1 else -2
@5+]       # Prepend 5.0
q++pa      # Calculate partial sums
{0.>}TW    # Take until 0 or less
sa-.       # Length of this -1 (to account for pushed 5.0)
j[~2./     # Take the remaining height /2
.+         # Add this time 
############ Mins Secs ##############
J60./fo    # Calc number of mins 
J60.*      # N mins * 60
j.-        # Calc seconds over

If we don't have to deal with the mins/secs rule:

Burlesque, 49 bytes


Try it online!


Mathematica 211 bytes

(s=ToString;t=Length[#[[;;(FirstPosition[#,x_/;x<=0,1]-1/. 0->Length@#)]]]&[x=500+Accumulate[If[#=="Fly",100,-200]&/@(StringSplit@#)]];
If[#>=60,s@Floor[#/60]~~" min ",""]~~s@Mod[#,60]~~ " sec"&[t+x[[t]]/200.])&

This algorithm assumes the flight ends when the altitude reaches 0 for the first time. Looking at some of the other answers, I think people have the helicopter flying below ground level. I don't allow "touch and go" either.


in="Fly Dance Fly Golf Fly Fly";
(* find last positive before first nonpositive *)
t=Length[altList[[;;(%/. 0->l)]]]
If[%>=60,s@Floor[%/60]~~" min ",""]~~s@Mod[%,60]~~ " sec"

Scala, 88 bytes

s=>{val n=s.split(" ").count("Fly"==)*1.5+2.5
(if(n<60)""else n/60+"min ")+n%60+" secs"}


  val n=s.split(" ").count("Fly"==)*1.5+2.5
      (n/60)+"min "
  +" secs"

Uses the same formula as Copper's python answer.


PHP, 55 54 bytes


Loops through command line arguments; run with -r.

Height is divided by 100 to simplify output and calculation: If height is below zero (i.e. -100m),
adding half of it (-50 divided by 100: -.5) to the time removes half a second from the result.

That combined with the magic of Elvis golfed 5 6 bytes from my initial program.


Python 2, 85 91 bytes

print('%d mins'%(t/60))*(t>59),`t%60`,'secs'

Takes input on STDIN, surrounded by quotes (single or double). Simply counts the number of Flys there are in the input, performs some path to calculate the number of seconds, then formats it.

Now case-insensitive!

Fixed a bug with the same bytecount thanks to Jonathan Allan!

Saved 2 bytes thanks to Shebang!

  • \$\begingroup\$ Bit of a nitpick, but "Marty McFly went to the future" would print 4.0 secs rather than 2.5 secs. Fixable for one byte with t=(5+input().split().count('Fly')*3)/2. \$\endgroup\$ – Jonathan Allan Nov 17 '16 at 6:26
  • 1
    \$\begingroup\$ ...but you can golf that cost off with t=2.5+input().split().count('Fly')*1.5. \$\endgroup\$ – Jonathan Allan Nov 17 '16 at 6:29
  • 1
    \$\begingroup\$ Since I don't think leading spaces matter, this print statement should work: print('%d mins'%(t/60))*(t>59),``t%60``,'secs' (replace double ticks with single ticks). It only has a leading space if t < 60. \$\endgroup\$ – Kade Nov 18 '16 at 17:26
  • \$\begingroup\$ Also this would return a float for the minutes if it's a float above 60, i.e. 80.5 secs -> 1.3416666 minutes. \$\endgroup\$ – Kade Nov 18 '16 at 17:44
  • \$\begingroup\$ @Shebang It doesn't return a float for the minutes. The %d implicitly casts to an int -- ideone.com/JKOS0H. Thanks for the tip though! \$\endgroup\$ – Copper Nov 18 '16 at 18:54

AWK, 150 bytes

Below is a rather straightforward implementation in AWK, the nice thing about it is that you can run it interactively and feed it by typing commands by hand (just don't forget to remove RS=" ", if you want one command per line).


awk 'BEGIN{RS=" ";h=500;t=0}toupper($0)~/FLY/{h+=100;t+=1;next}{h-=200;t+=1;}END{t+=h/200;F=(t>59?"%d mins ":"%.0d") "%.1f secs";printf F,t/60,t%60;}'


echo "Fly Dance FLy Golf Fly Fly" |\
awk 'BEGIN{RS=" ";h=500;t=0}toupper($0)~/FLY/{h+=100;t+=1;next}{h-=200;t+=1;}END{t+=h/200;F=(t>59?"%d mins ":"%.0d") "%.1f secs";printf F,t/60,t%60;}'

8.5 secs

C 108

i=1,h=5;main(c,v)char**v;{for(;h>0;i++)h+=1-3*!!strcmp(v[i<c?i:0],"Fly");printf("%f secs\n",i-(h<0?1.5:1));}

Try it online!


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.