Last call to make your voice heard! Our 2022 Developer Survey closes in less than a week. Take survey.

Hot answers tagged

76 votes

Generating a single string with an Oxford comma from a list

CSS, 132 116 115 bytes a:not(:last-child):nth-child(n+2):after,a:nth-last-child(n+3):after{content:","}a+:last-child:before{content:"and " ...
user avatar
62 votes
Accepted

Index of a multidimensional array

APL, 1 byte Test it on TryAPL.
user avatar
  • 206k
55 votes

N-dimensional N^N array filled with N

Python, 32 bytes lambda n:eval('['*n+'n'+']*n'*n) Try it online! Makes a string like "[[[n]*n]*n]*n" with ...
user avatar
  • 137k
51 votes
Accepted

Cheating a multiple choice test

Jelly, 7 6 bytes _/ị“ḃ» Typing on phone. Will add description. (1,0) goes to A, ...
user avatar
  • 21.2k
47 votes

Be the First 1 (leave only the first Truthy)

Python 2, 35 bytes while 1:b=input();print b;True&=b<1 Try it online! Input and output are lines of True/False. Based on Dennis's solution. Redefines the ...
user avatar
  • 137k
44 votes
Accepted

Lossy Sorting (Implement Dropsort)

APL, 9 bytes ⊢(/⍨)⊢=⌈\ This is a monadic function train with diagram: ┌─┼───┐ ⊢ ⍨ ┌─┼─┐ ┌─┘ ⊢ = \ / ┌─┘ ⌈ The ...
user avatar
  • 21.2k
40 votes
Accepted

Flatten the Array!

K, 3 bytes ,// This is a fairly common idiom. "Join over converge". try it here with oK. How it works: Join (,) fuses ...
user avatar
  • 4,912
40 votes

Remove surrounding zeroes of a 2d array

Wolfram Language (Mathematica), 42 bytes #&@@CellularAutomaton[{,{},0{,}},{#,0},0]& Try it online! Cellular automata are indeed the answer to life, the ...
user avatar
  • 13.6k
39 votes

Output a string's cumulative slope

Python, 29 bytes lambda s:ord(s[-1])-ord(s[0]) The sum of the differences forms a telescopic series, so most summands cancel out and (s1 - s0) + (s2 - s1) + &...
user avatar
  • 206k
38 votes

Flatten the Array!

JavaScript (ES6), 35 bytes Inspired by @user81655's answer: f=a=>a.map?[].concat(...a.map(f)):a
user avatar
  • 1,037
38 votes
Accepted

Is it true? Ask Jelly!

Jelly, 3 bytes ṭFẠ F flattens the input list. tacks on the original input list as an ...
user avatar
  • 3,664
37 votes
Accepted

Count trailing truths

Dyalog APL, 6 2 bytes ⊥⍨ Test it on TryAPL. How it works ⊥ (uptack, dyadic: decode) performs base conversion. If the left operand is a vector, it performs ...
user avatar
  • 206k
36 votes

Xorting an array

Pyth, 40 36 31 30 bytes Ju.|G^2slHxMf>FT.:Q2Z|tSIxRJQJ Try it online: Demonstration or Test Suite Each of the big test-cases finishes in a couple of seconds. ...
user avatar
  • 21.8k
36 votes
Accepted

Be the First 1 (leave only the first Truthy)

APL, 2 bytes <\ Evaluates to the function "scan using less-than". Try it online! Explanation In APL, the operator \ (...
user avatar
  • 42k
35 votes

Surrounded Countries

Pyth, 7 bytes n{Q_{_Q Run the code on test cases. ...
user avatar
  • 137k
35 votes

Remove every N-th N

awk, 10 bytes Input is expected on STDIN, one number per line. ++a[$1]%$1 Explanation Keeps a counter for each number in an associative array, prints only if ...
user avatar
  • 2,507
35 votes

Covering a Skyline with brush strokes

JavaScript (Node.js), 38 bytes a=>a.map(v=>(n+=v>p&&v-p,p=v),p=n=0)|n Try it online! Simply a greedy algorithm which scan from left to right, ...
user avatar
  • 28.6k
34 votes

Execute a Superb Shuffle™

Ruby, 31 bytes ->x{(0..53).map{|r|x[r*17%54]}} Try it online! Explanation: I'm picking one card, then skipping over the next 16 and start from the first card ...
user avatar
  • 17.8k
33 votes

Turn an integer n into a list containing it n times

Jelly, 1 byte x Try it online! Note that this is not the “repeat n n times” built-in — ...
user avatar
  • 65.5k
32 votes

N-dimensional N^N array filled with N

J, 4 bytes $~#~ Try it online! Explanation ...
user avatar
  • 16.4k
32 votes
Accepted

Find the longest word in an array

Imperative Tampio, 168 bytes ...
user avatar
  • 5,067
31 votes

Consolidate an Array

Pyth, 3 bytes !DQ Explanation: Q Input !D Sort by logical NOT Try it here.
user avatar
  • 21.2k
30 votes

Invert a boolean array

05AB1E, 1 byte Code: _ Explanation: _ # Logical not Try it online!
user avatar
  • 43.4k
30 votes

Halve the falses

Retina, 4 bytes 00 0 Try it online!
user avatar
30 votes

Detect heat waves

C (gcc), 88 75 73 bytes Saved two bytes thanks to ceilingcat. h,e,a;t(int*_){for(h=e=a=0;*_;h+=e>4&a>2)e=*_<25?a=0:++e,a+=*_++>29;e=h;} Try it ...
user avatar
29 votes

Mode (most common element) of a list

Python 2 - 18 max(d,key=d.count) Since your python answer doesn't seem to print, I expect this is what you want. Add 6 bytes for ...
user avatar
29 votes

Yahtzee Small Straight Detection

MATL, 7 12 11 9 8 6 bytes Thanks a lot to @lirtosiast for removing 2 bytes ud7BXf Truthy is an array of nonzero values. Falsy is empty array (no output displayed). ...
user avatar
  • 99.4k
29 votes
Accepted

Zigzagify a Matrix

J, 31 30 14 12 11 bytes [:;<@|.`</. Ych. Too big. Takes a matrix as input. Explanation J has an advantage here. There's a command called oblique (...
user avatar
  • 11.2k
29 votes

Find the minimum and maximum integers in an array, without using builtins

Jelly, 3 bytes Ṣ.ị Try it online! Sort the array, and then takes the 0.5-th element. Jelly uses 1-indexing, and floating points indexing means take its floor ...
user avatar
  • 48.2k
29 votes

Covering a Skyline with brush strokes

05AB1E,  8 7  5 bytes Saved 2 bytes thanks to @Adnan 0š¥þO Try it online! How? This is using the algorithm that was first found by @tsh. If you like this answer, ...
user avatar
  • 166k

Only top scored, non community-wiki answers of a minimum length are eligible