Hot answers tagged

65

Python 2, 36 bytes lambda n:[2**i*2/3for i in range(n)] Try it online! Explanation: The binary representation of \$\frac{2}3\$ is 0.101010101... so it simply remains to multiply it by an appropriate power of 2 and take the integer portion.


55

Jelly, 5 bytes DḤ+¥/ Try it online! Explanation The cast D is a monad (single argument function): digits, turning 1234 into [1, 2, 3, 4]. Ḥ is a monad that doubles its single argument. + is a dyad (two argument function) that adds its left and right arguments. From there, it gets a little tricky. Here’s what happens at parse time D, Ḥ, and + are read....


55

C, 59 bytes i;f(char*s){while(*s&3?*s&9||(i+=i+*s%5):putchar(i),*s++);} Magic numbers, magic numbers everywhere! (Also, C shorter than Python, JS, PHP, and Ruby? Unheard of!) This is a function that takes a string as input and outputs to STDOUT. Walkthrough The basic structure is: i; // initialize an integer i to 0 f(char*s){ while(.....


51

This isn't the same solution as llhuii's, but it's also 42 bytes long. n=0;exec'print n;n^=(n^n+2)%3/2;n+=2;'*400 Try it online! Thanks to @JonathanFrech, we're now at 40 bytes. n=0;exec'print n;n=n+2^(n^n+2)/2%3;'*400 Try it online! There's another byte to be saved, for a total of 39. n=0;exec'print n;n=n+2^-(n^n+2)%3;'*400 Try it online!


43

Jelly, 41 40 35 33 bytes Thanks to @Dennis for making the end ṖṖCG! (complement C = 1-x in place of logical not ¬ ) “ƝƓỊ⁹Ȥ⁶Ị⁺‘ẋ8_“¤ÐŒ#'/‘¦32BFs21ṖṖCG TryItOnline How? “ƝƓỊ⁹Ȥ⁶Ị⁺‘ẋ8_“¤ÐŒ#'/‘¦32BFs21ṖṖCG - Main link: no arguments “ƝƓỊ⁹Ȥ⁶Ị⁺‘ - code page indexes [150,147,176,137,154,134,176,138] (...


41

Actually, 58 bytes 73*8╙:13542├`≈"÷≥"E`M"«%s₧ªn%s6û"7*%"♠n≥6û"+¿├`' +`M╪♂Σ♂Ri Try it online! Explanation There are three main parts here, so I'm going to break it down accordingly. Part 1: Constructing the base-256 string We're actually going to construct the binary string reversed, to take advantage of Actually's stack-based (LIFO) structure and to ...


40

x86_64 machine language on Linux, 6 bytes 0: f3 48 0f bd c7 lzcnt %rdi,%rax 5: c3 ret Requires Haswell or K10 or higher processor with lzcnt instruction. Try it online!


39

vim, 126 80 77 76 /\v1011\_.{9}(1110\_.{9}){2}1111<cr>:exe'norm Go'.join(getpos('.'))<cr>xxdawhPXXd{ Expects input in the form 111001111110 110100100000 010001111101 100100100100 100101100111 111111000010 110111000001 100111100001 100111011111 111110011111 100001010111 110011000011 And outputs (with 1-based indices) as 4 5 / ...


38

Python, 53 bytes f=lambda n,i=1:n*[f]and[i]+f(n-1,2*i)+i%2*f(n-1,i-~i) Try it online! The recursive function generates the sorted list as a pre-order walk down this tree (example with n=4): 1 / \ 2 3 / / \ 4 6 7 / / / \ 8 12 14 15 1 2 4 8 3 6 12 7 14 15 Left branches double the value, and right branches do i->i*2+...


37

80386 Machine Code, 4 bytes F3 0F B8 C1 which takes the integer in cx and outputs the count in ax, and is equivalent to: popcnt ax, cx ; F3 0F B8 C1 And here is an 11 10 byte solution not using POPCNT: 31 C0 D1 E9 10 E0 85 C9 75 F8 which is equivalent to: xor ax, ax ; 31 C0 Set ax to 0 shr cx, 1 ; D1 E9 Shift cx to the right by ...


37

JavaScript (ES6), 28 bytes f=n=>n?[...f(n&~-n),n&-n]:[] Try it online!


35

JavaScript (ES6), 18 bytes f=n=>n&&1+f(n>>>1) <input type=number min=0 step=1 value=8 oninput="O.value=f(this.value)"> <input id=O value=4 disabled>


30

05AB1E, 2 bytes bg Try it online!


30

Python 2, 46 45 bytes f=lambda n,k=1:`k`in bin(n^n/2)and-~f(n,k*10) Try it online! How it works By XORing n and n/2 (dividing by 2 essentially chops off the last bit), we get a new integer m whose unset bits indicate matching adjacent bits in n. For example, if n = 1337371, we have the following. n = 1337371 = 101000110100000011011₂ n/2 = 668685 = ...


29

Jelly, 20 17 16 bytes ṡ€4ḄZw€“¿ÇÇБĖUṀ Input is in form of a Boolean matrix, output is the 1-based index pair (Y, X). Try it online! or verify all test cases. How it works ṡ€4ḄZw€“¿ÇÇБĖUṀ Main link. Argument: M (2D array of Booleans) ṡ€4 Split each row into 9 chunks of length 4. Ḅ Convert these chunks from base 2 to ...


29

MATL, 4 bytes BSXB Try it at MATL Online Explanation % Implicitly grab input as an array % STACK: [10, 17, 19, 23] B % Convert each element to binary where each decimal number results in a row % STACK: [0 1 0 1 0; % 1 0 0 0 1; % 1 0 0 1 1; % 1 0 1 1 1] S % Sort each column, placing all of the ...


28

x86 Assembly, 4 bytes Assuming Constant in EBX: bsr eax,ebx inc eax EAX contains the number of bits necessary for Constant. Bytes: ☼¢├@ Hexadecimal: ['0xf', '0xbd', '0xc3', '0x40']


28

x86 Machine Code, 15 14 bytes F3 0F B8 C1 0F BD D1 03 C0 42 2B D0 D6 C3 This is a function using Microsoft's __fastcall calling convention (first and only parameter in ecx, return value in eax, callee is allowed to clobber edx), though it can trivially be modified for other calling conventions that pass arguments in registers. It returns 255 as truthy, ...


27

GS2, 4 bytes dΦ(" Try it online! Hexdump 0000000: 64 e8 28 22 d.(" How it works (implicit) Read all input and push it on the stack. Φ Map the previous token over all characters in the string: d Even; push 1 for even characters, 0 for odd ones. ( Take the minimum of the resulting list of Booleans. ...


26

Getting 39 bytes This is an explanation of how I got a 39-byte solution, which Dennis and JonathanFrech found separately as well. Or, rather, it explains how one could arrive at the answer in hindsight, in a way that's much nicer than my actual path to it, which was full of muddy reasoning and dead ends. n=0 exec"print n;n=n+2^-(n+2^n)%3;"*400 Writing ...


24

JavaScript (ES6), 41 40 36 34 bytes Saved 4 bytes thanks to @ThePirateBay f=x=>(k=x&x/2)?f(k):Math.log2(x)|0 Test cases f=x=>(k=x&x/2)?f(k):Math.log2(x)|0 console.log(f(0)) console.log(f(142)) console.log(f(48)) console.log(f(750)) How? General case x > 0 We recursively AND the input x with x / 2 which progressively ...


24

Hexagony, 78 70 bytes 2"1"\.}/{}A=<\?>(<$\*}[_(A\".{}."&.'\&=/.."!=\2'%<..(@.>._.\=\{}:"<><$ Try it online! Isn't this challenge too trivial for a practical language? ;) side length 6. I can't fit it in a side length 5 hexagon. Explanation


22

JavaScript (ES6), 169 ... 136 135 bytes let f = _=>"0213021203131214".replace(x=/./g,v=>0+[a=1768714102,a-8192,a-=66265089,a+8192,3][v].toString(2)).replace(x,(c,i)=>` `[+!(i%21)]+c) console.log(f()); Saved 2 bytes thanks to Andrakis Saved 4 bytes thanks to Hedi Saved 3 5 bytes thanks to Neil Colored version, 249 bytes (237 bytes ...


22

MATL, 7 bytes BJ*Y'^s Try it online! How it works Consider input 4538 for example. B % Implicit input. Convert to binary % STACK: [1 0 0 0 1 1 0 1 1 1 0 1 0] J* % Multiply by 1i % STACK: [1i 0 0 0 1i 1i 0 1i 1i 1i 0 1i 0] Y' % Run-length encoding % STACK: [1i 0 1i 0 1i 0 1i 0], [1 3 2 1 3 1 1 1] ^ % Power, element-wise ...


22

Python, 39 bytes f=lambda n:4**(n>1)*(n<16)or 2*f(n**.5) Counts how many times one must take the square root for n to be below 16, with some special-casing to avoid outputs of 2. If 2 were included, we could do f=lambda n:n<2or 2*f(n**.5) with True for 1. 41 bytes: f=lambda n,i=1:i*(2**i>n)or f(n,i<<1+i%2) Repeatedly doubles the ...


21

J, 26 bytes (' ';' # '){~4 4$_16{.#: An anonymous verb. Thankfully, J is very good at drawing boxes. Let's try it out: f =. (' ';' # '){~4 4$_16{.#: f 4242 +---+---+---+---+ | | | | # | +---+---+---+---+ | | | | | +---+---+---+---+ | # | | | # | +---+---+---+---+ | | | # | | +---+---+---+---+ As some commenters have ...


21

Befunge, 36 bytes I know this is an old question, but I wanted to give it a try because I thought it would be an interesting challenge in Befunge. >~:0`| >20`:>$.@ |` " "< *8*82<^p24* Try it online! It outputs 1 if the input is corrupted (i.e. contains an odd byte), and 0 if it's OK. Explanation The problem is how to determine odd bytes ...


21

JavaScript (ES6), 63 60 56 bytes s=>[(i=s.search(/1011.{9}(1110.{9}){2}1111/))%13,i/13|0] Takes input as a 155-character space-delimited string of 12 12-digit binary strings, returns zero-indexed values. Edit: Saved 3 bytes thanks to @JörgHülsermann. Saved 4 bytes thanks to @ETHproductions.


21

Jelly, 10 8 bytes Ba\ÐƤṀċ¬ Try it online! How it works Ba\ÐƤṀċ¬ Main link. Argument: n B Binary; convert n to base 2. ÐƤ Apply the link to the left to all postfixes of the binary array. a\ Cumulatively reduce by logical AND. For example, the array [1, 0, 1, 1, 1, 0, 1, 1, 1, 0] becomes the ...


21

Python 2, 30 bytes lambda n:(2*n^2*n+3)**2==8*n+9 Try it online! Note that 2*n^2*n+3 is the bitwise xor of 2*n and 2*n+3, because that's Python's operator precedence.


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