Periodic Arrays

Perl, 48 47 bytes

Includes +3 for -p

Give input as a list of numbers on STDIN

repeats.pl <<< "3 5 7 9 12 14 16 18"

repeats.pl:

#!/usr/bin/perl -p
s//0 /;s/\d+/$'-$&/eg;/(.+?)\1*-/;$_=s/$1//g

Does not work if the input is a single 0 (not clear if that is a valid input). Fixing that takes 2 more bytes:

#!/usr/bin/perl -p
$_=s//0 /%s/\d+/$'-$&/eg-/^(.+?)\1*-/||s/$1//g

Explanation

Adds a 0 in front of the sequence and then constructs a list of differences. The example input

3 5 7 9 12 14 16 18

becomes:

3 2 2 2 3 2 2 2 -18

The -18 is because the last difference is calculated relative to the end of the string which in perl will behave like 0. On this transformed list the problem is equivalent to finding the most often repeating substring before the - sign (this is why a single 0 as input does not work because only in that case there is no minus sign)


Retina, 38 bytes

\d+
$*
(?<=(1*) )\1

(.+?)(| \1)+$
$#2

Input should be space-separated.

Try it online! (The first line enables a linefeed-separated test suite.)

Explanation

\d+
$*

Convert input to unary by replacing each number n with n copies of 1.

(?<=(1*) )\1

Compute consecutive differences, by removing from each number the previous number.

(.+?)(| \1)+$
$#2

Try to match the entire input as a repetition of difference (favouring more repetitions of fewer differences) and replace it with the number of repetitions.


05AB1E, 21 20 bytes

0)«¥©v®NôÙgi®Nôg}})Z

Explanation

0)«                   # prepend a 0 to the beginning of the list
   ¥©                 # calculate deltas and store in register
     v           }    # for each N in range(0, len(deltas))
      ®Nô             # split deltas into N chunks
         Ù            # uniquify list
          gi    }     # if length == 1
            ®Nôg      # push chunk length
                  )Z  # return max chunk length

Try it online!

Modified test suite

Tags:

Math

Code Golf