Questions tagged [open-ended-function]
For challenges where an exact output is not required but some property must still be fulfilled.
102
questions
81
votes
82
answers
8k
views
Print some JSON
This challenge is straightforward, but hopefully, there are plenty of avenues you can approach it:
You need to print/return a valid JSON object of at least 15 characters, not counting unessential ...
67
votes
161
answers
10k
views
Print all integers
Write a program or function which will provably print all integers exactly once given infinite time and memory.
Possible outputs could be:
...
55
votes
36
answers
5k
views
Turing-Complete Language Interpreter
A challenge I thought that would be very cool is to make an interpreter for a Turing-complete language of your choosing.
The rules are simple:
You may use any language to create this interpreter ...
51
votes
30
answers
12k
views
What an Odd Function
Your task here will be to implement a function1 that forms a permutation on the positive integers (A bijection from the positive integers onto themselves). This means that each positive integer ...
47
votes
4
answers
4k
views
f(g(x)) decreases while g(f(x)) increases
For this challenge you need to implement two functions, f and g, on the integers, such that f ∘ g is a strictly decreasing function while g ∘ f is a strictly increasing function. In other words, if ...
44
votes
32
answers
4k
views
Define a function f such that f(f(n)) = -n for all non-zero integers n
This challenge was inspired by a programming blog I frequent. Please see the original post here: A Programming Puzzle
Challenge
Define a function \$f:\mathbb{Q}\to\mathbb{Q}\$ such that \$f(f(n)) = -...
42
votes
70
answers
5k
views
Infinitely many ℕ
Background:
A sequence of infinite naturals is a sequence that contains every natural number infinitely many times.
To clarify, every number must be printed multiple times!
The Challenge:
Output a ...
42
votes
21
answers
2k
views
Three other numbers
Given three distinct numbers from \$1\$ to \$7\$, output three other distinct numbers from \$1\$ to \$7\$, that is having no numbers in common with the original numbers. Your code must produce a ...
38
votes
54
answers
3k
views
Output a random unary string
Your task is simple: output the letter x a random number of times. Every possible length of xs must have a non-zero probability ...
35
votes
14
answers
3k
views
Put together a Senate majority
There are one hundred members of the United States Senate. Assuming that nobody filibusters and that the Vice President isn't cooperating, at least fifty-one members are necessary to pass a bill. But ...
35
votes
1
answer
2k
views
Write a program whose nontermination is independent of Peano arithmetic
Challenge
Write a program P, taking no input, such that the proposition “the execution of P eventually terminates” is independent of Peano arithmetic.
Formal rules
(In case you are a mathematical ...
34
votes
19
answers
3k
views
Egyptian fraction representations of 1
An Egyptian fraction is a representation of a rational number using the sum of distinct unit fractions (a unit fraction is of the form \$ \frac 1 x \$ where \$ x \$ is a positive integer).
For all[1] ...
33
votes
10
answers
2k
views
Build a nest
The challenge is simple: write a program or function that, when given a finite non-negative integer, outputs a nested array.
The rules
Your code must produce a unique valid nested array for every ...
32
votes
6
answers
1k
views
Implement true string addition
Many languages allow strings to be "added" with +. However this is really concatenation, a true addition would follow the group axioms:
It is closed (...
29
votes
19
answers
3k
views
Transform characters of your choice into "Hello, world!"
Write a program or function that takes a character as input and outputs a character. Also, choose a list of 13 distinct ASCII printable characters (32-126). When a character from this list is passed ...
28
votes
11
answers
2k
views
Infinite ordinals from a well-ordering
Your task is to write a short program that represents a large (infinite) ordinal, using a well-ordering of the set of positive integers. Your program will take two different positive integers and ...
27
votes
63
answers
3k
views
Create an array with repeated numbers
Challenge
Your task in this question is to write a program or a named function which takes a positive integer n (greater than 0) as input via STDIN, ARGV or ...
27
votes
17
answers
3k
views
Give me odd, even, square, cube, prime and composite 3-digit numbers
Given a string which is guaranteed to be either odd, even, square, ...
27
votes
9
answers
2k
views
Walk Across a Keyboard
Given a word (or any sequence of letters) as input, you must interpolate between each letter such that each adjacent pair of letters in the result is also adjacent on a QWERTY keyboard, as if you ...
27
votes
6
answers
5k
views
Golf a number bigger than Loader's number
As a follow up to Shortest terminating program whose output size exceeds Graham's number and Golf a number bigger than TREE(3), I present a new challenge.
Loader's number is a very large number, ...
26
votes
7
answers
1k
views
Decode the Void
A void list is a list that at no level contains any non-list objects. Or if you prefer a recursive definition
The empty list is void
A list containing only other void lists is void
All void lists ...
25
votes
10
answers
2k
views
Floor of complex number
Background
Complex floor is a domain extension of the mathematical floor function for complex numbers. This is used in some APL languages to implement floor ⌊, ...
25
votes
20
answers
1k
views
Kolmogorov-simple numbers
If you visit Code Golf often, you may have heard of Kolmogorov complexity. It's usually defined as the amount of bytes required to express some string in a programming language.
Here the Kolmogorov-...
24
votes
11
answers
3k
views
Bijective function ℤ → ℤⁿ
It is trivially possible to create a bijective function from \$\mathbb{Z}\$ (the set of all integers) to \$\mathbb{Z}\$ (e.g. the identity function).
It is also possible to create a bijective ...
24
votes
16
answers
1k
views
Every Possible Cycle Length
A function (or program) which takes inputs and provides outputs can be said to have a cycle if calling the function on its own output repeatedly eventually reaches the original number. For instance, ...
24
votes
5
answers
2k
views
Add two real numbers ... probably
The problem statement here is pretty simple, take two real numbers on the range [0,1) as input and output their sum, with probability 1.
The catch here is that there are a lot of real numbers. There ...
22
votes
24
answers
2k
views
Generate an arbitrary half of a string
Given a string \$ x \$, we say another string \$ y \$ is half of it, if both of the following properties are true:
\$ y \$ is a (not necessarily continuous) subsequence of \$ x \$ - there exists a ...
22
votes
17
answers
3k
views
Don't repeat yourself
In this challenge you will be tasked with implementing a sequence of natural numbers such that:
Each number appears a natural number of times
No two numbers appear the same number of times
No two ...
21
votes
13
answers
2k
views
Output every sublist ... eventually
You will be given as input an infinite stream of positive integers.
Your task is to write a program which outputs an infinite sequence of lists with two requirements:
All lists in the output are ...
21
votes
11
answers
2k
views
Create a nibble shorthand
I recently stumbled across this image on wikimedia commons. It's a little bit of an information overload at first, but after examining it a bit it shows an interesting number system for writing ...
21
votes
10
answers
3k
views
An unknowably odd function
This challenge initially appeared in this challenge as a an extra brain teaser. I am posting it with permission from the original author so that we can have a formal competition.
Your task here ...
21
votes
2
answers
514
views
Rational decomposition
Write functions \$x(a)\$, \$y(a)\$ and \$z(a)\$ such that for any rational \$a\$ all functions return rational numbers and
$$x(a) \times y(a) \times z(a) \times (x(a) + y(a) + z(a)) = a$$
You may ...
20
votes
25
answers
3k
views
Give f and g that sometimes commute
Let \$Z\$ be either the integers, the positive integers, or the non-zero integers; pick whatever's convenient. Give two functions \$f\$ and \$g\$, each \$Z \to Z\$, such that:
\$f(g(a)) = g(f(a))\$, ...
20
votes
9
answers
1k
views
Split some points
proposed by @Adám in chat
Given an even number of finite points return a line \$y=mx+b\$ that evenly splits the points on both sides.
Specs
Take a list of distinct points \$(x,y)\$ (or a list of x-...
19
votes
10
answers
2k
views
Enumerate the rationals
The cardinality of the set \$\mathbb Q\$ of rational numbers is known to be exactly the same as that of the set \$\mathbb Z\$ of integers. This means that it is possible to construct a bijection ...
19
votes
1
answer
613
views
Demonstrate some advanced abstract algebra
Consider a binary operator \$*\$ that operates on a set \$S\$. For simplicity's sake, we'll assume that \$*\$ is closed, meaning that its inputs and outputs are always members of \$S\$. This means ...
18
votes
22
answers
3k
views
Be big more often
Related
You are a manager at a large number factory. You want to show everyone your business is doing well, by showing randomly chosen samples. Unfortunately, your business is not doing that well. But ...
18
votes
20
answers
1k
views
Generate a regular graph
Inspired by this Mathematica.SE post
Given two positive integers \$n, k\$ with \$n > k \ge 1\$, output a binary \$n\times n\$ matrix such that every row and column contains exactly \$k\$ 1s, and ...
18
votes
15
answers
2k
views
Design a commutative injective function between any (restricted) infinite set and unordered pairs thereof
Related, but this only requires positive integers and does not have to be commutative
The Cantor Pairing Function is described in this Wikipedia article. Essentially, it is an operation such that ...
18
votes
8
answers
870
views
Make every fixed list
We are to define the idea of a fixed list as follows:
A fixed list is a list of fixed lists.
This definition is recursive so it's useful to look at some examples.
The empty list ...
18
votes
9
answers
1k
views
Surjection from one string to two strings
This is the inverse of negative seven's question.
Write a program or function which, given any single, possibly-empty string of printable ASCII (codes \$[32,126]\$) outputs or returns two strings of ...
18
votes
7
answers
2k
views
Hilbert's binary Hotel
In this challenge you will be asked to implement any function (or full program) that fulfills two properties. Those properties are:
Your function must be an injective (reversible) function from the ...
17
votes
15
answers
1k
views
Enumerate all pure sets
In set theory, a set is an unordered group of unique elements. A pure set is either the empty set \$\{\}\$ or a set containing only pure sets, like \$\{\{\},\{\{\}\}\}\$.
Your challenge is to write a ...
17
votes
12
answers
1k
views
Encode the input to exclude a given character (part 1)
Your task is to write 2 functions/programs. They may share code.
The first function must, given a string and a character, output a new string that does not contain that character.
The second function ...
17
votes
11
answers
2k
views
Shortest total non-primitive recursive function
Natural numbers ≡ \$\mathbb{N}≡\{0,1,2,...\}\$
The submission can be either a program or a function, both cases will henceforth be referred to as "function".
The task is to golf the shortest ...
17
votes
7
answers
1k
views
As close to flat as possible
Write a program or function that fulfills the following
Scores less than 101 bytes under normal code-golf rules
Takes no input1 and always outputs a single integer.
Every integer is a possible output....
17
votes
7
answers
2k
views
Define the finite field GF(9)
\$GF(9)\$ or \$GF(3^2)\$ is the smallest finite field whose order isn't a prime or a power of two. Finite fields of prime order aren't particurlarly interesting and there are already challenges for \$...
17
votes
13
answers
3k
views
BigNum Bakeoff Reboot
Some of you may be familiar with the BigNum Bakeoff, which ended up quite interestingly. The goal can more or less be summarized as writing a C program who's output would be the largest, under some ...
17
votes
9
answers
2k
views
Is it irrational?
Your task is to make a program that decides if a real number between 0 and 1 is irrational or not. As stated, this is obviously impossible, so instead we will use the following definition:
...
16
votes
15
answers
1k
views
Encode integers with some others
Input a non-empty array of positive (greater than 0) integers. Output another non-empty array of positive integers which encode the input array. Output array does not use any numbers used in the input ...