Finden Sie die längste Zahl von Wahrheiten in einer Liste von Booleschen Werten. Geben Sie dieselbe Liste mit allen anderen gefälschten Wahrheiten zurück.
Input-Output
Eine Liste; Jedes übliche Format (z. B. eine durch Trennzeichen getrennte Liste als Zeichenfolge).
Einzelheiten
Richtig und falsch kann alles sein, was Ihre Sprache normalerweise für diese Werte verwendet, oder die Ganzzahlen 1 und 0. Wenn Sie einzelne Zeichen verwenden, kann die Liste eine Verkettung sein (z. B. 10001
. ).
Wenn es für den längsten Lauf ein Unentschieden gibt, lassen Sie alle Bindungsläufe wahr und verfälschen Sie alle kürzeren Läufe.
Beispiele
input ↦ output
1,0,1,0,1 ↦ 1,0,1,0,1
1,1,0,1,1,0,1 ↦ 1,1,0,1,1,0,0
1,1,0,1,1,1,0,1,1 ↦ 0,0,0,1,1,1,0,0,0
1,1,1 ↦ 1,1,1
0,0,1 ↦ 0,0,1
0,0 ↦ 0,0
1,1,1,0,0,0,1,1,1,1,0,1,0,0,1,1,0,1,1,1,1,0,0,1,0 ↦ 0,0,0,0,0,0,1,1,1,1,0,0,0,0,0,0,0,1,1,1,1,0,0,0,0
(direkt von https://stackoverflow.com/q/37447114 )
code-golf
array-manipulation
msh210
quelle
quelle
Haskell,
59,58,55, 64 BytesFun note, this works on any list of values where
falsy < truthy
. SoFalse/True
,0/1
,'f'/'t'
, etc.Note:
Wie mehrere Personen (einschließlich
@proud haskeller
und@nimi
) betont haben, ist die vorherige Version in einer Liste aller falschen Werte fehlgeschlagen. Die Hinzufügung von.([1<2]:)
hat dies behoben, wie von vorgeschlagen@proud haskeller
. Ich lasse die Erklärung vorerst gleich, weil ich denke, dass sie immer noch Sinn macht. Wenn jemand einen Kommentar abgibt und nach einer Erklärung für die Bearbeitung fragt, werde ich sie bearbeiten.Erläuterung:
Ich werde erst ohne Zucker
group
und dann wieder hinzufügen. Erstens finde ich, dass Wörter für die Augen oft einfacher sind als Symbole, deshalb werde ich einige Substitutionen vornehmen. (Beachten Sie, dass=<<
es "nobel" ist, so dass es für Listen und Funktionen anders gilt. Ich rufebind
die Version von=<<
for functions auf.)Die letzten Details
x <$ list
ersetzen jedes Element vonlist
mitx
undgroup list
teilen daslist
Up in Teile gleicher Elemente auf. Sogroup [1, 1, 2, 3, 3, 3] == [[1, 1], [2], [3, 3, 3]]
.Zusammenfassend teilt die Funktion die Liste der Werte in Gruppen von nur wahr und Gruppen von nur falsch. Ersetzen Sie dann für jede Gruppe jedes Element durch das Ergebnis der Anweisung
this is the biggest group
(die größte Gruppe vontrue
ist die größte) und verketten Sie die Gruppen.Vier Bytes gespeichert von
@Zgarb
quelle
(\y->(maximum g==y)<$y)
mit((<$)=<<(==maximum g))
. Ich habe es aber nicht getestet.f
by the point-free function((=<<)=<<(=<<)(<$).(==).maximum).group
. Saves three bytes and is utterly unreadable!b=(=<<);b b(b(<$).(==).maximum).group
is one byte shorter still. I’ve never seen anything like this before in Haskell golf :)(:[t])
before the maximum or something similarRetina,
474336Try it online! or try all the test cases
Thanks to msh210 for golfing 4 bytes!
Also big thanks to Martin for 7 bytes!
Explanation:
Replace all
0
s with!
s. This is done to make matching groups of1
s shorter, as now1!
and!1
will have a word boundary (\b
) between them, which also matches either the start or the end of the string.This is a configuration option saying that after applying the regex after the backtick to the input, in every match translate every printable ascii character into a
0
character.This regex matches groups of
1
s that are surrounded by zeroes, but do cannot match a1
followed by itself anywhere in the string. These are the non-maximal groups that will be falsified. In addition, this also matches the!
characters we added to convert them back into0
s.quelle
MATL, 14 bytes
Try it Online!
Modified version with all test cases
Explanation
quelle
Python 2, 62 bytes
Test it on Ideone.
How it works
s.split('0')
splits the input string s into runs of zero or more 1'sFor each run t, we check whether
t+'1'
is a substring of s.If it is, the run isn't maximal,
t+'1'in s
return True,1-(t+'1'in s)
return 1 - True = 0 and the run is replaced with a run of 0's of the same length.If it isn't, the run is maximal,
t+'1'in s
return False,1-(t+'1'in s)
return 1 - False = 1 and the run is replaced with a run of 1's of the same length, i.e., by itself.Finally,
'0'.join
restores all removed 0's.quelle
J, 25 bytes
This is a monadic verb that takes and returns a 0-1 array. Use it like this:
Explanation
quelle
;.
.Pyth,
26242321 bytesTest suite.
1/0
ortrue/false
in input.true/false
in output.Explanation
Previous 23-byte
Test suite.
1/0
ortrue/false
in input.1/0
in output.Previous 24-byte
Test suite.
1/0
ortrue/false
in input.1/0
in output.Previous 26-byte
Test suite.
1/0
ortrue/false
in input.1/0
in output.quelle
Jr.b,N&YNrQ8)9qReSJJ
orJrm,hd*FdrQ8 9qReSJJ
. Both versions save one byte. Or go even crazier withJrXR1*FdrQ8 9qReSJJ
and save two. ;-)Oracle SQL 12.1,
137135 bytesUn-golfed
Input use single characters. Ex: '1100111'
quelle
Mathematica,
4641Works on lists of
0
and1
. I thought I had done pretty well until I looked at the other answers!Explanation for 46 character version; I shall update when I cannot improve it further.
An explanation of this code was requested.
A non-code-golf equivalent (using version 10 operator forms) is:
This means a function made up of five steps (sub-functions) applied in order from top to bottom.
Split
: break up into runs of identical elements: {1,1,0,1,1,0,1} ↦ {{1,1}, {0}, {1,1}, {0,0}}Map[# Tr@# &]
: For each sub-list (Map
) multiply it (#
) by its sum (vector trace,Tr
): {1,1} ↦ {2, 2}# - Max[1, #] &
subtract from every element the maximum value appearing anywhere in the list of lists, or one, whichever is higher. (The one handles the case of all zeros.)UnitStep
: equal to 0 for x < 0 and 1 for x >= 0, applied to every element.Apply[Join]
: join the sub-lists into a single list. Could also be done withFlatten
orCatenate
, but in short formJoin@@
is more terse.quelle
C,
135129 bytesTry Online
Ungolfed
quelle
JavaScript (ES6), 56 bytes
Works by checking all runs of 1s and replacing the characters with 0s unless the run is (equally) longest, as measured by searching the string for a longer run of 1s.
Previous 72-byte recursive solution:
Does nothing if there are no runs of 1s (i.e. single 1s at most). Otherwise, subtracts one
1
from each1
or run thereof, then calls itself recursively on the shorter runs, then adds one1
back on the (now equally longest) runs. The number of recursive calls is one less than the length of the longest run.quelle
Julia, 51 bytes
Try it online!
How it works
replace
finds all all runs of one or more 1's in the input string s via the regexr"1+"
and calls the lambdat->map(c->c-contains(s,"1"t),t)
to determine the replacement string.The lambda maps
c->c-contains(s,"1"t)
over all characters in the run of ones t.If
"1"t
(concatenation) is a substring of s, the run isn't maximal,contains
returns true andc-contains(s,"1"t)
returns '1' - true = '0', replacing all 1's in that run with 0's.If
"1"t
(concatenation) is not a substring of s, the run is maximal,contains
returns false andc-contains(s,"1"t)
returns '1' - false = '1', leaving the run unmodified.quelle
APL, 22 chars
In English (from right to left in blocks):
quelle
Java 8, 205 bytes
This is a lambda expression for a
Function<String,String>
:input/output is a
String
where true is represented by 1 and false is represented by 0. There are no delimiter characters separating the values.code with explanation:
see ideone for the test cases
quelle
Clojure, 137 bytes
First partitions the input into consecutive zeros and ones and maps these into "tuples" of partitions' first element and the count of elements. It then repeats the needed number of zeros or ones, depending if this is the max-length sequence of ones or not.
Less golfed:
quelle
Perl 5, 68 bytes
67, plus 1 for
-pe
instead of-e
Expects and prints a string (concatenation) of 0s and 1s.
quelle