How to detect an inconsistent pattern of coordinates? - algorithm

For simplicity lets assume that I have a list of X,Y coordinates. X has a limited range of 0-100 mm, and Y has 0 - inf mm. Every iteration a new list of coordinates is received. All the coordinates where the delta between their Y to the last Y is bigger then a configurable value (lets say 300M) are removed from the list.
I need to find an algorithm that will help me select all the co-ordinates that are repeating in a pattern.
I have a way of solving this problem but I would prefer a more "methodical" way.
For example, Lets say I have a list with the following coordinates (X, Y):
(2, 9), (2, 18), (2, 27), (2, 54), (2, 63), ...
Y delta is configured as 9mm.
Almost all Y deltas are 9 except between the 3rd and the 4th coordinates. Those coordinates are missing but still it is a pattern and the algorithm should be able to tell that all those coordinates are from the same pattern.
Another example:
(2, 9), (2, 13), (2, 18), (2, 22), (2, 27), (2, 54), (2, 63), ...
Here I expect the same result as the first example even though there are more coordinates that are not part of the pattern.
I need to be able to know exactly which coordinates are in the detected pattern.

The deltas of the first example are
9 9 27 9
and you can observe that the most frequent is 9, which is a divisor of 27, and with some good will, you can infer that two data points are missing.
The deltas of the second are
-5 14 4 5 27 9
and it makes no sense to see a pattern there.

From what I can see, the patterns you want are in small-shift deltas and GCDs. Since the first example solves pretty quickly, let's look at the second.
9, 13, 18, 22, 27, 54, 63
Here, the shift-1 (e.g. 13-9, 54-27) and shift-2 (18-9, 54-22) deltas are
4, 5, 4, 5, 27, 9
9, 9, 9, 32, 36
and the GCDs of those adjacent deltas (e.g. (4,5), (27,9), or (9, 9) ) are
1, 1, 1, 1, 9
9, 9, 1, 1
This makes 9 a very strong suspect, being the only non-unity value in the list. A second pass to look for multiples of 9 will easily reveal the preponderance of such values. Non-conforming values get removed; multiples of 9 indicate that we need to impute missing values.
You can hard-code those searches in your favorite language. Many modern languages have one or two packages that will handle such a shift with a simple operator; in Python, both numpy and pandas will do this.
Can you take it from there?

Related

Project Euler 68: What is the maximum 16-digit string for a “magic” 5-gon ring? misunderstand the problem

((7, 5, 4), (10, 4, 2), (8, 2, 6), (7, 6, 3), (8, 3, 5)) This is the solution set from my algorithm which gave max sum string. Please some one can tell me what's wrong with this solution set. The numbers string of this solution set produce large number than the answer
Question in the ProjectEuler
I found what's wrong here with help of a my friend. The solution set must contains all numbers from 1-10. In my solution set all numbers from 1-10 are not included.

Dice Rolling Probability in using R studio

So I want to know how to set this up to get the correct answer in R.
If you roll two standard 6-sided die, estimate the probability that the difference between them is 3, 4, or 5 (and not 0, 1, or 2).
I know how to set up the basic model of finding a sum but am not sure on how I can find the difference. PLEASE HELP!!!
I don't know exactly what you are doing. I guess you simulate the die rolls? So you have a data.frame with two columns, each containing the rolls of one die?
Like this:
die_rolls <- data.frame(die1 = c(1, 2, 6, 3, 1, 4), die2 = c(3, 5, 6, 2, 1, 1))
Then you could create a new column that calculates the difference. And then uses the absolute value of it, i.e. you won't have negative values.
die_rolls$difference <- abs(die_rolls$die1 - die_rolls$die2)

Dynamic programming - board with multiplier

I got quite standard DP problem - board nxn with integers, all positive. I want to start somewhere in the first row, end somewhere in the last row and accumulate as much sum as possible. From field (i,j) I can go to fields (i+1, j-1), (i+1, j), (i+1, j+1).
That's quite standard DP problem. But we add one thing - there can be an asterisk on the field, instead of the number. If we meet the asterisk, then we got 0 points from it, but we increase multiplier by 1. All numbers we collect later during our traversal are multiplied by multiplier.
I can't find out how to solve this problem with that multiplier thing. I assume that's still a DP problem - but how to get the equations right for it?
Thanks for any help.
You can still use DP, but you have to keep track of two values: The "base" value, i.e. without any multipliers applied to it, and the "effective" value, i.e. with multipliers. You work your way backwards through the grid, starting in the previous-to-last row, get the three "adjacent" cells in the row after that (the possible "next" cells on the path), and just pick the one with the highest value.
If the current cell is a *, you get the cell where base + effective is maximal, otherwise you just get the one where the effective score is highest.
Here's an example implementation in Python. Note that instead of * I'm just using 0 for multipliers, and I'm looping the grid in order instead of in reverse, just because it's more convenient.
import random
size = 5
grid = [[random.randint(0, 5) for _ in range(size)] for _ in range(size)]
print(*grid, sep="\n")
# first value is base score, second is effective score (with multiplier)
solution = [[(x, x) for x in row] for row in grid]
for i in range(1, size):
for k in range(size):
# the 2 or 3 values in the previous line
prev_values = solution[i-1][max(0, k-1):k+2]
val = grid[i][k]
if val == 0:
# multiply
base, mult = max(prev_values, key=lambda t: t[0] + t[1])
solution[i][k] = (base, base + mult)
else:
# add
base, mult = max(prev_values, key=lambda t: t[1])
solution[i][k] = (val + base, val + mult)
print(*solution, sep="\n")
print(max(solution[-1], key=lambda t: t[1]))
Example: The random 5x5 grid, with 0 corresponding to *:
[4, 4, 1, 2, 1]
[2, 0, 3, 2, 0]
[5, 1, 3, 4, 5]
[0, 0, 2, 4, 1]
[1, 0, 5, 2, 0]
The final solution grid with base values and effective values:
[( 4, 4), ( 4, 4), ( 1, 1), ( 2, 2), ( 1, 1)]
[( 6, 6), ( 4, 8), ( 7, 7), ( 4, 4), ( 2, 4)]
[( 9, 13), ( 5, 9), ( 7, 11), (11, 11), ( 9, 9)]
[( 9, 22), ( 9, 22), ( 9, 13), (11, 15), (12, 12)]
[(10, 23), ( 9, 31), (14, 27), (13, 17), (11, 26)]
Thus, the best solution for this grid is 31 from (9, 31). Working backwards through the grid solution grid, this corresponds to the path 0-0-5-0-4, i.e. 3*5 + 4*4 = 31, as there are 2 * before the 5, and 3 * before the 4.

Fast lightweight method of detecting skipped values in a cyclic sequence

I have a single value that, over time, increases from 1 to 6 and then returns to 1. So over several frames the sequence seen would be:
1, 2, 3, 4, 5, 6, 1, 2, 3, 4...
If a hardware device is defective one of those numbers may not read correctly and so the sequence may look like this:
1, 2, 6, 4, 5, 6, 1, 2, 6, 4...
What is a fast, short method of detecting this inconsistency?
Note:
This is designed for a hard real-time environment
My current method is to check that each new value every frame has a difference no greater than 1 from the last value, then there is a special case for the difference between 1 and 6 which is allowed to be 5
I am wondering whether there is a simple algorithm or trick to solving this problem
The sequence is also valid if it is in reverse, e.g.:
6, 5, 4, 3, 2, 1, 6, 5, 4, 3....
You can use the modulo operator:
(current_value - previous_value + 6) mod 6 == 1
The +6 is there to ensure a positive argument to mod. Different languages treat negative values differently, but if -5 mod 6 evaluates to 1 in yours, you can omit the +6.

Riffling Cards in Mathematica

My friend posed this question to me; felt like sharing it here.
Given a deck of cards, we split it into 2 groups, and "interleave them"; let us call this operation a 'split-join'. And repeat the same operation on the resulting deck.
E.g., { 1, 2, 3, 4 } becomes { 1, 2 } & { 3, 4 } (split) and we get { 1, 3, 2, 4 } (join)
Also, if we have an odd number of cards i.e., { 1, 2, 3 } we can split it like { 1, 2 } & { 3 } (bigger-half first) leading to { 1, 3, 2 }
(i.e., n is split up as Ceil[n/2] & n-Ceil[n/2])
The question my friend asked me was:
HOW many such split-joins are needed to get the original deck back?
And that got me wondering:
If the deck has n cards, what is the number of split-joins needed if:
n is even ?
n is odd ?
n is a power of '2' ? [I found that we then need log (n) (base 2) number of split-joins...]
(Feel free to explore different scenarios like that.)
Is there a simple pattern/formula/concept correlating n and the number of split-joins required?
I believe, this is a good thing to explore in Mathematica, especially, since it provides the Riffle[] method.
To quote MathWorld:
The numbers of out-shuffles needed to return a deck of n=2, 4, ... to its original order are 1, 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, ... (Sloane's A002326), which is simply the multiplicative order of 2 (mod n-1). For example, a deck of 52 cards therefore is returned to its original state after eight out-shuffles, since 2**8=1 (mod 51) (Golomb 1961). The smallest numbers of cards 2n that require 1, 2, 3, ... out-shuffles to return to the deck's original state are 1, 2, 4, 3, 16, 5, 64, 9, 37, 6, ... (Sloane's A114894).
The case when n is odd isn't addressed.
Note that the article also includes a Mathematica notebook with functions to explore out-shuffles.
If we have an odd number of cards n==2m-1, and if we split the cards such that during each shuffle the first group contains m cards, the second group m-1 cards, and the groups are joined such that no two cards of the same group end up next to each other, then the number of shuffles needed is equal to MultiplicativeOrder[2, n].
To show this, we note that after one shuffle the card which was at position k has moved to position 2k for 0<=k<m and to 2k-2m+1 for m<=k<2m-1, where k is such that 0<=k<2m-1. Written modulo n==2m-1 this means that the new position is Mod[2k, n] for all 0<=k<n. Therefore, for each card to return to its original position we need N shuffles where N is such that Mod[2^N k, n]==Mod[k, n] for all 0<=k<n from which is follows that N is any multiple of MultiplicativeOrder[2, n].
Note that due to symmetry the result would have been exactly the same if we had split the deck the other way around, i.e. the first group always contains m-1 cards and the second group m cards. I don't know what would happen if you alternate, i.e. for odd shuffles the first group contains m cards, and for even shuffles m-1 cards.
There's old work by magician/mathematician Persi Diaconnis about restoring the order with perfect riffle shuffles. Ian Stewart wrote about that work in one of his 1998 Scientific American Mathematical Recreation columns -- see, e.g.: http://www.whydomath.org/Reading_Room_Material/ian_stewart/shuffle/shuffle.html
old question I know, but strange no one put up an actual mathematica solution..
countrifflecards[deck_] := Module[{n = Length#deck, ct, rifdeck},
ct = 0;
rifdeck =
Riffle ##
Partition[ # , Ceiling[ n/2], Ceiling[ n/2], {1, 1}, {} ] &;
NestWhile[(++ct; rifdeck[#]) &, deck, #2 != deck &,2 ]; ct]
This handles even and odd cases:
countrifflecards[RandomSample[ Range[#], #]] & /# Range[2, 52, 2]
{1, 2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 28, 5, 10, 12, 36,
12, 20, 14, 12, 23, 21, 8}
countrifflecards[RandomSample[ Range[#], #]] & /# Range[3, 53, 2]
{2, 4, 3, 6, 10, 12, 4, 8, 18, 6, 11, 20, 18, 28, 5, 10, 12, 36, 12,
20, 14, 12, 23, 21, 8, 52}
You can readily show if you add a card to the odd-case the extra card will stay on the bottom and not change the sequence, hence the odd case result is just the n+1 even result..
ListPlot[{#, countrifflecards[RandomSample[ Range[#], #]]} & /#
Range[2, 1000]]

Resources