Given a sequence of elements, foreseer tries to predict the next elements.
Foreseer searches for a pattern previously seen in the input sequence. It then gets the following elements (which should therefore happen in the future) and calculates the probability that each of them presents again.
Let's see look at some examples:
-
Consider this input:
010:- The last element is
0. - In the "past" there has already been an occurence of
0. That time it was followed by1. - We therefore predict that the next value will be
1again.
- The last element is
-
Consider this input:
0100:- The last element is
0. - In the "past" there have been two occurences of
0. Once it was followed by1, once by0. - We therefore predict that the next value will be either
1or0, with 50% chance, respectively.
- The last element is
-
Consider this input:
11101001:- The last elements are
01. - In the "past" there has been one occurence of
01. 01has always been followed by0, so we could output01with 100% chance.- However, we can also consider
1as the last element. - In the "past" there have been many occurences of
1. 1has been followed twice by1and once by0.- By combining these results we get that the next element could be
1or0, with 50% chance, respectively.
- The last elements are
- Open
main.swift - Edit the sequence
- Choose how many items you want to predict by changing the
branchesparameter offoresee(::) - Open a terminal and run
swift main.swift
Using this input 1,1,0,0,1 the output will look like:
1 - 0.50
┣╸0 - 0.33
┗╸1 - 0.67
0 - 0.50
┣╸0 - 0.50
┗╸1 - 0.50
You can use whatever you want. Just give credits :3