47
2
Intro
Every year, Dyalog Ltd. holds a student competition. The challenge there is to write good APL code. This is a language agnostic code-golf 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.
2Can we use bit lists or other truthy/falsy list representations that are more natural in our language of choice? – Martin Ender – 2017-05-07T20:32:29.973
@MartinEnder Yes, of course, I thought that was covered by default rules. No?
– Adám – 2017-05-07T20:33:16.2171Well yeah, if you talk about "truthy" and "falsy" in the challenge instead of "booleans", "true" and "false". ;) – Martin Ender – 2017-05-07T20:36:15.143
1I'm not clear on the booleans. Can we use 0/1 even if our language has True/False? – xnor – 2017-05-07T20:59:44.070
@MartinEnder Thanks. Fixed. – Adám – 2017-05-07T21:08:58.043
@xnor See edit. – Adám – 2017-05-07T21:09:08.980
@Adám Are we choosing which values to expect as Truthy value and Falsey value? Does the output have to use the same ones as the input? – xnor – 2017-05-07T21:10:45.833
1@xnor Ah, good point. I think it would be fair to allow choosing input, but output must match, don't you think so? – Adám – 2017-05-07T21:12:04.893
@Adám I think that's reasonable, though it needs limits to stop people form embedding code into their choice of values (is that a loophole?). Matching output feels right for the challenge. – xnor – 2017-05-07T21:14:07.343
@Adám Actually, I'm a bit sad a language like Haskell can't use an arithmetic solution just because 0's and 1's aren't truthy or falsey. But saying "any two distinct values" would let people use True as False and False as True, which is also silly. Not sure how to handle that. – xnor – 2017-05-07T21:16:00.483
1@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? – Adám – 2017-05-07T21:19:12.507
It doesn't matter for this challenge (because the language doesn't have a working interpreter yet), but I'm working on a language which has booleans but not the ability to store them in arrays (not even by printing them one at a time; outputting boolean false would exit the program). How would that work in this challenge? (Normally you'd just use if statements to make them into something else, but the challenge bans that.) – None – 2017-05-08T03:54:15.157
Just to be sure, the input Truthy/falsy values should also be equal to the output T/F? Or can you use 0/1 as input and get TRUE FALSE as output? – JAD – 2017-05-08T07:04:18.730
1@JarkoDubbeldam The first: All your truthies must be identical, and all your falsies must be identical. This includes output. – Adám – 2017-05-08T07:05:39.773
Are two trailing falsey values ok? – Christopher – 2017-05-15T22:38:25.177
@Christopher What do you mean? – Adám – 2017-05-16T00:09:04.237
@Adám Input of 0 1 0 output of 0 1 0 0. Two extra false values at the end – Christopher – 2017-05-16T00:10:49.797
@Adám https://tio.run/nexus/brain-flak#XY1RCsAgDEOv0s/kz/2XXkQ8SenZXYs4tyEUSV6SiT7oHvDo4FCjWknhsT7L@@muWzMwFCCNvjsKKNeKRqU0WwSpMslMe1g5imQy78kZ5bOT1afxWVv7czZpcsm5r3cD example
– Christopher – 2017-05-16T00:11:50.973@Christopher I'd say no. That just isn't the right result. – Adám – 2017-05-16T00:28:53.290
@Adám thought so – Christopher – 2017-05-16T01:29:10.760
If working a language with text input separated by newlines, is an empty line acceptable, or should the line be removed entirely? – Dom Hastings – 2017-10-27T07:36:13.953
@DomHastings An empty line is fine. – Adám – 2017-10-27T07:37:28.690