Largest smallest gap

24

1

Write a function or program which takes as input a set of distinct, non-negative integers \$\{x_1, \ldots, x_n\}\$ and outputs the smallest gap between any two values in the set, i.e. \$\min_{i\neq j}|x_i-x_j|\$.

Scoring

This is not code golf. Your score is the smallest gap of the set of code points of characters in your code (highest score wins). In other words: convert your code to integer code points (using ASCII or whatever code page is standard for your language); remove duplicates; use that as input to your program; the output is your score.

For instance, if your code is min(alldifferences(input)), to get your score convert m->109, i->105, n->110, (->40, ... and your score will be 1, since the codepoints for m and n are only 1 apart.

You may assume that the input contains at least 2 distinct values; behaviour is undefined otherwise. Your code must include at least two distinct characters.

If your encoding is ASCII, you can use this online R code to verify your score.

Test cases

Input         | Output
0 1 2 3 4     | 1
2 4 8 16 32   | 2
1             | undefined
3 14 159 2653 | 11
4 6 8 9 12 14 | 1
3 14 2        | 1

Default rules for input/output apply. In particular, since the input is a set, you may assume any structure for the input.

Robin Ryder

Posted 2019-12-09T08:25:26.297

Reputation: 6 625

1Something that I just understood: we remove duplicates when scoring our code, but we get distinct integers when it’s normal inputs. So we don’t have to remove duplicates ourselves in the code, right? – Fatalize – 2019-12-09T09:32:38.753

@Fatalize That's correct: the input is made of distinct integers. – Robin Ryder – 2019-12-09T09:38:20.117

1Suggested test case: [3,14,2]. Output should be 1, right? – Fatalize – 2019-12-09T09:38:44.367

@Fatalize Yes. Edited in. – Robin Ryder – 2019-12-09T09:39:30.127

Unary, Lenguage like language may get any large score if they are allowed. Otherwise, answers in languages like Aheui, PerlYuYan would win this challenge. But sadly I can speak none of them. – tsh – 2019-12-09T11:27:47.500

@LuisMendo Yes, largest score wins. It is already in the text, in bold. :-) – Robin Ryder – 2019-12-09T11:28:49.530

@tsh Agreed. I don't find answers in Unary and Lenguage very interesting for this challenge. I have added your suggestion. – Robin Ryder – 2019-12-09T11:55:11.730

@Robin Ah, sorry, I hadn’t seen it – Luis Mendo – 2019-12-09T12:31:35.110

1Where a language has its own codepage, can we opt to use UTF-8? Or do we have to use the language-specific codepage? – Nick Kennedy – 2019-12-09T17:06:03.847

@NickKennedy If the language has its own codepage, you may use ASCII if all your characters are ASCII, but you may not use UTF-8. – Robin Ryder – 2019-12-09T17:11:59.000

Unfortunately, since braces pairs ((), [],{}) and the lambda expression symbols (=>) are consecutive (or 1 apart for the case of {}), it seems impossible to get a score of more than 1 for most "non-golf" languages - Any chance to relax the scoring a bit so that these symbols are ignored? – G0BLiN – 2019-12-10T08:51:53.083

@G0BLiN [] are also 2 apart, like {}. I agree this makes the challenge difficult, but adding exceptions would defeat the point of the challenge. Non-golfing languages should probably aim for a score of 2 or 3. For instance, R makes a heavy use of (), but I have an (unposted) R solution with a score of 2, which was fun to work on. – Robin Ryder – 2019-12-10T09:36:55.990

Robin Ryder - it's your question :). My point is that any answer in a non-golfing answer is likely to reach the highest score possible without much effort - while with the relaxation of e.g. ignoring braces, these suddenly there's a possibility to reach a higher than trivial score - so there's a challenge, a competition and things become more interesting... – G0BLiN – 2019-12-10T12:47:36.663

Answers

11

05AB1E, score: 22 49 69 (5 4 bytes)

ê¥êн

Uses the 05AB1E encoding, where the bytes have the codepoints [234,165,234,14] (ê¥ are 69 apart).

+27 score by porting @Arnauld's approach of sorting.
+20 score (and even -1 byte at the same time) thanks to @Grimmy.

Try it online or verify all test cases.

Explanation:

ê     # Sort and uniquify the (implicit) input-list (in ascending order)
 ¥    # Get the deltas of this sorted list
  ê   # Sort and uniquify this again
   н  # Pop this list and push its first item
      # (which is output implicitly as result)

Kevin Cruijssen

Posted 2019-12-09T08:25:26.297

Reputation: 67 575

3ê¥êн scores 69. – Grimmy – 2019-12-09T12:55:11.240

@Grimmy, isn't 7 (172-165)? – Nahuel Fouilleul – 2019-12-09T13:06:46.760

@NahuelFouilleul No.

– Grimmy – 2019-12-09T13:08:03.413

@Grimmy Very nice, not only do you increase the score, but it also saves a byte if this were a code-golf challenge. Thanks! – Kevin Cruijssen – 2019-12-09T13:18:28.340

4

Brachylog, Score = 12

⊇ᶠl₂ˢ-ˢȧˢ⌋

Try it online!

The minimum gap is 12, between and , and also between and ˢ (see the code page).

Explanation

⊇ᶠ             Find all subsets of the input
  l₂ˢ          Only keep those that have a length of 2
     -ˢ        Compute the difference of each subset
       ȧˢ      Compute the absolute value of each difference 
         ⌋     Output is the smallest one

Fatalize

Posted 2019-12-09T08:25:26.297

Reputation: 32 976

4

Charcoal, 13 bytes, score 3

I⌊Eθ⌊ΦEθ↔⁻ιλλ

Try it online! Link is to verbose version of code and takes the code's own code points as input. Explanation:

   θ            Input array
  E             Map over elements
       θ        Input array
      E         Map over elements
          ι     Outer element
           λ    Inner element
        ↔⁻      Take the difference
     Φ      λ   Filter out zero values
 ⌊  ⌊           Take the minimum of the minima
I               Cast to string for implicit print

Charcoal's loop variables are implicit but fortunately Map consumes two variables and even more fortunately there is no variable j so there is actually a minimum difference of 3.

Neil

Posted 2019-12-09T08:25:26.297

Reputation: 95 035

4

Jelly, 5 bytes, score = 24

This is most probably sub-optimal. Uses Jelly code page.

ṢIṢ1ị

Try it online! (with the code points of the source code)

Commented

ṢIṢ1ị  - a monadic link taking a list, e.g. [183, 73, 49, 216]
Ṣ      - sort in ascending order -> [49, 73, 183, 216]
 I     - get increments -> [24, 110, 33]
  Ṣ    - sort in ascending order -> [24, 33, 110]
   1ị  - extract the first element -> 24

Arnauld

Posted 2019-12-09T08:25:26.297

Reputation: 111 334

4

MathGolf, score = 35

ê│╓

Try it online!

Uses MathGolf's codepage along with this simple Python script.

ê    Take user input as array of integers
 │   Take differences
  ╓  Find minimum

79037662

Posted 2019-12-09T08:25:26.297

Reputation: 1 739

3

Gaia, 3 bytes, score = 36

ȯọ⌋

Try it online!

Uses the Gaia codepage. ȯọ are 36 apart.

Giuseppe

Posted 2019-12-09T08:25:26.297

Reputation: 21 077

3

Jelly, score 53

’ÞIṂ

Try it online!

The code page is here. The code points are 253 20 73 179 which has minimal difference of 73-20=53 as may be seen in this self-evaluation.

How?

      ’ÞIṂ - Link: list of numbers, L     e.g. [253, 20, 73, 179]
 20:   Þ   - sort L by:
253:  ’    -   decrement                       [20, 73, 179, 253]
 73:    I  - incremental differences           [53, 106, 74]
179:     Ṃ - minimum                           53

Jonathan Allan

Posted 2019-12-09T08:25:26.297

Reputation: 67 804

Why does the Jelly wiki say that Þ is outer product/table and you say it's sort by? – Neil – 2019-12-09T22:49:53.943

Because there are two thorns in the code-page: 20 (Þ) = sort by key function; and 31 (þ) = outer product. – Jonathan Allan – 2019-12-09T22:55:12.287

Ah, sorry, I didn't realise that my search was case insensitive... – Neil – 2019-12-10T01:18:36.523

2

Japt -g, 6 bytes, score: 43

n¹än¹n

Try it (includes codepoints test case)

Shaggy

Posted 2019-12-09T08:25:26.297

Reputation: 24 623

2

Jelly, 12 bytes, score = 35

ạ€`«/Ƈ€«/€«/

Try it online!

A monadic link taking a list of (optionally distinct) integers as its argument and returning the smallest gap. Generates an outer table of absolute differences between the inputs, filters out zero differences, finds the smallest in each row and then the smallest of those values. TIO link has a footer to translate the code into numeric codepoints within Jelly codepage (technically each is +1, but there is no effect on differences between pairs).

Pairwise differences shown here

Nick Kennedy

Posted 2019-12-09T08:25:26.297

Reputation: 11 829

2

MATL, Score 16

SdS9z)

Try it online!

For anything up to SdS) I could get a score of 17, but generating a 1 for indexing (because X< is too close to S) bumped the score down to 16.

Let's see if any ASCII-based language beats this...

Sanchises

Posted 2019-12-09T08:25:26.297

Reputation: 8 530

@Giuseppe Oh I didn't notice the input is a set, not a list. That'll probably help a lot. – Sanchises – 2019-12-10T17:37:50.727

2

Runic Enchantments, Score 1 (79 bytes)

D?)0]li[0y̤<<<<<<<<
Ro"/{~D-{:ii"6Xkq";$~\"akq"/~{R{0[il]0)?/:}S-:}}:{)?"E͍$;

Try it online!

(Note that due to the way input is coerced to the most appropriate type and we want decimal value 49 not decimal value 1 for such bytes; this applies to the newline, 0, 6 (etc), as well as the injection of all the spaces).

There's a few reasons I can't score higher. In decreasing order of troublesomeness.

  1. @ and ? are adjacent (? is required for conditionals, @ is one of two terminators)
  2. : and ; are adjacent (: is required to duplicate the top of the stack for non-destructive comparisons, ; is the other terminator)
  3. < and ; are adjacent
  4. > and ? are adjacent (using other entry points possible, but we're already score-limited, v is best option as ^ and ] are adjacent)
  5. D and E are adjacent (replacing D with / or \ or not using Eval possible)
  6. l and k are adjacent (l is required to read all input, k is required to inject a newline and entry point into the evaluated code; Eval not strictly needed)
  7. } and ~ are adjacent (pop-discarding an arbitrary value without corrupting the stack is difficult otherwise, [] does work, but limits score to 2, assuming we could overcome the other points, 0*+ also works, but + and * are adjacent)
  8. o and q limit score to 2
  9. [ and ] limit score to 2 (but not strictly necessary, just simplifies the input reading process, but lril1- is still score limited to 3 on its own plus r and q are adjacent as well as 0 and 1)
  10. l and o limit score to 3
  11. pre-processing the code and injecting troublesome bytes with reflection necessitates a minimum of 2 of 1234567890, all of which are going to cause low score limits.

Draco18s no longer trusts SE

Posted 2019-12-09T08:25:26.297

Reputation: 3 053

2

R, 47 bytes, score = 2

`-`=`mi\x6E`;`~`=diff;`?`=`s\x63\x61\x6E`;-~?""

Try it online!


The input is assumed to be unique and sorted ascending.

Verify the score here

digEmAll

Posted 2019-12-09T08:25:26.297

Reputation: 4 599

Well done for the first answer with score >1 in a verbose language! – Robin Ryder – 2019-12-20T13:14:33.727

0

C++, score : 1, bytes : 132

#include <cmath>
int f(int* a,int c){int m=abs(a[0]-a[1]),i,j,d;for(i=-1;++i<c;)for(j=i;++j<c;)m=(d=abs(a[i]-a[j]))<m?d:m;return m;}

The very need of the abs function kills my score, and any other C++ answer's.
It's actually return.

Try it online!

Varad Mahashabde

Posted 2019-12-09T08:25:26.297

Reputation: 171

1() being 1 apart also kills any C++ answers (in fact, very many practical languages will have a score of 1 due to this). – 79037662 – 2019-12-11T15:23:03.973

@79037662 Aww man – Varad Mahashabde – 2019-12-11T19:36:13.937

@VaradMahashabde I'm sorry - I was commenting on the other person's comment. I wasn't making a suggestion to you. I didn't mean to cause any confusion. I've deleted my comment so that I don't confuse anyone else. – Jerry Jeremiah – 2019-12-19T20:41:50.480

But return is usually unnecessary, as gcc produces a warning and then effectively returns the variable that was mentioned the last, and seems to return m here. – my pronoun is monicareinstate – 2019-12-20T13:53:40.567

@mypronounismonicareinstate It would matter if this was code golf, but as @79037662 said, () kills any dreams right away. Also that is UB AF, and I don't like it when there is even a remote chance of a kitten beam being shot into space or a space beam being shot into a kitten :-) – Varad Mahashabde – 2019-12-21T18:02:16.747