A message from our CEO about the future of Stack Overflow and Stack Exchange. Read now.

# Tag Info

56

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....

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+...

32

CJam, 24 23 22 21 19 bytes ri_)2b,,W%{2\#(md}/ This is an O(log n) approach, where n is the input, that completes the last test case instantly. It converts n directly to skew binary, using modular division by the values of the digit 1. This code finishes with an error that goes to STDERR with the Java interpreter, which is allowed according to the ...

28

Python 2, 67 bytes def f(n):x=len(bin(n+1))-3;y=2**x-1;return n and n/y*10**~-x+f(n%y) Seems to work for the given test cases. If I've got this right, this should be about O(place values set in output), so it does the last case with ease. Call like f(100). Returns a decimal representation equal to the skew binary. Python 3, 65 bytes def g(n,x=1):*a,b=n&...

27

05AB1E, 4 bytes D1ìH Explanation D Duplicate input 1ì Prepend 1 H Interpret as hexadecimal and implicitly display the value in base 10 If the input has invalid hex characters, H won't push anything so the last value on the stack will be the duplicated input, that's why the program prints its input in case of invalid input. Try it online!

27

Jelly, 3 bytes ‘b@ Try it online! Returns the polynomial as a list of coefficients. Since we know the polynomial has non-negative integer coefficients, f(b) can be interpreted as "the coefficients of the polynomial, taken as base b digits," by the definition of a base. This is subject to the condition that none of the coefficients exceeds or is equal to ...

26

Python, 27 22 bytes lambda s:(max(s)-8)%39 This requires the input to be a bytestring (Python 3) or a bytearray (Python 2 and 3). Thanks to @AleksiTorhamo for golfing off 5 bytes! Test it on Ideone. How it works We begin by taking the maximum of the string. This the code points of letters are higher than the code points of digits, this maximal ...

23

Python 2: 58 chars n=input() s="" while n:s="0+-"[n%3]+s;n=-~n/3 print s or 0 Generates the balanced ternary digit-by-digit from the end. The last digit is given by the residue n%3 being -1,0, or +1. We then remove the last digit and divide by 3 using Python's floor-divide n=(n+1)/3. Then, we proceed recursively with the new last digit until the number is ...

22

Pyth, 43 - 40 = 3 bytes ?&K@J.HQG-JG"Mix"%"Only %sers"?K"lett""numb Test suite This achieves the bonus. Only numbers and Only letters fortunately only differ by 4 letters. printf-style formatting is used with %. The selection system is done by both taking the intersection of the hex with G, the alphabet, and subtracting out G. If neither ends up ...

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 ...

20

Python, 25 bytes lambda x:int(max(x),36)+1 Defines a lambda that takes the string x. Finds the largest digit in the string (sorted with letters above digits, by python's default), and converts to base 36. Adds 1, because 8 is not in base 8.

20

Python 2, 49 37 31 30 Bytes Now this will take a binary number in a decimal representation, since Python can handle arbitrarily large integers. b=lambda n:n and n%2+2*b(n/10) thanks to xnor for saving a byte :) The easiest way to see how this works is by seeing a basic formula for converting binary to decimal: = 101010 = 1*(2^5) + 0*(2^4) + 1*(2^3) + 0*...

20

Python, 29 bytes lambda n:int(bin(n)[:1:-1],2) Try it online! This is an anonymous, unnamed function which returns the result. First, bin(n) converts the argument to a binary string. We would ordinarily reverse this with the slice notation [::-1]. This reads the string with a step of -1, i.e. backwards. However, binary strings in Python are prefixed ...

20

Mathematica, 56 bytes #//.x_/;(b=Max[d=IntegerDigits@x]+1)<11:>d~FromDigits~b& Try it online! (Using Mathics.) I thought I'd check out what the sequence looks like: And here is a plot of the number of steps it takes to find the result: (Click for larger versions. See the revision history for plots only up to n = 1000.) Looks like a very ...

19

Mathematica, 42 21 bytes Thanks to alephalpha for halving the score. #~IntegerReverse~2^2& The actual reason I did this in Mathematica was because I wanted to look at a plot... it sure looks funny:

19

x86-16 machine code, IBM PC DOS, 54 47 45 bytes Binary: 00000000: be82 00b3 04b1 08ac d0d8 d0d4 e2f9 8ac4 ................ 00000010: 41d4 0a50 8ac4 84c0 75f6 580c 30b4 0ecd A..P....u.X.0... 00000020: 10e2 f74b 7406 b02e cd10 ebd9 c3 ...Kt........ Build and test BIN2IP.COM using xxd -r from above. Unassembled listing: BE 0082 MOV SI, 82H ...

18

Jelly, 6 bytes Ḷ2*ẆS€ This qualifies for the imaginary bonus. Try it online! How it works Ḷ2*ẆS€ Main link. Argument: n Ḷ Unlength; yield [0, ..., n-1]. 2* Yield [2**0, ..., 2**(n-1)]. Ẇ Sliding window; yield all subarrays of consecutive elements. The subarrays are sorted by length, then from left to right. S€ Map the sum ...

17

Jelly, 1 byte ¬ This uses base k+2, in which case there's a single 0 iff i is 0. It takes two arguments, but applies the logical NOT to only the first one. If we don't want to cheat: 7 bytes - 30% = 4.9 -1.1 points by @Dennis rb⁵$¬SS This gets the bonus. dyadic link: r inclusive range b⁵$ Convert all to base input. ...

17

Python, 29 bytes lambda n:int(bin(n)[:1:-1],2) Try it online

17

Pure Bash (no external utils), 64 2 bytes saved thanks to @NahuelFouilleul x={A..Z} eval f=($x$x-%03d-$x$x) printf ${f[$1/1000]} $[$1%1000] Try it online! - takes about 10s to run over the 7 testcases. Line #1 is a simple assignment of a string to a variable Line #2 is a brace expansion to build an array of printf format strings, one for all 456,976 ...

16

Pyth, 23 22 bytes sXc.<sXz=+GdJ^UK3K1KJG Try it online: Regular Input / Test Suite Thanks to @isaacg for one byte. Explanation: sXc.<sXz=+GdJ^UK3K1KJG =+Gd append a space the G (preinitialized with the alphabet) K3 assign 3 to K J^UK K assign all 3-digit ternary numbers ...

16

Jelly, 4 bytes ṀØBi Requires uppercase. Try it online! or verify all test cases. How it works ṀØBi Main link. Arguments: s (string) Ṁ Yield the maximum of s. ØB Yield "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz". i Find the 1-based index of the maximum in that string.

16

JavaScript (ES6), 15 bytes s=>'0x1'+s-0||s How it works '0x1'+s converts the input into a literal hexadecimal string with a prepended 1, e.g. 0x105ab1e. Then -0 casts the result to a number. JavaScript sees the 0x at the beginning and implicitly tries to convert from hexadecimal; if s contains any non-hexadecimal chars, this returns NaN. Since this is ...

16

Jelly, 6 bytes BḄ-8ƤṀ A monadic link taking a number and returning a number. Try it online! How? Uses a nice quick, Ƥ, developed by miles... BḄ-8ƤṀ - Link: number B - convert to a binary list Ƥ - for loop over some slices to be determined... -8 - this is a negative nilad, therefore: use overlapping outfixes of length 8 - (exactly ...

15

Pyth, 6 bytes !-.HQG .HQ # Converts the input to hexadecimal - G # Deletes all letters ! # If empty, output True, else False Test it here

15

Pyth, 6 bytes f*FjQT Verify all the test cases. How it works f*FjQT ~ Full program. f ~ First positive integer where the condition is truthy. jQT ~ The input converted to base of the current element. *F ~ Product. If the list contains 0, then it's 0, else it is strictly positive. 0 -> Falsy; > 0 -> Truthy. ~ Output the ...

15

Python 2, 31 bytes f=lambda n:n and 3*f(n/2)+n%2+1 Try it online!

14

Java 35 31 bytes. Yay!!! A java answer under 40 bytes. new int[]{n>>16,n>>8&255,n&255} Interestingly, java integers are always stored as Binary numbers. Consequently this snippet returns [69,161,156] whether n = 0x45A19C, 4563356, or 0b10001011010000110011100.

14

Pyth, 14 13 jbf!-jQTU2tSQ Thanks to Jakube for pointing out the new S function. Try it here. The online version is too slow to do 1234321. This simply converts the input to each base from 2 to itself and discards the results that contain values other than 0 and 1. Explanation: : Q=eval(input) (implicit) jb ...

14

CJam, 33 31 bytes q~:X;2b64Te["|-"f=8/{X*X<z}2*N* Test it here. Explanation q~ e# Read and eval input. :X; e# Store the side length in X and discard it. 2b e# Convert to base 2. 64Te[ e# Left-pad to length 64 with zeroes. "|-"f= e# Select '|' for 0 and '=' for 1. 8/ e# Split into chunks of 8 bits. { e# Do the following ...

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