Hot answers tagged

50

Python, 32 bytes lambda n:eval('['*n+'n'+']*n'*n) Try it online! Makes a string like "[[[n]*n]*n]*n" with n multiplcations, and evaluates it as Python code. Since the evaluation happens within the function scope, the variable name n evaluates to the function input.


47

Haskell, 19 bytes do n<-[1..];[1-n,n] Produces the infinite list [0,1,-1,2,-2,3,-3,4,-4,5,-5,6,-6,7,-7... Haskell allows infinite lists natively. Printing such a list will prints its elements one a time forever.


46

Python, 24 bytes range(123,790,111).count An anonymous function that outputs 0 or 1. It creates the list [123, 234, 345, 456, 567, 678, 789] and counts how many times the input appears. f=range(123,790,111).count f(123) => 1 f(258) => 0


46

Retina, 9 bytes [1-9].* 1 Try it online! Replaces a non-zero digit and everything after it with 1. This leaves a potential leading - intact and changes all numbers except 0 itself to absolute value 1.


42

C (GCC), 24 23 22 18 bytes Thanks to @aross and @Steadybox for saving a byte! f(n){n=!!n|n>>31;} Not guaranteed to work on all systems or compilers, works on TIO.


41

Python3, 21 27 values Characters: 3479% Unique numbers: [1,2,3,4,5,6,7,8,9,11,12,19,20,21,24,29,34,35,36,37,39,43,46,47,49,73,74] As it was requested, here are the permutations that fell in the range [1, 120]. Try it online! 347%9 5 349%7 6 34%79 34 34%97 34 374%9 5 379%4 3 37%49 37 37%94 37 394%7 2 397%4 1 39%47 39 39%74 39 3%479 ...


40

Python 2, 41 bytes lambda l:[sorted(l)[x==min(l)]for x in l] Try it online! For each element x we check whether x==min(l). If not, this is False, which is treated as 0 when used as a list index into sorted(l), giving the smallest element. Otherwise, it's True aka 1, giving the second-smallest element, since that element itself is smallest and should be ...


39

Taxi, 1118 bytes 1 is waiting at Starchild Numerology.Go to Post Office:w 1 l 1 r 1 l.Pickup a passenger going to Chop Suey.Go to Chop Suey:n 1 r 1 l 4 r 1 l.Pickup a passenger going to Crime Lab.'-' is waiting at Writer's Depot.Go to Writer's Depot:n 1 l 3 l.Pickup a passenger going to Crime Lab.Go to Crime Lab:n 1 r 2 r 2 l.Switch to plan "n" if no one is ...


36

Python 2, 18 16 bytes +beauty thanks to @Sarge Borsch `99066**2`.strip Try it online! 99066**2 is just a shorter way to generate a string that contains 0~9


36

Python 3, 102 bytes try:l=eval(input());k=1#)]0[*k**l(tnirp except:k=2#2=k:tpecxe print(k**l*[0])#1=k;))(tupni(lave=l:yrt Try it online! This is of O(1^n), since this is what the program does: evaluate the input create the array [0] print it Reversed: try:l=eval(input());k=1#)]0[*l**k(tnirp except:k=2#2=k:tpecxe print(l**k*[0])#1=k;))(tupni(lave=l:yrt ...


35

C++, 15 operations I have no idea why while loops are allowed as they destroy the whole challenge. Here is an answer without any: int64_t is_negpow2(int64_t n) { int64_t neg = uint64_t(n) >> 63; // n >>> 63 n = (n ^ -neg) + neg; // if (n < 0) n = -n; int64_t evenbits = n & int64_t(0xaaaaaaaaaaaaaaaaull >> neg); ...


34

Python, 24 bytes lambda n:n%111==12<n<900 Just a lot of condition chaining.


34

05AB1E, 27 38 41 numbers 4·>Ìn Generates the unique numbers: [4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 16, 17, 18, 19, 25, 27, 32, 33, 34, 35, 36, 37, 38, 49, 50, 52, 54, 64, 65, 66, 67, 72, 73, 74, 81, 83, 98, 100, 101, 102] Uses the constant 4 with the operations +1, +2, *2 and ^2.


34

Mathematica, 4 bytes Clip How about not using the built-in Sign and still scoring 4 bytes? ;) Clip with a single argument clips (or clamps) the input value between -1 and 1. Since the inputs will only be integers, this is the same as using Sign.


33

x86_64 machine language (Linux), 16 bytes 0: f2 48 0f 2a c7 cvtsi2sd %rdi, %xmm0 5: 66 48 0f 7e c0 movq %xmm0, %rax a: f3 48 0f b8 c0 popcnt %rax, %rax f: c3 retq Accepts a single 64-bit integer parameter in RDI, converts it to a floating-point value in XMM0, stores those bits ...


32

Python, 18 numbers 237#- Produces as valid results: 1, 2, 3, 4, 5, 7, 16, 23, 24, 25, 27, 32, 35, 37, 69, 71, 72, 73 EDIT: I can attest that TuukkaX's solution is optimal for Python. I ran the following code bruteforcing all possible combinations of 5 printable ASCII characters: from itertools import permutations,combinations_with_replacement def perms(...


32

Python 2, 23 bytes lambda n:~n/2+n%2*(n|2) Odd inputs give roughly n/2, even ones roughly -n/2. So, I started with -n/2+n%2*n and tweaked from there. Try it online!


32

Jelly, 1 byte x Try it online! Note that this is not the “repeat n n times” built-in — its function is more general than that. For example 4,5,6x1,2,3 equals [4, 5, 5, 6, 6, 6]. Given only one argument, Jelly just happens to use it as both the left and right argument for the supplied link, but this functionality is not inherent to x. If this doesn’t ...


31

J, 4 bytes $~#~ Try it online! Explanation $~#~ Input: integer n #~ Create n copies of n $~ Shape n into an array with dimensions n copies of n


31

Python 3,  115  90 bytes from unicodedata import* lambda n:name(chr(n%13+n%13//11+[6,0,4,2][-n//13]*8+127137))[13:] An unnamed function returning the string in uppercase. Try it online! How? Unicode characters have names. The names of some of these are like "PLAYING CARD TWO OF SPADES", hence we can get the characters of the Unicode character ...


30

Brainfuck, 6 bytes This makes use of the cell wrapping and prints all possible values. In Brainfuck, the native integer representation is by byte value. .+[.+] Try it online!


29

Retina, 20 bytes Byte count assumes ISO 8859-1 encoding. $.' S`> %O#`\d+ ¶ > Try it online! (The first line enables a linefeed-separated test-suite.) Explanation A simple way to find a valid permutation is to start by inserting the numbers from 0 to N in order, and then to reverse the numbers surrounding each substring of >s. Take <><<>...


29

COW, 225 213 201 bytes oomMOOmoOmoOmoOmoOMoOMoOmOomOomOoMoOMMMmoOMMMMOOMOomOo mOoMOomoOmoOmoomOomOoMMMmoOmoOmoOMMMMOOOOOmoOMOoMOomOo mOomOoMoOMMMmoOMMMMOOMOomOomOoMoOmoOmoOmoomoOmoomOomOo mOomoomoOMOOmoOmoOmoOMOoMMMOOOmooMMMOOM Try it online! The way that this code works is that it determines the sign by alternating adding and subtracting bigger numbers, ...


29

Lost, 29 27/2 = 13.5 bytes %?\>>>>>>>>>> >>\"*"@" Try it online! or verify that it is deterministic Seemed like the right language to use. Explanation: Lost is a 2D language where the pointer starts anywhere, going in any direction. This generally leads to a lot of double checking that the ...


27

Cubix, 14 12 bytes .(.\OSo;?.>~ Test it online! You can now adjust the speed if you want it to run faster or slower. How it works The first thing the interpreter does is remove all whitespace and pad the code with no-ops . until it fits perfectly on a cube. That means that the above code can also be written like this: . ( . \ O S o ; ? . > ...


27

Jelly, 4 bytes ḣ2ṁ⁼ Try it online! How it works ḣ2ṁ⁼ Main link. Argument: A (array) ḣ2 Head 2; truncate A after its second element. If A has two or less elements, this returns A itself. ṁ Mold; cyclically repeat the elements of the previous result to create an array that has the same shape/length as A. ⁼ Test the result for ...


26

Jelly, 7 3 bytes ḤḢ_ Try it online! Background The deltas of (a, b, c, d) are b - a, c - b, and d - c. Cumulatively reducing (a, b - a, c - b, d - c) by subtractiong yields a - (b - a) = 2a - b, 2a - b - (c - b) = 2a - c, and 2a - c - (d - c) = 2a - d, so the correct result is (2a - a, 2a - b, 2a - c, 2a - d). How it works ḤḢ_ Main link. Argument: A (...


26

Mathematica, 22 bytes EvenQ@Log2@Max[#,-2#]& Try it online! (Using Mathics instead, where this solution also works.) I tried to find a solution with bitwise operators for a while, and while one definitely exists, I ended up finding something which is probably simpler: Max[#,-2#] multiplies the input by -2 if it's negative. Multiplying by another ...


26

Python 2/C (clang), 109 107 100 84 95 88 89 88 87 84 bytes i=0; #/* while 1:i+=1L;print`i`[::-1] '''*/ a(){for(;;)printf("%i %1$i ",++i);}//''' Python: Try it online! C: Try it online! The L in the Python code is part of the delimiter. Explanation: In the C code, It first sets i to 0. Then, it starts a comment (# is valid code in C for #include ...


25

Haskell, 22 bytes (`elem`[123,234..789]) An anonymous function. Generates the evenly-spaced list [123, 234, 345, 456, 567, 678, 789] and checks if the input is an element.


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