| Author | PiotrekG | 
| Submission date | 2018-08-24 07:58:59.581729 | 
| Rating | 5964 | 
| Matches played | 274 | 
| Win rate | 58.39 | 
Use rpsrunner.py to play unranked matches on your computer.
from __future__ import division
import random
import bisect
beat = {'R': 'P', 'P': 'S', 'S': 'R'}
class Markov_model():
    def __init__(self, matrix):
        self.matrix = matrix
    def update_matrix(self, olag2, ilag1, lim):
        self.matrix[olag2][ilag1]['N'] = self.matrix[olag2][ilag1]['N'] + 1
        sum_n = 0
        for i in self.matrix[olag2]:
            sum_n += self.matrix[olag2][i]['N']
        for i in self.matrix[olag2]:
            self.matrix[olag2][i]['Pr'] = self.matrix[olag2][i]['N'] / sum_n
        for i in self.matrix[olag2]:
            self.matrix[olag2][i]['N'] = self.matrix[olag2][i]['N'] * lim / sum_n
    @staticmethod
    def weighted_choice(choices):
        values, weights = zip(*choices)
        total = 0
        cum_weights = []
        for w in weights:
            total += w
            cum_weights.append(total)
        x = random.random() * total
        i = bisect.bisect(cum_weights, x)
        return values[i]
    def predict(self, olag1):
        prs = []
        for i in self.matrix[olag1]:
            prs.append((i, self.matrix[olag1][i]['Pr']))
        prediction = self.weighted_choice(prs)
        return prediction
lim = 5
if input == '':
    output = random.choice(list(beat.keys()))
    olag1 = ''
    olag2 = ''
    ilag1 = ''
    # fist level is my output lag(2) and second level is input lag(1)
    matrix = {
        'R': {'R': {'Pr': 1 / 3, 'N': lim / 3},
              'P': {'Pr': 1 / 3, 'N': lim / 3},
              'S': {'Pr': 1 / 3, 'N': lim / 3}},
        'S': {'R': {'Pr': 1 / 3, 'N': lim / 3},
              'P': {'Pr': 1 / 3, 'N': lim / 3},
              'S': {'Pr': 1 / 3, 'N': lim / 3}},
        'P': {'R': {'Pr': 1 / 3, 'N': lim / 3},
              'P': {'Pr': 1 / 3, 'N': lim / 3},
              'S': {'Pr': 1 / 3, 'N': lim / 3}}
    }
    model = Markov_model(matrix)
elif olag2 != '':
    model.update_matrix(olag2, ilag1, lim)
    predicted_input = model.predict(olag1)
    output = beat[predicted_input]
else:
    output = random.choice(list(beat.keys()))
olag2 = olag1
olag1 = output
ilag1 = input