Array Pattern Matching Language
popularity-contest array-manipulation language-design
Everyone is (or should be) acquainted with the ways of the regular expression. At its core, a regex is made for matching strings. However, it's just matching something, right? Why not match arrays? That would be something!
Description
Your task now is to design and implement a new numeric array pattern-matching language. The only requirement is that your created language satisfies the mandatory tasks detailed below. However, this being a popularity contest, you may wish to opt for the bonus tasks and maybe even some competitive golfing value.
To be more precise, your language should be able to take patterns and match them against arbitrary arrays of integers. They should minimally be used to determine whether or not an array matches the pattern. This can be done by return a boolean, two different strings, etc.
Voting
As a voter, you should keep in mind the criteria mentioned, and ask yourselves these questions:
- Is the language trivial? That is, is there a lot that can be done other than the detailed tasks, or is the language minimally designed for these tasks?
- Is it well described? Does the author explain the solutions and the concept behind the language?
Tasks
(Thanks to Nathan Merrill for a lot of tasks!) Note that, for each task, the empty array can go in either category, unless otherwise stated.
1. That's odd!
Write a program in your language that will match the input array if all of its members are odd; otherwise, do not match.
MATCH
[1, 3, 5, 11, -1]
[5, 9, 1023, 3243]
NOT MATCH
[2, 1, 3, 4]
[0, 5, 6, 0]
2. Increasing in membership
Match an array that has each member more than the previous member.
MATCH
[0,1,2,3]
[2,5,100,203123]
[-432,-421,123,322]
NOT MATCH
[0,0,1,2]
[-2,3,-3]
[1,3,2]
3. Bodyguards
Match only arrays with a 0
on both sides.
MATCH
[0, 1, 2, 0]
[0, 5, 0]
[0, 0]
[0]
NOT MATCH
[1, 2, 3]
[4, 5]
[99]
4. Mountain
Match all arrays of the form [1, 2, 3, ..., N-2, N-1, N, N-1, N-2, ..., 3, 2, 1]
, for any N
.
MATCH
[1, 2, 3, 4, 5, 6, 5, 4, 3, 2, 1]
[1, 2, 3, 4, 3, 2, 1]
[1, 2, 1]
[1]
NOT MATCH
[1, 2, 4, 8, 4, 2, 1]
[3, 4, 5, 4, 3]
[3, 2, 1, 2, 3]
[2, 1, 1]
5. Shifting, 1 to 5
Match only arrays that consist of the numbers 1, 2, 3, 4, 5, 4, 3, 2
"in order", where the elements can be rotated around the array.
MATCH
[1, 2, 3, 4, 5, 4, 3, 2]
[2, 3, 4, 5, 4, 3, 2, 1]
[5, 4, 3, 2, 1, 2, 3, 4]
NOT MATCH
[1, 2, 3, 4, 5, 3, 2, 4]
[2, 3, 4, 5, 2, 3, 4, 1]
[1, 2, 3, 4, 5, 2, 3, 4]
[1, 2, 3]
[2]
[]
6. Not in my prime
Match an array if and only if all of it's members are not prime.
MATCH
[4, 6, 8, 9, 24]
[20, 40, 42, 45]
[1, 10, 100]
[]
NOT MATCH
[1, 100, 200, 300, 400, 401]
[2, 3, 4]
[2, 4, 8]
[3, 5, 7]
[2]
7. Unique
Match only arrays that contain no duplicates.
MATCH
[1, 2, 3, 4]
[5, 15, 25]
[21]
[]
NOT MATCH
[3, 4, 5, 3]
[10, 11, 10, 13]
[2, 2]
8. Not close
Match only arrays that, for each member N
, do not also have a member N+1
.
MATCH
[1, 3, 45, 30]
[20, 23, 26]
[]
NOT MATCH
[200, 201, 202]
[3, 9, 100, 4]
[2, 4, 5]
[0, 1]
9. Arithmetic Progression
Match only arrays that are arithmetic progressions; i.e. there elements increase by a constant factor. (Singletons may go either way.)
MATCH
[2, 6, 10, 14]
[5, 10, 15, 20]
[20, 19, 18]
[1, 2, 3]
[5, 6, 7, 8]
NOT MATCH
[5, 25, 125]
[2, 6, 5]
10. Fibonacci-esque
Match only arrays (of length greater than 2) where each element is the sum of the previous two (not counting the first 2), and match all arrays of length 2.
MATCH
[100, 1, 101, 102, 203, 305, 508, 813]
[-5, 5, 0, 5, 5, 10, 15, 25, 40]
[2, 1, 3, 4, 7, 11]
[0, 0, 0, 0, 0, 0]
[1, 0, 1, 1, 2]
[1, 2, 3, 5]
[10, 2]
NOT MATCH
[5, 10, 15, 20, 25]
[91, 32, 32]
[5, 5, 5]
[2]
[]
11. Self-descriptive
Match arrays that contain their lengths.
MATCH
[1]
[12, 2]
[4, 3, 2]
NOT MATCH
[0]
[]
[123, 122, 121]
[0, 2, 4, 6, 8]
12. Palindrome
Match all palindromic arrays. That is, arrays that are the same when reversed.
MATCH
[1]
[]
[2, 3, 4, 3, 2]
[-1, -1]
NOT MATCH
[3, 4, 2, 0]
[0, 0, 0, 1]
[1, 11]
13. Kth element at most K
(You can use 1-based or 0-based indexing. I will give test cases for 0-based indexing.)
Match all arrays whose Kth element is at most K.
MATCH
[0, 0, 2]
[0, 0, 0, 1]
[-4, -1, 0]
NOT MATCH
[2, 42]
[-4, -92, 4]
14. Twins
Match all arrays of even length whose first half is equal to the second half.
MATCH
[1, 2, 3, 1, 2, 3]
[4, 4]
[0, 0, 0, 0]
NOT MATCH
[4, 9, 9, 4]
[9, 2, 13, 2, 9]
[1, 2, 3, 1, 2]
15. A simplified date
Match all 3-element arrays which constitute a valid date. The array will be [year, month, day]
. That is, year >= 0
, 1 <= month <= 12
, and 1 <= day <= 31
.
MATCH
[2018, 6, 16]
[1990, 1, 1]
[0, 6, 31]
[3014, 7, 13]
[1293, 9, 9]
[12, 12, 12]
NOT MATCH
[-4, 12, 9]
[100, 100, 4]
[1929, 13, 31]
[4102, 12, 32]