import os
import re
from PIL import Image
import numpy as np
# from .common.log import logger
import torch
from torch.utils.data import Dataset, DataLoader
import torchvision.transforms as transforms
from torchvision.datasets import Caltech256

class Caltech256AnomalyDataset(Caltech256):
        def __init__(self, root, inliners, outliners, transform=None, target_transform=None, download=False):
            super(Caltech256AnomalyDataset, self).__init__(root, transform, target_transform, download)

            def is_normal(i):
                return i in inliners
            def is_abnormal(i):
                return i in outliners

            self.index = []
            self.y = []
            for (i, c) in enumerate(self.categories):
                n = len(list(
                    filter(
                        lambda p: p.endswith('jpg'),
                        os.listdir(os.path.join(self.root, "256_ObjectCategories", c))
                    )
                ))
                self.index.extend(range(1, n + 1))
                self.y.extend(n * [i])

            self._imgs_path = []
            self._labels = []

            self._train_normal_imgs_path = []
            self._test_normal_imgs_path = []
            self._abnormal_imgs_path = []

            self._class_num = np.zeros((257, ))
            for item in self.y:
                self._class_num[item] += 1
            # print(np.argsort(self._class_num)[::-1])

            self._normal_samples_num, self._abnormal_samples_num = 0, 0

            for index in range(len(self.y)):
                img_path = os.path.join(self.root,
                                        "256_ObjectCategories",
                                        self.categories[self.y[index]],
                                        "{:03d}_{:04d}.jpg".format(self.y[index] + 1, self.index[index]))
                label = self.y[index]

                if is_normal(label):
                    self._imgs_path += [img_path]
                    if self.index[index] < self._class_num[self.y[index]] * 0.8:
                        self._train_normal_imgs_path += [img_path]
                    else:
                        self._test_normal_imgs_path += [img_path]
                    self._labels += [0]
                    self._normal_samples_num += 1
                if is_abnormal(label):
                    self._imgs_path += [img_path]
                    self._abnormal_imgs_path += [img_path]
                    self._labels += [1]
                    self._abnormal_samples_num += 1

        def __getitem__(self, index):
            img = Image.open(self._imgs_path[index])

            img_np = np.array(img)
            if len(img_np.shape) < 3:
                t = np.empty((3, ) + img_np.shape, dtype=img_np.dtype)
                t[0] = np.array(img_np)
                t[1] = np.array(img_np)
                t[2] = np.array(img_np)
                img = Image.fromarray(t.transpose(1, 2, 0))

            target = self._labels[index]

            if self.transform is not None:
                img = self.transform(img)

            if self.target_transform is not None:
                target = self.target_transform(target)

            return img, target

        def __len__(self):
            return len(self._imgs_path)

def gpnd_caltech256_dataloader(dataroot='/data/datasets/', inliners=[250, 144, 252, 22, 67], outliners=[256], train_batch_size=64, test_batch_size=64, image_size=32, num_workers=8):

    transform = transforms.Compose([
        transforms.Resize([image_size, image_size]),
        transforms.ToTensor(),
        # transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.229, 0.224, 0.225])
        # transforms.Lambda(lambda img: img / 255.)
    ])
    dataset = Caltech256AnomalyDataset(os.path.join(dataroot, './Caltech-256/data/'), inliners, outliners, transform=transform)

    train_normal_imgs_path, test_normal_imgs_path = dataset._train_normal_imgs_path, dataset._test_normal_imgs_path
    abnormal_imgs_path = dataset._abnormal_imgs_path

    if len(test_normal_imgs_path) > len(abnormal_imgs_path):
        test_normal_imgs_path = test_normal_imgs_path[:len(abnormal_imgs_path)]
    else:
        abnormal_imgs_path = abnormal_imgs_path[:len(test_normal_imgs_path)]

    train_set = Caltech256AnomalyDataset(os.path.join(dataroot, './Caltech-256/data/'), inliners, outliners, transform=transform)
    train_set._imgs_path = train_normal_imgs_path
    train_set._labels = [0 for _ in train_normal_imgs_path]

    test_set = Caltech256AnomalyDataset(os.path.join(dataroot, './Caltech-256/data/'), inliners, outliners, transform=transform)
    test_set._imgs_path = test_normal_imgs_path + abnormal_imgs_path
    test_set._labels = [0 for _ in test_normal_imgs_path] + [1 for _ in abnormal_imgs_path]

    print('train normal sample {}, test normal sample {}, '
                'test abnormal sample {}'.format(len(train_normal_imgs_path), len(test_normal_imgs_path), len(abnormal_imgs_path)))

    train_loader = DataLoader(train_set, batch_size=train_batch_size, shuffle=True, num_workers=num_workers, pin_memory=True, drop_last=True)
    test_loader = DataLoader(test_set, batch_size=test_batch_size, shuffle=False, num_workers=num_workers, pin_memory=True, drop_last=True)

    return train_loader, test_loader


from Stagiaire import Stagiaire
from Gestion import Gestion
# initialisation des stagiaires :
stg1 = Stagiaire("Karimi","loubna","TSDM",1,11,16,17)
stg2 = Stagiaire("Jamal","Youssef","TSDM",2,14,15,20)
stg3 = Stagiaire("Ilham","Fayrouz","TSDM",3,12,13,15)

# print(stg1)
# print(stg2)
# print(stg3)

# stg2.setMatricule(1)

# print(stg1.Equals(stg2))

# initialisation de la classe Gestion:
liste = Gestion()

# add objects to the liste:
liste.setListeStagiares(stg1,stg2,stg3)

# print(liste.getListeStagiaires())

# for m in liste.getListeStagiaires():
#     print(m.Calcul())

# classer les objet par moyenne décroissante:
liste.Classement()

# print(liste.getListeStagiaires())

# for m in liste.getListeStagiaires():
#     print(m.Calcul())

# affichage de la liste des stagiaires:
liste.Affichage()

# searh by matricule number:
print(liste.SearchByMatricule(2))

#afficher la meilleur moyenne et son propriétère:
print(liste.BestGrade())

# mofidier un objet avec la classe Gestion:
liste.Modify(stg1,"ahmed")


# С помощью функции parse_sh_cdp_neighbors из задания 10.2, обработать вывод  команды sh cdp neighbor из файлов:
# Объединить все словари, которые возвращает функция parse_sh_cdp_neighbors, в
# один словарь topology и записать его содержимое в файл topology.yaml.
#  Структура словаря topology должна быть такой:
#            {'R4': {'Fa0/1': {'R5': 'Fa0/1'},
#                    'Fa0/2': {'R6': 'Fa0/0'}},
#             'R5': {'Fa0/1': {'R4': 'Fa0/1'}},
#             'R6': {'Fa0/0': {'R4': 'Fa0/2'}}}

import glob
import re
import os

#################################################################################################
# читает файлы и передает результат в виде списка
def read_file (file_name):
    temp_list=[]
    try:                                          # обработка исключения на наличие файла
        with open(file_name, 'r') as f:
            for line in f:
               temp_list.append(line.rstrip())    # исключиние дополнитеьлного символа перевода строки и заполнение вспомогательного списка
    except IOError:
        print('---FUNCTION---No such file')
    return temp_list
#################################################################################################
# парсим файл по строчно (получаем его как список строк)
def parse_sh_cdp_neighbors (out_file):
    dict_device = {}
    dict_temp = {}
    # цикл необходим тк в некоторых файлах сверх есть пустая строка
    # логика цикла ищем совпадение (служебный вывод либо None )
    # если служебный вывод то получаем из строки либо индекс строки либо индекс того чего ищем
    for line_in_file in out_file:
        posit = re.search('[>]',line_in_file)                           # выражение для поиска >
        if posit:                                                       # если совпадеине найдено
            posit= re.search('[>]',line_in_file).start()                # получаем позицию >
            my_name = line_in_file[:posit:]                             # получаем имя устройсва до >

    for line_in_file in out_file:
        end_of_header_line= re.match('Device ID\s+',line_in_file)       # ищем нижню строчку шапки файла
        if end_of_header_line:                                          # если совпадеине найдено
            end_of_header_line = re.match('Device ID\s+',line_in_file).group()
            index_header=out_file.index(line_in_file)                   # получаем индек этой строки в списк е out_file

    for line_in_file in out_file[index_header+1::]:
        # R1           Eth 0/1         122           R S I           2811       Eth 0/0
        match =re.search('(?P<my_int>\S+)\s+(?P<neighbors_name>\S+\s\S+).+?(?P<neighbors_int>[Eth]\S+\s\d+\S\d+)',line_in_file)
        #  'R5': {'Fa0/1': {'R4': 'Fa0/1'}}
        my_int = match.group('my_int')
        neighbors_name =match.group('neighbors_name')
        neighbors_int =match.group('neighbors_int')
        dict_temp2= {}
        dict_temp1= {}
        dict_temp1[my_int]= neighbors_int
        dict_temp2[neighbors_name]=dict_temp1
        dict_temp.update(dict_temp2)

    dict_device[my_name]= dict_temp
    return dict_device

#################################################################################################

if __name__ == '__main__':
    directory = os.getcwd()+'/'              #/home/ubuntu/workspace/python/tasks/10_type_files/10_2/
    sh_cdp_n_files = glob.glob('sh_cd*')     # список имен файлов
    list_of_file = []
    dict_total = {}
    for file_name in sh_cdp_n_files:
        full_name = directory+file_name
        list_of_file = read_file (full_name)
        parse_sh_cdp_neighbors (list_of_file)
        dict_total.update(parse_sh_cdp_neighbors (list_of_file))
    print (dict_total)


# gesture control python program for controlling certain functions in windows pc
# Code by BalaAppu
# Website: www.electronicshub.org

import serial                                      # add Serial library for serial communication
import pyautogui                                   # add pyautogui library for programmatically controlling the mouse and keyboard.

Arduino_Serial = serial.Serial('com3',9600)       # Initialize serial and Create Serial port object called Arduino_Serial

while 1:
    print("line received")
    incoming_data = str (Arduino_Serial.readline())
    print("line read")
    print(incoming_data)

    if 'next' in incoming_data:                    # if incoming data is 'next'
        pyautogui.hotkey('ctrl', 'pgdn')           # perform "ctrl+pgdn" operation which moves to the next tab
        print("h")

    if 'previous' in incoming_data:                # if incoming data is 'previous'
        pyautogui.hotkey('ctrl', 'pgup')           # perform "ctrl+pgup" operation which moves to the previous tab
        print("h")
    if 'down' in incoming_data:                    # if incoming data is 'down'
        #pyautogui.press('down')                   # performs "down arrow" operation which scrolls down the page
        pyautogui.press('r')
        print("h")
    if 'up' in incoming_data:                      # if incoming data is 'up'
        #pyautogui.press('up')
        # performs "up arrow" operation which scrolls up the page
        print("h")
        pyautogui.press('k')

    if 'change' in incoming_data:                  # if incoming data is 'change'
        pyautogui.keyDown('alt')                   # performs "alt+tab" operation which switches the tab
        pyautogui.press('tab')
        pyautogui.keyUp('alt')
        print("h")
    incoming_data = "";                            # clears the data


'''
Created on 15 Nov 2017

@author: ahmed.salem
'''

import sys

sys.dont_write_bytecode = True

from sklearn.metrics import classification_report, accuracy_score
import theano.tensor as T
import numpy as np
import lasagne
import theano
import argparse
from sklearn.metrics import precision_recall_fscore_support
from sklearn.metrics import precision_score
from sklearn.metrics import recall_score

def iterate_minibatches(inputs, targets, batch_size, shuffle=True):
    assert len(inputs) == len(targets)
    if shuffle:
        indices = np.arange(len(inputs))
        np.random.shuffle(indices)

    start_idx = None
    for start_idx in range(0, len(inputs) - batch_size + 1, batch_size):
        if shuffle:
            excerpt = indices[start_idx:start_idx + batch_size]
        else:
            excerpt = slice(start_idx, start_idx + batch_size)
        yield inputs[excerpt], targets[excerpt]

    if start_idx is not None and start_idx + batch_size < len(inputs):
        excerpt = indices[start_idx + batch_size:] if shuffle else slice(start_idx + batch_size, len(inputs))
        yield inputs[excerpt], targets[excerpt]

def get_cnn_model(n_in, n_hidden, n_out):
    net = dict()
    #print(n_in)
    net['input'] = lasagne.layers.InputLayer(shape=(None, 3, 32, 32))

    net['conv1'] = lasagne.layers.Conv2DLayer(net['input'], num_filters=32, filter_size=(5, 5),
            pad='same',
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))

    net['maxPool1'] = lasagne.layers.MaxPool2DLayer(net['conv1'], pool_size=(2, 2))

    net['conv2'] = lasagne.layers.Conv2DLayer(
            net['maxPool1'], num_filters=32, filter_size=(5, 5),
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))
    net['maxPool2'] = lasagne.layers.MaxPool2DLayer(net['conv2'], pool_size=(2, 2))

    net['fc'] = lasagne.layers.DenseLayer(
        net['maxPool2'],
        num_units=n_hidden,
        nonlinearity=lasagne.nonlinearities.tanh)

    net['output'] = lasagne.layers.DenseLayer(
        net['fc'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net

def get_cnn2_model(n_in, n_hidden, n_out):
    net = dict()
    face = False
    if (face):
        net['input'] = lasagne.layers.InputLayer(shape=(None, 1, 62, 47))
    else:
        net['input'] = lasagne.layers.InputLayer(shape=(None, 1, 28, 28))

    net['conv1'] = lasagne.layers.Conv2DLayer(net['input'], num_filters=32, filter_size=(5, 5),
            pad='same',
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))

    net['maxPool1'] = lasagne.layers.MaxPool2DLayer(net['conv1'], pool_size=(2, 2))

    net['conv2'] = lasagne.layers.Conv2DLayer(
            net['maxPool1'], num_filters=32, filter_size=(5, 5),
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))
    net['maxPool2'] = lasagne.layers.MaxPool2DLayer(net['conv2'], pool_size=(2, 2))

    net['fc'] = lasagne.layers.DenseLayer(
        net['maxPool2'],
        num_units=n_hidden,
        nonlinearity=lasagne.nonlinearities.tanh)

    net['output'] = lasagne.layers.DenseLayer(
        net['fc'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net

def get_nn_model(n_in, n_hidden, n_out):
    net = dict()
    net['input'] = lasagne.layers.InputLayer((None, n_in))
    net['fc'] = lasagne.layers.DenseLayer(
        net['input'],
        num_units=n_hidden,
        nonlinearity=lasagne.nonlinearities.tanh)
    net['output'] = lasagne.layers.DenseLayer(
        net['fc'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net

def get_Dropnn_model(n_in, n_hidden, n_out,drop1,drop2):
    net = dict()
    net['input'] = lasagne.layers.InputLayer((None, n_in))

    net['inputDropOut'] = lasagne.layers.DropoutLayer(net['input'], p=drop1)

    net['fc'] = lasagne.layers.DenseLayer(
        net['inputDropOut'],
        num_units=n_hidden,
        nonlinearity=lasagne.nonlinearities.tanh)

    net['fcDropOut'] = lasagne.layers.DropoutLayer(net['fc'], p=drop2)

    net['output'] = lasagne.layers.DenseLayer(
        net['fcDropOut'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net
def get_Droppcnn_model(n_in, n_hidden, n_out,drop1,drop2,drop3):
    net = dict()
    net['input'] = lasagne.layers.InputLayer(shape=(None, 3, 32, 32))

    net['inputDropOut'] = lasagne.layers.DropoutLayer(net['input'], p=drop1)

    net['conv1'] = lasagne.layers.Conv2DLayer(net['inputDropOut'], num_filters=32, filter_size=(5, 5),
            pad='same',
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))

    net['maxPool1'] = lasagne.layers.MaxPool2DLayer(net['conv1'], pool_size=(2, 2))

    net['conv2'] = lasagne.layers.Conv2DLayer(
            net['maxPool1'], num_filters=32, filter_size=(5, 5),
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))

    net['maxPool2'] = lasagne.layers.MaxPool2DLayer(net['conv2'], pool_size=(2, 2))

    #net['maxPool2DropOut'] = lasagne.layers.DropoutLayer(net['maxPool2'], p=drop1)

    net['fc'] = lasagne.layers.DenseLayer(
        net['maxPool2'],
        num_units=n_hidden,
        nonlinearity=lasagne.nonlinearities.tanh)

    net['fcDropOut'] = lasagne.layers.DropoutLayer(net['fc'], p=drop2)

    net['output'] = lasagne.layers.DenseLayer(
        net['fcDropOut'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net

def get_Droppcnn2_model(n_in, n_hidden, n_out,drop1,drop2,drop3):
    net = dict()
    face = False
    if (face):
        net['input'] = lasagne.layers.InputLayer(shape=(None, 1, 62, 47))
    else:
        net['input'] = lasagne.layers.InputLayer(shape=(None, 1, 28, 28))

    net['inputDropOut'] = lasagne.layers.DropoutLayer(net['input'], p=drop1)

    net['conv1'] = lasagne.layers.Conv2DLayer(net['inputDropOut'], num_filters=32, filter_size=(5, 5),
            pad='same',
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))

    net['maxPool1'] = lasagne.layers.MaxPool2DLayer(net['conv1'], pool_size=(2, 2))

    net['conv2'] = lasagne.layers.Conv2DLayer(
            net['maxPool1'], num_filters=32, filter_size=(5, 5),
            nonlinearity=lasagne.nonlinearities.rectify,
            W=lasagne.init.GlorotUniform(gain='relu'))

    net['maxPool2'] = lasagne.layers.MaxPool2DLayer(net['conv2'], pool_size=(2, 2))

    #net['maxPool2DropOut'] = lasagne.layers.DropoutLayer(net['maxPool2'], p=drop1)

    net['fc'] = lasagne.layers.DenseLayer(
        net['maxPool2'],
        num_units=n_hidden,
        nonlinearity=lasagne.nonlinearities.tanh)

    net['fcDropOut'] = lasagne.layers.DropoutLayer(net['fc'], p=drop2)

    net['output'] = lasagne.layers.DenseLayer(
        net['fcDropOut'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net

def get_softmax_model(n_in, n_out):
    net = dict()
    net['input'] = lasagne.layers.InputLayer((None, n_in))
    net['output'] = lasagne.layers.DenseLayer(
        net['input'],
        num_units=n_out,
        nonlinearity=lasagne.nonlinearities.softmax)
    return net

def train_model(dataset, n_hidden=50, batch_size=100, epochs=100, learning_rate=0.01, model='cnn', l2_ratio=1e-7,
          rtn_layer=True):
    print("classfier.py -- train_model")

    myTest = (model == 'softmax')

    train_x, train_y, test_x, test_y = dataset
    #print(train_x)
    n_in = train_x.shape[1]
    silent = False
    #print(train_x.shape)
    n_out = len(np.unique(train_y))

    if batch_size > len(train_y):
        batch_size = len(train_y)
    if (not myTest):
        print('Building model with {} training data, {} classes...'.format(len(train_x), n_out))
    if model=='cnn' or model=='cnn2' or model=='Droppcnn' or  model=='Droppcnn2':
        input_var = T.tensor4('x')
    else:
        input_var = T.matrix('x')
    target_var = T.ivector('y')
    if model == 'cnn':

        print('Using conv neural network...')
        net = get_cnn_model(n_in, n_hidden, n_out)

    elif model == 'cnn2':
        print('Using conv neural network...')
        net = get_cnn2_model(n_in, n_hidden, n_out)
    elif model == 'nn':
        print('Using neural network...')
        net = get_nn_model(n_in, n_hidden, n_out)
    elif model == 'Dropnn':
        print('Using neural network with Dropout')
        net = get_Dropnn_model(n_in, n_hidden, n_out,0.75,0.5)
    elif model == 'Droppcnn':
        print('Using neural network with Dropout')
        net = get_Droppcnn_model(n_in, n_hidden, n_out,0.2,0.5,0.5)

    elif model == 'Droppcnn2':
        print('Using Conv neural network with Dropout')
        net = get_Droppcnn2_model(n_in, n_hidden, n_out,0.5,0.5,0.5)
    else:
        if (not myTest and not silent):
            print('Using softmax regression...')
        net = get_softmax_model(n_in, n_out)

    net['input'].input_var = input_var

    output_layer = net['output']
    # create loss function
    prediction = lasagne.layers.get_output(output_layer)
    loss = lasagne.objectives.categorical_crossentropy(prediction, target_var)
    loss = loss.mean() + l2_ratio * lasagne.regularization.regularize_network_params(output_layer,
                                                                                lasagne.regularization.l2)
    # create parameter update expressions
    params = lasagne.layers.get_all_params(output_layer, trainable=True)
    updates = lasagne.updates.adam(loss, params, learning_rate=learning_rate)
    train_fn = theano.function([input_var, target_var], loss, updates=updates)
    # use trained network for predictions
    test_prediction = lasagne.layers.get_output(output_layer, deterministic=True)
    test_fn = theano.function([input_var], test_prediction)
    if (not myTest and not silent):
        print('Training...')
    counter = 1
    for epoch in range(epochs):
        print(epoch)
        loss = 0
        for input_batch, target_batch in iterate_minibatches(train_x, train_y, batch_size):
            #input_batch = (np.reshape(input_batch,(len(input_batch),3,32,32)))
            #print("classfier.py -- func -- train_model -- iterate_minibatches")

            loss += train_fn(input_batch, target_batch)

        loss = round(loss, 4)
        if (not myTest and not silent):
            print('Epoch {}, train loss {}'.format(epoch, loss))

        counter = counter +1
    pred_y = []
    for input_batch, _ in iterate_minibatches(train_x, train_y, batch_size, shuffle=False):
        #input_batch = (np.reshape(input_batch,(len(input_batch),3,32,32)))
        pred = test_fn(input_batch)
        pred_y.append(np.argmax(pred, axis=1))
    pred_y = np.concatenate(pred_y)
    if (not myTest and not silent):
        print(classification_report(train_y, pred_y))

    if test_x is not None:
        if (not myTest and not silent):
            print('Testing...')
        pred_y = []

        if batch_size > len(test_y):
            batch_size = len(test_y)

        for input_batch, _ in iterate_minibatches(test_x, test_y, batch_size, shuffle=False):
            #input_batch = (np.reshape(input_batch,(len(input_batch),3,32,32)))
            pred = test_fn(input_batch)
            pred_y.append(np.argmax(pred, axis=1))
        pred_y = np.concatenate(pred_y)
        if (not myTest and not silent):
            print('Testing Accuracy: {}'.format(accuracy_score(test_y, pred_y)))

    print(classification_report(test_y, pred_y))

    if ( myTest):
        pres= round(precision_score(test_y, pred_y,average='weighted'),4)
        recall= round(recall_score(test_y, pred_y,average='weighted'),4)
        accuracy= round(accuracy_score(test_y, pred_y),5)
        print("precision",pres)
        print("recall",recall)
        print("accuracy",accuracy)
        return pres,recall, accuracy
        # return classification_report(test_y, pred_y) #precision_recall_fscore_support(test_y, pred_y,average=None)

    if rtn_layer:

        return output_layer
    else:
        return pred_y

def load_dataset(train_feat, train_label, test_feat=None, test_label=None):

    train_x = np.genfromtxt(train_feat, delimiter=',', dtype='float32')
    train_y = np.genfromtxt(train_label, dtype='int32')

    min_y = np.min(train_y)
    train_y -= min_y
    if test_feat is not None and test_label is not None:
        test_x = np.genfromtxt(train_feat, delimiter=',', dtype='float32')
        test_y = np.genfromtxt(train_label, dtype='int32')
        test_y -= min_y
    else:
        test_x = None
        test_y = None
    return train_x, train_y, test_x, test_y

def main():
    parser = argparse.ArgumentParser()
    parser.add_argument('train_feat', type=str)
    parser.add_argument('train_label', type=str)
    parser.add_argument('--test_feat', type=str, default=None)
    parser.add_argument('--test_label', type=str, default=None)
    parser.add_argument('--model', type=str, default='nn')
    parser.add_argument('--learning_rate', type=float, default=0.01)
    parser.add_argument('--batch_size', type=int, default=100)
    parser.add_argument('--n_hidden', type=int, default=50)
    parser.add_argument('--epochs', type=int, default=100)
    args = parser.parse_args()
    print(vars(args))
    dataset = load_dataset(args.train_feat, args.train_label, args.test_feat, args.train_label)
    train_model(dataset,
          model=args.model,
          learning_rate=args.learning_rate,
          batch_size=args.batch_size,
          n_hidden=args.n_hidden,
          epochs=args.epochs)

if __name__ == '__main__':
    main()


from flask import Flask, render_template, flash

app = Flask(__name__)
app.secret_key = 'this is a secret'

@app.route('/')
def index():
    flash('Welcome to my website guest!')
    return render_template('index.html')

if __name__ == '__main__':
    app.run(debug=True)


import random
import math
from pathlib import Path
from typing import Tuple, List, Optional

import torch
import torch.nn as nn
import numpy as np
import hydra
from omegaconf import DictConfig
import audiomentations as aud
import youtokentome as yttm

import core

def get_bpe_path(cfg, original_path: Optional[Path] = None):
    path = Path(cfg.common.files_path, cfg.bpe.model_path)
    return Path(original_path / path if original_path else hydra.utils.to_absolute_path(path))

def get_lengths_path(cfg, lengths_type: str, original_path: Optional[Path] = None):
    """
    :param lengths_type: either "waveform" or "utterance" for waveform and utterance lengths respectively
    :param original_path: Path object with original working directory. It is used when hydra.utils.to_absolute_path(path)
    is not available (in subprocess)
    """
    path = Path(cfg.common.files_path, cfg.lengths.lengths_filename.format(lengths_type))
    return Path(original_path / path if original_path else hydra.utils.to_absolute_path(path))

def fix_seeds(seed=1337):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    np.random.seed(seed)
    random.seed(seed)
    torch.backends.cudnn.enabled = False
    torch.backends.cudnn.deterministic = True

def get_waveform_transforms(transforms: DictConfig, device=None):
    """
    get all necessary transforms from config
    :param transforms: transforms from config
    :param device: device if transforms need to be ported to device
    :return: transforms composed into aud.Compose
    """
    if transforms is None:
        return None
    if device is not None:
        return aud.Compose([
            hydra.utils.instantiate(transform).to(device)
            for transform in transforms
        ])
    else:
        return aud.Compose([
            hydra.utils.instantiate(transform)
            for transform in transforms
        ])

def pad_collate(batch: List[Tuple]) -> Tuple[torch.Tensor, torch.Tensor, List[torch.Tensor],
                                             torch.Tensor, List[int], torch.Tensor]:
    """
    Collator that transforms batch into tuple of lists and pads spectrograms and utterances
    :param batch: List of tuples of batch components
    :return: Tuple of tensors and lists
    """
    waveforms, spectrograms, sample_rates, utterances = zip(*batch)

    input_lengths = torch.tensor([math.ceil(spectrogram.shape[1] / 2) for spectrogram in spectrograms])
    target_lengths = torch.tensor([utterance.shape[0] for utterance in utterances])

    utterances = nn.utils.rnn.pad_sequence(utterances, True, 0)
    spectrograms = nn.utils.rnn.pad_sequence([s.transpose(0, 1) for s in spectrograms], True, 0)
    spectrograms = spectrograms.transpose(1, 2)

    return input_lengths, target_lengths, waveforms, spectrograms, sample_rates, utterances

def ctc_decode(seq: torch.Tensor, blank_idx: int):
    """
    Simple ctc decoding without beam search
    :param seq: torch tensor with tokens of shape (n_labels, len)
    :param blank_idx: index of blank character
    :return: torch tensor with ctc decoded tokens
    """
    seq = seq.argmax(dim=0)
    seq = torch.cat([seq[[0]], seq[1:][(seq[1:] - seq[:-1]).bool()]])  # Remove consecutive duplicates
    seq = seq[seq != blank_idx]  # Remove blanks
    return seq

def get_texts(utterances, logprobs, bpe: core.transforms.BPETransform, blank_idx) -> Tuple[List[str], List[str]]:
    """
    Get utterances and logprobs, ctc decode, remove padding, bpe decode and return texts
    :param utterances: bpe encoded utterances from dataset
    :param logprobs: output from network
    :param bpe: BPE encoder object for encoding (duh)
    :param blank_idx: index of ctc blank value
    :return: two lists of strings for true and predicted texts
    """
    true_tokens = [np.trim_zeros(utt.tolist()) for utt in utterances]
    pred_tokens = [np.trim_zeros(ctc_decode(prob, blank_idx).tolist()) for prob in logprobs]
    true_texts = [coded[0] if len(coded) > 0 else "" for coded in [bpe.bpe.decode(true) for true in true_tokens]]
    pred_texts = [coded[0] if len(coded) > 0 else "" for coded in [bpe.bpe.decode(pred) for pred in pred_tokens]]
    return true_texts, pred_texts


def kernel (input,c=1,p=2): #polymonial kernel
  return ((np.transpose(input)*input) + c)**p

class svm():
  def __init__(self):
    self.w=[]
    self.b=[] #not used
    starting_w=1
    starting_b=10
    self.w=[kernel(starting_w),kernel(starting_w), starting_b]
    self.C = 1.0
  def fit (self,data,labels,epochs=100):
    temp_data= np.array(data) #rolled bias into w
    bias = np.array(np.ones(len(labels)))
    bias *=-1
    bias=bias.reshape(len(bias),1)
    final = np.c_[temp_data,bias]
    data=final
    for epoch in range(1,epochs):
      for i in range (len(labels)):
        kernalized_data = [kernel(data[i,0]),kernel(data[i,1]),-1]
        if (1-(labels[i]*(np.dot(self.w,kernalized_data) ))) >0:
          w_grad = -1*((np.transpose(self.w)*(1/epoch)) - (labels[i]*self.C)*np.transpose(kernalized_data) )
        else:
          w_grad=-1*((1/epoch)*np.transpose(self.w))
        self.w=self.w+w_grad
  def predict(self,data):
    kernalized_data = [kernel(data[0]),kernel(data[1]),-1]
    return (np.sign(np.dot(self.w,kernalized_data)))


import PySimpleGUI as sg
import download

sg.theme("DarkGrey12")

def tela_principal():
    layout = [
        [sg.Text("YOUTUBE DOWNLOAD")],
        [sg.Button("Baixar Músicas"),sg.Button("Baixar Vídeos")]
    ]
    return sg.Window("Youtube Download",layout,finalize=True)

def tela_musica():
    layout = [
        [sg.Text("Música Download")],
        [sg.Text("Link do vídeo:"),sg.InputText("",key="link_video")],
        [sg.Text("Diretorio da Pasta",key="text_diretorio"),sg.FolderBrowse(key="diretorio")],
        [sg.Button("Baixar Música"),sg.Button("Voltar")]
    ]
    return sg.Window("Música Download",layout,finalize=True)

def tela_video():
    layout = [
        [sg.Text("Vídeo Download")],
        [sg.Text("Link do vídeo:"),sg.InputText("",key="link_video1")],
        [sg.Text("Diretorio da Pasta",key="text_diretorio1"),sg.FolderBrowse(key="diretorio1")],
        [sg.Button("Baixar Vídeo"),sg.Button("Voltar")]
    ]
    return sg.Window("Vídeo Download",layout,finalize=True)

janela_principal = tela_principal()
janela_musica = None
janela_video = None

while True:
    janelas,eventos,valores = sg.read_all_windows()
    if eventos == sg.WINDOW_CLOSED:
        break

    if janelas == janela_principal and eventos == "Baixar Músicas":
        janela_principal.hide()
        janela_musica = tela_musica()

    if janelas == janela_musica and eventos == "Voltar":
        janela_musica.hide()
        janela_principal.un_hide()

    if janelas == janela_principal and eventos == "Baixar Vídeos":
        janela_principal.hide()
        janela_video = tela_video()

    if janelas == janela_video and eventos == "Voltar":
        janela_video.hide()
        janela_principal.un_hide()

    if janelas == janela_musica and eventos == "Baixar Música" and valores["diretorio"] == "":
        sg.Popup("Por Favor informe o local onde o arquivo vai ser salvo")

    if janelas == janela_musica and eventos == "Baixar Música" and valores["diretorio"] != "":
        link_video = valores["link_video"]
        pasta_arquivo = valores["diretorio"]
        sg.Popup("Vai aparecer que o youtube download não está respondendo. Isso é porque o download está sendo feito.Por favor click em OK e espere aparecer um Popup dizendo que o download está concluido")
        download.download_musica(link_video,pasta_arquivo)
        sg.Popup("Download Concluido")
        janela_musica["text_diretorio"].update("Por Favor informe o diretorio novamente")

    if janelas == janela_video and eventos == "Baixar Vídeo" and valores["diretorio1"] == "":
        sg.Popup("Por Favor informe o local onde o arquivo vai ser salvo")

    if janelas == janela_video and eventos == "Baixar Vídeo" and valores["diretorio1"] != "":
        link_video = valores["link_video1"]
        pasta_arquivo = valores["diretorio1"]
        sg.Popup("Vai aparecer que o youtube download não está respondendo. Isso é porque o download está sendo feito.Por favor click em OK e espere aparecer um Popup dizendo que o download está concluido")
        download.download_video(link_video,pasta_arquivo)
        sg.Popup("Download Concluido")
        janela_video["text_diretorio1"].update("Por Favor informe o diretorio novamente")


"""
Tic Tac Toe Player
"""

import copy, math

X = "X"
O = "O"
EMPTY = None

def initial_state():
    """
    Returns starting state of the board.
    """
    return [[EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY], [EMPTY, EMPTY, EMPTY]]

def player(board):
    """
    Returns player who has the next turn on a board.
    """
    if not terminal(board):
        moves = 0
        for row in range(3):
            for col in range(3):
                if board[row][col] != EMPTY:
                    moves += 1
        if moves % 2 == 0:
            return X
        return O
    return None

def actions(board):
    """
    Returns set of all possible actions (i, j) available on the board.
    """
    if not terminal(board):
        actions = set()
        for row in range(3):
            for col in range(3):
                if board[row][col] == EMPTY:
                    actions.add((row, col))
        return actions
    return None

def result(board, action):
    """
    Returns the board that results from making move (i, j) on the board.
    """
    if board[action[0]][action[1]] != EMPTY:
        raise ValueError("This action is not possible")

    new_board = copy.deepcopy(board)
    new_board[action[0]][action[1]] = player(board)
    return new_board

def winner(board):
    """
    Returns the winner of the game, if there is one.
    """
    for row in board:
        if row[0] == row[1] == row[2]:
            return row[0]

    for i in range(3):
        if board[0][i] == board[1][i] == board[2][i]:
            return board[0][i]

    if board[0][0] == board[1][1] == board[2][2]:
        return board[0][0]
    if board[0][2] == board[1][1] == board[2][0]:
        return board[0][2]

    return None

def terminal(board):
    """
    Returns True if game is over, False otherwise.
    """
    counter = 0
    for row in range(3):
        for col in range(3):
            if board[row][col] == EMPTY:
                counter += 1
    if counter == 0 or winner(board) is not None:
        return True
    return False

def utility(board):
    """
    Returns 1 if X has won the game, -1 if O has won, 0 otherwise.
    """
    game_winner = winner(board)
    if game_winner == X:
        return 1
    elif game_winner == O:
        return -1
    else:
        return 0

def minimax(board):
    """
    Returns the optimal action for the current player on the board.
    """
    if terminal(board):
        return None

    if player(board) == X:
        return max(board, alpha=-math.inf, beta=math.inf)[1]
    else:
        return min(board, alpha=-math.inf, beta=math.inf)[1]

def max(board, alpha, beta):
    if terminal(board):
        return [utility(board), None]

    v = -math.inf
    move = None
    for action in actions(board):
        try:
            v_prime = min(result(board, action), alpha, beta)[0]
            if v_prime > v:
                v = v_prime
                move = action
            if v_prime >= beta:
                return [v, move]
            if v_prime > alpha:
                alpha = v_prime
        except ValueError:
            continue
    return [v, move]

def min(board, alpha, beta):
    if terminal(board):
        return [utility(board), None]

    v = math.inf
    move = None
    for action in actions(board):
        try:
            v_prime = max(result(board, action), alpha, beta)[0]
            if v_prime < v:
                v = v_prime
                move = action
            if v_prime <= alpha:
                return [v, move]
            if v_prime < beta:
                beta = v_prime
        except ValueError:
            continue
    return [v, move]


from django.contrib.auth.models import AbstractUser
from django.db import models

class CustomUser(AbstractUser):
    # Add additional fields here

    class Meta():
        permissions = (
            ('G1', 'Pode ver x'),
            ('G2', 'Pode ver y'),
            ('G3', 'Pode ver z')
        )

    def __str__(self):
        return self.email


import numpy as np
import scipy.io as sio
import datetime as dt
import io
import requests
import pandas as pd

def _get_timestamp(line):
    """ Get a datetime and epoch value from a standard DAT file line """
    timestamp = dt.datetime.strptime(' '.join(line.strip().split(' ')[1:3]), '%Y/%m/%d %H:%M:%S.%f')
    epoch = np.float64(timestamp.replace(tzinfo=dt.timezone.utc).timestamp()) # 'epoch' is unix time
    return timestamp, epoch

def _get_token(response):
    for key, value in response.cookies.items():
        if key.startswith('download_warning'):
            return value
    return None

def open_gdrive_file(blob_id):
    """ Read a file from Google Drive into memory. Returns an open (BytesIO) file-like object. """

    url = "https://docs.google.com/uc?export=download"
    session = requests.Session()
    response = session.get(url, params = { 'id' : blob_id }, stream = True,)
    token = _get_token(response)
    if token:
        params = { 'id' : blob_id, 'confirm' : token }
        response = session.get(url, params = params, stream = True)
    file_bytes = response.content
    return io.BytesIO(file_bytes)

def read_nav(blob_id, dive_number):
    f = open_gdrive_file(blob_id)
    mat = sio.loadmat(f, squeeze_me=True)
    nrows = len(mat['rnv']['t'].take(0))
    timestamp = []
    for i in range(nrows):
        timestamp.append(dt.datetime.utcfromtimestamp(mat['rnv']['t'].take(0)[i]))
    epoch = list(mat['rnv']['t'].take(0))
    dive_number = [dive_number] * nrows
    lat = list(mat['rnv']['lat'].take(0))
    lon = list(mat['rnv']['lon'].take(0))
    depth = list(mat['rnv']['pos'].take(0)[:,2])
    height = list(mat['rnv']['alt'].take(0))
    heading = list(mat['rnv']['pos'].take(0)[:,3])
    pitch = list(mat['rnv']['pos'].take(0)[:,4])
    roll = list(mat['rnv']['pos'].take(0)[:,5])

    # convert to dataframe
    return pd.DataFrame({'timestamp': timestamp, 'epoch': epoch, 'dive_number': dive_number,
                        'lat': lat, 'lon': lon, 'depth': depth, 'heading': heading,
                        'pitch': pitch, 'roll': roll, 'height': height})

def read_paros(blob_id, dive_number):
    paros_list = []
    f = open_gdrive_file(blob_id)
    for line in f:
        line = line.decode('utf-8').strip()
        if 'RAW' in line[0:3]:
            if 'P2=' in line:
                if len(line) == 57: # good lines from this instrument are length 57
                    timestamp, epoch = _get_timestamp(line)
                    a = line.split(' ')[3].split(',')[0].split('=')[1]
                    b = line.split(' ')[3].split(',')[1]
                    paros_list.append([timestamp, epoch, dive_number, np.float64(a), np.float64(b)])

    # convert to dataframe (tau and eta are the the pressure and temperature signal periods in microseconds)
    return pd.DataFrame(paros_list, columns=['timestamp', 'epoch', 'dive_number', 'tau', 'eta'])

def read_ustrain(blob_id, dive_number):
    ustrain_list = []
    f = open_gdrive_file(blob_id)
    for line in f:
        line = line.decode('utf-8').strip()
        if 'MSA3' in line[0:4]:
            if len(line.split(' ')) == 32: # good lines from this instrument will have 32 fields
                timestamp, epoch = _get_timestamp(line)
                ustrain_list.append([timestamp, epoch, dive_number] + list(map(np.float64, line.split(' ')[3:-1])))

    # convert to dataframe
    return pd.DataFrame(ustrain_list, columns=['timestamp','epoch','dive_number','a',
                                              'b','c','d','e','f','g','h','i','j','k',
                                              'l','m','n','o','p','q','r','s','t','u',
                                              'v','w','x','y','z','aa','bb'])

def read_sbe3(blob_id, dive_number):
    sbe3_list = []
    f = open_gdrive_file(blob_id)
    for line in f:
        try:
            line = line.decode('utf-8').strip()
        except:
            line = ''
        if 'SBE3' in line[0:4]:
            if len(line) == 58: # good lines from this instrument are length 58
                timestamp, epoch = _get_timestamp(line)
                counts_0 = np.int64(line.split(' ')[4])
                counts_1 = np.int64(line.split(' ')[6])
                if counts_0 > 500000 and counts_0 < 815000 and counts_1 > 450000 and counts_1 < 770000:
                    sbe3_list.append([timestamp, epoch, dive_number, counts_0, counts_1])

    # convert to dataframe
    return pd.DataFrame(sbe3_list, columns=['timestamp','epoch','dive_number','counts_0','counts_1'])

# ITS-90 equation
def _its90(f, g, h, i, j, f0):
    return 1/(g + h*(np.log(f0/f)) + i*((np.log(f0/f))**2) + j*((np.log(f0/f))**3)) - 273.15

# convert sbe3 counts to temperature
def convert_sbe3(counts, g, h, i, j, f0):
    Fxtal = 4.91548
    f = 512*Fxtal/(counts/1e6)
    T = _its90(f, g, h, i, j, f0)
    return T

# convert Paros values to temperature and pressure
def convert_paros(eta, tau, C1, C2, C3, D1, D2, T1, T2, T3, T4, T5, U0, Y1, Y2, Y3):

    U = eta - U0
    C = C1 + C2*U + C3*U**2
    D = D1 + D2*U
    T0 = T1 + T2*U + T3*U**2 + T4*U**3 + T5*U**4

    T = Y1*U + Y2*U**2 + Y3*U**3
    P = C*(1-T0**2/tau**2)*(1-D*(1-T0**2/tau**2))
    return T, P

# read Nortek ADV data
def read_nortek(blob_id, dive_number):
    nortek_list = []
    f = open_gdrive_file(blob_id)
    for line in f:
        try:
            line = line.decode('utf-8').strip()
        except:
            line = ''
        if 'VVD' in line[0:4]:
            if len(line.split(' ')) == 21: # good lines from this instrument will have 21 fields
                timestamp, epoch = _get_timestamp(line)
                nortek_list.append([timestamp, epoch, dive_number] +
                                    list(map(np.float64, line.split(' ')[8:11])) +
                                    list(map(np.float64, line.split(' ')[12:15])) +
                                    list(map(np.float64, line.split(' ')[16:19])))

    # convert to dataframe
    return pd.DataFrame(nortek_list, columns=['timestamp','epoch','dive_number','v0','v1','v2',
                                              'c0','c1','c2','a0','a1','a2'])


# 133. Clone Graph
# Medium
# https://leetcode.com/problems/clone-graph/

"""
# Definition for a Node.
class Node:
    def __init__(self, val = 0, neighbors = None):
        self.val = val
        self.neighbors = neighbors if neighbors is not None else []
"""

# Larry, https://www.youtube.com/watch?v=DcL9uvLtOAE
class Solution:
    def cloneGraph(self, root: 'Node') -> 'Node':
        if root is None:
            return None

        queue = collections.deque()
        seen = set()

        def maybe_enqueue(node):
            if node.val not in seen:
                seen.add(node.val)
                queue.append(node)

        dupe = {}
        def getter(node):
            if node.val not in dupe:
                dupe[node.val] = Node(node.val)
            return dupe[node.val]

        first = getter(root)
        maybe_enqueue(root)
        while len(queue) > 0:
            current = queue.popleft()
            current_dupe = getter(current)

            for neighbor in current.neighbors:
                current_dupe.neighbors.append(getter(neighbor))

            for neighbor in current.neighbors:
                maybe_enqueue(neighbor)

        return first
# 02/23/2022 17:04

# 7/25/2021
# Pathrise's solution in Graph Preworkshop
from collections import deque

class Solution:
    def cloneGraph(self, node: 'Node') -> 'Node':
        if not node: return None
        root = node
        clone = {}

        queue = deque([node])
        all_nodes = set([node])
        while queue:
            node = queue.popleft()
            for neighbor in node.neighbors:
                if neighbor not in all_nodes:
                    queue.append(neighbor)
                    all_nodes.add(neighbor)

        for node in all_nodes:
            clone[node] = Node(node.val)

        for node in all_nodes:
            for neighbor in node.neighbors:
                clone[node].neighbors.append(clone[neighbor])

        return clone[root]


class Solution(object):
    def findDisappearedNumbers(self, nums):
        """
        :type nums: List[int]
        :rtype: List[int]
        """
        i, n = 0, len(nums)
        while i < n:
            while nums[i] != nums[nums[i] - 1]:
                tmp = nums[i]
                nums[i] = nums[tmp - 1]
                nums[tmp - 1] = tmp
            i += 1
        ret = []
        for i in xrange(n):
            if nums[i] != i + 1:
                ret.append(i+1)
        return ret


from pytsa.models import dquad_2sphere
from pytsa.sampler import setup_sampler
from scipy.stats import uniform, chi2, expon

# Example sampler setup file for double quadratic model.

# Initialize sampler object with the model (imported at the tope)
# and a name (for identification)
setup = setup_sampler.SamplerMethods(dquad_2sphere, "dquad_2sphere_example")

# Setup analysis hyper parameters
setup.set_analysis_params(tols=[1e-5, 1e-5])

# Setup field initial conditions; here we sample on the domain [-20, 20]
setup.set_field(0, 1, method=uniform(-20, 40))

# Setup dot field initial conditions; here we just use slow roll values
setup.set_dot_field(0, 1, method="sr")

# Setup parameter sampling; here we sample each mass parameter from an
# exponential distribution
setup.set_param(0, 1, method=expon(scale=0.01))

# We also sample the metric parameter, R. Here we sample from the chi2
# distribution.
# We chose this dist. since it is positive definite and the metric depends
# on R^2, so we don't over sample.
setup.set_param(2, method=chi2(3))

# Finally, build the sampler
setup.build_sampler()


#The purpose of this program is counting the expected length of a string coded by hoffman code.
#The character is provided with their frequency.
file=open('huffman.txt','r')
index=-1
all_data=[() for i in range(1000)]
all_data_copy=[() for i in range(1000)]

for lines in file:
    lines=int(lines)
    if lines!=1000:       # because the first line is the total number of data, 1000.
        index=index+1
        all_data[index]=([index],lines)
        all_data_copy[index]=([index],lines)   # The original list will be sorted by their frequency.
                                            #I'll need a copy of the original data.

# This functon is for sorting purpose.
def ind(a):
    return a[1]

# I'll need a array storing the length for each character
length=[0 for i in range(1000)]

# Sorting according to their frequency
all_data.sort(key=ind)

while len(all_data)>1:
    for i in all_data[0][0]:            # Add a digit to the least two frequent substring
        length[i]=length[i]+1
    for i in all_data[1][0]:
        length[i]=length[i]+1
    temp_node=all_data[0][0]            # Combining the substring with the least frequency
    temp_node.extend(all_data[1][0])
    temp_weight=all_data[0][1]+all_data[1][1]  # Combining the frequency of substrings with the least frequency
    new_node=(temp_node,temp_weight)    # create the combination of the least two strings
    all_data.pop(0)                     # take the least two substrings out
    all_data.pop(0)
    all_data.append(new_node)           # adding the new substring into the tree
    all_data.sort(key=ind)              # sorting by the frequence

length.sort()
print(length)

Greedy Algorithms, Minimum Spanning Trees, and Dynamic Programming/3_3/algo3_3.py


# 누적합의 경우의 수의 합과 100의 차이가 최소가 되었을 때 출력

score_list = [int(input()) for _ in range(10)] # 입력받아서
sum_list = []                                   # 각 누적합을 넣음.

for i in range(10):                             # 총 10가지 경우의 수
    tmp = abs(100 - sum(score_list[0:i+1]))     # 100과의 차이의 최솟값
    sum_list.append(tmp)                        # 그 차이를 리스트에 담아서

                                                # 그 차이의 최솟값을 출력
print(min(sum_list)+100)                        # 100이 빠진 차이값이니 100을 더함

# 누적합의 경우의 수의 합과 100의 차이가 최소가 되었을 때 출력

score_list = [int(input()) for _ in range(10)] # 버섯 갯수 10개 입력받아서 리스트에 넣음
sum_list = []       # 각 경우의 누적합들을 담은 리스트
abs_list = []       # 100 과의 차이의 절댓값을 씌운 것의 리스트
comp_list = []      # 차이의 최소값이 복수개 일때 비교하기 위한 리스트

for i in range(10):                     # 누적합은 총 10개의 경우의 수가 있음
    tmp1 = sum(score_list[0:i+1])       # 각 경우의 합
    sum_list.append(tmp1)

    tmp2 = abs(100 -sum_list[i])        # 그 합을 100에서 뺐을 때의 차이
    abs_list.append(tmp2)

for j in range(10):                     # 100과의 차이가 최소가 되는 값이 복수개 일 경우에
    if abs_list[j] == min(abs_list):
        comp_list.append(sum_list[j])   # 그에 해당하는 누적합을 리스트에 담음

try:
    print(max(comp_list))               # 둘 중 누적합이 큰 것이 답임
except:                                 # 리스트에 한개만 있으면 max를 못쓰니까 except
    print(comp_list)                    # 그 한개를 그대로 출력

# print(min(sum_list)+100)


from openpyxl import Workbook, load_workbook
import re
import os

#Initialisation
#1. Initialisation de l'expression régulière décrivant les débuts de recipes.
expr_reg_debut_recipe=r"disz (?:na|kimin|ki\.min|ki-min|lu2)"
#2. Initialitation du Système S
systeme_S={"disz":1,"u":10,"gesz2":60,"geszu":600,"szar2":3600}
#3. Initialisation des unités de mesure:
Unites_de_mesure=["ban2","gin2","ma-na","sila3","barig","gur","sze","gu2","gun2"]

def prochain_mot(pos,chaine):#trouve le prochain mot, strictement après la position pos dans la chaine chaine
        interruption=False
        mot=""
        i=pos+1
        l=len(chaine)
        if chaine[i]=="{":#On ne tient pas compte des déterminatifs
                determinant=True
                while determinant:
                        i+=1
                        if chaine[i]=="}":
                                determinant=False
                                i+=1
        if chaine[i]=="_":#pour les sumérogrammes. On élimine simplement le signe indiquant que c'est un sumérogramme.
                i+=1
        while chaine[i]==" ":#élimine les espaces devant
                i+=1
        while (not interruption) and (i<l):
                if chaine[i] == " ":
                        interruption=True
                        continue
                mot=mot+chaine[i]
                i+=1
        return(mot)

def determinant_avant(pos, chaine):#trouve le déterminatif qui précède la position pos dans la chaine chaine.
        i=pos
        if not (chaine[i]=="}"):
                cont=True
                while cont:
                        i-=1
                        if i<0:
                                return("")
                        if (chaine[i]==" "):#Dans ce cas, on ne renvoie rien, car le mot qui contient la position pos n'admet pas de déterminatif le précédant.
                                return("")
                        if chaine[i]== "}":
                                cont=False
                #return("")
        det=""
        while (not chaine[i]=="{") and (i>-1):
                i-=1
                det=chaine[i]+det
        return(det[1:])

def determinant_apres(pos,chaine):#retourne le déterminatif qui succède au mot situé à la position pos.
        i=pos
        l=len(chaine)
        if not (chaine[i]=="{"):
                cont=True
                while cont:
                        i+=1
                        if i>l-1:
                                return("")
                        if (chaine[i]==" "):#Car cela veut dire qu'il n'y a pas de déterminatif accolé à la fin du mot considéré.
                                return("")
                        if chaine[i]=="{":
                                cont=False
                #return("")
        det=""

        while (not chaine[i]=="}") and (i<l):
                i+=1
                det=det+chaine[i]
        return(det)

#Analyse ce qui précède l'unité (sachant que pos se situe juste avant le début de l'unité) dans la ligne chaine. Le but étant de trouver les nombres précédant l'unité.
def analyse_avant(pos,chaine):
        fini=False#Permet de savoir si on a fini de parser tous les nombres précédant l'unité.
        position_courante=pos
        def isoler_mot(p,c):#isole le mot qui précède; p est une position et c une chaîne de caractère.
                interruption=False
                i=p
                mot=""
                if c[i]=="}":
                        determinant=True
                        while determinant:
                                i-=1
                                if c[i]=="{":
                                        determinant=False
                                        i-=1
                if c[i]=="_":
                        i-=1
                while c[i]==" ":
                        i-=1
                while (not interruption) and (i>-1):
                        if c[i]== " ":
                                interruption=True
                                continue
                        mot=c[i]+mot
                        i-=1
                return(i,mot)
        total=float(0)
        while (not fini) and position_courante>-1:
                position_courante,mot_courant=isoler_mot(position_courante,chaine)
                for cle,valeur in systeme_S.items():
                        m=re.search(r"(\S*)\("+cle+r"\)\W*",mot_courant) #On cherche si des mots-clefs du système S
                        if m:
                                nombre=m.group(1)
                                fraction=re.search(r"(\D*)([0-9]*)/([0-9]*)",nombre)
                                if fraction:
                                        total+=float(fraction.group(2))/float(fraction.group(3))*valeur
                                else:
                                        entier=re.search(r"(\D*)([0-9]*)",nombre)
                                        if entier:
                                                total+=float(entier.group(2))*valeur
                        else:
                                fini=True
        return(total)

#Et pour regarder un mot qui précède strictement pos dans la chaine chaine:
def mot_davant(pos,chaine):
        #ce qui suit permet de trouver le mot juste précédant
        interruption=False
        mot=""
        i=pos
        #On élimine les déterminants, etc.:
        if chaine[i]=="}":
                determinant=True
                while determinant:
                        i-=1
                        if chaine[i]=="{":
                                determinant=False
                                i-=1
        if chaine[i]=="_":#ce caractère indique que l'on a une partie en sumérien
                i-=1
        while chaine[i]==" ":
                i-=1
        while (not interruption) and (i>-1):
                if chaine[i]==" ":
                        interruption=True
                        continue
                mot=chaine[i]+mot
                i-=1
        return(mot)

def beginning_recipe(ligne):#Controle si on est au début d'un nouveau texte
        if ligne==[]:
                return(False)
        if ligne[0]=="&":
                return(True)
        return(False)

#Fonction pour voir si une nouvelle recette commence dans la ligne l. On revoit toutes les sous-listes, ainsi que le nombre d'occurences.
def nouvelle_recette(l):
        debut_recette=expr_reg_debut_recipe
        a=re.split(debut_recette,l.lower())
        return(len(a)-1,a)

#Initialisation workbook Excel
workbook=Workbook()
sheet = workbook.active #First sheet
sheet.title="Raw data"

#Pour donner des informations sur l'encodage
sh_metadata = workbook.create_sheet("CDLI Metadata",1)

Nombre_unites=[0 for i in range(len(Unites_de_mesure))]#nombre de fois où l'unité est employé dans une recette

#Pour analyser les mesures plus précisément
sh_gin=[]
for i in range(len(Unites_de_mesure)):
        sh_gin.append(workbook.create_sheet("Utilisation "+Unites_de_mesure[i],2+i))
        sh_gin[i]["A1"].value="Texte"
        sh_gin[i]["B1"].value="Rang dans le texte"
        sh_gin[i]["C1"].value="Mot précédent l'unité"
        sh_gin[i]["F1"].value="Mot suivant l'unité"
        sh_gin[i]["D1"].value="Déterminatif précédant l'unité"
        sh_gin[i]["E1"].value="Déterminatif suivant l'unité"
        sh_gin[i]["G1"].value="Rang de la recette dans le texte"
        sh_gin[i]["H1"].value="Quantité"

#Lecture du fichier cdli texte
chemin_data=os.path.join(os.path.dirname(os.path.abspath(__file__)),"Data")#chemin vers les données.
fichier_cdli=open(os.path.join(chemin_data,"cdli_subgenre-medical_allperiod.txt"),"r")
Liste_lignes_cdli=fichier_cdli.readlines()
fichier_cdli.close()

#Pour compter le numéro du texte et savoir où on en est dans le fichier Excel
numero_texte=0

#Première ligne
sheet["A1"].value="Numéro de texte"
dd=len(Unites_de_mesure)
for i in range(dd):
        sheet.cell(row=1,column=i+2).value=Unites_de_mesure[i]
sheet.cell(row=1,column=dd+2).value="Traduction?"#pour savoir si une traduction est disponible
sheet.cell(row=1,column=dd+3).value="Langue"
sheet.cell(row=1,column=dd+4).value="Epoque"
sheet.cell(row=1,column=dd+5).value="Provenance"
sheet.cell(row=1,column=dd+6).value="Nombre de recettes probablement contenues"#évalue le nombre de recettes contenues grâce à la fonction nouvelle_recette

#fonction d'écriture des données d'une ligne dans Excel
def ecrire_ligne(numero_ligne,Nb_unites, est_trad,language,nb_rec):
        d=len(Unites_de_mesure)
        for i in range(d):
                sheet.cell(row=numero_ligne,column=i+2).value=Nb_unites[i]
        if est_trad:
                sheet.cell(row=numero_ligne,column=d+2).value= "X"
        if not language=="":
                sheet.cell(row=numero_ligne,column=d+3).value=language
        sheet.cell(row=numero_ligne,column=d+6).value=nb_rec

#Pour regarder les mots précédant ou suivant les unités de mesure
#C'est une liste telle que Mots_precedants_suivants[i] contienne une liste de toutes les occurences de la mesure i, et donc Mots_precedants_suivants[i][j] est une liste à 7 éléments décrivant la jème occurence de l'unité i
#Cette liste à 7 éléments contient, dans l'ordre: la référence du texte, le mot qui précède, le déterminant qui rpécède, le déterminant qui succède, le mot qui succède, le numéro de recette
Mots_precedants_suivants=[[] for i in range(len(Unites_de_mesure))]

#Pour stocker les quantités
#Quantites_unites=[[] for i in range(len(Unites_de_mesure))]

est_traduit=False # dit si le texte dispose d'une traduction
langue=""#langue si indiquée

#Pour voir les langues différentes référencées
Liste_langues=[]

for ligne_texte in Liste_lignes_cdli:
        if beginning_recipe(ligne_texte):
                nom=ligne_texte[1:]#Référence du texte

                #On finit d'écrire ce qui est relatif au texte précédent
                if numero_texte>0:
#                        for i in range(len(Unites_de_mesure)):
#                                sheet.cell(row=numero_texte+1,column=i+2).value=Nombre_unites[i]
                        ecrire_ligne(numero_texte+1, Nombre_unites,est_traduit, langue,nombre_recette_dans_texte)

                nombre_recette_dans_texte=0#on écrit le nombre de recettes contenues dans ce texte
                numero_texte+=1
                sheet["A"+str(numero_texte+1)].value = nom#On met le nom du nouveau texte
                Nombre_unites=[0 for i in range(len(Unites_de_mesure))]#on remet à zéro
                est_traduit=False
                langue=""
                if numero_texte%10==0:
                        print(numero_texte)#Permet simplement de se rendre compte que le programme travaille bien quand on l'execute.

        #On regarde ce qui se passe pour des lignes particulières
        if ligne_texte[0]=="#":
        #Si la tablette est traduite
                if not re.search("#tr.en|#tr-en",ligne_texte.lower())==None:
                        est_traduit=True
                        continue

        #La langue
                if not re.search("#atf: lang",ligne_texte.lower())==None:
                        langue=prochain_mot(9,ligne_texte)
                        if not langue in Liste_langues:
                                Liste_langues.append(langue)

        #On regarde si une nouvelle recette commence:
        nbr,liste_sous_lignes=nouvelle_recette(ligne_texte)
        nombre_recette_dans_texte+=nbr

        for riga in liste_sous_lignes:
                #on itère sur la ligne pour y trouver les ingrédients
                for i in range(len(Unites_de_mesure)):
                        for match in re.finditer("( |_|\})"+Unites_de_mesure[i]+"( |_|#|\{|\?|\*|\!)",riga.lower()):
                                Nombre_unites[i]+=1
                                mot_suivant=prochain_mot(match.end()-1,riga)
                                det_precedant=determinant_avant(match.end()-1,riga)
                                det_suivant=determinant_apres(match.start(),riga)
                                mot_precedant=mot_davant(match.start(),riga)
                                q=analyse_avant(match.start(),riga)
                                Mots_precedants_suivants[i].append([nom,mot_precedant,det_precedant,det_suivant,mot_suivant,nombre_recette_dans_texte,q])

#On s'assure que la dernière ligne soit écrite:
ecrire_ligne(numero_texte+1, Nombre_unites,est_traduit,langue,nombre_recette_dans_texte)
##for i in range(len(Unites_de_mesure)):
##        sheet.cell(row=numero_texte+1,column=i+2).value=Nombre_unites[i]

#On veut regarder tous les mots clefs disponibles après #
sh_metadata["A1"].value="Informations données par #"

sh_metadata["B1"].value="Numéro du premier texte d'apparition"
Liste_possibles=[]
i=0#pour la ligne d'Excel
j=0#pour le numéro de texte
for l in Liste_lignes_cdli:
        if l==[]:
                continue
        if beginning_recipe(l):
                j+=1
        if l[0]=="#":
                m=prochain_mot(0,l)#premier mot après #
                if not m in Liste_possibles:
                        Liste_possibles.append(m)
                        i+=1
                        sh_metadata["A"+str(i+1)].value=m
                        sh_metadata["B"+str(i+1)].value=j
                        print(i)#histoire de savoir où on en est

#On ajoute les langues
sh_metadata["A"+str(i+3)].value="Langues différentes référencées"
for k in range(len(Liste_langues)):
        sh_metadata["A"+str(i+4+k)].value=Liste_langues[k]
        print(k)#Pour savoir où on en est

#On rajoute les mots suivant, précédant, etc gin2

for k in range(len(Unites_de_mesure)):
        i=0#pour compter les lignes de la worksheet
        j=0#pour compter les occurences dans un texte
        premier_texte=Mots_precedants_suivants[1][0][0]
        for occurence in Mots_precedants_suivants[k]:
                i+=1
                j+=1
                sh_gin[k]["A"+str(i+2)].value=occurence[0]
                if not (occurence[0]==premier_texte):
                        premier_texte=occurence[0]
                        j=1
                sh_gin[k]["B"+str(i+2)].value=j
                sh_gin[k]["C"+str(i+2)].value=occurence[1]
                sh_gin[k]["D"+str(i+2)].value=occurence[2]
                sh_gin[k]["E"+str(i+2)].value=occurence[3]
                sh_gin[k]["F"+str(i+2)].value=occurence[4]
                sh_gin[k]["G"+str(i+2)].value=occurence[5]
                if not occurence[6]==0:
                        sh_gin[k]["H"+str(i+2)].value=occurence[6]

#On cherche à présent à attribuer une période aux textes
print("Attribution de la période")
#Liste des fichiers
Fichiers=["neo-assyr.txt","Neo-bab.txt","Achaemenid.txt","Hellenistic.txt","Middle Assyrian.txt","Middle Babylonian.txt", "Old Babylonian.txt", "ED IIIb.txt"]
#Liste des noms d'époques
NomsEpoques=["Neo-Assyrian (ca. 911-612 BC)","Neo-Babylonian (ca. 626-539 BC)","Achaemenid (547-331 BC)","Hellenistic (323-63 BC)","Middle Assyrian (ca. 1400-1000 BC)","Middle Babylonian (ca. 1400-1100 BC)","Old Babylonian (ca. 1900-1600 BC)","ED IIIb (ca. 2500-2340 BC)"]

#On parcourt les différents fichiers. L'idée est qu'on regarde, en parcourant le fichier associé à une époque donnée, les numéros de texte trouvés et on cherche ce numéro dans le fichier Excel.
indice=0
for nom_fichier in Fichiers:
        print(nom_fichier)

        fichier_cdli = open(os.path.join(chemin_data,"Data chronologie",nom_fichier),"r")
        Liste_lignes = fichier_cdli.readlines()
        fichier_cdli.close()

        k=0
        epoque=NomsEpoques[indice]
        marqueur=1
        for ligne in Liste_lignes:
                if k%1000==0:
                        print("*ligne ",k," dans le document txt")#pour s'assurer à execution que le programme fonctionne bien.
                if ligne==[]:
                        continue
                if beginning_recipe(ligne):
                        nom=ligne[1:]
                        for i in range(1,sheet.max_row+1):
                                #if i%100==0:
                                #       print("*      Ligne ",i," dans le fichier XSL")#pour s'assurer que le programme fonctionne.
                                if nom==sheet["A"+str(i)].value:
                                        if not sheet.cell(row=i,column=dd+4).value==None:
                                               m=sheet.cell(row=i,column=dd+4).value
                                               sheet.cell(row=i,column=dd+4).value=m+";"+epoque
                                        else:
                                                sheet.cell(row=i,column=dd+4).value=epoque
                                        marqueur=i
                                        break
                k=k+1
        indice=indice+1
        print(nom_fichier)

#On cherche à présent à attribuer une provenance. On fait comme avec l'époque.
print("Attribution de la période")
#Liste des fichiers
Fichiers_prov=["assur.txt","babylon.txt","Nineveh (Kuyunjik).txt","Nippur.txt","sippar-yahrurum.txt","Uruk.txt"]
Nom_prov=["Assur (mod. Qalat Sherqat)","Bābili (mod. Babylon)","Nineveh (mod. Kuyunjik)","Nippur (mod. Nuffar)","Sippar-Yahrurum (mod. Tell Abu Habbah)","Uruk (mod. Warka)"]

indice=0
for nom_fichier in Fichiers_prov:
        print(nom_fichier)

        fichier_cdli = open(os.path.join(chemin_data,"Data provenance",nom_fichier),"r")
        Liste_lignes = fichier_cdli.readlines()
        fichier_cdli.close()

        k=0
        provenance=Nom_prov[indice]
        marqueur=1
        for ligne in Liste_lignes:
                if k%1000==0:
                        print("*ligne ",k," dans le document txt")
                if ligne==[]:
                        continue
                if beginning_recipe(ligne):
                        nom=ligne[1:]
                        for i in range(1,sheet.max_row+1):
                                #if i%100==0:
                                #       print("*      Ligne ",i," dans le fichier XSL")
                                if nom==sheet["A"+str(i)].value:
                                        if not sheet.cell(row=i,column=dd+5).value==None:
                                               m=sheet.cell(row=i,column=dd+5).value
                                               sheet.cell(row=i,column=dd+5).value=m+";"+provenance
                                        else:
                                                sheet.cell(row=i,column=dd+5).value=provenance
                                        marqueur=i
                                        break
                k=k+1
        indice=indice+1
        print(nom_fichier)

workbook.save(filename="cdli_analysis.xlsx")


# -*- coding: utf-8 -*-
__author__ = 'Yoon Ho Han, Kevin Zhang'
__email__ = 'yhhan@ucsd.edu, ktzhang@ucsd.edu'

from collections import deque

def ac3(csp, arcs=None):
    """Executes the AC3 or the MAC (p.218 of the textbook) algorithms.

    If the parameter 'arcs' is None, then this method executes AC3 - that is, it will check the arc consistency
    for all arcs in the CSP.  Otherwise, this method starts with only the arcs present in the 'arcs' parameter
    in the queue.

    Note that the current domain of each variable can be retrieved by 'variable.domains'.

    This method returns True if the arc consistency check succeeds, and False otherwise."""

    queue_arcs = deque(arcs if arcs is not None else csp.constraints.arcs())

    # loop through the arcs
    while (queue_arcs):
        arc = queue_arcs.popleft();
        # check if arc needs to be revised
        if (revise(csp, arc[0], arc[1])):
            # if no more in domain, arc consistency fails
            if (len(arc[0].domain) == 0):
                return False;
            else :
                # loop through all arcs with arc[0]
                for neighbor in csp.constraints[arc[0]].arcs():
                    # skip if neighbor is the same arc
                    if (neighbor[0] == arc[0] or neighbor[1] == arc[0]):
                        continue;
                    # add arc to queue if current arc has been revised
                    queue_arcs.append(neighbor);
    return True;

def revise(csp, xi, xj):
    revised = False;
    # loop through domain of xi
    for xVal in xi.domain:
        found = False;
        # loop through domain of xj
        for yVal in xj.domain:
            # check if domain for xj satisfies constraints
            satisfied = True;
            for constraint in csp.constraints[xi, xj]:
                if (not constraint.is_satisfied(xVal, yVal)):
                    # satisfied is false if value of xj doesn't satisfy any of the constraints
                    satisfied = False;
                    break;
            # if any value of domain for xj is consistent, don't have to check any other values
            if (satisfied):
                found = True;
                break;
        # none of the domain of xj satisfies constraint with xi
        if (not found):
            xi.domain.remove(xVal);
            revised = True;
    return revised;


# sort - сортировка
# pop - видалити по індексу
# remove - видалити по значенню
# append - dodaty
# extend - добавити в список
# insert - вставляємо на (місце, аргумент)
l = []
for i in range(1, 11):
    if i % 2 == 0:
        l.append(i)

print(l)

l = [i for i in range(2, 11, 2)]
print(l)
l2 = [i if not i%2 else 0 for i in range(1, 11)]
print(l2)
l3 = [0 if i%2  else 1 for i in range(10)]
print(l3)

l3 = [1 if i%2 != 0 else 0 for i in range(10)]
print(l3)

l4 = [i for i in range(10, 0, -1)]
print(l4)

s = 'Hello World!'
l5 = [i for i in s if i not in 'aeoyuiAEOYUI']
print(l5)
l7 = [i for i in s if i in ['a', 'e', 'o', 'y', 'u', 'i']]
l6 = [i for i in s if i in 'a e o y u i A E O Y U I'.split(' ')]
print(l6)


from numpy import *
from scipy import interpolate
from spivet import pivlib, tlclib, spivetconf
from spivet.steputil import _parsefn, _fretrieve
import sys, os, urlparse
from spivet import compat

def assemble_epoch(accobpath):
    """
    Assembles the individual PIVDATA files that have been accumulated
    in accobpath into one PIVData object.  Each PIVDATA file is
    expected to hole a single Epoch.  Returns the object.
    """
    # Find out how many Epochs need to be processed.
    dl = os.listdir(accobpath)
    dl.sort()
    cp = 0
    for f in range( len(dl) ):
        if ( not dl[cp].startswith('PIVDATA') ):
            dl.pop(cp)
        else:
            cp = cp +1

    # Expects file names to have the form
    #     PIVDATA-E%i.ex2
    _cmprule = lambda x,y: cmp( int(x[0:-4].split('-')[1][1::]),
                                int(y[0:-4].split('-')[1][1::]) )
    dl.sort(_cmprule)

    # Assemble the results.
    for i in range(len(dl)):
        pd = pivlib.loadpivdata( "%s/%s" % (accobpath,dl[i]) )

        if ( i == 0 ):
            epd = pivlib.PIVData(pd.cellsz,pd.origin,pd.desc)

        epd.append(pd[0])
    epd.save("PIVDATA.ex2")
    return epd

accobpath = "./OUTPUT/FG0"
assemble_epoch(accobpath)


#!/usr/bin/python
import sys

"""
Dette script er lavet til et af mine hobby-projekter; nand2tetris. Det er et kursus der er lavet for at give et simpelt overblik
af hvordan en computer er bygget fra bunden og op. Her har jeg lavet en assembler der tager imod simpel assemblykode og omdanner det
til binær maskinkode som den simple computer jeg har bygget (virtuelt) kan forstå. Når jeg nu skal vise dette script bliver jeg konfronteret
med hvor dårlig jeg har været til at skrive kommentarer til den. Jeg er meget opmærksom på at ordentlige kommentarer er med til at gøre
ens kode forståeligt for andre men eftersom jeg kun har skrevet dette script for min egen skyld har jeg været lidt for afslappet omkring det.

Helt grundlæggende tager den imod en tekstfil og outputter en .hack fil (filformatet til førnævnte computer!). Først gennemgår den tekstfilen
for symboler der endnu ikke er i symbol-ordbogen.Hvis de ikke er i symbol-ordbogen bliver de tilføjet og får en ubrugt adresse i hack-computerens
hukommelse. Derefter gennemgår den scriptet og omdanner hver linje til en binær kode primært ved at matche linjens indhold med de forskellige ord-
bøger der indeholder de rigtige binære koder!

I projektet nand2tetris foreskriver de egentlig en anden (mere kompliceret) måde at bygge assembleren på. Her har jeg taget en genvej ved at udnytte
pythons måde at fungere på. Får jeg ekstra tid en dag kunne jeg godt tænke mig at bygge assembleren efter deres specifikationer, for at få et bredere
perspektiv på forskellige programmerings-praksisser og -sprog.
"""

script, assemblycode = sys.argv
filename = assemblycode.split(".")[0] + ".hack"

compdict = {"0": "0101010", "1": "0111111", "-1": "0111010", "D": "0001100", "A": "0110000",
            "!D": "0001101", "!A": "0110001", "-D": "0001111", "-A": "0110011", "D+1": "0011111",
            "A+1": "0110111", "D-1": "0001110", "A-1": "0110010", "D+A": "0000010", "D-A": "0010011",
            "A-D": "0000111", "D&A":"0000000", "D|A": "0010101", "M":"1110000", "!M": "1110001",
            "-M": "1110011", "M+1": "1110111", "M-1": "1110010", "D+M": "1000010", "D-M": "1010011",
            "M-D": "1000111", "D&M": "1000000", "D|M": "1010101"}
destdict = {"M": "001", "D": "010", "MD": "011", "A": "100", "AM": "101", "AD": "110", "AMD": "111"}
jumpdict = {"JGT": "001", "JEQ": "010", "JGE": "011", "JLT": "100", "JNE": "101", "JLE": "110", "JMP": "111"}
symboldict = {"SP": "0000000000000000", "LCL": "0000000000000001", "ARG": "0000000000000010",
              "THIS": "0000000000000011", "THAT": "0000000000000100", "R0": "0000000000000000",
              "R1": "0000000000000001", "R2": "0000000000000010", "R3": "0000000000000011",
              "R4": "0000000000000100", "R5": "0000000000000101", "R6": "0000000000000110",
              "R7": "0000000000000111", "R8": "0000000000001000", "R9": "0000000000001001",
              "R10": "0000000000001010", "R11": "0000000000001011", "R12": "0000000000001100",
              "R13": "0000000000001101", "R14": "0000000000001110", "R15": "0000000000001111",
              "SCREEN": "0100000000000000", "KBD": "0110000000000000"}

with open(assemblycode, "r") as f:
    romcounter = 0
    for line in f:  # TODO check that this parser actually works!
        line = line.strip()
        line = line.rstrip("\r\n")
        if len(line) < 1:
            continue
        if line[0:2] == "//":
            continue
        if "//" in line:
            line = line.split("//")[0].rstrip()
        first = line[0]
        if first == "@" or first == "D" or first == "M" or first == "A" or first == "0":
            romcounter += 1
        if line[0] == "(":
            symboldict[line.strip("()")] = "{:016b}".format(romcounter)

with open(assemblycode, "r") as f, open(filename, "w") as f2:
    counter = 16
    for line in f:
        line = line.strip()
        line = line.rsplit("//")[0]
        line = line.rstrip("\r\n")
        if len(line) < 1:
            continue
        if line[0:2] != "//":
            if "//" in line:
                line = line.split("//")[0].rstrip("\r\n ")
        if line[0] == "/":
            continue
        elif line[0] == "\r":
            continue
        elif "@" in line:
            line = (line.split("//")[0].rstrip())
            line = line.strip("@")
            if line[0].isdigit():
                f2.write("0" + "{:015b}".format(int(line)) + "\r\n")
            else:
                try:
                    f2.write(symboldict[line] + "\r\n")
                except KeyError:
                    number = "0" + "{:015b}".format(counter)
                    symboldict[line] = number
                    counter += 1
                    f2.write(symboldict[line] + "\r\n")
        elif "(" in line:
            continue
        else:
            line = str(line.split("//")[0].rstrip("\r\n"))
            line = line.strip()
            if ";" in line:
                jump = jumpdict[line[-3:]]
                line = line[:-4]
            else:
                jump = "000"
            if "=" in line:
                destination = line.split("=")[0]
                dest = destdict[str(destination)]
                line = line.split("=")[1]
            else:
                dest = "000"
            compbin = compdict[line]
            f2.write("111" + compbin + dest + jump + "\r\n")


'''209. Minimum Size Subarray Sum
Medium

Given an array of positive integers nums and a positive integer target, return the minimal length of a
subarray
whose sum is greater than or equal to target. If there is no such subarray, return 0 instead.

Example 1:

Input: target = 7, nums = [2,3,1,2,4,3]
Output: 2
Explanation: The subarray [4,3] has the minimal length under the problem constraint.

Example 2:

Input: target = 4, nums = [1,4,4]
Output: 1

Example 3:

Input: target = 11, nums = [1,1,1,1,1,1,1,1]
Output: 0

Constraints:

    1 <= target <= 109
    1 <= nums.length <= 105
    1 <= nums[i] <= 104
'''

def minSubArrayLen(target,nums):
    l=0 #initialize left pointer
    total=0
    res =float("inf") #this is infinity ,to set a number as infinity we use this
    subarray=[] #for my purpose to see which subarray is the least i used this empty sub array
    for r in range(len(nums)): # r in for loop is right pointer iterating through each number
        total=total+nums[r] #we start from left only but we dont consider left pointer because loop running from start only so no issue
        while total>=target: #use while instead of if, bcos in final it is not considering minimum subarray,also while loop run only once because we write a code inside while loop which reduces total in 3rd line
            res=min((r-l)+1,res) #this is for from which left pointer to right pointer nos the target has been acheived
            subarray=nums[l:r+1] #for personal to visualize which subarray is moving and adding up
            total=total-nums[l] #to move to 2nd number we minus the 1st value from total
            l+=1 #move the left pointer
    print(subarray)
    return 0 if res==float("inf") else res

target = 7
nums = [2,3,1,2,4,3]
a=minSubArrayLen(target,nums)
print(a)
print()
target = 4
nums = [1,4,4]
a=minSubArrayLen(target,nums)
print(a)
print()
target = 11
nums = [1,1,1,1,1,1,1,1]
a=minSubArrayLen(target,nums)
print(a)

Sliding Window-209.Minimum Size Subarray Sum -Medium.py


import logging
import os

import config

def setup_logger(logger_name, log_file, level=logging.INFO):
    """
    Factory method for creating loggers
    :param logger_name:
    :param log_file:
    :param level:
    :return:
    """
    logger = logging.getLogger(logger_name)
    formatter = logging.Formatter(u'%(asctime)s %(message)s')
    fileHandler = logging.FileHandler(log_file, mode='a')
    fileHandler.setFormatter(formatter)
    streamHandler = logging.StreamHandler()
    streamHandler.setFormatter(formatter)

    logger.setLevel(level)
    logger.addHandler(fileHandler)
    # logger.addHandler(streamHandler)

    return logger

if not os.path.exists(config.data_dir):
    os.makedirs(config.data_dir)

error_log_path = os.path.join(config.data_dir, f'{config.bot_info["channel"]}_error-log.txt')
event_log_path = os.path.join(config.data_dir, f'{config.bot_info["channel"]}_event-log.txt')

error_logger = setup_logger('error_logger', error_log_path, level=logging.WARNING)
event_logger = setup_logger('event_logger', event_log_path, level=logging.INFO)


import asyncio
from functools import wraps
from logging import getLogger
from time import perf_counter
from urllib.parse import urljoin

import httpx
from httpx import StatusCode, HTTPError

from api_composer import settings
from api_composer.hawk import HawkAuth

PAGE_SIZE = 1000
MAX_RESULTS = 10000
NUM_PAGES = MAX_RESULTS // PAGE_SIZE
TIMEOUT_SECS = 10

logger = getLogger(__name__)

def async_trace(func):
    @wraps(func)
    async def wrapper(*args, **kwargs):
        start_ts = perf_counter()

        ret = await func(*args, **kwargs)

        end_ts = perf_counter()
        logger.info(
            f'{func.__name__}() args={args} kwargs={kwargs} executed in {end_ts-start_ts:.3f}s',
        )
        return ret

    return wrapper

@async_trace
async def get_composed_companies(market_of_interest):
    dh_companies, ds_companies = await asyncio.gather(
        _get_data_hub_companies(NUM_PAGES),
        _get_data_science_companies(NUM_PAGES, market_of_interest),
    )

    ds_company_mapping = {
        company['datahub_company_id']: company
        for company in ds_companies
        if company['datahub_company_id']
    }

    filtered_dh_companies = [
        {
            'data_hub': company,
            'data_science': ds_company_mapping[company['id']],
        }
        for company in dh_companies
        if company['id'] in ds_company_mapping
    ]

    return {
        'results': filtered_dh_companies,
        'counts': {
            'data_hub_results_fetched': len(dh_companies),
            'data_science_results_fetched': len(ds_companies),
            'data_science_results_fetched_with_data_hub_ids': len(ds_company_mapping),
        },
    }

@async_trace
async def _get_data_hub_companies(num_pages):
    pages = await asyncio.gather(
        *(_get_data_hub_companies_page(page_num) for page_num in range(num_pages)),
    )
    return [
        result
        for page in pages
        for result in page['results']
    ]

@async_trace
async def _get_data_science_companies(num_pages, market_of_interest):
    pages = await asyncio.gather(
        *(
            _get_data_science_companies_page(page_num, market_of_interest)
            for page_num in range(num_pages)
        ),
    )
    return [
        result
        for page in pages
        for result in page['result']
    ]

@async_trace
async def _get_data_hub_companies_page(page_num):
    url = urljoin(str(settings.DATA_HUB_API_URL), '/v4/public/search/company')
    auth = HawkAuth(
        settings.DATA_HUB_API_HAWK_ID,
        str(settings.DATA_HUB_API_HAWK_KEY),
        verify_response=False,
    )
    response = await httpx.post(
        url,
        auth=auth,
        json={
            'original_query': '',
            'offset': PAGE_SIZE * page_num,
            'limit': PAGE_SIZE,
        },
        timeout=TIMEOUT_SECS,
    )
    response.raise_for_status()
    return response.json()

@async_trace
async def _get_data_science_companies_page(page_num, market_of_interest):
    url = urljoin(str(settings.DATA_SCIENCE_API_URL), '/api/v1/company/search/')
    auth = HawkAuth(
        settings.DATA_SCIENCE_API_HAWK_ID,
        str(settings.DATA_SCIENCE_API_HAWK_KEY),
        verify_response=False,
    )

    body = {
        'filters': {
            'market_of_interest': [market_of_interest]
        },
    } if market_of_interest else {}

    try:
        response = await httpx.post(
            url,
            auth=auth,
            params={
                'offset': PAGE_SIZE * page_num,
                'limit': PAGE_SIZE,
            },
            json=body,
            timeout=TIMEOUT_SECS,
        )
        response.raise_for_status()
    except HTTPError as exc:
        if exc.response and exc.response.status_code == StatusCode.NOT_FOUND:
            return {
                'result': [],
            }
        raise

    return response.json()


"""
preprocess input data into feature and stores binary as python shelve DB
each chunk is gzipped JSON string
"""
import argparse
import gzip
import json
import subprocess as sp
import shelve
import os
from os.path import dirname, exists, join

import torch
from pytorch_pretrained_bert import GPT2Tokenizer
from tqdm import tqdm

from env import END_OF_TEXT_TOKEN

class InputFeatures(object):
    def __init__(self, conv_id, input_ids, position_ids, token_type_ids,
                 lm_labels, attn_masks, weights, input_len=None):
        self.conv_id = conv_id
        self.input_ids = input_ids
        self.position_ids = position_ids
        self.token_type_ids = token_type_ids
        self.lm_labels = lm_labels
        self.attn_masks = attn_masks
        self.weights = weights
        if input_len is None:
            self.input_len = len(input_ids)
        else:
            self.input_len = input_len

def _get_file_len(corpus):
    print(f"wc -l {corpus}")
    n_line = int(sp.check_output(f"wc -l {corpus}".split(),
                                 universal_newlines=True).split()[0])
    return n_line

def _norm_text(text):
    toks = text.strip().split()
    w = 1.0
    return w, ' '.join(toks)

def _get_inputs_from_text(text, tokenizer):
    srcs, tgt, attn_masks, types, positions = text.strip().split('\t')

    weights = []
    inputs = []
    for src in srcs.split(' EOS '):
        context_id = [int(num) for num in src.strip().split()]
        weights.append(1.0)
        inputs.append(context_id)
    response_id = [int(num) for num in tgt.strip().split()]
    weights.append(1.0)
    inputs.append(response_id)

    attn_masks = [[int(c) for c in seq] for seq in attn_masks.split(',')]
    position_ids = [int(num) for num in positions.strip().split()]
    type_ids = [int(num) for num in types.strip().split()]
    return weights, inputs, attn_masks, position_ids, type_ids

def _make_features(id_, weights, inputs, attn_masks, position_ids, type_ids, tokenizer, max_len):
    end_of_text_id = tokenizer.encoder[END_OF_TEXT_TOKEN]
    features = []
    sents = []
    ws = []
    len_ = 0
    i = 0
    for ids, w in zip(inputs, weights):
        sents.append(ids)
        ws.append(w)
    assert len(sents) == 2
    feat = _make_feature(id_, sents, ws, attn_masks, position_ids, type_ids, end_of_text_id)
    if feat is not None:
        features.append(feat)

    return features

def _make_feature(id_, sents, ws, attn_masks, position_ids, token_type_ids, eos):
    if all(w == 0 for w in ws[1:]):
        return None
    input_ids = [i for s in sents for i in s+[eos]][:-1]
    lm_labels = []
    weights = []
    #token_type_ids = []  # this becomes round ids
    for i, (s, w) in enumerate(zip(sents, ws)):
        if i == 0:
            lm_labels += [-1] * len(s)
            weights += [0.0] * len(s)
            continue

        if w == 0.0:
            lm_labels += [-1] * (len(s) + 1)
            weights += [0.0] * (len(s) + 1)
        else:
            lm_labels += (s + [eos])
            weights += [w] * (len(s) + 1)

    # handle trailing -1's
    i = len(lm_labels) - 1
    while i >= 0:
        if lm_labels[i] != -1:
            break
        i -= 1
    input_ids = input_ids[:i+1]
    lm_labels = lm_labels[:i+1]
    weights = weights[:i+1]
    token_type_ids = token_type_ids[:i+1]
    attn_masks = attn_masks[:i+1]
    position_ids = position_ids[:i+1]
    token_type_ids = token_type_ids[:i+1]

    # pad to multiples of 8
    while len(input_ids) % 8 != 0:
        input_ids.append(0)
        token_type_ids.append(0)
        lm_labels.append(-1)
        weights.append(0.0)
        attn_masks.append([0]*(len(attn_masks)+1))
        position_ids.append(0)

    assert (len(input_ids) == len(position_ids) == len(token_type_ids)
            == len(lm_labels) == len(weights) == len(attn_masks))
    assert len(input_ids) % 8 == 0
    if len(input_ids) == 0:
        import ipdb
        ipdb.set_trace()
    feature = InputFeatures(id_, input_ids, position_ids, token_type_ids,
                            lm_labels, attn_masks, weights)
    return feature

def main(args):
    # TODO specify vocab path to avoid download
    if args.tokenizer is not None:
        toker = GPT2Tokenizer.from_pretrained(args.tokenizer)
    else:
        toker = GPT2Tokenizer.from_pretrained('gpt2')
    assert args.corpus.endswith('.txt') or args.corpus.endswith('.tsv')
    db_path = f'{args.corpus[:-4]}.db/db'
    if exists(dirname(db_path)):
        raise ValueError('Found existing DB, please backup')
    else:
        os.makedirs(dirname(db_path))
    with open(args.corpus, "r", encoding="utf-8") as reader, \
            shelve.open(db_path, 'n') as db:
        chunk = []
        n_chunk = 0
        n_example = 0
        for line in tqdm(reader, total=_get_file_len(args.corpus)):
            try:
                if len(chunk) >= args.chunk_size:
                    # save and renew chunk
                    db[f'chunk_{n_chunk}'] = gzip.compress(
                        json.dumps(chunk[:args.chunk_size]).encode('utf-8'))
                    chunk = chunk[args.chunk_size:]
                    n_chunk += 1

                weights, inputs, attn_masks, position_ids, type_ids = _get_inputs_from_text(line, toker)
                if len(weights) < 2:
                    continue
                features = _make_features(n_example, weights, inputs, attn_masks, position_ids, type_ids, toker, args.max_seq_len)
                for feature in features:
                    chunk.append(vars(feature))
                    n_example += 1
            except Exception as e:
                raise e

        # save last chunk
        db[f'chunk_{n_chunk}'] = gzip.compress(
            json.dumps(chunk).encode('utf-8'))
    # save relevant information to reproduce
    meta = {'n_example': n_example,
            'chunk_size': args.chunk_size,
            'max_seq_len': args.max_seq_len}
    with open(join(dirname(db_path), 'meta.json'), 'w') as writer:
        json.dump(meta, writer, indent=4)
    torch.save(toker, join(dirname(db_path), 'tokenizer.pt'))

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--corpus', required=True,
                        help='file name of training corpus (should be .tsv)')
    parser.add_argument('--chunk_size', type=int, default=65536,
                        help='num of data examples in a storing chunk')
    parser.add_argument('--max_seq_len', type=int, default=512,
                        help='discard data longer than this')
    parser.add_argument('--tokenizer', help='pretrained tokenizer path')

    args = parser.parse_args()

    main(args)