67
\$\begingroup\$

Inspired by, and in memory of, my dear friend and colleague,

Dan Baronet

Dan Baronet, 1956 – 2016. R.I.P.

He found the shortest possible APL solution to this task:

Task

Given a Boolean list, count the number of trailing truth values.

Example cases

{}0

{0}0

{1}1

{0, 1, 1, 0, 0}0

{1, 1, 1, 0, 1}1

{1, 1, 0, 1, 1}2

{0, 0, 1, 1, 1}3

{1, 1, 1, 1, 1, 1}6

\$\endgroup\$
7
  • \$\begingroup\$ Can we take the list as a string of zeros and ones? e.g. 01100? \$\endgroup\$
    – Adnan
    Nov 6, 2016 at 12:57
  • \$\begingroup\$ @Adnan only if that is the most normal way for your language to represent boolean lists. \$\endgroup\$
    – Adám
    Nov 6, 2016 at 15:42
  • 78
    \$\begingroup\$ Sorry for your loss. \$\endgroup\$ Nov 6, 2016 at 17:03
  • 10
    \$\begingroup\$ @MartinEnder Thank you. It will be tough going forward. Dan taught me all I needed to know to work for Dyalog. \$\endgroup\$
    – Adám
    Nov 6, 2016 at 17:32
  • 7
    \$\begingroup\$ Farewell to Dan. RIP... \$\endgroup\$ Nov 7, 2016 at 12:46

62 Answers 62

1 2
3
0
\$\begingroup\$

MBASIC, 112 bytes

1 INPUT B$:T=0:FOR I=LEN(B$) TO 1 STEP -1:C$=MID$(B$,I,1):IF C$="0" THEN 4
2 IF C$="1" THEN T=T+1
3 NEXT
4 PRINT T

Just wanted to see if I could do it.

Explanation

Input is a string of 1's and 0's. String is traversed from right to left. If the current digit is a 0, bail out and print the total. If the digit is a 1, increment the total and continue to loop.

Output

? 01100
 0

? 11011
 2

? 11101
 1

? 111111
 6
\$\endgroup\$
0
\$\begingroup\$

05AB1E, 3 bytes

γθO

Try it online!

γθO  # full program
  O  # sum of...
 θ   # last...
γ    # group of consecutive equal elements in...
     # implicit input
     # implicit output
\$\endgroup\$
1 2
3

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

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