52
\$\begingroup\$

Intro

Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. This is a language agnostic edition of this year's eighth problem.

I have explicit permission to post this challenge here from the original author of the competition. Feel free to verify by following the provided link and contacting the author.

Problem

Given a Boolean* list, "turn off" all the Truthies after the first Truthy.

No Truthies? No problem! Just return the list unmodified.

Examples

[falsy,truthy,falsy,truthy,falsy,falsy,truthy][falsy,truthy,falsy,falsy,falsy,falsy,falsy]

[][]

[falsy,falsy,falsy,falsy][falsy,falsy,falsy,falsy]


* All your truthies must be identical, and all your falsies must be identical. This includes output.

\$\endgroup\$
24
  • 2
    \$\begingroup\$ Can we use bit lists or other truthy/falsy list representations that are more natural in our language of choice? \$\endgroup\$ May 7 '17 at 20:32
  • 2
    \$\begingroup\$ Well yeah, if you talk about "truthy" and "falsy" in the challenge instead of "booleans", "true" and "false". ;) \$\endgroup\$ May 7 '17 at 20:36
  • 1
    \$\begingroup\$ I'm not clear on the booleans. Can we use 0/1 even if our language has True/False? \$\endgroup\$
    – xnor
    May 7 '17 at 20:59
  • 1
    \$\begingroup\$ @xnor Ah, good point. I think it would be fair to allow choosing input, but output must match, don't you think so? \$\endgroup\$
    – Adám
    May 7 '17 at 21:12
  • 1
    \$\begingroup\$ @xnor I hear you, but if Haskell cannot treat numbers as Booleans, or cannot do arithmetic on Booleans, then that is a real limitation in the golfing power of Haskell, and ought to be reflected in the byte count by necessitating conversions or other work-arounds. What do you think of the footnote formulation? \$\endgroup\$
    – Adám
    May 7 '17 at 21:19

66 Answers 66

1 2
3
0
\$\begingroup\$

Kotlin, 31 bytes

{List(size){it==indexOf(true)}}

{                             }  // lambda; input is `this`
 List(    )                      // build new list
      size                       // same size as input
           {                 }   // based on lambda
            it==                 // index of this element equal to
                indexOf(true)    // index of first true in input

To actually solve this challenge you only need to know two things about the input:

  • its length
  • where the first true is.

The shotgun approach of just generating a new list worked out best here.

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Io, 38 bytes

method(x,x map(i,v,i==x indexOf(1<2)))

Try it online!

Explanation

method(x, // Take an operand x
    x map(i,v, // Map every item with index/value
        i==    // Is the index equal to
        x indexOf(1<2) // the index of true in x?
    )
)
\$\endgroup\$
0
\$\begingroup\$

Processing.js (Khanacademy version), 67 bytes

var a=[];for(var i=0;i<a.length;i++){if(i!==0){a[i]=0;}}print(a);

A is the input (khanacademy version of processing has no real input method)

The output is printed to console. Truth = 1, false = 0;

Try it online!

\$\endgroup\$
0
\$\begingroup\$

Ruby -nl, 22 bytes

Input is STDIN.

a|=p
p !a&&a|=eval($_)

Try it online!

Explanation

                  # For each line of STDIN:
a|=p              # Set a to falsy if it wasn't defined before
                  #  (needed or else the next line crashes)
p !a&&a|=eval($_) # Output false if a is truthy, otherwise OR it with the value in STDIN
                  #  and output that instead
\$\endgroup\$
0
\$\begingroup\$

sed, 8 bytes

0,/./!c

Try it online!

Note the trailing linefeed.

Inputs are linefeed separated. Any single character is truthy (I used 1) and an empty line is falsey.

Input should not have a trailing linefeed, as that will be interpreted as an extra falsey value. Note that TIO adds a final linefeed, and thus a falsey value, at the end. This is a limitation of TIO, the program works just fine without.

Commented:

0,/./ #Run the following commands from the first line to the first line containing a character
#If we are *not* in the previous range, replace the line with an empty line
!c 

To make it easier to read the output, you can make 0 falsey and replace the program with

0,/1/!c0

at the cost of one byte. Try it online!

\$\endgroup\$
-1
\$\begingroup\$

PowerShell, 25 bytes

$args|%{$_-and!$f;$f+=$_}

Previous "non legal" version

{$_*!$f;if($_){$f=1}}

Try it online!

The one more version with the same length of 25 bytes

$args|%{!!$f-lt$_;$f+=$_}

Try it online!

\$\endgroup\$
4
  • \$\begingroup\$ Is this a legal answer? I am honestly not sure. You cannot run this on its own as a function or program. It is a process scriptblock for the foreach-object cmdlet. I would have expected you to use $args \$\endgroup\$
    – Matt
    May 8 '17 at 18:34
  • \$\begingroup\$ @Matt I would say no, it's not legal. \$\endgroup\$
    – briantist
    May 9 '17 at 20:05
  • \$\begingroup\$ @briantist Well that is good then. I don't feel the need to go back to my old answers now. \$\endgroup\$
    – Matt
    May 9 '17 at 20:06
  • 1
    \$\begingroup\$ @Matt here's the relevant clarification for PowerShell specifically. \$\endgroup\$
    – briantist
    May 9 '17 at 20:08
1 2
3

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.