Stack Exchange Network

Stack Exchange network consists of 174 Q&A communities including Stack Overflow, the largest, most trusted online community for developers to learn, share their knowledge, and build their careers.

Visit Stack Exchange

Questions tagged [function]

For challenges that ask for a function or involve functions in some way. (Also known as methods or subroutines.)

31
votes
62answers
5k views

P Pr Pre Pref Prefi Prefix Prefixe Prefixes

Given some finite list, return a list of all its prefixes, including an empty list, in ascending order of their length. (Basically implementing the Haskell function ...
16
votes
14answers
1k views

Arbitrary Length Hashing

Consider you have a hash function \$\mathcal{H}\$ which takes strings of length \$2n\$ and returns strings of length \$n\$ and has the nice property that it is collision resistant, i.e. it is hard to ...
0
votes
1answer
112 views

Symmetric Numbers [duplicate]

write a function to tell if a number is symmetric or not. for example: input: 151 output: True input: 142 output: False and so on: 1111 True, 2390 False, 1226221 True
29
votes
15answers
4k views

Black Box Trigonometry

Write a program or function that can distinguish the following 12 trigonometric functions: sin, cos, ...
-4
votes
2answers
188 views

Fastest way to perform the equivalent of an if-statement in x86 assembly

The task is simple: Create an if-statement from scratch in x86 assembly that runs in as few clock-cycles as possible. Must be x86 assembly. Can't use any of the conditional jump/branch instructions (...
21
votes
13answers
2k views

Make me some curry

Having a function f that takes arguments x1, x2, …, xn                         &...
31
votes
67answers
7k views

How many arguments were passed?

Using your language of choice, write a function that takes a variable number of arguments and returns the number of arguments it was called with. Specifics: Your language needs to support variadic ...
15
votes
4answers
556 views

Tuple addition in pointfree

What is the shortest way we can express the function f(a,b)(c,d)=(a+c,b+d) in point-free notation? pointfree.io gives us ...
16
votes
7answers
545 views

Approximate definite integrals using Riemann sums

Left and right Riemann sums are approximations to definite integrals. Of course, in mathematics we need to be very accurate, so we aim to calculate them with a number of subdivisions that approaches ...
17
votes
7answers
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 ...
21
votes
7answers
2k views

Half-Exponential Function

A half-exponential function is one which when composed with itself gives an exponential function. For instance, if f(f(x)) = 2^x, then ...
6
votes
4answers
335 views

Compute the Lambert W function

Your challenge is to compute the Lambert W function. The W of x is defined to be the real value(s) y such that y = W(x) if x = y*(e^y) where ...
17
votes
15answers
1k 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 ...
40
votes
22answers
4k 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 ...
8
votes
3answers
150 views

Two interwoven chains

In this question I defined a "chain" function as a function that: is a permutation, meaning that every value maps to and is mapped to by exactly one value. and allows any value can be obtained from ...
14
votes
5answers
900 views

Golf a bijection within the natural numbers which map the primes to a proper subset of the primes

Definitions A bijection from a set S to a set T is a function from S to ...
52
votes
38answers
4k views

Arbitrary-length currying

Write a function, f, that takes in a positive integer and returns a function. The new function returned should be identical to f...
41
votes
4answers
3k 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 ...
14
votes
3answers
449 views

Compute the most efficient binary function

Today, we'll be computing the most efficient binary function. More specifically, we'll be computing the function which, when an expression is created from applying the function to the constant input 0 ...
10
votes
2answers
346 views

Convert pointfree to pointful

Being a Haskell hacker, I prefer pointfree notation over pointful. Unfortunately some people find pointfree notation hard to read, and I find it hard to get the correct number of parentheses when I ...
14
votes
5answers
181 views

Properties of Binary Functions

Many important topics in abstract algebra involve a binary function acting on a set. A number of properties of such functions have been defined in the investigation of such topics. Your challenge ...
2
votes
4answers
704 views

Pi Bowling to 10 in 10 [closed]

Your Goal: Estimate pi to at least 10 decimal places. The format is your choice - it can output as a number, as a string, as an output to STDOUT, etc. Seems easy, right? But wait, it's not as simple ...
1
vote
2answers
1k views

Javascript function challenge add(1,2) and add(1)(2) both should return 3 [closed]

A friend of mine challenged me to write a function that works with both of these scenarios add(1,2) // 3 add(1)(2) // 3 My instinct was the write an add() ...
9
votes
5answers
2k views

Implement t9-like functionality

Your challenge today is to implement a t9-like functionality. You will implement a function that will only have 2 parameters. You will receive 1 phone number in a string and the content of a text ...
33
votes
30answers
6k views

The Ackermann function

The Ackermann function is notable for being the one of the simplest examples of a total, computable function that isn't primitive recursive. We will use the definition of ...
2
votes
4answers
705 views

JavaScript: Tricks to shorten onkeydown, setInterval etc

Are there any tricks to shorten defined function names like setInterval, onkeydown, onkeyup, createLinearGradient or even return? When using it more than once it's nice to put it as a string into a ...
8
votes
12answers
1k views

Function/macro that returns true if one of its arguments contains a call to itself

Write a function (or macro) that returns true if and only if at least one of its arguments contains a call to the function itself and false otherwise. For example: ...
20
votes
18answers
4k views

Find nearest number in a given array

This is inspired by a real world problem I had. I'm curious to see if there is any clever way to go about this. You are given two unsorted arrays, A and B, each containing an arbitrary number of ...
12
votes
13answers
2k views

Theatre Seating

Task A theatre has 10 rows, labelled A to J from front to back, and 15 seats in each row, numbered 1 to 15 from left to right. ...
-1
votes
4answers
429 views

Riffle Shuffle Golf Time

Anyone who has spent any time around a deck of playing cards will recognize the riffle shuffle. The deck is cut in half and the two stacks are riffled simultaneously into the same pile before being ...
34
votes
66answers
7k views

Determine if all decimal digits are unique

Deleted questions on Stack Overflow sometimes make for great golf material. Write a function that takes a nonnegative integer as input, and returns true if all the digits in the base 10 ...
16
votes
24answers
6k views

Old MacDonald function

Create a function in your chosen language that prints out the following: ...
4
votes
5answers
460 views

Pack sequences of non-negative integers

Let N be the set of nonnegative integers, i.e. {0, 1, 2, 3, ...}. Let c0(N) be the set of all infinite sequences in N that converge to 0, i.e. { (xn) ∈ NN | xn → 0 as n → ∞}. c0(N)...
16
votes
21answers
2k views

Incremental Betting

Several months ago I had this question as a pre-screening puzzle for an interview. Recently when thinking about blog material, it popped in my head as a good example to use for solving a problem ...
-16
votes
13answers
1k views

Write a function that returns something different every time it is called [closed]

All these functional languages that return the same value every time a function is called give me a headache. Your task is to write a function that returns something different every time it is ...
77
votes
78answers
13k views

Call a method without calling it [closed]

Inspired by a now deleted StackOverflow question. Can you come up with a way to get a particular method executed, without explicitly calling it? The more indirect it is, the better. Here's what I ...
25
votes
7answers
3k views

Lifetime of a Worm

Terms A worm is any list of nonnegative integers, and its rightmost (i.e., last) element is called the head. If the head is not 0, the worm has an active segment consisting of the longest contiguous ...
6
votes
9answers
2k views

Shortest C \ C++ function to generate -1, 0 ,1 [closed]

Let a function receive two integers i and j between 0 and ...
28
votes
13answers
2k views

Write a function that tells you which of its lines has been removed

Write a function that contains five lines. If you run the function as-is, it should return 0. If you remove any one of the five lines and run the function, it should tell you which of the lines has ...
42
votes
35answers
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:Q->Q such ...
3
votes
2answers
313 views

Finding limits of functions

A limit in math determines the value that a function f(x) approaches as x gets closer and closer to a certain value. Let me use ...
-7
votes
6answers
837 views

Code Golf: simple way to get min & max

You will take a set of input numbers, and output the minimum and maximum of the set. You can either write two programs, with 1 program doing each function, or one combined program. Here's an example ...
9
votes
5answers
413 views

Golfed fixed point combinator

Write a fixed point combinator in as few characters as possible, in the language of your choice. free form (i.e., whatever's shortest): whole program, actual function, code snippet you may not use ...
13
votes
2answers
2k views

Roman numeral converter function

Create the shortest function to convert a string of Roman numerals to an integer. The rules for each letter can be found at the Wikipedia page. Letters above 1,000 will have parentheses placed ...