Search Results
Search type | Search syntax |
---|---|
Tags | [tag] |
Exact | "words here" |
Author |
user:1234 user:me (yours) |
Score |
score:3 (3+) score:0 (none) |
Answers |
answers:3 (3+) answers:0 (none) isaccepted:yes hasaccepted:no inquestion:1234 |
Views | views:250 |
Code | code:"if (foo != bar)" |
Sections |
title:apples body:"apples oranges" |
URL | url:"*.example.com" |
Saves | in:saves |
Status |
closed:yes duplicate:no migrated:no wiki:no |
Types |
is:question is:answer |
Exclude |
-[tag] -apples |
For more details on advanced search visit our help page |
For challenges where an exact output is not required but some property must still be fulfilled.
16
votes
f(g(x)) decreases while g(f(x)) increases
Python, 40 bytes
f=lambda x:x*(-1)**x
g=lambda x:3*f(x)+1
Try it online! Some outputs are floats that equal integers because (-1)**(-3) gives a float for instance.
Based off ideas from Peter Tay …
5
votes
Decode the Void
Python 2, 96 bytes
Try it online! to test the bijection.
f=lambda l:len(l)and f(l[0])*2+1<<f(l[1:])
Takes void lists to non-negative integers. 42 bytes.
g=lambda n:n*[g]and[g(n/(n&-n)/2)]+g(len …
9
votes
Surjection from one string to two strings
Python 2, 42 bytes
lambda s:(s.split("1"+len(s)/2*"0")*2)[:2]
Try it online!
The inverse is:
def F(a,b):
return a+"1"+len(a+b)*"0"+b
A tempting idea for the inverse is to encode the pair …
4
votes
Surjection from one string to two strings
Haskell, 38 bytes
f(' ':a:b)=(a:)<$>f b
f s=(drop 1s,"")
Try it online!
Not using built-in parsing.
21 bytes (doesn't quite work)
last.(("",""):).reads
Try it online!
(As pointed out by …
2
votes
An unknowably odd function
Python, 22 bytes
lambda s:s[0]+s[:0:-1]
Try it online!
Based on Mitchell Spector's idea. Leaves the first digit in place and reverses the rest. Input and output are as strings.
6
votes
Shortest total non-primitive recursive function
Haskell, 29 bytes
(+1)?(2?)
z?f=(iterate f z!!)
Try it online!
An Ackermann-like function. Starting the base numerical value at 2 makes for a simple base case. An annoying number of bytes are sp …
10
votes
Infinitely many ℕ
Haskell, 17 bytes
[[1..x]|x<-[1..]]
Try it online!
Since the challenge seems to allow non-flat output, we can simply generate a list of the lists [1],[1,2],[1,2,3,],..., as suggested by @AZTECCO.
H …
13
votes
Infinitely many ℕ
Python 2, 31 bytes
R=1,
while 1:print R;R+=len(R),
Try it online!
Thanks to @Danis for saving a byte here over R+=R[-1]+1,. This
Prints:
(1,)
(1, 1)
(1, 1, 2)
(1, 1, 2, 3)
(1, 1, 2, 3, 4)
(1, 1, 2, …
3
votes
An unknowably odd function
Haskell, 18 bytes
f(h:t)=h:reverse t
Try it online!
Based on Mitchell Spector's idea. Leaves the first digit in place and reverses the rest. Input and output are as strings.
76
votes
Print some JSON
Python 2, 14 bytes
print{`str`:1}
Outputs:
{"<type 'str'>": 1}
The backticks get the string representation in Python 2. Usually, this outputs inside creates single quotes, which Python recognize …
1
vote
Bijective mapping from integers to a variable number of bits
Haskell, 35 bytes
h 1=[]
h n=mod n 2:h(div n 2)
h.(+1)
Haskell doesn't have a binary built-in, so the (reversed) conversion is done manually. To remove the initial 1, the base case has 1 transform t …
3
votes
Golf a bijection \$\mathbb{N}^n\to\mathbb{N}\$
Haskell, 27 bytes
foldr1(\a b->2^a*(2*b+1)-1)
Try it online!
Uses a different bijection from the Cantor pairing function. Every positive integer can be uniquely split into a power of 2 times an odd …
4
votes
Golf a bijection \$\mathbb{N}^n\to\mathbb{N}\$
Python 2, 38 bytes
f=lambda a,*l:l and(a-~a<<f(*l))-1or a
Try it online!
Takes input splatted like f(1,2,3).
Uses the pairing function \$p(a,b)=(2a+1)2^b\$. We use bit-shift <<b to shorten *2**b, an …
2
votes
Every Possible Cycle Length
Python, 43
The inverse of Sp3000's function, implemented recursively.
f=lambda n,k=1:n>k and k+f(n-k,k+1)or n%k+1
The function is a one-cycle followed by a two-cycle followed by a three-cycle, . …
10
votes
Bijective mapping from integers to a variable number of bits
Python, 20 bytes
lambda n:bin(~n)[4:]
Test:
>> [bin(~n)[4:] for n in range(16)]
['', '0', '1', '00', '01', '10', '11', '000', '001', '010', '011', '100', '101', '110', '111', '0000']
Doing lam …