-7
\$\begingroup\$

Challenge

In this challenge, the input will be an ordered set of numbers and the program should be able to tell if the set of numbers is an Arithmetic Sequence.

Input

  • The input will be a list separated by ,(comma) or , (comma+space). The minimum length of the list should be 3 elements.

Output

  • The Output can be a Boolean (True or False), or a number (1 for True, 0 for False).

Test cases

In: 1, 3, 5
Out: True

In: 1,4, 7
Out: 1

In: 5, 9,10
Out: 0

In: 5,3,1
Out: False

Rules

  • This is , so the shortest code by bytes wins.
  • The input will be finite

Best of Luck

\$\endgroup\$
  • 7
    \$\begingroup\$ Welcome to Code Golf Stack Exchange. Nice challenge. It is highly recommended to allow input and output by our default rules, instead of having arbitrary restrictions like prohibiting functions. Some languages only have functions. \$\endgroup\$ – Adám Jul 3 at 11:52
  • 5
    \$\begingroup\$ It is also highly recommended to provide some test cases. \$\endgroup\$ – Adám Jul 3 at 11:53
  • 2
    \$\begingroup\$ Will the input have a minimum of 2 elements. \$\endgroup\$ – Adám Jul 3 at 11:57
  • 5
    \$\begingroup\$ You say "a set of numbers". Does that mean they might not be in order? \$\endgroup\$ – xnor Jul 3 at 12:03
  • 4
    \$\begingroup\$ Why is 5,3,1 false? \$\endgroup\$ – xnor Jul 3 at 21:57
1
\$\begingroup\$

MathGolf, 5 bytes

ê│▀£┴

Input ,-delimited; outputs 1/0 for truthy/falsey respectively.

Try it online. (MathGolf is able to execute multiple test cases at once for each line of STDIN input, but feel free to only use a single input if you'd like.)

Explanation:

ê      # Read the comma-delimited input-string as integer-list
 │     # Get the forward differences of each overlapping pair in this list
  ▀    # Uniquify these differences
   £   # Pop and push the length to get the amount of unique differences
    ┴  # Check that this length is equal to 1 (1 if 1; 0 if >= 2)
       # (after which the entire stack joined together is output implicitly as result)
| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

Retina 0.8.2, 47 bytes

\d+
$*
A`(1+),(?!\1)
(?<=(1+),)\1

^1*(,1*)\1*$

Try it online! Only works on nondescending sequences of at least two integers. Explanation:

\d+
$*

Convert to unary.

A`(1+),(?!\1)

Check that the sequence is ascending.

(?<=(1+),)\1

Compute the differences after the first value.

^1*(,1*)\1*$

Check that all of the differences are equal.

| improve this answer | |
\$\endgroup\$
  • \$\begingroup\$ If it only handles non-descending input, then it doesn't handle "a set of numbers" as specified by OP, does it? \$\endgroup\$ – Adám Jul 3 at 12:01
  • \$\begingroup\$ @Adám Well, I wasn't sure what ordering of the set I could assume... I could easily sort the numbers if necessary, it would only cost 7 bytes I think. \$\endgroup\$ – Neil Jul 3 at 12:20
1
\$\begingroup\$

APL (Dyalog Unicode), 8 bytes (SBCS)

1=≢∪2-/⎕

Try it online!

1= Is 1 equal to

 the count of

 unique

2-/ pairwise differences

 in the evaluated input?

The prompts for input and executes it. Luckily, , is APL's concatenation functions, so the required input format evaluates to a list. Spaces are ignored.

| improve this answer | |
\$\endgroup\$
1
\$\begingroup\$

05AB1E, 5 bytes

',¡¥Ë

Input ,-delimited; outputs 1/0 for truthy/falsey respectively.

Try it online or verify a few more test cases.

Explanation:

',¡   '# Split the (implicit) input-string on ","
   ¥   # Get the deltas (forward differences) of each overlapping pair in this list
    Ë  # Check if all differences are equal (1 if truthy; 0 if falsey)
       # (after which this result is output implicitly)

Of course, this would have been 2 bytes with default I/O rules by taking a list input: ¥Ë.

| improve this answer | |
\$\endgroup\$

Not the answer you're looking for? Browse other questions tagged or ask your own question.