# Be the First 1 (leave only the first Truthy)

### 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.

• Can we use bit lists or other truthy/falsy list representations that are more natural in our language of choice? May 7 '17 at 20:32
• Well yeah, if you talk about "truthy" and "falsy" in the challenge instead of "booleans", "true" and "false". ;) May 7 '17 at 20:36
• I'm not clear on the booleans. Can we use 0/1 even if our language has True/False?
– xnor
May 7 '17 at 20:59
• @xnor Ah, good point. I think it would be fair to allow choosing input, but output must match, don't you think so?
May 7 '17 at 21:12
• @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?
May 7 '17 at 21:19

# 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!

# 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?
)
)


## 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!

# 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


# 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!

## PowerShell, 25 bytes

$args|%{$_-and!$f;$f+=$_}  Previous "non legal" version {$_*!$f;if($_){$f=1}}  The one more version with the same length of 25 bytes $args|%{!!$f-lt$_;$f+=$_}


Try it online!

• 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
– Matt
May 8 '17 at 18:34
• @Matt I would say no, it's not legal. May 9 '17 at 20:05
• @briantist Well that is good then. I don't feel the need to go back to my old answers now.
– Matt
May 9 '17 at 20:06
• May 9 '17 at 20:08