'Monitor ActiveRecord records and find patterns
Well, I'll try to be as clear as possible in my problem, and I hope to get answers with Ruby because I'm currently studying Ruby on Rails, but of course other solutions and services are welcome if they solve the problem.
Assuming I have a Colors model, with its name, number attributes .
The colors that the model will receive will be limited to BLUE, GREEN and PINK only, and its 30 numbers: EX: 10 blue, 10 green and 10 pink.
So considering that my model colors will just register:
[{name: "blue", number: "3"}, {name: "blue", number: "4"}, {name: "green", number: "12"}] ...
The problem lies in how I can't figure out where to start looking for predefined patterns and having a response once the pattern is correct. For example:
Assume I have a list with the pattern: ["blue", "blue", "blue", "blue"]
Every time my model receives a new color, starting from the last one that came out and its previous ones formed the same pattern as above, I would get a type of return.
I just can't imagine where to start. I thought of a simple method like this being executed on a callback of every new record. But where should I implement this? where the list of colors will be persistent for adding new colors and getting to be the same as the proposed standard?
pattern = ["blue", "blue", "blue", "blue"]
last_record = Color.first
def monitor_pattern(last_record, pattern)
color_list = []
color_list.push(last_record.name)
if color_list == pattern
return true
else
return false
end
end
I don't know if I can be clear, but I appreciate any kind of help!
EDIT
Hello friends, thanks for the answer... Well here we go, here is my application https://historicosblaze.com/doubles
Now let's see if I can explain, if you notice in the application, a new color is being registered every few seconds, these colors are being registered in the bank.
If you look, colors can create patterns, like, 3 reds in a row, 7 blacks in a row, 1 red 1 black 1 red 1 black... and so on, presuming I have pre-established patterns and want to keep monitoring the arrivals of new records, I can't imagine where and with what I should monitor the creation of these patterns, if for example, in the last output of 1 red followed by 7 reds, it formed a pattern of 8 reds in a row, which was a pre-established pattern , 8 red, this must be being monitored and recorded somewhere.
Solution 1:[1]
I am not entirely sure if I understood your requirements correctly but it feels like this might be what you are looking for:
def monitor_pattern(pattern)
last_four_colors = Color.last(4)
pattern == last_four_colors.map(&:name)
end
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | spickermann |
