# coding= utf-8
from Core.PPlay.window import *
from Core.Components.Abstracts.DrawingComponent import *
from Core.PPlay.gameimage import *
from Core.Vector import Vector2

from Core.Scene.SceneManager import SceneManager

# Destruir objetos instanciados dinamicamente ao inciar cena

class Game:

    DEVELOPMENT_MODE = False

    # Configurações da janela
    WINDOW_TITLE = 'Space Warriors'
    WINDOW_WIDTH = 1400
    WINDOW_HEIGHT = 560
    DELTA_TIME = 0

    SPEED_MULTIPLIER = 1
    GAME_DIFFICULTY = 1
    GAME_MODE = 2

    moveSpeedBase = 360
    window = None
    player = None
    debug = {
		'scene': None
	}
    score = 0

    def __init__(self):
        self.__bootstrap()

#------------------------------- ESTÁTICOS -------------------------------------
    @classmethod
    def getWindowCenter(cls):
        ''' Obtém o ponto central da janela do jogo '''
        return Vector2(
            Game.WINDOW_WIDTH / 2,
            Game.WINDOW_HEIGHT / 2
        )

    @classmethod
    def getKeyboard(cls):
        ''' Obtém a atual instância do teclado sendo utilizada '''
        return Game.window.get_keyboard()

    @classmethod
    def getMouse(cls):
        ''' Obtém a atual instância do mouse sendo utilizada '''
        return Game.window.get_mouse()

    @staticmethod
    def findGameObjectWithName(name):
        return SceneManager.getCurrentScene().getGameObjectWithName(name)

    @classmethod
    def developmentMode(cls):
        cls.DEVELOPMENT_MODE = True

    @classmethod
    def gameOver(cls):
        from .FileScoreManager import gravaPontuacao

        SceneManager.changeScene('main_menu')

        playerName = input("Digite o seu nome: ")
        gravaPontuacao((playerName, Game.score))

        print('Jogo salvo com sucesso!')
        cls.window.close()

    @classmethod
    def newLevel(cls):
        from GameObjects.EnemiesGrid import EnemiesGrid
        cls.GAME_DIFFICULTY += 0.5

        enemyGrid = EnemiesGrid(6, 2)
        enemyGrid.setName('enemies')

        SceneManager.getCurrentScene().addGameObject(enemyGrid)

# ------------------------------- ADIÇÃO DE ELEMENTOS -------------------------------------
    def setBackground(self, backgroundPath):
        ''' Adiciona um background para o Game '''
        self.background = GameImage(backgroundPath)

    @classmethod
    def setPlayer(cls, player):
        ''' Seta o jogador para que o mesmo possa ser visto globalmente '''
        cls.player = player

# ------------------------------- LIFECYCLE -------------------------------------
    def __bootstrap(self):
        window = Window(Game.WINDOW_WIDTH, Game.WINDOW_HEIGHT)
        window.set_title(Game.WINDOW_TITLE)

        Game.window = window

    def start(self):
        if(Game.DEVELOPMENT_MODE):
            from Core.Debug import index

            #SceneManager.changeScene(SceneManager.getSceneByIndex(1).getSceneName())
            SceneManager.changeScene(SceneManager.getSceneByIndex(0).getSceneName())

            index.createDebugScene()
            index.showFramerate()

            Game.debug['scene'].activeScene()
        else:
            SceneManager.changeScene(SceneManager.getSceneByIndex(0).getSceneName())

        self.__gameLoop()

    def __gameLoop(self):
        while(True):
            Game.DELTA_TIME = self.window.delta_time()

            # Inputs
            if (Game.getKeyboard().key_pressed('ESC')):
                SceneManager.changeScene('main_menu')

            # Desenhando background
            try:
                self.background.draw()
            except Exception:
                Game.window.set_background_color((0, 0, 0))

            SceneManager.getCurrentScene().play()

            if(Game.DEVELOPMENT_MODE):
                Game.debug['scene'].play()

            # Atualizando a janela
            Game.window.update()


if True:
    print("swapna")

if False:
    print("swapna")
a = 100
b = 120
if a > b:
    print("a is big")
if a < b:
    print("b is big")
a = 110
b = 149
if a > b:
    print("a is big")
else:
    print("b is big")
a = 110
b = 149
c = 400
if a > b and a > c:
    print("a is big")
elif b > a and b > c:
    print("b is big")
elif c > a and c > b:
    print("c is big")

if a > b:
    if a > c:
        print("a is big")
    else:
        print("c is big")
else:
    if b > c:
        print("b is big")
    else:
        print("c is big")
# loooooops
# i=1
# while i<=10:
# print(i)
# quetion::::
names = ["mobile", "banana", "cherry"]
for x in names:
    print(x)

for x in "banana", "apple", "cherry":
    print(x)
names = ["mobile", "banana", "cherry"]
for x in names:
    print(x)
    if x == "banana":
        break

names = ["mobile", "banana", "cherry"]
for x in names:
    if x == "banana":
        break
    print(x)
a= "juice"
b= "wine"
g=a
a=b
b=g
print(a)
print(b)
list=[1,2,3,4,5,6]


"""Unit tests for User profile API."""
from backend import app, logger
import unittest
import json
from .repository import Repository

class ProfileApiTestCase(unittest.TestCase):
    """Tests for User Profile Api."""

    def setUp(self):
        """Setup API settings for tests."""
        app.config['TESTING'] = True
        self.app = app.test_client()
        repository = Repository({
            'ELASTICSEARCH_URL': 'http://localhost:9200',
            'ELASTICSEARCH_USER': ''
        })
        repository.insert(build_mock_people("mlacerda"))

    def tearDown(self):
        """Destroy settings created for tests."""
        pass

    def test_index_response(self):
        """TEST PROFILE API: get home."""
        rv = self.app.get('/')
        assert 200 == rv.status_code

    def test_api_get_version(self):
        """TEST PROFILE API: get version."""
        rv = self.app.get(
            '/api/public/version',
            content_type='application/json')
        self.assertTrue('0.0.BUILD_NUMBER' in rv.data, 'Invalid version')

    # def test_api_get_profile(self):
    #     """TEST PROFILE API: get user profile."""
    #     id = 'mlacerda'
    #     rv = self.app.get(
    #         '/api/profiles/'+id,
    #         content_type='application/json')
    #     # print rv.data
    #     user = json.loads(rv.data)
    #     logger.info('response: %s' % user)
    #     self.assertEquals(user['login'], id)
    #     self.assertEquals(user['name'], 'MARCUS VINICIUS DE OLIVEIRA LACERDA')
    #     self.assertEquals(user['role']['name'], 'Developer')

def build_mock_people(login):
    doc = {
        "login": login,
        "name": "MARCUS VINICIUS DE OLIVEIRA LACERDA",
        "area": {
          "code": 20,
          "name": "BU South"
        },
        "company": {
          "code": 1,
          "name": "CIT Software S.A."
        },
        "cityBase": {
          "acronym": "BH",
          "code": 6,
          "name": "Belo Horizonte"
        },
        "project": {
          "code": 9347,
          "name": "Coca Cola"
        },
        "role": {
          "code": 96,
          "name": "Developer"
        }
    }

    return doc


import numpy as np
import rpy2
import rpy2.robjects.numpy2ri as numpy2ri
import rpy2.robjects as ro
from rpy2.rinterface_lib.embedded import RRuntimeError

def run_bcp(
	y,
	nsample=10000,
	chains=5,
	**kwargs
):
	ro.conversion.py2ri = numpy2ri
	numpy2ri.activate()
	from rpy2.robjects.packages import importr
	bcp = importr('bcp')
	#R_null = ro.rinterface.NULL
	# Assemble inputs to bcp
	kwargs['return.mcmc'] = True
	kwargs['mcmc'] = nsample
	kwargs['burnin'] = kwargs.get('burnin', int(0.1*nsample))
	burnin = kwargs.get('burnin')
	# Run chains
	for _ in range(chains):
		bcp_obj = bcp.bcp(y=y, **kwargs)
		# Extract output
		inclusions = bcp_obj.rx2('mcmc.rhos').T
		# Process for consistency with BLiP / susie (no burnin)
		inclusions = inclusions[burnin:]
		inclusions = np.concatenate(
			[np.zeros((nsample, 1)),
			 inclusions[:, 0:-1]],
			 axis=1
		)

	return inclusions


'''
Created on Sep 13, 2014

@author: DemiCow
'''

import pygame
import scene
import label
import maphandler
import squid
import prepare

class Cutscene0(scene.Scene):

    def __init__(self):
        scene.Scene.__init__(self)

        self.next = "GAMEPLAY"
        self.init_variables()
        self.init_objects()

    def init_variables(self):
        """ init all variables needed in scene"""
        self.speed = self.MASTER_SPEED
        self.scroll_to_left = True

        self.title_time = self.time
        self.wave_number = 0
        #self.collisiontimer = 0
        #self.shield_counter = 0
        self.shield_time = -100
        self.collision_time = -100
        self.click_counter = 5
        self.text_delay = 100
        self.secret_line = True

    def init_objects(self):
        """ creates objects needed in specfic scenes"""

        self.squid = squid.Squid(self)
        self.squid.rect.y += 325

        self.foreground_map = maphandler.ScrollingMap(self, 'splash')
        self.maps = [self.foreground_map]
        #pygame.mixer.init()
        self.ocean_noises =  pygame.mixer.Sound(prepare.SOUNDS['31762__slanesh__ocean'])
        self.seagull_noises =  pygame.mixer.Sound(prepare.SOUNDS['144836__eelke__sea-seagulls-crows-windfilter'])
        self.click_sound = pygame.mixer.Sound(prepare.SOUNDS['157539__nenadsimic__click'])

        self.tutorial = label.Label(self, size = (800,150), location = (800,150), font_size = 20)
        if self.android:
            self.tutorialtext = [['Welcome to "it has a squid in it".', 'touch to skip'],
                                 ['this squid is going', 'on a journey', 'your task is to guide him'],
                                  ['what will you find', 'at the end?'],
                                  ['tilt to move around', 'eat shrimp for a power-up'],
                                  ['hit those who would stand in your way', 'with ink bullets'],
                                  ['target groups for', 'a combo bonus.'],
                                  ['when you run out of lives, your',' journey is over. good luck!'],
                                  ['"Fate is a sea without shore, and the soul is a rock that abides;',
                                   '"But her ears are vexed with the roar and her face with the foam of the tides."',
                                   "- Swinburne"]
                                  ]

        else:
            self.tutorialtext = [
                                  ['"Fate is a sea without shore,' ],
                                   ['and the soul is a rock that abides;'],
                                   ['But her ears are vexed with the roar '],
                                   ['and her face with the foam of the tides." - Swinburne'],
                                  ]

        self.linecounter = 0

        self.tutorial.textlines = self.tutorialtext[0]
        self.tutorial.clickable = False
        self.tutorial.textcolor = (pygame.color.Color("black"))
        self.tutorial.backgroundcolor = self.tutorial.transparent_color

        self.sprites = [self.foreground_map, self.squid,
                        self.tutorial]

    def startup(self, time, persistant):
        if prepare.MUSIC_ON == True:
            self.ocean_noises.play(loops=-1)
            self.seagull_noises.play(loops=-1)
        self.start_time = time
        return scene.Scene.startup(self, time, persistant)

    def cleanup(self):
        return scene.Scene.cleanup(self)

    def update_specifics(self, time, time_delta):
        self.click_counter -= 1
        self.squid.dx = self.squid.dy = 0
        self.squid.currentimage = self.squid.imgsquidge

        if self.click_counter%2 == 0:
            self.squid.rect.x += 2
            self.squid.rect.y += 1
        else: pass
        if self.clicked ==True:
            if prepare.MUSIC_ON == True:
                self.click_sound.play()
                self.start_time= self.time

        if self.clicked ==True and self.time - self.start_time > 100000*time_delta:#self.click_counter < -self.text_delay:
            self.click_counter = 100
            self.start_time = self.time
            self.linecounter += 1
            if self.linecounter == len(self.tutorialtext):
                if prepare.MUSIC_ON == True:
                    self.ocean_noises.fadeout(1000)
                    self.seagull_noises.fadeout(1000)
                self.done = True
            else:
                self.tutorial.image.fill(self.tutorial.fillcolor)
                self.tutorial.textlines = self.tutorialtext[self.linecounter]

class Cutscene1(scene.Scene):
    def __init__(self):
        scene.Scene.__init__(self)
        self.next = "TITLE"
        self.init_variables()
        self.init_objects()

    def init_objects(self):
        """ creates objects needed in specfic scenes"""
        self.collision_time = -100
        self.shield_time = -100
        self.speed = self.MASTER_SPEED
        self.label = label.Label(self, font_size = 50)
        self.label.textlines = ["THANK YOU FOR PLAYING", "IT HAS A SQUID IN IT"]
        self.menu = label.Label(self, font_size = 50)
        if self.android:
            self.menu.textlines = ["touch to go back to title"]
        else: self.menu.textlines = ["click to go back to title"]
        self.menu.clickable = True
        self.menu.rect.y += 300
        self.background = pygame.Surface(self.screen.get_size())
        self.backgroundrect = self.background.get_rect()
        self.background.fill(pygame.color.Color("black"))
        self.screen.blit(self.background, (0, 0))
        self.sprites = [self.label, self.menu]
        self.click_sound = pygame.mixer.Sound(prepare.SOUNDS['157539__nenadsimic__click'])

    def mouse_controls(self, time_delta):
        pass

    def startup(self, time, persistant):
        self.start_time = time
        return scene.Scene.startup(self, time, persistant)

    def cleanup(self):
        return scene.Scene.cleanup(self)

    def fill_background(self):
        self.screen.blit(self.background, (0, 0))

    def update_specifics(self, time,time_delta):
        """update title screen"""
        if self.clicked ==True:
            if self.menu.option_highlighted == 0:
                if prepare.MUSIC_ON == True:
                    self.click_sound.play()

                self.done = True

def main():
    pygame.init()
    pygame.mixer.init()
    import gameplay
    import title
    run_it = scene.Control()
    state_dict = {"TITLE" : title.Title(),
                  "INTRO" : Cutscene0(),
                  "GAMEPLAY" : gameplay.gamePlay(),
                  "ENDING": Cutscene1()
                   }
    run_it.setup_states(state_dict, "INTRO")
    run_it.main()

if __name__ == "__main__":
    main()


#%% Imports
import matplotlib
import matplotlib.pyplot as plt
import numpy as np
import scipy.constants as constants
from uncertainties import *
import os
from scipy.optimize import curve_fit
# pregunta 5
#%% Funciones
def ec_de_la_clase(tau):    return (0.7104-0.1331*np.exp(-3.449*tau))

def T_T_eff(log_tau):   return [(((3/4)*(10**i+ec_de_la_clase(10**i)))**(1/4))
                                for i in log_tau]

def q(tau, T):  return [((4./3.)*((T[i]/T_eff_sol)**4)-(tau[i]))
                        for i in range(len(tau))]

def ec_de_la_clase2(tau):   return [(0.7104-0.1331*np.exp(-3.449*i))
                                    for i in tau]

# Constantes
sigma = 5.67*10**-5                 #Constate de Stefan-Boltzman en cgs
C = constants.speed_of_light * 100  #velocidad de la luz en cm/s
T_eff_sol = 5778 #kelvin

#%% Datos
archivo = open('data.txt','r')
data = archivo.readlines()
tau_0, T, log_p_g, log_p_e, log_k_p, x = ([] for i in range(6))
for i in range(len(data)):
    data[i] = data[i].split(' ')
    data[i][len(data[i])-1] = data[i][len(data[i])-1].strip('\n')
    tau_0.append(float(data[i][0]))
    T.append(float(data[i][1]))
    log_p_g.append(float(data[i][2]))
    log_p_e.append(float(data[i][3]))
    log_k_p.append(float(data[i][4]))
    x.append(float(data[i][5])*(10**5)) #En [cm]
tau = [10**i for i in tau_0] #quitandole el log
P_rad, k_0 = [], []
for i in range(len(log_p_e)):
    p_e = 10**log_p_e[i]
    k0_pe = 10**log_k_p[i]
    k_0.append(k0_pe * p_e)
    P_rad.append(np.log10(4*sigma*(T[i]**4)/(3*C)))
#%% Parte a
# Plots
fig, ((ax1, ax2), (ax3, ax4), (ax5, ax6), (ax7, ax8), (ax9, ax10)) = plt.subplots(5, 2, sharex='col',
                                                                                sharey='row',figsize=(10, 8))
opciones = {'markersize':4, 'marker':'+', 'markeredgecolor':'black'}
ax1.plot(tau_0, T, 'r--', **opciones)
ax2.plot(x, T, 'r--', **opciones)
ax3.plot(tau_0, P_rad, 'y--', **opciones)
ax4.plot(x, P_rad, 'y--', **opciones)
ax5.plot(tau_0, log_p_g, 'b--', **opciones)
ax6.plot(x, log_p_g, 'b--', **opciones)
ax7.plot(tau_0, log_p_e, 'g--', **opciones)
ax8.plot(x, log_p_e, 'g--', **opciones)
ax9.plot(tau_0, k_0, 'm--', **opciones)
ax10.plot(x, k_0, 'm--', **opciones)
# Ajustes de los ejes
#ticks
ax1.set_yticks(np.arange(3000, 10000, 1000))
ax3.set_yticks(np.arange(0, 1.5, .2))
ax5.set_yticks(np.arange(3, 8, .5))
ax7.set_yticks(np.arange(-2, 5, 1))
ax9.set_yticks(np.arange(0, 15, 2))
#labels
opciones_de_labels = {'fontsize':14, 'rotation':0,
                    'horizontalalignment':'right',
                    'verticalalignment':'center'}
ax1.set_ylabel(r'$T^{\circ}$', **opciones_de_labels)
ax3.set_ylabel(r'$log(P_{rad})$', **opciones_de_labels)
ax5.set_ylabel(r'$log(P_{g})$', **opciones_de_labels)
ax7.set_ylabel(r'$log(P_{e})$', **opciones_de_labels)
ax9.set_ylabel(r'$\kappa_{0}$', **opciones_de_labels)
ax9.set_xlabel(r'$log(\tau_{0})$', fontsize=16)
ax10.set_xlabel('Altura geometrica [cm]', fontsize=16)
#guardado
plt.savefig('grafico_p5_01.pdf')
plt.show()
#%% Parte 2
xp = np.linspace(-4,1,100)
plt.plot(tau_0, [(i/T_eff_sol) for i in T],'r--', markersize=6, marker='+',
        markeredgecolor='black', label='Datos de la Tabla')
plt.plot(xp, T_T_eff(xp),'b--', label='Ecuacion mostrada en clases')
plt.xlabel(r'$log(\tau)$', fontsize=16)
plt.ylabel(r'$T\ /\ T_{eff}$', fontsize=16)
plt.legend()
plt.savefig('grafico_p5_02.pdf')
plt.show()
#%% otro grafico de la parte b
xp = np.linspace(-1,15,100)
plt.plot(xp, ec_de_la_clase2(xp),'b--', label='Ec. de hopf')
plt.plot(tau, q(tau,T), 'r--', marker = '+',
        markeredgecolor='black', markersize=7, label='Datos a partir de la tabla')
plt.ylabel(r'$q(\tau)$', fontsize=16)
plt.xlabel(r'$\tau$', fontsize=16)
plt.legend(loc=2)
plt.xlim(-.5,2.6)
plt.ylim(0.3,1.4)
plt.subplots_adjust(left=0.12, right=0.95, top=0.9, bottom=0.2)
plt.savefig('grafico_p5_04.pdf')
plt.show()
#%% Parte c
for i in range(len(tau_0)):
    if tau[i]+q(tau,T)[i]>=4/3:
            Teff_igual_T = i
            print('tau + q(tau): {:.3f}'.format(tau[i]+q(tau,T)[i]))
            print('indice de la lista: {}'.format(i))
            print('tau correspondiente: {:.3f}'.format(tau[i]))
            print('log(tau) correspondiente: {:.3f}'.format(tau_0[i]))
            break


# this file is to test the scrollbar

from tkinter import *
global C,s
root = Tk()
root.title("scrollbar_test")
root.geometry('300x300+100+100')

def shownumber(x):

    C.delete(ALL)
    C.create_text(100,70,text = str(x),font=(font,15))

s = Scrollbar(root, orient = HORIZONTAL, jump = 0)
C = Canvas(root,bg='gray',xscrollcommand=s.set)

C.place(x=50,y=50,width=200,height=150)
s.place(x=50,y=200,width=200)

w = Scale(root, from_=0, to=1, orient = HORIZONTAL,showvalue = 0)
w["command"] = shownumber
w.place(x=50,y=250,width=200)


import random
import sys

import matplotlib.pyplot as plt
import numpy as np
from sklearn.model_selection import train_test_split

########################################################
# 全局变量
########################################################
# 显示中文标签
plt.rcParams['font.sans-serif'] = ['SimHei']
# 显示负号
plt.rcParams['axes.unicode_minus'] = False

# 簇个数范围
k_min = 1
k_max = 1
max_iteration = 10  # 最大迭代次数
test_rate = 0.2  # 训练集所占比例
all_point_num = 0
data_x = []  # 点的x坐标列表
data_y = []  # 点的y坐标列表
data_SSE_list = []  # 数据集损失目标函数列表
train_SSE_list = []  # 训练集损失目标函数列表
test_SSE_list = []  # 测试集损失目标函数列表

########################################################
# 读入数据
########################################################
def read_data(path):
    global all_point_num
    global test_rate
    global max_iteration
    global k_min
    global k_max
    data = open(path)  # 打开数据文件
    for line in data:
        # 将数据文件的每一行按空格分隔，分别添加到x,y列表
        data_x.append(float(line.split()[0]))
        data_y.append(float(line.split()[1]))
        all_point_num += 1
    print("请输入k的范围（空格分隔，例如2 5，代表闭区间[2,5]）：", end="")
    k_min, k_max = map(int, input().split())
    if k_min > k_max or k_min < 1 or k_max < 1:
        print("ERROR: 簇的个数输入错误！")
        sys.exit(1)
    print("请输入测试集所占比例：", end="")
    test_rate = float(input())
    if test_rate < 0 or test_rate > 1:
        print("ERROR: 测试集所占比例输入错误！")
        sys.exit(1)
    print("请输入最大迭代次数：", end="")
    max_iteration = int(input())
    if max_iteration < 0:
        print("ERROR: 最大迭代次数输入错误！")
        sys.exit(1)

########################################################
# 算法
########################################################
def algorithm(x, y, k):
    # 初始化
    n = 0  # 迭代次数
    SSE = 0  # 损失（目标）函数
    point_array = np.array([x, y])  # 生成点的numpy数组，例如三个点(1，-1),(2，-2),(3，-3)生成[[ 1  2  3 ][ -1 -2 -3 ]]
    point_num = point_array.shape[1]  # 获取点的个数，所给的数据集为1000个点，取0.8为800个
    # 如果点的个数<簇的个数，则报错异常退出
    if point_num < k:
        print("ERROR: 点的个数<簇的个数！")
        sys.exit(1)
    distance = np.zeros(k)  # 初始化k维0向量，用来存储这个点到的k个簇中心的距离
    point_distance = np.zeros(point_num)  # 初始化point_num维0向量，用来存储每个点到其簇中心的距离
    center = np.zeros((2, k))  # 生成2*k的零矩阵，初始化k个簇的中心点，后面将先计算每个簇的重心作为初始簇中心
    point_k1 = np.arange(0, k, 1)  # 先生成0~k-1的数组，保证每个簇至少有一个点
    point_k2 = np.random.randint(0, k, size=point_num - k)  # 将剩下的点随机分到任意一个簇，返回长度为point_num-k的向量，每个为0~k-1的随机整数
    point_k = np.concatenate((point_k1, point_k2))  # 将前两个数组合成为一个
    np.random.shuffle(point_k)  # 最后再打乱一次point_k数组，保证随机性
    point_k_bak = point_k.copy()  # point_k的备份，用于判断前后两次迭代是否发生变化
    # 输出相关信息
    print("点个数为：", point_num, sep="")
    # 开始K-means算法的迭代
    # 只要迭代次数没有超过最大迭代次数上限max_iteration并且两次迭代的结果还有变化，就继续迭代
    while n <= max_iteration:
        # 计算每个簇的中心
        size = np.bincount(point_k, minlength=k)  # 计算每个簇的规模，k维向量，每一维为每个簇里点的个数
        for i in range(k):  # 对于k个簇中的每一个簇i
            # 不是空簇，防止出现除数为0，空簇下面单独处理
            if size[i] != 0:
                # point_sum是一个二维向量，代表i簇中的所有点的x,y坐标值的和
                point_sum = np.zeros(2)  # 初始化为0
                # 计算i簇中的所有点的x,y坐标值的和
                for point in range(point_num):  # 对于每一个点point
                    if point_k[point] == i:  # 如果这个点point属于簇i
                        point_sum = point_sum + point_array[:, point]  # 将这个点的x,y坐标加到point_sum上
                        # note: array[:, i]就是取array的第i列，刚好就是这个点
                # 求平均数，因此要/这个簇中点的个数size[i]
                # 将计算出的簇中心存在center矩阵中的第i列，代表第i个簇的中心点
                center[:, i] = point_sum / size[i]
        # 对所有的空簇单独处理
        # 处理方式为把与自己所在的簇中心距离最大的点作为空簇的簇中心
        for c in np.where(size == 0)[0]:
            # 将距离最大的点的所在簇改为空簇的簇号
            point_k[np.argmax(point_distance)] = c
            # 将空簇中心改为这个点
            center[:, c] = point_array[:, np.argmax(point_distance)]

        # 将每一个点分到与簇中心最近的簇
        for point in range(point_num):  # 对于每一个点point
            for i in range(k):  # 对于k个簇中的每一个簇i
                # 计算该点point到该簇i的距离
                # note: 使用矩阵的2范式，来计算欧氏距离
                #  point_array[:, point] - center[:, i]得到向量[p_x-c_x, p_y-c_y]
                #  对该向量求2范式刚好是欧式距离sqrt((p_x-c_x)^2+(p_y-c_y)^2)
                distance[i] = np.linalg.norm(point_array[:, point] - center[:, i])
            # 使用numpy的argmin()返回distance中最小值的下标，也就是点point到簇中心距离最小的簇
            point_k[point] = np.argmin(distance)
            # 将该点到其簇中心的距离记录下来
            point_distance[point] = distance[point_k[point]]

        # 迭代次数+1
        n += 1
        # 判断两次迭代是否有变化，没有变化就跳出迭代循环
        if (point_k == point_k_bak).all():
            break
        # 将point_k备份
        point_k_bak = point_k.copy()
        # 输出每次迭代目标函数值
        SSE = np.sum(point_distance)
        print("n=", n - 1, "，SSE=", SSE, sep="")

    # 输出相关信息
    print("总迭代次数为：", n - 1, sep="")
    print("每个簇的中心：", sep="")
    for i in range(k):
        print("(", center[0, i], ",", center[1, i], ")", sep="")
    print("===================================================")

    return SSE, point_k

########################################################
# 画图展示
########################################################
# 根据每个点所在的簇，获取点的颜色，返回一个列表
def get_color_list(point_k, k):
    # 随机生成k个颜色
    RGB = ['1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'B', 'C', 'D', 'E', 'F']
    colors = []
    i = 0
    while i < k:
        color = ""
        for j in range(6):
            color += RGB[random.randint(0, 14)]
        # 只有随机产生的颜色不相同，才加到colors中，颜色个数+1
        if "#" + color not in colors and color != "FFFFFF":
            colors.append("#" + color)
            i += 1
    # 对每个点，根据point_k中记录的点所在的簇，将颜色添加到列表中
    color_list = []
    for c in point_k:
        color_list.append(colors[c])
    return color_list

# 画散点图展示聚类结果（数据集，训练集、测试集）
def show(x, y, result, x1, y1, result1, x2, y2, result2, k):
    # 数据集图
    plt.subplot(131)
    # 设置标题
    plt.title("K-Means数据集")
    # 绘制散点图
    # note: scatter函数支持传入颜色列表，对每一个点使用列表相应下标对应的颜色
    plt.scatter(x, y, c=get_color_list(result, k))

    # 训练集图
    plt.subplot(132)
    # 设置标题
    plt.title("K-Means训练集")
    # 绘制散点图
    plt.scatter(x1, y1, c=get_color_list(result1, k))

    # 测试集图
    plt.subplot(133)
    # 设置标题
    plt.title("K-Means测试集")
    # 绘制散点图
    plt.scatter(x2, y2, c=get_color_list(result2, k))
    # 展示散点图

    plt.show()

# 画折线图展示SSE对比
def show_SSE():
    # 设置标题
    plt.title("K-Means在不同k下的损失")
    # 设置x轴坐标
    plt.xlim(k_min - 1, k_max + 2)
    plt.xlabel("k")  # 设置x轴标注
    plt.ylabel("SSE")  # 设置y轴标注
    # 绘制K-Means在不同k下训练集和测试集的损失
    plt.plot(range(k_min, k_max + 1), data_SSE_list, lw=1, c='blue', marker='v', ms=4, label='数据集')
    plt.plot(range(k_min, k_max + 1), train_SSE_list, lw=1, c='red', marker='s', ms=4, label='训练集')
    plt.plot(range(k_min, k_max + 1), test_SSE_list, lw=1, c='green', marker='o', label='测试集')
    plt.legend()  # 图例
    plt.show()

########################################################
# 开始运行
########################################################
# 读入数据
read_data("cluster.dat")
# 划分训练集和测试集
train_x, test_x, train_y, test_y = train_test_split(data_x, data_y, test_size=test_rate, random_state=1)
# 对k从2~5进行聚类
for k_num in range(k_min, k_max + 1):
    print("###################################################")
    print("簇个数为：", k_num, sep="")
    # 数据集
    print("数据集：")
    data_SSE, data_result = algorithm(data_x, data_y, k_num)
    data_SSE_list.append(data_SSE)
    # 训练集
    print("训练集：")
    train_SSE, train_result = algorithm(train_x, train_y, k_num)
    train_SSE_list.append(train_SSE)
    # 测试集
    print("测试集：")
    test_SSE, test_result = algorithm(test_y, test_y, k_num)
    test_SSE_list.append(test_SSE)
    # 输出平均SSE
    print("数据集最终平均损失函数AVG_SSE：", data_SSE / all_point_num)
    print("训练集最终平均损失函数AVG_SSE：", train_SSE / (all_point_num * (1 - test_rate)))
    print("测试集最终平均损失函数AVG_SSE：", test_SSE / (all_point_num * test_rate))
    # 画图
    show(data_x, data_y, data_result, train_x, train_y, train_result, test_x, test_y, test_result, k_num)
# 画图展示SSE
show_SSE()


import sys
sys.stdin = open('bj1764.txt','r')

N,M=map(int,input().split())
L=[input()for _ in range(N+M)]
L.sort()
Set=set(L)
print(N+M-len(Set))
for i in range(N+M-1):
    if L[i]==L[i+1]:print(L[i])


import numpy as np

from tensorflow.core.protobuf.tpu import topology_pb2
from tensorflow.python.util.tf_export import tf_export

def _tpu_device_name(job, task, device):
  """Returns the device name for the TPU `device` on `task` of `job`."""
  if job is None:
    return "/task:%d/device:TPU:%d" % (task, device)
  else:
    return "/job:%s/task:%d/device:TPU:%d" % (job, task, device)

def _tpu_host_device_name(job, task):
  """Returns the device name for the CPU device on `task` of `job`."""
  if job is None:
    return "/task:%d/device:CPU:0" % task
  else:
    return "/job:%s/task:%d/device:CPU:0" % (job, task)

@tf_export("tpu.experimental.Topology")
class Topology(object):
  """Describes a set of TPU devices.

  Represents both the shape of the physical mesh, and the mapping between
  TensorFlow TPU devices to physical mesh coordinates.
  """

  def __init__(self, serialized=None, mesh_shape=None, device_coordinates=None):
    """Builds a Topology object.

    If `serialized` is not `None`, the topology is parsed from `serialized` and
    the other arguments are ignored. Otherwise, the topology is computed from
    `mesh_shape` and `device_coordinates`.

    Args:
      serialized: A serialized `TopologyProto`, or `None`. If not `None`, the
        serialized proto is parsed to discover the topology.
      mesh_shape: A sequence of 4 positive integers, or `None`. If not `None`,
        the shape of the TPU topology, in number of cores. Ignored if
        `serialized` is not `None`.
      device_coordinates: A rank 3 numpy array that describes the mapping from
        TensorFlow TPU devices to TPU fabric coordinates, or `None`. If
        specified, array is a rank 3 int32 array with shape
        `[tasks, devices, axis]`.  `tasks` is the number of tasks in the TPU
        cluster, `devices` is the number of TPU devices per task, and `axis` is
        the number of axes in the TPU cluster topology. Each entry gives the
        `axis`-th coordinate in the topology of a task/device pair. TPU
        topologies are 4-dimensional, with dimensions `(x, y, z, core number)`.
        This arg is ignored if `serialized is not `None`.

    Raises:
      ValueError: If `serialized` does not describe a well-formed topology.
      ValueError: If `serialized` is `None` and `mesh_shape` is not a sequence
        of 4 positive integers.
      ValueError: If `serialized` is `None` and `device_coordinates` is not a
        rank 3 numpy int32 array that describes a valid coordinate mapping.
    """

    self._serialized = serialized

    if serialized:
      self._parse_topology(serialized)
    else:
      self._mesh_shape = np.asarray(mesh_shape, dtype=np.int32)
      self._device_coordinates = np.asarray(device_coordinates, np.int32)
      if len(self._mesh_shape) != 4 or any(self._mesh_shape < 1):
        raise ValueError("`mesh_shape` must be a sequence of 4 positive "
                         f"entries; got `mesh_shape={self._mesh_shape}`")

      if (len(self._device_coordinates.shape) != 3 or
          self._device_coordinates.shape[2] != len(self._mesh_shape)):
        raise ValueError(
            "`device_coordinates` must be a rank 3 int32 array "
            "with minor dimension equal to the `mesh_shape` rank"
            "got device_coordinates.shape={} len(device_coordinates.shape)={} device_coordinates.shape[2]={} mesh_shape={}, len(mesh_shape)={}"
            .format(self._device_coordinates.shape,
                    len(self._device_coordinates.shape),
                    self._device_coordinates.shape[2], self._mesh_shape,
                    len(self._mesh_shape)))

    self._topology_tasks, self._topology_devices = self._invert_topology()

    # Coordinates of devices that are missing
    self._missing_devices = np.argwhere(self._topology_tasks < 0)

  def _parse_topology(self, serialized):
    """Parses a serialized `TopologyProto` into `self`."""
    proto = topology_pb2.TopologyProto()
    proto.ParseFromString(serialized)

    self._mesh_shape = np.array(proto.mesh_shape, dtype=np.int32)
    if len(self._mesh_shape) != 4 or any(self._mesh_shape < 1):
      raise ValueError("`mesh_shape` must be a vector of size 4 with positive "
                       "entries; got {}".format(self._mesh_shape))

    if proto.num_tasks < 0:
      raise ValueError("`num_tasks` must be >= 0; got {}".format(
          proto.num_tasks))
    if proto.num_tpu_devices_per_task < 0:
      raise ValueError("`num_tpu_devices_per_task` must be >= 0; got {}".format(
          proto.num_tpu_devices_per_task))

    expected_coordinates_size = (
        proto.num_tasks * proto.num_tpu_devices_per_task * len(
            proto.mesh_shape))
    if len(proto.device_coordinates) != expected_coordinates_size:
      raise ValueError("`device_coordinates` must have shape num_tasks ({}) * "
                       "num_tpu_devices_per_task ({}) * len(mesh_shape) ({}); "
                       "got shape {}".format(proto.num_tasks,
                                             proto.num_tpu_devices_per_task,
                                             proto.mesh_shape,
                                             len(proto.device_coordinates)))

    coords = np.array(proto.device_coordinates, dtype=np.int32)
    if any(coords < 0):
      raise ValueError(
          "All values in `device_coordinates` must be >= 0, got {}"
          .format(coords))
    coords = coords.reshape((proto.num_tasks, proto.num_tpu_devices_per_task,
                             len(proto.mesh_shape)))
    self._device_coordinates = coords

  def _invert_topology(self):
    """Inverts a [task,device,axis] topology to [x,y,z] -> task/device maps."""
    tasks = np.full(list(self.mesh_shape), -1, dtype=np.int32)
    devices = np.full(list(self.mesh_shape), -1, dtype=np.int32)
    for task in range(self.device_coordinates.shape[0]):
      for device in range(self.device_coordinates.shape[1]):
        x, y, z, core = self.device_coordinates[task, device, :]
        tasks[x, y, z, core] = task
        devices[x, y, z, core] = device
    return tasks, devices

  @property
  def mesh_shape(self):
    """A rank 1 int32 array describing the shape of the TPU topology."""
    return self._mesh_shape

  @property
  def mesh_rank(self):
    """Returns the number of dimensions in the mesh."""
    return len(self._mesh_shape)

  @property
  def device_coordinates(self):
    """Describes the mapping from TPU devices to topology coordinates.

    Returns:
      A rank 3 int32 array with shape `[tasks, devices, axis]`.
      `tasks` is the number of tasks in the TPU cluster, `devices` is the number
      of TPU devices per task, and `axis` is the number of axes in the TPU
      cluster topology. Each entry gives the `axis`-th coordinate in the
      topology of a task/device pair. TPU topologies are 4-dimensional, with
      dimensions `(x, y, z, core number)`.
    """
    return self._device_coordinates

  @property
  def missing_devices(self):
    """Array of indices of missing devices."""
    return self._missing_devices

  def task_ordinal_at_coordinates(self, device_coordinates):
    """Returns the TensorFlow task number attached to `device_coordinates`.

    Args:
      device_coordinates: An integer sequence describing a device's physical
        coordinates in the TPU fabric.

    Returns:
      Returns the TensorFlow task number that contains the TPU device with those
      physical coordinates.
    """
    return self._topology_tasks[tuple(device_coordinates)]

  def tpu_device_ordinal_at_coordinates(self, device_coordinates):
    """Returns the TensorFlow device number at `device_coordinates`.

    Args:
      device_coordinates: An integer sequence describing a device's physical
        coordinates in the TPU fabric.

    Returns:
      Returns the TensorFlow device number within the task corresponding to
      attached to the device with those physical coordinates.
    """
    return self._topology_devices[tuple(device_coordinates)]

  def cpu_device_name_at_coordinates(self, device_coordinates, job=None):
    """Returns the CPU device attached to a logical core."""
    return _tpu_host_device_name(
        job, self._topology_tasks[tuple(device_coordinates)])

  def tpu_device_name_at_coordinates(self, device_coordinates, job=None):
    """Returns the name of the TPU device assigned to a logical core."""
    return _tpu_device_name(job,
                            self._topology_tasks[tuple(device_coordinates)],
                            self._topology_devices[tuple(device_coordinates)])

  @property
  def num_tasks(self):
    """Returns the number of TensorFlow tasks in the TPU slice."""
    return self._device_coordinates.shape[0]

  @property
  def num_tpus_per_task(self):
    """Returns the number of TPU devices per task in the TPU slice."""
    return self._device_coordinates.shape[1]

  def serialized(self):
    """Returns the serialized form of the topology."""
    if self._serialized is None:
      proto = topology_pb2.TopologyProto()
      proto.mesh_shape[:] = list(self._mesh_shape)
      proto.num_tasks = self._device_coordinates.shape[0]
      proto.num_tpu_devices_per_task = self._device_coordinates.shape[1]
      proto.device_coordinates.extend(list(self._device_coordinates.flatten()))
      self._serialized = proto.SerializeToString()

    return self._serialized


# -*- coding: utf-8 -*-
import torch
import argparse
import gym
import os, glob
import numpy as np
from collections import deque
from utils.agent import Agent, Action_Scheduler, DQN
from helper.functions import preprocess, frame_to_tensor, clip_reward, generate_gif
import pickle

from copy import deepcopy

def train(n_actions, agent, env, env_name, path, rewards=None,frame_number=0, max_frames=30000000,
          max_episode_length=18000,start_learning=50000, evaluate_every=200000, eval_length=10000,
          network_update_every=10000, update_every=4):
    last_lives = 0
    terminal_life_lost = False
    if rewards == None:
        rewards = []

    losses = []

    states_for_action = deque(maxlen=4)

    try:
        action_scheduler = Action_Scheduler(num_actions=n_actions, max_frames=max_frames,
                                            replay_memory_start_size=start_learning-frame_number, model=agent.main_dqn)

        while frame_number < max_frames:

            #training on Deterministic does not lead to reliable agents on random episodes
            env = gym.make('{0}-v4'.format(env_name))
            agent.main_dqn.train()
            epoch_frame = 0

            while epoch_frame < evaluate_every:
                terminal_life_lost = False
                state = env.reset()
                states_for_action.append(torch.FloatTensor(preprocess(state)/255.))
                episode_reward_sum = 0
                for _ in range(max_episode_length):
                    action = action_scheduler.get_action(frame_number, torch.stack(list(states_for_action)).to(DEVICE))
                    next_state, reward, terminal, info = env.step(action)

                    if info['ale.lives'] < last_lives:
                        terminal_life_lost = True
                    else:
                        terminal_life_lost = terminal
                    last_lives = info['ale.lives']

                    frame_number += 1
                    epoch_frame += 1
                    episode_reward_sum += reward
                    reward = clip_reward(reward)

                    agent.replay_memory.add_experience(action=action,
                                                       frame=preprocess(next_state),
                                                       reward=reward,
                                                       terminal=terminal_life_lost)

                    state = next_state

                    if frame_number % update_every == 0 and frame_number > start_learning:
                        loss = agent.optimize()
                        losses.append(loss.item())

                    if frame_number % network_update_every == 0 and frame_number > start_learning:
                        agent.target_dqn.load_state_dict(agent.main_dqn.state_dict())

                    if terminal:
                        terminal = False
                        break
                rewards.append(episode_reward_sum)

                if len(rewards) % 10 == 0:
                    print(len(rewards), frame_number, np.mean(rewards[-100:]), np.mean(losses[-100:]))
                    with open('{0}/rewards.dat'.format(path), 'a') as reward_file:
                        print(len(rewards), frame_number, np.mean(rewards[-100:]), file=reward_file)

            torch.save(agent.main_dqn.state_dict(), "{0}/checkpoints/{3}_{1}_reward{2}.pth".format(path, frame_number, \
                                                                                np.mean(rewards[-100:]), env_name))
            with open('{}/logs/rewards.log'.format(path),'wb') as f: pickle.dump(rewards, f)
            terminal = True
            gif = True
            frames_for_gif = []
            eval_rewards = []
            evaluation_frame_number = 0

            env = gym.make('{0}-v4'.format(env_name))
            agent.main_dqn.eval()

            for i in range(eval_length):
                if terminal:
                    terminal_life_lost = True
                    state = env.reset()
                    episode_reward_sum = 0
                    terminal = False

                states_for_action.append(torch.FloatTensor(preprocess(state)/255.))
                starting_point = np.random.randint(0, 30)

                action = 1 if terminal_life_lost and i < starting_point \
                         else action_scheduler.get_action(frame_number, torch.stack(list(states_for_action)).to(DEVICE), evaluation=True)

                next_state, reward, terminal, info = env.step(action)

                evaluation_frame_number += 1
                episode_reward_sum += reward
                state = next_state
                if gif:
                    frames_for_gif.append(next_state)
                if terminal:
                    eval_rewards.append(episode_reward_sum)
                    gif = False

            print("Evaluation score:\n", np.mean(eval_rewards))

            try:
                generate_gif(frame_number, frames_for_gif, eval_rewards[0], '{0}/gifs/'.format(path))
                print("Gif saved.")
            except IndexError:
                print("No evaluation game finished")

            print("Model saved")
            frames_for_gif = []

    except KeyboardInterrupt:
        torch.save(agent.main_dqn.state_dict(), "{3}/checkpoints/{2}_{0}_reward{1}.pth".format(frame_number, int(np.mean(rewards[-100:])), env_name, path))
        with open('{}/logs/rewards.log'.format(path),'wb') as f: pickle.dump(rewards, f)
        print("Training stopped")

def test(agent, runs=1, render=False):
    env = gym.make('{0}-v4'.format(args.env))
    state = env.reset()
    done = False
    transition = deque(maxlen=4)
    eps_reward = 0
    total_reward = []

    i = 0
    while i < runs:
        transition.append(frame_to_tensor(preprocess(state)))

        if len(transition) < 4:
            action = env.action_space.sample()
        else:
            action = agent.predict_action(torch.stack(list(transition)).to(DEVICE).unsqueeze(0))
        next_state, reward, done, _ = env.step(action)

        if render:
            env.render()

        eps_reward += reward
        if done:
            env = gym.make('{0}-v4'.format(args.env))
            state = env.reset()
            total_reward.append(eps_reward)
            print("[{}] Total episode reward: {}".format(len(total_reward), eps_reward))
            eps_reward = 0
            i += 1
            done = False
        else:
            state = next_state

    print("Average reward over {} runs: {}+-{}".format(runs, np.average(total_reward), np.std(total_reward)))

def get_args():
    parser = argparse.ArgumentParser()
    subparsers = parser.add_subparsers(help='commands')
    train_parser = subparsers.add_parser('train', help='Train an Atari agent or load a checkpoint.')
    train_parser.add_argument('--env', default='Pong', type=str, help='Name of the Atari game')
    train_parser.add_argument('--load', default=False, type=bool, help='Load a checkpoint and train from there')
    train_parser.add_argument('--lr', default=0.0000625, type=float, help='Set the learning rate')
    train_parser.add_argument('--start_learning', default=50000, type=float, help='Set the amount of frames after which the agent starts to learn.')
    train_parser.add_argument('--gamma', default=0.99, type=float, help='Set the rewards discount factor')
    train_parser.add_argument('--hidden', default=128, type=int, help='Set the hidden size of the agent')
    train_parser.add_argument('--update_target', default=10000, type=int, help='Set the amount of frames after which the target network is updated.')
    train_parser.add_argument('--batch_size', default=32, type=int, help='Set the batch size')
    train_parser.add_argument('--memory_size', default=1000000, type=int, help='Set the memory size')
    train_parser.add_argument('--transition_size', default=4, type=int, help='Set the amount of stacked frames that form a transition')
    train_parser.add_argument('--eps_initial', default=1.0, type=float, help='Set the initial epsilon for the action scheduler')
    train_parser.add_argument('--eps_final', default=0.1, type=float, help='Set the first final epsilon for the action scheduler')
    train_parser.add_argument('--eps_final_frame', default=0.01, type=float, help='Set the second epsilon for the action scheduler')
    train_parser.add_argument('--eps_annealing_frames', default=1000000, type=int, help='Set for how many frames epsilon should be reduced to the first final epsilon')
    train_parser.add_argument('--replay_memory_start_size', default=50000, type=int, help='Set the amount of frames for which epsilon stays at the initial value')
    train_parser.add_argument('--max_frames', default=30000000, type=int, help='Set the amount overall training frames')
    train_parser.add_argument('--save_path', default='../tests/', type=str, help='Set the saving directory')

    test_parser = subparsers.add_parser('test', help='Test a preveously trained model')
    test_parser.add_argument('--env', default='Pong', type=str, help='Name of the Atari game')
    test_parser.add_argument('-i', default=None, metavar='.pth file', type=argparse.FileType('r'), help='Path to the trained model')
    test_parser.add_argument('--hidden', default=128, type=int, help='Hyperparameter necessary to load the agent')
    test_parser.add_argument('--render', default=False, type=bool, help='Choose to render the game if there\'s a display available.')
    test_parser.add_argument('--runs', default=1, type=int, help='Set the amount of runs the agent is tested for.')
    return parser.parse_args()

if __name__ == "__main__":

    args = get_args()
    DEVICE = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")

    if 'lr' in args:
        print("Creating environment...")
        env = gym.make('{0}-v4'.format(args.env))
        rewards = []
        print("Initializing the agent...")
        agent = Agent(n_actions=env.action_space.n, device=DEVICE, hidden=args.hidden,
                      learning_rate=args.lr, gamma=args.gamma, batch_size=args.batch_size,
                      agent_history_length=args.transition_size, memory_size=args.memory_size)
        print("Preparing save dirs..")
        path = "{0}{1}".format(args.save_path, args.env)
        os.makedirs(path, exist_ok=True)
        os.makedirs("{0}/checkpoints/".format(path), exist_ok=True)
        os.makedirs("{0}/gifs/".format(path), exist_ok=True)
        os.makedirs("{0}/logs/".format(path), exist_ok=True)
        print("Created directories successfully!")
        if args.load:
            print("Search for model..")
            paths = glob.glob('{0}/checkpoints/*.pth'.format(path))
            if len(paths) > 0:
                latest_model = max(paths, key=os.path.getctime)
                frame_number = latest_model.split('_')[1]
                frame_number = int(frame_number.split('.')[0])
                agent.main_dqn.load_state_dict(torch.load(latest_model))
                agent.target_dqn.load_state_dict(torch.load(latest_model))
                agent.target_dqn.eval()
                args.start_learning = frame_number + args.start_learning
                try:
                    with open('{}/logs/rewards.log'.format(path),'rb') as f: rewards = pickle.load(f)
                except:
                    print("Failed loading rewards from log.")
                    rewards = []
            else:
                "No checkpoint found."
                frame_number = 0
                rewards = []
        else:
            frame_number = 0
        print("Start training at frame number {0}".format(frame_number))
        train(n_actions=env.action_space.n, agent=agent, env=env, env_name=args.env, path=path, frame_number=frame_number, start_learning=args.start_learning, network_update_every=args.update_target, rewards=rewards)

    if 'render' in args:
        print("Creating environment...")
        env = gym.make('{0}-v4'.format(args.env))
        print("Load agent...")
        agent = DQN(n_actions=env.action_space.n, hidden=args.hidden)
        agent.load_state_dict(torch.load(str(args.i.name)))
        agent = agent.to(DEVICE)
        agent.eval()
        print("Start!")
        test(agent=agent, runs=args.runs, render=args.render)


import random

top_of_range = input("Type a number that will be the top of you range ")

if top_of_range.isdigit():
    top_of_range = int(top_of_range)

    if top_of_range <= 0:
        print("The number has to be greater than 0")
        quit()

else:
    print("Input a number next time")
    quit()
random_number = random.randrange(0, top_of_range)
guesses = 0

while True: #will keep asking until user gets the answer
    guesses += 1 #keeps track of the number of gueses made
    user_guess = input("What is your guess ")

    if user_guess.isdigit():
        user_guess = int(user_guess)

        if user_guess == random_number:
            print("You got it champ")
            break
        elif user_guess < random_number:
            print("try again that was less than the answer\n")
        else:
            print("Try again, that was greater than the number\n")
    else:
        print("Enter a number!! ")
        continue
print(f"You got it in {guesses} guesses")
print(random_number)


import sqlite3
from unittest import result

# Builds the list of events "db" from the database
def load_db(start_time, end_time):
    sqlite_connection = sqlite3.connect('database.db')
    try:
        sqlite_connection.execute('CREATE TABLE IF NOT EXISTS events (id TEXT PRIMARY KEY ON CONFLICT FAIL, start TEXT, end TEXT, reg_nro TEXT, merkki TEXT, asiakas TEXT, puh_nro TEXT, tyomaarays TEXT, invoice INT NOT NULL CHECK (invoice IN (0, 1)));')
#       sqlite_connection.execute('CREATE TABLE IF NOT EXISTS events (id TEXT PRIMARY KEY ON CONFLICT FAIL, start TEXT, end TEXT, reg_nro TEXT, merkki TEXT, asiakas TEXT, puh_nro TEXT, tyomaarays TEXT, invoice INT NOT NULL CHECK (invoice IN (0, 1)), FOREIGN KEY (id) REFERENCES services (varaus_id) ON DELETE CASCADE);')
#       sqlite_connection.execute('CREATE TABLE IF NOT EXISTS events (id INTEGER PRIMARY KEY ASC, ... # to get automatic id
        sqlite_connection.execute('CREATE TABLE IF NOT EXISTS services (varaus_id TEXT, rivinumero INTEGER, service TEXT, price TEXT);')
#       sqlite_connection.execute('CREATE TABLE IF NOT EXISTS services (varaus_id TEXT, rivinumero INTEGER, service TEXT, price TEXT, FOREIGN KEY (varaus_id) REFERENCES events (id) ON DELETE CASCADE);')
        sqlite_connection.commit()
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)

    sqlite_connection.row_factory = sqlite3.Row #without this line, fetchall will return Tuples, not Row objects!
    sqlite_cursor = sqlite_connection.cursor()

    sql_string = "SELECT * FROM events WHERE start BETWEEN ? AND ? ORDER BY start;"
    sqlite_cursor.execute(sql_string, (start_time, end_time))

    db = [] # a list of dictionaries, to enable jsonify(db)
    for row in sqlite_cursor.fetchall():
        # print(type(row)) # it should be sqlite.Row (not Tuple).
        row_dictionary = dict(row)
        db.append(row_dictionary)
    #sqlite_connection.close()  # not needed?
    return db

def append_db(event):
    try:
        sqlite_connection = sqlite3.connect('database.db')
        sqlite_cursor = sqlite_connection.cursor()
        sql_string = "INSERT INTO events VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);"
        sqlite_cursor.execute(sql_string, (
            event["id"],\
            event["start"],
            event["end"],
            event["reg_nro"],
            event["merkki"],\
            event["asiakas"],\
            event["puh_nro"],\
            event["tyomaarays"],
            event["invoice"] ) )
        sqlite_connection.commit()
        result = "OK"
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)
        result = "NOT_OK"
    return result

def get_services(id):
    sqlite_connection = sqlite3.connect('database.db')
    sqlite_connection.row_factory = sqlite3.Row #without this line, fetchall will return Tuples, not Row objects!
    sqlite_cursor = sqlite_connection.cursor()
    sql_string = "SELECT rivinumero, service, price FROM services WHERE varaus_id = ? ORDER BY rivinumero;"
    sqlite_cursor.execute(sql_string, (id,))

    service_dictionary = {} # includes field names (service or price) and their values
    for row in sqlite_cursor.fetchall():
        wanted_row_dictionary = {}
        # here we don't use dict(row)
        row_keys = row.keys()
        row_values = tuple(row)
        row_number = row_values[0] # its type is class 'int', an integer

        # build a dictionary of this row
        wanted_row_dictionary[ row_keys[1] + str(row_number) ] = row_values[1] # service field
        wanted_row_dictionary[ row_keys[2] + str(row_number) ] = row_values[2] # price field
        service_dictionary.update(wanted_row_dictionary)
    #sqlite_connection.close()  # not needed?
    return service_dictionary

    # previous version returned a list of dictionaries without field names
    service_row_list = [] # a list of dictionaries, to enable jsonify()
    for row in sqlite_cursor.fetchall():
        row_dictionary = {}
        row_keys = row.keys()
        row_values = tuple(row)
        value_index = 0
        # build a dictionary of this row
        for key in row_keys:    # one key-value pair at a time
            row_dictionary[key] = row_values[value_index] #could there be a smarter way? Yes, row_dictionary = dict(row)
            value_index += 1
        service_row_list.append(row_dictionary)
    #sqlite_connection.close()  # not needed?
    return service_row_list

#def add_services(varaus_id, line_list):
def add_services(services):
    try:
        sqlite_connection = sqlite3.connect('database.db')
        sqlite_cursor = sqlite_connection.cursor()
        sql_string = "INSERT INTO services VALUES (?, ?, ?, ?);"
        varaus_id = services["id"]

        for line_index in range( len(services) // 2 ):
#            print(services["service"+str(line_index)], services["price"+str(line_index)] )
            sqlite_cursor.execute(sql_string, (varaus_id, line_index, services["service"+str(line_index)], services["price"+str(line_index)]) )

        sql_string = "UPDATE events SET invoice = ? WHERE id = ?"
        sqlite_cursor.execute(sql_string, ( 1, varaus_id ))

#    Old version:
#       line_index = 0
#       for row_dictionary in line_list:
#           line_index += 1
#           sqlite_cursor.execute(sql_string, (varaus_id, line_index, row_dictionary["service"], row_dictionary["price"]) )

        sqlite_connection.commit()
        result = "OK"
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)
        result = "NOT_OK"
    return result

def move_db(event):
    sql_string = "UPDATE events SET start = ?, end = ? WHERE id = ?"
    try:
        sqlite_connection = sqlite3.connect('database.db')
        sqlite_cursor = sqlite_connection.cursor()
        sqlite_cursor.execute(sql_string, (
            event["newStart"],
            event["newEnd"],
            event["id"] ))
        sqlite_connection.commit()
        result = "OK"
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)
        result = "NOT_OK"
    return result

def update_db(event):
    sql_string = "UPDATE events SET "\
    + "reg_nro = ?, "\
    + "merkki = ?, "\
    + "asiakas = ?, "\
    + "puh_nro = ?, "\
    + "tyomaarays = ? "\
    + "WHERE id = ?"
    try:
        sqlite_connection = sqlite3.connect('database.db')
        sqlite_cursor = sqlite_connection.cursor()
        sqlite_cursor.execute(sql_string, (\
            event["reg_nro"],\
            event["merkki"],\
            event["asiakas"],\
            event["puh_nro"],\
            event["tyomaarays"],\
            event["id"] ))
        sqlite_connection.commit()
        result = "OK"
    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)
        result = "NOT_OK"
    return result

def delete_db(event_id):
    try:
        sqlite_connection = sqlite3.connect('database.db')
        sqlite_cursor = sqlite_connection.cursor()
        sql_string = "DELETE FROM events WHERE id = ?;"
        sqlite_cursor.execute(sql_string, (event_id,) ) # extra comma needed to define Tuple with only one element

        sql_string = "DELETE FROM services WHERE varaus_id = ?;"
        sqlite_cursor.execute(sql_string, (event_id,) ) # extra comma needed to define Tuple with only one element

        sqlite_connection.commit()
        result = "OK"

#       Test if CASCADE works. The services should have been deleted for this event
        print("The services for the deleted event:", get_services(event_id))

    except Exception as ex:
        template = "An exception of type {0} occurred. Arguments:\n{1!r}"
        message = template.format(type(ex).__name__, ex.args)
        print(message)
        result = "NOT_OK"
    return result


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, val=0, next=None):
#         self.val = val
#         self.next = next

import random

class Solution:

    def __init__(self, head: Optional[ListNode]):
        self.head = head

    def getRandom(self) -> int:
        res = self.head.val
        n = 2
        curr = self.head.next
        while curr:
            isSwap = random.randrange(0, n)
            if isSwap == 0:
                res = curr.val
            curr = curr.next
            n += 1
        return res

# Your Solution object will be instantiated and called as such:
# obj = Solution(head)
# param_1 = obj.getRandom()


import re
import pprint
import operator

NEW_PAGE = '----------------> new page <---------------\n'

def remove_numbers(text):
    return re.sub(r'\d+', '', text)

FILE_NAME = 'ABB1F.txt'
# FILE_NAME = 'Shipyard.txt'

dict_of_spaces = {}
lst_spaces = []

line_leading_spaces = 0
with open(FILE_NAME) as f:
    for line in f:
        if line.strip() and (line[0].isdigit() or line[1].isdigit()) and line.find('......') == -1:
            # Found a line with the number at the beginning
            if line.split()[0].isdigit():

                # Position number of the digit of each line, plus it's length
                char_num = line.find(line.split()[0]) + len(line.split()[0])

                line_without_number = line[char_num:]
                # count the leading spaces in a string
                line_leading_spaces = len(line_without_number) - len(line_without_number.lstrip(' '))
                # Stopping condition is the first line with a number
                if line_without_number.strip():
                    #                     print(f"line: {line} spaces = {line_leading_spaces}")
                    break

# print(line_leading_spaces)
text = ''

with open(FILE_NAME) as f:
    # length of the number character on a line
    char_num = 2
    count = 0
    lst_line = []
    for i, line in enumerate(f):

        #         if i > 81:
        #             count += 1
        #             lst_line.append(line)
        #         if count == 15:
        #             break

        # Found the end of a page
        if line[0] == '\x0c':
            # print(NEW_PAGE)
            line = line[1:]
            text += NEW_PAGE

        # Check if the line is not empty and first or second characters are digits
        # And line doesn't belong to a table of contents
        if line.strip() and (line[0].isdigit() or line[1].isdigit()) \
                and line.find('......') == -1 \
                and line.split()[0].isdigit():  # Found a line with the number at the beginning
            # print(f"line if: {line}")
            # Slice the line so the sentence starts after the number
            # Discard if the line is empty

            # Position number of the digit of each line, plus it's length
            if line.find(line.split()[0]) + len(line.split()[0]) > 2:
                char_num = line.find(line.split()[0]) + len(line.split()[0])

            line_without_number = line[char_num + line_leading_spaces:]

            # When concatenate '\n' to a string it just concatenate ''
            # Thus we check if line is empty or == ''
            # If line is empty, thus == ''
            if not line_without_number.strip():
                text += '\n'
            else:
                text += line_without_number
        else:

            # Find leading spaces of the current string are
            # larger then leading spaces of the first line, which is
            # stored in 'line_leading_spaces'
            #             print(f"curr_line_spaces: {len(line) - len(line.lstrip(' '))}, line_leading_spaces: {line_leading_spaces}, char_num: {char_num}")
            if len(line) - len(line.lstrip(' ')) >= line_leading_spaces + char_num:

                #                 print(f"line if:{line[line_leading_spaces + char_num:]}")
                text += line[line_leading_spaces + char_num:]
            else:
                #                 print(f"line else: {line}")
                text += line

print(text)


# Reading an excel file using Python
import xlrd

from py2neo import Graph

graph=Graph('bolt://localhost:7687',auth=("neo4j","MyGraphP@ss"))

def getEmail(s):
    email=""
    i=3
    while i<len(s)-1:
        email=email+s[i]
        i=i+1
    return email

def getBusinessRole(s):
    role=""
    i=3
    while i<len(s)-1:
        role=role+s[i]
        i=i+1
    return role

ActorRole={}
BusinessRole=[]

def getData(indexRow,indexCol):
    if(sheet.cell_value(indexRow,indexCol)=='[]'):
        return
    array=sheet.cell_value(indexRow, indexCol).split(')')
    firstItem=0
    #[('steve.olinde@enron.com', 25, ['specialist', 'trading'])
    for item in array:
        if item ==']':  #last item
            break;
        email=""
        role=""
        oneActor=item.split(",")
        if firstItem==0:
            #[('steve.olinde@enron.com
            email=getEmail(oneActor[0])
            #['specialist'
            if not oneActor[2] == '[]':
                role=getBusinessRole(oneActor[2])
            firstItem=1
        else:
            email=getEmail(oneActor[1])
            if not oneActor[3] == '[]':
                role=getBusinessRole(oneActor[3])

        ActorRole[email]=role
        if(not role=='' and role not in BusinessRole ):
                    BusinessRole.append(role)

# Give the location of the file
loc = ("C:/Users/user/Desktop/Courses/Master2/Stage/MyPapers/chatbot/Marwa Files/resultm__forney.xlsx")

# To open Workbook
wb = xlrd.open_workbook(loc)
sheet = wb.sheet_by_index(0)

#get emails from actRecipients
i=1
while i<sheet.nrows:
    getData(i,6)
    i=i+1

#get emails from actInCC
i=1
while i<sheet.nrows:
    getData(i,7)
    i=i+1

print(ActorRole)
print(BusinessRole)

#create query to insert data in neo4j
''''
for actor in ActorRole:
    query="MERGE (n: Actor {Email: '"+actor+"'})"
    try:
        data=graph.run(query)
    except:
        print("Error1 ")
        print("actor not created "+actor)

    if not ActorRole[actor]=='':
        query2="MERGE (m: Role {Name: '"+ActorRole[actor]+"'})"
        try:
            data=graph.run(query2)
        except:
            print("Error2 ")

        query3="MATCH (n: Actor{Email: '"+actor+"'}),(m: Role {Name: '"+ActorRole[actor]+"'})" \
                "MERGE (n)-[:ActorWithRole]->(m)"
        try:
            data=graph.run(query3)
        except:
            print("Error3 please repeat the question with another syntax")
'''


width =int(input("enter pattern width "))

for i in range(1,width+1,1): # how many lines till max width
    for j in range(i): # * how many times per line
        print('*', end = "")
    print('')

for i in range(width-1,0,-1):
    for j in range(i):
        print('*', end="")
    print('')

width =int(input("enter pattern width "))
count = 0
for i in range(2*width+1):
    count += 1
    if count <= width + 1:
        print('*' * i, count)
        j = i
    else:
        j -=1
        print("*" * j,count,j)


"""Implements Distributed Prioritized Experience Replay.

https://arxiv.org/abs/1803.00933"""

from __future__ import absolute_import
from __future__ import division
from __future__ import print_function

import collections
import os
import random
import time
import threading

import numpy as np
from six.moves import queue

import ray
from ray.rllib.evaluation.sample_batch import SampleBatch, DEFAULT_POLICY_ID, \
    MultiAgentBatch
from ray.rllib.optimizers.policy_optimizer import PolicyOptimizer
from ray.rllib.optimizers.replay_buffer import PrioritizedReplayBuffer
from ray.rllib.utils.actors import TaskPool, create_colocated
from ray.rllib.utils.timer import TimerStat
from ray.rllib.utils.window_stat import WindowStat

SAMPLE_QUEUE_DEPTH = 2
REPLAY_QUEUE_DEPTH = 4
LEARNER_QUEUE_MAX_SIZE = 16

@ray.remote(num_cpus=0)
class ReplayActor(object):
    """A replay buffer shard.

    Ray actors are single-threaded, so for scalability multiple replay actors
    may be created to increase parallelism."""

    def __init__(self, num_shards, learning_starts, buffer_size,
                 train_batch_size, prioritized_replay_alpha,
                 prioritized_replay_beta, prioritized_replay_eps):
        self.replay_starts = learning_starts // num_shards
        self.buffer_size = buffer_size // num_shards
        self.train_batch_size = train_batch_size
        self.prioritized_replay_beta = prioritized_replay_beta
        self.prioritized_replay_eps = prioritized_replay_eps

        def new_buffer():
            return PrioritizedReplayBuffer(
                self.buffer_size, alpha=prioritized_replay_alpha)

        self.replay_buffers = collections.defaultdict(new_buffer)

        # Metrics
        self.add_batch_timer = TimerStat()
        self.replay_timer = TimerStat()
        self.update_priorities_timer = TimerStat()
        self.num_added = 0

    def get_host(self):
        return os.uname()[1]

    def add_batch(self, batch):
        # Handle everything as if multiagent
        if isinstance(batch, SampleBatch):
            batch = MultiAgentBatch({DEFAULT_POLICY_ID: batch}, batch.count)
        with self.add_batch_timer:
            for policy_id, s in batch.policy_batches.items():
                for row in s.rows():
                    self.replay_buffers[policy_id].add(
                        row["obs"], row["actions"], row["rewards"],
                        row["new_obs"], row["dones"], row["weights"])
        self.num_added += batch.count

    def replay(self):
        if self.num_added < self.replay_starts:
            return None

        with self.replay_timer:
            samples = {}
            for policy_id, replay_buffer in self.replay_buffers.items():
                (obses_t, actions, rewards, obses_tp1, dones, weights,
                 batch_indexes) = replay_buffer.sample(
                     self.train_batch_size, beta=self.prioritized_replay_beta)
                samples[policy_id] = SampleBatch({
                    "obs": obses_t,
                    "actions": actions,
                    "rewards": rewards,
                    "new_obs": obses_tp1,
                    "dones": dones,
                    "weights": weights,
                    "batch_indexes": batch_indexes
                })
            return MultiAgentBatch(samples, self.train_batch_size)

    def update_priorities(self, prio_dict):
        with self.update_priorities_timer:
            for policy_id, (batch_indexes, td_errors) in prio_dict.items():
                new_priorities = (
                    np.abs(td_errors) + self.prioritized_replay_eps)
                self.replay_buffers[policy_id].update_priorities(
                    batch_indexes, new_priorities)

    def stats(self, debug=False):
        stat = {
            "add_batch_time_ms": round(1000 * self.add_batch_timer.mean, 3),
            "replay_time_ms": round(1000 * self.replay_timer.mean, 3),
            "update_priorities_time_ms": round(
                1000 * self.update_priorities_timer.mean, 3),
        }
        for policy_id, replay_buffer in self.replay_buffers.items():
            stat.update({
                "policy_{}".format(policy_id): replay_buffer.stats(debug=debug)
            })
        return stat

class LearnerThread(threading.Thread):
    """Background thread that updates the local model from replay data.

    The learner thread communicates with the main thread through Queues. This
    is needed since Ray operations can only be run on the main thread. In
    addition, moving heavyweight gradient ops session runs off the main thread
    improves overall throughput.
    """

    def __init__(self, local_evaluator):
        threading.Thread.__init__(self)
        self.learner_queue_size = WindowStat("size", 50)
        self.local_evaluator = local_evaluator
        self.inqueue = queue.Queue(maxsize=LEARNER_QUEUE_MAX_SIZE)
        self.outqueue = queue.Queue()
        self.queue_timer = TimerStat()
        self.grad_timer = TimerStat()
        self.daemon = True
        self.weights_updated = False

    def run(self):
        while True:
            self.step()

    def step(self):
        with self.queue_timer:
            ra, replay = self.inqueue.get()
        if replay is not None:
            prio_dict = {}
            with self.grad_timer:
                grad_out = self.local_evaluator.compute_apply(replay)
                for pid, info in grad_out.items():
                    prio_dict[pid] = (
                        replay.policy_batches[pid]["batch_indexes"],
                        info["td_error"])
            # send `replay` back also so that it gets released by the original
            # thread: https://github.com/ray-project/ray/issues/2610
            self.outqueue.put((ra, replay, prio_dict, replay.count))
        self.learner_queue_size.push(self.inqueue.qsize())
        self.weights_updated = True

class AsyncReplayOptimizer(PolicyOptimizer):
    """Main event loop of the Ape-X optimizer (async sampling with replay).

    This class coordinates the data transfers between the learner thread,
    remote evaluators (Ape-X actors), and replay buffer actors.

    This optimizer requires that policy evaluators return an additional
    "td_error" array in the info return of compute_gradients(). This error
    term will be used for sample prioritization."""

    def _init(self,
              learning_starts=1000,
              buffer_size=10000,
              prioritized_replay=True,
              prioritized_replay_alpha=0.6,
              prioritized_replay_beta=0.4,
              prioritized_replay_eps=1e-6,
              train_batch_size=512,
              sample_batch_size=50,
              num_replay_buffer_shards=1,
              max_weight_sync_delay=400,
              debug=False):

        self.debug = debug
        self.replay_starts = learning_starts
        self.prioritized_replay_beta = prioritized_replay_beta
        self.prioritized_replay_eps = prioritized_replay_eps
        self.max_weight_sync_delay = max_weight_sync_delay

        self.learner = LearnerThread(self.local_evaluator)
        self.learner.start()

        self.replay_actors = create_colocated(ReplayActor, [
            num_replay_buffer_shards,
            learning_starts,
            buffer_size,
            train_batch_size,
            prioritized_replay_alpha,
            prioritized_replay_beta,
            prioritized_replay_eps,
        ], num_replay_buffer_shards)

        # Stats
        self.timers = {
            k: TimerStat()
            for k in [
                "put_weights", "get_samples", "sample_processing",
                "replay_processing", "update_priorities", "train", "sample"
            ]
        }
        self.num_weight_syncs = 0
        self.num_samples_dropped = 0
        self.learning_started = False

        # Number of worker steps since the last weight update
        self.steps_since_update = {}

        # Otherwise kick of replay tasks for local gradient updates
        self.replay_tasks = TaskPool()
        for ra in self.replay_actors:
            for _ in range(REPLAY_QUEUE_DEPTH):
                self.replay_tasks.add(ra, ra.replay.remote())

        # Kick off async background sampling
        self.sample_tasks = TaskPool()
        if self.remote_evaluators:
            self.set_evaluators(self.remote_evaluators)

    # For https://github.com/ray-project/ray/issues/2541 only
    def set_evaluators(self, remote_evaluators):
        self.remote_evaluators = remote_evaluators
        weights = self.local_evaluator.get_weights()
        for ev in self.remote_evaluators:
            ev.set_weights.remote(weights)
            self.steps_since_update[ev] = 0
            for _ in range(SAMPLE_QUEUE_DEPTH):
                self.sample_tasks.add(ev, ev.sample_with_count.remote())

    def step(self):
        assert len(self.remote_evaluators) > 0
        start = time.time()
        sample_timesteps, train_timesteps = self._step()
        time_delta = time.time() - start
        self.timers["sample"].push(time_delta)
        self.timers["sample"].push_units_processed(sample_timesteps)
        if train_timesteps > 0:
            self.learning_started = True
        if self.learning_started:
            self.timers["train"].push(time_delta)
            self.timers["train"].push_units_processed(train_timesteps)
        self.num_steps_sampled += sample_timesteps
        self.num_steps_trained += train_timesteps

    def _step(self):
        sample_timesteps, train_timesteps = 0, 0
        weights = None

        with self.timers["sample_processing"]:
            completed = list(self.sample_tasks.completed())
            counts = ray.get([c[1][1] for c in completed])
            for i, (ev, (sample_batch, count)) in enumerate(completed):
                sample_timesteps += counts[i]

                # Send the data to the replay buffer
                random.choice(
                    self.replay_actors).add_batch.remote(sample_batch)

                # Update weights if needed
                self.steps_since_update[ev] += counts[i]
                if self.steps_since_update[ev] >= self.max_weight_sync_delay:
                    # Note that it's important to pull new weights once
                    # updated to avoid excessive correlation between actors
                    if weights is None or self.learner.weights_updated:
                        self.learner.weights_updated = False
                        with self.timers["put_weights"]:
                            weights = ray.put(
                                self.local_evaluator.get_weights())
                    ev.set_weights.remote(weights)
                    self.num_weight_syncs += 1
                    self.steps_since_update[ev] = 0

                # Kick off another sample request
                self.sample_tasks.add(ev, ev.sample_with_count.remote())

        with self.timers["replay_processing"]:
            for ra, replay in self.replay_tasks.completed():
                self.replay_tasks.add(ra, ra.replay.remote())
                if self.learner.inqueue.full():
                    self.num_samples_dropped += 1
                else:
                    with self.timers["get_samples"]:
                        samples = ray.get(replay)
                    self.learner.inqueue.put((ra, samples))

        with self.timers["update_priorities"]:
            while not self.learner.outqueue.empty():
                ra, _, prio_dict, count = self.learner.outqueue.get()
                ra.update_priorities.remote(prio_dict)
                train_timesteps += count

        return sample_timesteps, train_timesteps

    def stats(self):
        replay_stats = ray.get(self.replay_actors[0].stats.remote(self.debug))
        timing = {
            "{}_time_ms".format(k): round(1000 * self.timers[k].mean, 3)
            for k in self.timers
        }
        timing["learner_grad_time_ms"] = round(
            1000 * self.learner.grad_timer.mean, 3)
        timing["learner_dequeue_time_ms"] = round(
            1000 * self.learner.queue_timer.mean, 3)
        stats = {
            "sample_throughput": round(self.timers["sample"].mean_throughput,
                                       3),
            "train_throughput": round(self.timers["train"].mean_throughput, 3),
            "num_weight_syncs": self.num_weight_syncs,
            "num_samples_dropped": self.num_samples_dropped,
            "learner_queue": self.learner.learner_queue_size.stats(),
            "replay_shard_0": replay_stats,
        }
        debug_stats = {
            "timing_breakdown": timing,
            "pending_sample_tasks": self.sample_tasks.count,
            "pending_replay_tasks": self.replay_tasks.count,
        }
        if self.debug:
            stats.update(debug_stats)
        return dict(PolicyOptimizer.stats(self), **stats)


from luna import *
from data_root import Data

class User_Settings(Data) :
    def __init__(self, data_directory : str, id : int) -> None :
        self.id = str(id)
        super().__init__(data_directory)

        # Add the item if it doesn't exist
        if not self.item_exists("user", self.id) :
            self.add_item("user", self.id)

        if not "settings" in self.data()["user"][self.id] :
            data = self.data()
            data["user"][self.id]["settings"] = {
                "blocked_users" : [],
                "accept_whisper" : True
            }
            self.write(data),

    def get(self) -> int :
        return self.data()["user"][self.id]["settings"]

    def get_setting(self, setting : str) :
        return self.get()[setting]

    def edit_setting(self, setting : str, value) -> None :
        data = self.data()
        data["user"][self.id]["settings"][setting] = value
        self.write(data)

    def block(self, user_id : int) -> None :
        data = self.data()
        if user_id in data["user"][self.id]["settings"]["blocked_users"] : return
        data["user"][self.id]["settings"]["blocked_users"].append(user_id)
        self.write(data)

    def unblock(self, user_id : int) -> None :
        data = self.data()
        if user_id in data["user"][self.id]["settings"]["blocked_users"] : return
        data["user"][self.id]["settings"]["blocked_users"].remove(user_id)
        self.write(data)


import pdb

from models.animal import Animal
from models.vet import Vet
import repositories.animal_repository as animal_repository
import repositories.vet_repository as vet_repository

animal_repository.delete_all()
vet_repository.delete_all()

vet_1 = Vet("Dr Doolittle")
vet_repository.save(vet_1)

vet_2 = Vet("Dr Herriot")
vet_repository.save(vet_2)

vet_3 = Vet("Dr Hamster")
vet_repository.save(vet_3)

vet_4 = Vet("Dr Greene")
vet_repository.save(vet_4)

vet_repository.select_all()

animal_1 = Animal("Spot", "Cat", "15-06-2005", "15", "Deep gash on nose to be treated", "Commander Data", "07922175003", "star@trek.com", vet_1)
animal_repository.save(animal_1)

animal_2 = Animal("Twiggy", "Stick Insect", "10-10-2019", "1", "Leg pulled off by spider. Replacement leg needed", "David Bellamy", "07922175003", "david@bellamy.com", vet_1)
animal_repository.save(animal_2)

animal_3 = Animal("Zeus", "Silverback", "17-10-1990", "30", "Ingrowing toenail, left pinky toe", "Jo Mobutu", "07922175003", "big@thief.com", vet_3)
animal_repository.save(animal_3)

animal_4 = Animal("Cornelius", "Chimpanzee", "12-11-1995", "25", "Nasty bruise to forehead. Possible X-Ray", "George Taylor", "07922175003", "dirty@ape.com", vet_4)
animal_repository.save(animal_4)

# pdb.set_trace()

# for vet in vet_repository.select_all():
#     print(vet.__dict__)

# for animal in animal_repository.select_all():
#     print(animal.__dict__)

# for vet in vet_repository.select(id):
#     print(vet.__dict__)


def find(pattern, text):
    result = []
    if not (pattern and text):
        return result
    m = len(pattern)
    n = len(text)
    h = pow(256,m-1,100)
    p = 0
    t = 0
    if m>n:
        return result;
    for i in range(m): # we have a hash of the pattern and first m letters of text
        p = (256*p+ord(pattern[i]))%100 # hash of pattern is constant
        t = (256*t+ord(text[i]))%100 # this will change through the for loop
    for s in range(n-m+1):
        if p == t: # if hashes are the same we check letters value by value
            are_same = True
            for i in range(m):
                if pattern[i] != text[s+i]:
                    are_same = False
                    break
            if are_same: # if they match each other we add position to the list
                result.append(s)
        if s < n-m: # we are creating hash for next set of letters in the text
            t = (t-h*ord(text[s]))%100 # we subtract hash of the previos first letter
            t = (t*256+ord(text[s+m]))%100 # and add hash of the last letter
            t = (t+100)%100 # in order to t > 0
    return result
# print (find ("xd", "x"))


#!/usr/bin/env python
#
# tournament.py -- implementation of a Swiss-system tournament
#

import psycopg2

def connect():
    """Connect to the PostgreSQL database.  Returns a database connection."""
    return psycopg2.connect("dbname=tournament")

def deleteMatches():
    """Remove all the match records from the database."""
    create_tournament()

def deletePlayers():
    """Remove all the player records from the database."""
    query = '''UPDATE players SET deleted = true;'''
    count = run_query(query)

def countPlayers():
    """Returns the number of players currently registered."""
    query = '''SELECT count FROM player_count;'''
    count = run_query(query)
    return count[0][0]

def registerPlayer(name):
    """Adds a player to the tournament database.

    The database assigns a unique serial id number for the player.  (This
    should be handled by your SQL database schema, not in your Python code.)

    Args:
      name: the player's full name (need not be unique).
    """
    tournament_id = get_tournament_id()
    query = '''INSERT INTO players (player_name, tournament_id) VALUES (%s, %s);'''
    run_query(query, (name, tournament_id))
    print(name + " registered.")

def playerStandings():
    """Returns a list of the players and their win records, sorted by wins.

    The first entry in the list should be the player in first place, or a player
    tied for first place if there is currently a tie.

    Returns:
      A list of tuples, each of which contains (id, name, wins, matches):
        id: the player's unique id (assigned by the database)
        name: the player's full name (as registered)
        wins: the number of matches the player has won
        matches: the number of matches the player has played
    """
    query = '''SELECT * FROM standings;'''
    return run_query(query)

def reportMatch(winner, loser):
    """Records the outcome of a single match between two players.

    Args:
      winner:  the id number of the player who won
      loser:  the id number of the player who lost
    """
    tournament_id = get_tournament_id()
    query = '''INSERT INTO matches (tournament_id, first_player_id, second_player_id, winner_id ) VALUES (%s, %s, %s, %s);'''
    run_query(query, (tournament_id, winner, loser, winner))

def swissPairings():
    """Returns a list of pairs of players for the next round of a match.

    Assuming that there are an even number of players registered, each player
    appears exactly once in the pairings.  Each player is paired with another
    player with an equal or nearly-equal win record, that is, a player adjacent
    to him or her in the standings.

    Returns:
      A list of tuples, each of which contains (id1, name1, id2, name2)
        id1: the first player's unique id
        name1: the first player's name
        id2: the second player's unique id
        name2: the second player's name
    """
    standings = playerStandings()
    matches = []
    match = ()
    match_count = 1;
    last_player = ''
    for player in standings:
        last_player = player
        if match_count == 2:
            match = match + (player[0], player[1])
            matches.append(match)
            match_count = 1
        else:
            match = (player[0], player[1])
            match_count += 1

    # if we end with player this player gets a free win to the next round
    '''if match_count == 1:
        reportMatch(standings[:-1][0], 0)'''

    return matches

def run_query(query, query_data = "()"):
    """Records the outcome of a single match between two players.

    Args:
      query:  the query to execute
      data_query:  any tuple params to make sure sql is escaped
    """
    conn = connect()
    c = conn.cursor()
    c.execute(query, query_data)
    return_data = ""

    if query.find("SELECT") < 0:
        conn.commit()
    else:
        return_data = c.fetchall()

    conn.close()
    return return_data

def get_tournament_id():
    """Records the outcome of a single match between two players.

    Return:
      The data from the excuted query
    """
    query = '''SELECT current_tournament_id FROM tournament;'''
    count = run_query(query)
    return count[0][0]

def create_tournament():
    """Creates a new tournament."""
    query = '''INSERT INTO tournaments (tournament_name) VALUES ('The Big thing!');'''
    count = run_query(query)

#Start off a tourment the first time the script is ran
create_tournament()


octop = [[int(octo) for octo in line] for line in open("AOC_txt_11").read().split("\n")]
flashes = 0

def add(f):
    for i in range(len(f)):
        for j in range(len(f[0])):
            f[i][j] += 1

def voisins(f, i, j):
    n = len(f)
    liste = [(i - 1, j - 1), (i - 1, j), (i - 1, j + 1), (i, j + 1), (i + 1, j + 1), (i + 1, j), (i + 1, j - 1),
             (i, j - 1)]
    return [vois for vois in liste if 0 <= vois[0] <= n - 1 and 0 <= vois[1] <= n - 1]

def update(f):
    add(f)
    n = len(f)
    flash = 0
    flashing = [(i, j) for i in range(n) for j in range(n) if f[i][j] > 9]
    while flashing:
        flash += 1
        i, j = flashing.pop()
        f[i][j] = 0
        v = [(o, k) for o, k in voisins(f, i, j) if f[o][k] != 0]
        for o, k in v:
            f[o][k] += 1
            if f[o][k] > 9 and (o, k) not in flashing:
                flashing.append((o, k))
    return flash

# PART 1
# for _ in range(100):
#     flashes += update(octop)

# print(flashes)

# PART 2
# k=0
# while octop != [[0]*len(octop)]*len(octop):
#     k+=1
#     update(octop)
# print(k)


import rospy
import actionlib
from move_base_msgs.msg import MoveBaseAction, MoveBaseGoal
import socket
import pickle ,time

def movebase_client():
    receiver = socket.socket(family=socket.AF_INET, type=socket.SOCK_DGRAM)
    receiver.bind((master_ip,7778))

    client = actionlib.SimpleActionClient('move_base',MoveBaseAction)
    client.wait_for_server()

    goal = MoveBaseGoal()
    goal.target_pose.header.frame_id = "map"
    goal.target_pose.header.stamp = rospy.Time.now()

    while True:
        bytepair = receiver.recvfrom(1024)
        message = pickle.loads(bytepair[0])
        print(message)
        goal.target_pose.pose.position.x = message[0]
        goal.target_pose.pose.position.y = message[1]
        goal.target_pose.pose.orientation.z = message[2]
        goal.target_pose.pose.orientation.w = message[3]
        time.sleep(0.1)
        break

    client.send_goal(goal)
    wait = client.wait_for_result()
    if not wait:
        rospy.logerr("Action server not available!")
        rospy.signal_shutdown("Action server not available!")
    else:
        return client.get_result()

master_ip = '192.168.0.16'

if __name__ == '__main__':

    try:
        rospy.init_node('movebase_client_py')
        result = movebase_client()
        if result:
            rospy.loginfo("Goal execution done!")
    except rospy.ROSInterruptException:
        rospy.loginfo("Navigation test finished.")


#!/usr/bin/env python
# coding: utf-8

# # importing libraries

# In[1]:

import matplotlib.pyplot as plt
import numpy as np
import cv2 as cv
import math

# # loading image

# In[2]:

img_path = './Bird2.jpg'
rgb_img = plt.imread(img_path)
rgb_img.shape

# # Convert to GrayScale

# In[3]:

grayscale = cv.cvtColor(rgb_img,cv.COLOR_RGB2GRAY)
grayscale.shape

# In[4]:

plt.figure(figsize = (50, 15))
plt.subplot(1, 2, 1)
plt.hist(grayscale.ravel(),256,[0,256]);

values, count = np.unique(grayscale, return_counts=True)
plt.subplot(1, 2, 2)
plt.bar([x for x in range(0,256)], count, width=0.9)

plt.savefig('./Histogram_Output.png')

# In[ ]:


# matrix mult function
def matMult(m1, m2, mod):
    tl = m1[0] * m2[0] + m1[1] * m2[2]
    tr = m1[0] * m2[1] + m1[1] * m2[3]
    bl = m1[2] * m2[0] + m1[3] * m2[2]
    br = m1[2] * m2[1] + m1[3] * m2[3]

    return (tl % mod, tr % mod, bl % mod, br % mod)

# inverse function mod p
def invMod(num, mod):
    for i in range(mod):
        if (num * i) % mod == 1:
            return i

    return -1 # this should never happen if mod is prime and num is nonzero

# matrix print function
def printMat(mat):
    print('|' + str(mat[0]) + ' ' + str(mat[1]) + '|')
    print('|' + str(mat[2]) + ' ' + str(mat[3]) + '|')
    print()

# matrix determinant function
def matDet(mat):
    return mat[0] * mat[3] - mat[1] * mat[2]

print('Welcome to Jason\'s general linear group subgroup generator!')
mod = int(input('Enter a prime number p to use GL(Z_p) as your group: '))

print('Matrix multiplication mod ' + str(mod) + ' is the operation for this general linear group.')
stack = []
subgroup = set()
subgroup.add((1, 0, 0, 1)) # ensure identity element is in subgroup

print('Enter an invertible 2x2 matrix from left-right, top-down to generate its subgroup: ')

tl = int(input('   Top  left: '))
tr = int(input('  Top  right: '))
bl = int(input('Bottom  left: '))
br = int(input('Bottom right: '))

if matDet([tl, tr, bl, br]) != 0:
    stack.append((tl % mod, tr % mod, bl % mod, br % mod))
else:
    print('Error: This matrix is not invertible.')

while stack: # stack will hold elements to be processed
    a = stack.pop() # element to process

    if matDet(a) != 0:

        if a not in subgroup:
            subgroup.add(a)

        # add inverse of a to stack:
        detInv = invMod(matDet(a), mod) # 1/determinant in mod p
        matSwapNeg = (a[3], -a[1], -a[2], a[0]) # swap a and d, negate b and c
        matInv = (detInv * matSwapNeg[0] % mod, detInv * matSwapNeg[1] % mod, detInv * matSwapNeg[2] % mod, detInv * matSwapNeg[3] % mod) # distribute factor in
        if matInv not in stack and matInv not in subgroup:
            stack.append(matInv)

        # add a * b and b * a for every b already in subgroup
        for b in subgroup:
            prod1 = matMult(a, b, mod) # a * b
            if prod1 not in stack and prod1 not in subgroup:
                stack.append(prod1)

            prod2 = matMult(b, a, mod) # b * a
            if prod2 not in stack and prod2 not in subgroup:
                stack.append(prod2)

print('Subgroup generated with ' + str(len(subgroup)) + ' elements:')
for mat in subgroup:
    printMat(mat)


options = ["Exit", "Learn Python", "Go mountainbiking", "Play Skyrim",\
          "Cook dinner", "Climb Mount Everest"]

selection = None

while selection != 0:

    for i in range(len(options)):
        print(f"{i}. {options[i]}")

    selection = int(input("\nPlease enter your selection "))
    while selection < len(options):
        print(f"You've selected {options[selection]}\n---------------------------")
        break
    else:
        print("Invalid entry \n")

else:
    print("You have Exited the program")

#test


import sys
import math
from decimal import Decimal

len_max_float = len(str(sys.float_info.max))

class Node(object):
    num_str = ""
    next = None
    prev = None

def big_div(num_str_arr, ret_arr):
    rem = 0
    val = ""
    ret = ""
    ans = 0
    i = 0
    while i < len(num_str_arr):

        val = num_str_arr[i]
        if rem == 1:
            val = "1" + val
            rem = 0

        if int(val) < 2 and i < len(num_str_arr) -1:
            ret += "0"
            val = val + num_str_arr[i + 1]
            i += 1

        ret = ret + str(int(math.floor(int(val)/2)))
        if (int(val) % 2 != 0):
            rem = 1
        i += 1

    ret_arr[0] = ret
    ret_arr[1] = str(rem)

def big_mult(num_str_arr, ret_arr):
    carry = 0
    val = ""
    ret = ""
    ans = 0
    i = len(num_str_arr) - 1
    if ret_arr[1] != "":
        carry = int(ret_arr[1])

    while i >= 0:
        val = num_str_arr[i]

        val = str(int(val) * 3)

        if carry > 0:
            val = str(int(val) + carry)
            carry = 0

        if int(val) > 9:
            carry = int(math.floor(int(val) / 10))
            val = str(val[1])

        ret =   val + ret
        i -= 1

    ret_arr[0] = ret
    ret_arr[1] = str(carry)

def big_add(num_str_arr, ret_arr):
    carry = 0
    val = ""
    ret = ""
    ans = 0
    i = len(num_str_arr) - 1
    if ret_arr[1] != "" and ret_arr[1] != "0":
        carry = int(ret_arr[1]) #should just be 1

    if int(num_str_arr[i]) + carry < 10:
        ret_arr[0] = num_str_arr[:-1] + str(int(num_str_arr[i]) + carry)
        carry = 0
        ret_arr[1] = "0"
        return

    else:
        while i >= 0:
            val = num_str_arr[i]

            if carry > 0:
                val = str(int(val) + carry)
                carry = 0
            if int(val) >= 10:
                carry = 1
                val = str(val[1])

            ret =  val + ret
            i -= 1

    ret_arr[0] = ret
    ret_arr[1] = str(carry)

def long_divide(num):
    orig_node = Node()
    node = orig_node
    count = 0

    for i in range(0, len(num)):
        if count == len_max_float:
            count = 0
            node.next = Node()
            node.next.prev = node
            node = node.next
        node.num_str += num[i]
        count += 1

    tail = node

    arr = ["", ""]
    node = orig_node
    temp1 = ""
    result = ""
    while node != None:
        temp1 = node.num_str
        if arr[1] != "0" and arr[1] != "":
            temp1 = arr[1] + node.num_str

        big_div(temp1, arr)
        if len(arr[0]) > len(node.num_str):
            arr[0] = arr[0][1:]
        result += arr[0]
        node = node.next

    if result[0] == "0":
        result = result[1:]
    return result

def long_multiply(num):
    orig_node = Node()
    node = orig_node
    count = 0

    for i in range(0, len(num)):
        if count == len_max_float:
            count = 0
            node.next = Node()
            node.next.prev = node
            node = node.next
        node.num_str += num[i]
        count += 1

    tail = node

    arr = ["", ""]
    node = tail
    temp1 = ""
    result = ""

    while node != None:
        temp1 = node.num_str

        big_mult(temp1, arr)
        result = arr[0] + result
        node = node.prev

    if arr[1] != "" and arr[1] != "0":
        result = arr[1] + result

    if result[0] == "0":
        result = result[1:]
    return result

def long_add(num):
    orig_node = Node()
    node = orig_node
    count = 0

    for i in range(0, len(num)):
        if count == len_max_float:
            count = 0
            node.next = Node()
            node.next.prev = node
            node = node.next
        node.num_str += num[i]
        count += 1

    tail = node

    arr = ["", ""]
    node = tail
    temp1 = ""
    result = ""
    arr[1] = 1

    while node != None:
        temp1 = node.num_str
        big_add(temp1, arr)
        result = arr[0] + result
        node = node.prev

    if arr[1] != "" and arr[1] != "0":
        result = arr[1] + result
    if result[0] == "0":
        result = result[1:]
    return result

with open(sys.stdin.readline()[:-1]) as ofo:
    for value in ofo:
        i = 0
        value = value.rstrip('\n')
        while int(value) != 1:
            if int(value[len(value) - 1]) % 2 == 0:
                value = long_divide(value)
            else:
                value = long_multiply(value)
                value = long_add(value)
            i += 1

        print(i)