import os
import torch
import torch.nn as nn
from torch.nn import init
from sklearn.metrics import f1_score
from torch.utils.data import Dataset, DataLoader

START_TAG = "<START>"
END_TAG = "<STOP>"
PAD = "<PAD>"
UNK = "<UNK>"

def build_corpus(split, make_vocab=True, data_dir="data"):
    assert split in ['train', 'dev', 'test']

    word_lists = []
    tag_lists = []
    with open(os.path.join(data_dir,split+".char.bmes"),"r", encoding="utf-8") as f:
        word_list = []
        tag_list = []
        for line in f:
            if line != '\n':
                word, tag = line.strip('\n').split()
                word_list.append(word)
                tag_list.append(tag)
            else:
                word_lists.append(word_list)
                tag_lists.append(tag_list)
                word_list = []
                tag_list = []

    word_lists = sorted(word_lists, key=lambda x:len(x),reverse= True)
    tag_lists = sorted(tag_lists,key= lambda x:len(x),reverse=True)

    # 如果make_vocab为True，还需要返回word2id和tag2id
    if make_vocab:
        word2id = build_map(word_lists)
        tag2id = build_map(tag_lists)
        word2id['<UNK>'] = len(word2id)
        word2id['<PAD>'] = len(word2id)

        tag2id["<START>"] = len(tag2id)
        tag2id['<PAD>'] = len(tag2id)
        tag2id["<STOP>"] = len(tag2id)
        return word_lists, tag_lists, word2id, tag2id
    else:
        return word_lists, tag_lists

def build_map(lists):
    maps = {}
    for list_ in lists:
        for e in list_:
            if e not in maps:
                maps[e] = len(maps)
    return maps

class MyDataset(Dataset):
    def __init__(self,datas, tags, word_2_index, tag_2_index):
        self.datas = datas
        self.tags = tags
        self.word_2_index = word_2_index
        self.tag_2_index = tag_2_index

    def __getitem__(self, index):
        data = self.datas[index]
        tag = self.tags[index]

        data_index = [self.word_2_index.get(i,self.word_2_index["<UNK>"]) for i in data ]
        tag_index = [self.tag_2_index.get(i) for i in tag]

        return data_index, tag_index

    def __len__(self):
        assert len(self.datas) == len(self.tags)
        return len(self.datas)

    def batch_data_pro(self,batch_datas):
        global device
        data, tag = [], []
        data_len = []
        for da, ta in batch_datas:
            data.append(da)
            tag.append(ta)
            data_len.append(len(da))
        max_len = max(data_len)
        data = [i + [self.word_2_index["<PAD>"]] * (max_len - len(i)) for i in data]
        tag = [i + [self.tag_2_index["<PAD>"]] * (max_len - len(i)) for i in tag]
        data = torch.tensor(data, dtype=torch.long, device=device)
        tag = torch.tensor(tag, dtype=torch.long, device=device)
        return data.transpose(0, 1), tag.transpose(0, 1), data_len

class CRFLayer(nn.Module):
    def __init__(self, tag_size):
        super(CRFLayer, self).__init__()
        # transition[i+1][i] 初始化转移矩阵
        self.transition = nn.Parameter(torch.randn(tag_size,tag_size))

        self.reset_parameters()

    def reset_parameters(self):
        init.normal_(self.transition)
        # 将START_TAG, END_TAG初始化成一个足够小的值
        self.transition.detach()[tag_2_index[START_TAG],:] = -10000
        self.transition.detach()[:, tag_2_index[END_TAG]] = -10000

    def forward_allpath(self, feats, mask):
        #前向算法，计算所有路径的得分
        """
        Arg:
          feats: (seq_len, batch_size, tag_size)
          mask: (seq_len, batch_size)
        Return:
          scores: (batch_size, )
        """
        seq_len, batch_size, tag_size = feats.size()
        #初始化到达所有标签节点的路径的对数指数得分
        alpha = feats.new_full((batch_size, tag_size), fill_value=-10000)
        #将START_TAG对数置为一个可取初值 log（1）
        alpha[:, tag_2_index[START_TAG]] = 0
        for i, feat in enumerate(feats):
            #广播：(batch_size, next_tag, current_tag)
            #无论标签如何发射得分都是相同的,所以从当前标签广播即可
            emit_score = feat.unsqueeze(-1)  # (batch_size, tag_size, 1)
            #每个样本的转移得分相同,从batchsize维度广播
            transition_score = self.transition.unsqueeze(0)  # (1, tag_size, tag_size)
            #下一个标签的alpha_score相同,从next_tag维度广播
            alpha_score = alpha.unsqueeze(1)  # (batch_size, 1, tag_size)
            alpha_score = alpha_score + transition_score + emit_score
            #从当前标签维度（current_tag）求logsumexp得到next_tag的alpha值,遇到pad则保留最后一个的alpha
            mask_i = mask[i].unsqueeze(-1) #(batch_size,1)
            alpha = torch.logsumexp(alpha_score, -1) * mask_i + alpha * (1 - mask_i)  # (batch_size, tag_size)
        #END_TAG结束
        alpha = alpha + self.transition[tag_2_index[END_TAG]].unsqueeze(0)
        return torch.logsumexp(alpha, -1)  # (batch_size, )

    def score_realpath(self, feats, tags, mask):
        #计算真实路径得分
        """
        Arg:
          feats: (seq_len, batch_size, tag_size)
          tags: (seq_len, batch_size)
          mask: (seq_len, batch_size)
        Return:
          scores: (batch_size, )
        """
        seq_len, batch_size, tag_size = feats.size()
        scores = feats.new_zeros(batch_size)
        #拼接一个START_TAG
        tags = torch.cat([tags.new_full((1, batch_size), fill_value=tag_2_index[START_TAG]), tags],0)  # (seq_len + 1, batch_size)
        for i, feat in enumerate(feats):
            #feat[tag[i+1]] 对齐
            emit_score = torch.stack([f[next_tag] for f, next_tag in zip(feat, tags[i + 1])])
            #self.transition[tag[i+1],tag[i]]
            transition_score = torch.stack([self.transition[tags[i + 1, b], tags[i, b]] for b in range(batch_size)])
            scores += (emit_score + transition_score) * mask[i]
        #self.tarnsitons[self.tag2idx[STOP_TAG],tags[-1]] 拼接end
        transition_to_end = torch.stack([self.transition[tag_2_index[END_TAG], tag[mask[:, b].sum().long()]] for b, tag in enumerate(tags.transpose(0, 1))])
        scores += transition_to_end
        return scores

    def viterbi_decode(self, feats, mask):
        #维特比解码
        """
        :param feats: (seq_len, batch_size, tag_size)
        :param mask: (seq_len, batch_size)
        :return best_path: (seq_len, batch_size)
        """
        seq_len, batch_size, tag_size = feats.size()
        scores = feats.new_full((batch_size, tag_size), fill_value=-10000)
        scores[:, tag_2_index[START_TAG]] = 0
        #记录回溯路径
        pointers = []
        for i, feat in enumerate(feats):
            scores_i = scores.unsqueeze(1) + self.transition.unsqueeze(0) #(batch_size, tag_size(transition), tag_size(scores))
            scores_temp, pointer = torch.max(scores_i, -1) # (batch_size, tag_size)
            scores_next = scores_temp + feat
            pointers.append(pointer)
            mask_i = mask[i].unsqueeze(-1)  # (batch_size, 1)
            scores = scores_next * mask_i + scores * (1 - mask_i)
        pointers = torch.stack(pointers, 0)  # (seq_len, batch_size, tag_size)
        scores += self.transition[tag_2_index[END_TAG]].unsqueeze(0)
        best_score, best_tag = torch.max(scores, -1)  # (batch_size, )
        #反向回溯
        best_path = best_tag.unsqueeze(-1).tolist()  #(batch_size, 1)
        for b in range(batch_size):
            best_tag_b = best_tag[b]
            seq_len_b = int(mask[:, b].sum())
            for ptr_t in reversed(pointers[:seq_len_b, b]):
                # ptr_t shape (tag_size, )
                best_tag_b = ptr_t[best_tag_b].item()
                best_path[b].append(best_tag_b)
            # pop掉START_TAG
            best_path[b].pop()
            best_path[b].reverse()
        return best_path

class BiLSTMCRF(nn.Module):
    def __init__(self, vocab_size, tag_size, embedding_size, hidden_size):
        super(BiLSTMCRF, self).__init__()
        self.embedding = nn.Embedding(vocab_size,embedding_size,padding_idx=word_2_index[PAD])
        self.bilstm = nn.LSTM(input_size=embedding_size,
                              hidden_size=hidden_size,
                              bidirectional=True)
        self.hidden2tag = nn.Linear(hidden_size * 2, tag_size)
        self.crf = CRFLayer(tag_size)

        self.reset_parameters()

    def reset_parameters(self):
        init.xavier_normal_(self.embedding.weight)
        init.xavier_normal_(self.hidden2tag.weight)

    def get_lstm_features(self, seq, mask):
        # lstm forward 计算发射矩阵
        """
        :param seq: (seq_len, batch_size)
        :param mask: (seq_len, batch_size)
        """
        embed = self.embedding(seq)  # (seq_len, batch_size, embedding_size)
        embed = nn.utils.rnn.pack_padded_sequence(embed, mask.sum(0).long().cpu(), enforce_sorted=False)
        lstm_output, _ = self.bilstm(embed)  # (seq_len, batch_size, hidden_size)
        lstm_output, _ = nn.utils.rnn.pad_packed_sequence(lstm_output)
        lstm_features = self.hidden2tag(lstm_output) * mask.unsqueeze(-1)  # (seq_len, batch_size, tag_size)
        return lstm_features

    def neg_log_likelihood(self, seq, tags, mask):
        #对数似然估计 crf loss backward
        """
        :param seq: (seq_len, batch_size)
        :param tags: (seq_len, batch_size)
        :param mask: (seq_len, batch_size)
        """
        lstm_features = self.get_lstm_features(seq, mask)
        forward_score = self.crf.forward_allpath(lstm_features, mask)
        gold_score = self.crf.score_realpath(lstm_features, tags, mask)
        loss = (forward_score - gold_score).sum()

        return loss

    def predict(self, seq, mask):
        # 预测，维特比解码
        """
        :param seq: (seq_len, batch_size)
        :param mask: (seq_len, batch_size)
        """
        lstm_features = self.get_lstm_features(seq, mask)
        best_paths = self.crf.viterbi_decode(lstm_features, mask)

        return best_paths

if __name__ == '__main__':
    device = "cuda:0" if torch.cuda.is_available() else "cpu"

    train_word_lists, train_tag_lists, word_2_index, tag_2_index = build_corpus("train")
    dev_word_lists, dev_tag_lists = build_corpus("dev", make_vocab=False)
    test_word_lists, test_tag_lists = build_corpus("test", make_vocab=False)

    vocab_size = len(word_2_index)
    tag_size = len(tag_2_index)

    train_batch_size = 10
    dev_batch_size = len(dev_word_lists)
    epoch = 100
    lr = 0.001
    embedding_num = 256
    hidden_num = 128

    train_dataset = MyDataset(train_word_lists, train_tag_lists, word_2_index, tag_2_index)
    train_dataloader = DataLoader(train_dataset, train_batch_size, False, collate_fn=train_dataset.batch_data_pro)

    dev_dataset = MyDataset(dev_word_lists, dev_tag_lists, word_2_index, tag_2_index)
    dev_dataloader = DataLoader(dev_dataset, dev_batch_size, False, collate_fn=dev_dataset.batch_data_pro)

    model = BiLSTMCRF(vocab_size, tag_size, embedding_num, hidden_num)
    model = model.to(device)
    opt = torch.optim.Adam(model.parameters(), lr=lr)

    for e in range(epoch):
        model.train()
        train_pre = []
        for data, tag, da_len in train_dataloader:
            mask = torch.ne(data, float(word_2_index[PAD])).float()
            loss = model.neg_log_likelihood(data, tag, mask)
            pre = model.predict(data, mask)  #list [batchsize, dalen]
            train_pre.append((pre, tag.transpose(0, 1), da_len))
            loss.backward()
            opt.step()
            opt.zero_grad()

        pres = []
        tags = []
        for pre, tag, da_len in train_pre:
            for p, t, d_len in zip(pre, tag.cpu(), da_len):
                pres.extend(p)
                tags.extend(t[:d_len])

        train_score = f1_score(tags, pres, average="micro")

        model.eval()  # F1,准确率,精确率,召回率
        for dev_data, dev_tag, dev_da_len in dev_dataloader:
            mask = torch.ne(dev_data, float(word_2_index[PAD])).float()
            pre_dev = model.predict(dev_data, mask)
            dev_tags = []
            pre_devs = []
            for m in range(mask.shape[1]):
                dev_tags.extend(dev_tag.cpu()[:int(mask[:, m].sum().item()), m].tolist())
                pre_devs.extend(pre_dev[m])
            score = f1_score(dev_tags, pre_devs, average="micro")
            print(f'train_score : {train_score:.5f}, dev_score : {score:.5f}')


from django import template
from django_comments.templatetags.comments import BaseCommentNode, RenderCommentListNode, RenderCommentFormNode
from django.db.models import Avg
from django.template.loader import render_to_string

register = template.Library()

class RatingAvgNode(BaseCommentNode):
    """Insert a count of reviews into the context."""

    def get_context_value_from_queryset(self, context, qs):
        return qs.aggregate(Avg('rating'))['rating__avg']

@register.tag
def get_rating_avg(parser, token):
    """
    Gets the review rating average for the given params and populates the template
    context with a variable containing that value, whose name is defined by the
    'as' clause.
    Syntax::
        {% get_rating_avg for [object] as [varname]  %}
        {% get_rating_avg for [app].[model] [object_id] as [varname]  %}
    Example usage::
        {% get_rating_avg for event as review_count %}
        {% get_rating_avg for calendar.event event.id as review_count %}
        {% get_rating_avg for calendar.event 17 as review_count %}
    """
    return RatingAvgNode.handle_token(parser, token)

class RenderReviewAggregateNode(RenderCommentListNode):
    """Render the review aggregate directly"""

    def render(self, context):
        ctype, object_pk = self.get_target_ctype_pk(context)
        if object_pk:
            template_search_list = [
                "reviews/%s/%s/aggregate.html" % (ctype.app_label, ctype.model),
                "reviews/%s/aggregate.html" % ctype.app_label,
                "reviews/aggregate.html"
            ]
            qs = self.get_queryset(context)
            context_dict = context.flatten()
            context_dict['count'] = qs.count()
            context_dict['avg'] = qs.aggregate(v=Avg('rating'))['v']
            liststr = render_to_string(template_search_list, context_dict)
            return liststr
        else:
            return ''

@register.tag
def render_review_aggregate(parser, token):
    return RenderReviewAggregateNode.handle_token(parser, token)

class RenderReviewListNode(RenderCommentListNode):
    """Render the review list directly"""

    def render(self, context):
        ctype, object_pk = self.get_target_ctype_pk(context)
        if object_pk:
            template_search_list = [
                "reviews/%s/%s/list.html" % (ctype.app_label, ctype.model),
                "reviews/%s/list.html" % ctype.app_label,
                "reviews/list.html"
            ]
            qs = self.get_queryset(context)
            context_dict = context.flatten()
            context_dict['review_list'] = self.get_context_value_from_queryset(context, qs)
            liststr = render_to_string(template_search_list, context_dict)
            return liststr
        else:
            return ''

    def get_context_value_from_queryset(self, context, qs):
        return qs.order_by('-submit_date')

@register.tag
def render_review_list(parser, token):
    """
    Render the review list (as returned by ``{% get_review_list %}``)
    through the ``reviews/list.html`` template
    Syntax::
        {% render_review_list for [object] %}
        {% render_review_list for [app].[model] [object_id] %}
    Example usage::
        {% render_review_list for event %}
    """
    return RenderReviewListNode.handle_token(parser, token)

class RenderReviewFormNode(RenderCommentFormNode):
    """Render the review form directly"""

    def render(self, context):
        ctype, object_pk = self.get_target_ctype_pk(context)
        if object_pk:
            template_search_list = [
                "reviews/%s/%s/form.html" % (ctype.app_label, ctype.model),
                "reviews/%s/form.html" % ctype.app_label,
                "reviews/form.html"
            ]
            context_dict = context.flatten()
            context_dict['form'] = self.get_form(context)
            formstr = render_to_string(template_search_list, context_dict)
            return formstr
        else:
            return ''

@register.tag
def render_review_form(parser, token):
    """
    Render the review form (as returned by ``{% render_review_form %}``) through
    the ``reviews/form.html`` template.
    Syntax::
        {% render_review_form for [object] %}
        {% render_review_form for [app].[model] [object_id] %}
    """
    return RenderReviewFormNode.handle_token(parser, token)


import re
from pprint import pprint as pp
from collections import Counter
from lxml import etree
from glob import glob
from unicsv import csv

list_files = glob('output_*.xml')

for file_input in list_files:
    filename = file_input.split('.')[0]
    print ("Processing %s" % filename)
    with open(filename + '.csv', 'wb') as csvfile:
        output_file = csv.writer(
            csvfile,
            delimiter=';',
            quotechar='"',
            quoting=csv.QUOTE_MINIMAL
        )

        # output from pdftohtml -noframes -s -xml t_scep_fr.pdf output.xml
        doc = etree.parse(file_input)
        font_size = '5'
        if (file_input in ['output_06.xml',
                           'output_07.xml',
                           'output_08.xml',
                           'output_09.xml']):
            font_size = '0'
        text_tags = doc.xpath('//pdf2xml/page/text[@font="' + font_size + '"]')

        left_right = {
            "left": [],
            "right": []
        }

        # On devine
        vals = [tag.attrib['left'] for tag in text_tags]
        c = Counter(vals)
        couple_sep = [c.most_common()[0][0], c.most_common()[1][0]]
        if (file_input in ['output_09.xml']):
            couple_sep = ['30', '473']  # 2013

        new_tags = []
        for tag in text_tags:
            if tag.attrib['left'] in couple_sep:
                if tag.getnext() is not None and tag.getnext().attrib[
                        'left'] in couple_sep:
                    tag.text = tag.text + " " + tag.getnext().text
                    new_tags.append(tag)
                elif tag.getprevious() is not None and tag.getprevious().attrib['left'] in couple_sep:
                    pass
                else:
                    new_tags.append(tag)
            else:
                new_tags.append(tag)
        for tag in new_tags:
            if tag.attrib['left'] == couple_sep[0]:
                left_right['left'].append(tag.text)
            elif tag.attrib['left'] == couple_sep[1]:
                left_right['right'].append(tag.text)
            elif int(tag.attrib['left']) > int(couple_sep[0]) and int(
                tag.attrib['left']
            ) < int(couple_sep[1]):
                left_right['left'].append(tag.text)
            else:
                left_right['right'].append(tag.text)
        source = left_right['left']
        source_conv = []
        for i in source:
            if re.match(r"^[\d \xa0]*$", i) is not None:
                i = i.encode('ascii', 'ignore') # clear non-ordinals
                i = i.replace(' ', '')
            else:
                i = i.replace('%', '')
                i = i.replace(',', '.')
            i = i.encode('utf-8')
            source_conv.append(i)
        for i in range(0, len(source_conv)):
            if i % 3 == 0:
                line = source_conv[i:i + 3]
                line.append('20' + filename.replace('output_', ''))
                output_file.writerow(line)


import torch
import gym

from matplotlib import pyplot as plt

from util import Environment

import pbt.train.ppo
import pbt.train.dqn

import argparse

from torch import multiprocessing as mp

parser = argparse.ArgumentParser(
        description='Trains an RL agent using Population-Based Training'
)

parser.add_argument('--env-name',
        default='LunarLander-v2',
        help='The environment to use')

parser.add_argument('--episode-length',
        default=2048,
        type=int,
        help='The maximum number of steps to evaluate in an episode')

parser.add_argument('--max-rounds',
        default=1024,
        type=int,
        help='The number of rounds to run per population member')

parser.add_argument('--num-threads',
        default=mp.cpu_count(),
        type=int,
        help='The number of threads to use')

parser.add_argument('--round-len',
        default=8,
        type=int,
        help='The number of rounds to run per population member')

parser.add_argument('--population-size',
        default=4,
        type=int,
        help='The number of models to train in parallel')

parser.add_argument('--algo',
        default='ppo',
        help='The algorithm to use')

parser.add_argument('--use-reward-normalization',
        type=bool,
        default=True,
        help='Whether or not to normalize the rewards')

args = parser.parse_args()

# Environment definition
env_name = args.env_name

training_module = {
    'ppo' : pbt.train.ppo,
    'dqn' : pbt.train.dqn
}[args.algo]

print('Algorithm:', args.algo)
print('Environment:', env_name)

trainer = training_module.train(lambda: Environment(env_name),
        num_threads=args.num_threads,
        episode_length=args.episode_length,
        max_rounds=args.max_rounds,
        round_len=args.round_len,
        population_size=args.population_size,
)

best_score = max(p.score for p in trainer.population)
best_members = [p for p in trainer.population if p.score == best_score]

print('Found', len(best_members), 'candidates for best model')

hst = trainer.score_history
t = list(range(1, len(hst)+1))

# Save the models
for i, mdl in enumerate(best_members):
    torch.save(mdl.model.cpu(), f'model{i+1}.pt')

plt.plot(t, hst)
plt.show()


def computepay(h, r):
    if h > 40:
        return (40 * r) + ((h - 40) * (r * 1.5))
    elif h < 40:
        return h * r
    else:
        return "Error"

hrs = float(input("Enter Hours:"))
rate = float(input("Enter rate"))
#testest hrs
p = computepay(hrs, rate)
print("Pay", p)


import re
import sys
from functools import wraps
from inspect import getmembers
from types import CoroutineType
from typing import AsyncGenerator, Dict, Optional, Type
from unittest import TestCase

from scrapy.http import Request
from scrapy.utils.python import get_spec
from scrapy.utils.spider import iterate_spider_output

class Contract:
    """Abstract class for contracts"""

    request_cls: Optional[Type[Request]] = None

    def __init__(self, method, *args):
        self.testcase_pre = _create_testcase(method, f"@{self.name} pre-hook")
        self.testcase_post = _create_testcase(method, f"@{self.name} post-hook")
        self.args = args

    def add_pre_hook(self, request, results):
        if hasattr(self, "pre_process"):
            cb = request.callback

            @wraps(cb)
            def wrapper(response, **cb_kwargs):
                try:
                    results.startTest(self.testcase_pre)
                    self.pre_process(response)
                    results.stopTest(self.testcase_pre)
                except AssertionError:
                    results.addFailure(self.testcase_pre, sys.exc_info())
                except Exception:
                    results.addError(self.testcase_pre, sys.exc_info())
                else:
                    results.addSuccess(self.testcase_pre)
                finally:
                    cb_result = cb(response, **cb_kwargs)
                    if isinstance(cb_result, (AsyncGenerator, CoroutineType)):
                        raise TypeError("Contracts don't support async callbacks")
                    return list(  # pylint: disable=return-in-finally
                        iterate_spider_output(cb_result)
                    )

            request.callback = wrapper

        return request

    def add_post_hook(self, request, results):
        if hasattr(self, "post_process"):
            cb = request.callback

            @wraps(cb)
            def wrapper(response, **cb_kwargs):
                cb_result = cb(response, **cb_kwargs)
                if isinstance(cb_result, (AsyncGenerator, CoroutineType)):
                    raise TypeError("Contracts don't support async callbacks")
                output = list(iterate_spider_output(cb_result))
                try:
                    results.startTest(self.testcase_post)
                    self.post_process(output)
                    results.stopTest(self.testcase_post)
                except AssertionError:
                    results.addFailure(self.testcase_post, sys.exc_info())
                except Exception:
                    results.addError(self.testcase_post, sys.exc_info())
                else:
                    results.addSuccess(self.testcase_post)
                finally:
                    return output  # pylint: disable=return-in-finally

            request.callback = wrapper

        return request

    def adjust_request_args(self, args):
        return args

class ContractsManager:
    contracts: Dict[str, Contract] = {}

    def __init__(self, contracts):
        for contract in contracts:
            self.contracts[contract.name] = contract

    def tested_methods_from_spidercls(self, spidercls):
        is_method = re.compile(r"^\s*@", re.MULTILINE).search
        methods = []
        for key, value in getmembers(spidercls):
            if callable(value) and value.__doc__ and is_method(value.__doc__):
                methods.append(key)

        return methods

    def extract_contracts(self, method):
        contracts = []
        for line in method.__doc__.split("\n"):
            line = line.strip()

            if line.startswith("@"):
                name, args = re.match(r"@(\w+)\s*(.*)", line).groups()
                args = re.split(r"\s+", args)

                contracts.append(self.contracts[name](method, *args))

        return contracts

    def from_spider(self, spider, results):
        requests = []
        for method in self.tested_methods_from_spidercls(type(spider)):
            bound_method = spider.__getattribute__(method)
            try:
                requests.append(self.from_method(bound_method, results))
            except Exception:
                case = _create_testcase(bound_method, "contract")
                results.addError(case, sys.exc_info())

        return requests

    def from_method(self, method, results):
        contracts = self.extract_contracts(method)
        if contracts:
            request_cls = Request
            for contract in contracts:
                if contract.request_cls is not None:
                    request_cls = contract.request_cls

            # calculate request args
            args, kwargs = get_spec(request_cls.__init__)

            # Don't filter requests to allow
            # testing different callbacks on the same URL.
            kwargs["dont_filter"] = True
            kwargs["callback"] = method

            for contract in contracts:
                kwargs = contract.adjust_request_args(kwargs)

            args.remove("self")

            # check if all positional arguments are defined in kwargs
            if set(args).issubset(set(kwargs)):
                request = request_cls(**kwargs)

                # execute pre and post hooks in order
                for contract in reversed(contracts):
                    request = contract.add_pre_hook(request, results)
                for contract in contracts:
                    request = contract.add_post_hook(request, results)

                self._clean_req(request, method, results)
                return request

    def _clean_req(self, request, method, results):
        """stop the request from returning objects and records any errors"""

        cb = request.callback

        @wraps(cb)
        def cb_wrapper(response, **cb_kwargs):
            try:
                output = cb(response, **cb_kwargs)
                output = list(iterate_spider_output(output))
            except Exception:
                case = _create_testcase(method, "callback")
                results.addError(case, sys.exc_info())

        def eb_wrapper(failure):
            case = _create_testcase(method, "errback")
            exc_info = failure.type, failure.value, failure.getTracebackObject()
            results.addError(case, exc_info)

        request.callback = cb_wrapper
        request.errback = eb_wrapper

def _create_testcase(method, desc):
    spider = method.__self__.name

    class ContractTestCase(TestCase):
        def __str__(_self):
            return f"[{spider}] {method.__name__} ({desc})"

    name = f"{spider}_{method.__name__}"
    setattr(ContractTestCase, name, lambda x: x)
    return ContractTestCase(name)


'''
Created on Apr 19, 2012

@author: Jimmy
'''

import os, sys, logging
from scorer.log import *

#log_handler = logging.FileHandler('OpenScore.log')
#formatter = logging.Formatter('%(asctime)s %(levelname)s %(message)s')
#log_handler.setFormatter(formatter)
#logger.addHandler(log_handler)
#logger.setLevel(logging.WARNING)

from scorer.bowlingscorer import BowlingScorer

progname = sys.argv[0]
progdir = os.path.dirname(progname)
sys.path.append(os.path.join(progdir,'components'))
logger.info("Starting OpenScore...")
if __name__ == '__main__':
    try:
        #logging.info("Creating main BowlingScorer object...")
        app = BowlingScorer()
        #logging.info("Entering run loop...")
        app.main()
    except:
        logger.info("Exception encountered. OpenScore will now close:")
        logger.exception('')


"""Add GFS prediction model

Revision ID: 69cbd7ca2477
Revises: de8355996f8e
Create Date: 2023-02-09 14:30:49.597571

"""
from alembic import op
import sqlalchemy as sa

# revision identifiers, used by Alembic.
revision = '69cbd7ca2477'
down_revision = 'de8355996f8e'
branch_labels = None
depends_on = None

def upgrade():
    op.execute('INSERT INTO prediction_models(name, abbreviation, projection)\
        VALUES(\'Global Forecast System\', \'GFS\', \'lonlat.0.5deg\')')

def downgrade():
    op.execute('DELETE FROM prediction_models WHERE abbreviation = \'GFS\'')


members = int(input())

total_a = 0
total_b = 0

for loop in range(members):
    weight_a = int(input())
    weight_b = int(input())

    total_a = (total_a + weight_a)
    total_b = (total_b + weight_b)

if(total_a > total_b):
    print("L'équipe 1 a un avantage")
else:
    print("L'équipe 2 a un avantage")

print("Poids total pour l'équipe 1 : " + str(total_a))
print("Poids total pour l'équipe 2 : " + str(total_b))


# Code pulled from 2017 day 10.

arraySize = 256

def getFreshRope(length):
    rope = []
    for i in xrange(arraySize):
        rope.append(i)
    return rope
### getFreshRope

def convertAsciiToLengths(inputAscii):
    lengths = []

    for character in inputAscii:
        lengths.append(ord(character))

    # append standard post-fix
    lengths += [17, 31, 73, 47, 23]

    return lengths
### convertAsciiToLengths

def reversePortionOfRope(rope, position, length):
    ropeLen = len(rope)
    double = rope + rope
    section = double[position:position + length]
    section = section[::-1]

    for i in xrange(len(section)):
        rope[(position + i) % ropeLen] = section[i]

    return rope
### reversePortionOfRope

def getNextPosition(arraySize, currentPosition, length, skipSize):
    nextIndex = currentPosition + length + skipSize
    nextIndex %= arraySize
    skipSize += 1
    return nextIndex, skipSize
### getNextPosition

def convertSparseHashToDenseHash(rope):
    output = []
    for b in xrange(16):
        h = 0
        for i in xrange(16):
            h ^= rope[b*16 + i]

        output.append( format(h, '#04x')[2:] )
    output = "".join(output)
    return output
### convertSparseHashToDenseHash

def performKnotHasing(rope, rounds, inputLengths):
    length = 0
    position = 0
    skipSize = 0

    # perform the rounds
    for i in xrange(rounds):
        for part in inputLengths:
            if type(part) == str:
                length = int(part.strip())
            else:
                length = part
            rope = reversePortionOfRope(rope, position, length)
            position, skipSize = getNextPosition(arraySize, position, length, skipSize)

    # encode the output hash
    return convertSparseHashToDenseHash(rope)
### performKnotHasing

def computeKnotHash(key):
    rope = getFreshRope(arraySize)
    inputLengths = convertAsciiToLengths(key)
    return performKnotHasing(rope, 64, inputLengths)
### knothash


from pytz import timezone
import time
from apscheduler.schedulers.background import BackgroundScheduler

class Scheduler:

    def start(self):
        self.sch = BackgroundScheduler()
        self.sch.configure(timezone=timezone('Asia/Seoul'))
        self.sch.start()
        self.sch.add_job(
            self.job1,
            'interval',
            seconds=10,
            max_instances=1
        )

    def job1(self):
        with open('./test.txt', mode='a') as f:
            f.write(time.strftime('%c', time.localtime(time.time())) + "\n")


from imbroglio import System
from customdicts import InvertibleDict, ReversibleDict, CustomDict
from .utils import Point3
from .utils import Sprite3
from .consoles import Console
import constants

class Cell3Dict(InvertibleDict):
    def __setitem__(self, item, value):
        super().__setitem__(item, Point3(*value))

class Pos3Dict(ReversibleDict):
    def __setitem__(self, item, value):
        super().__setitem__(item, Point3(*value))

class Sprite3Dict(CustomDict):
    def __setitem__(self, item, value):
        super().__setitem__(item, Sprite3(*value))

class World(System):

    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        self.console = Console("Initializing...")
        self._camera = None
        self._player = None
        self.focus = None
        self.setup()

    @property
    def player(self):
        return self._player

    @player.setter
    def player(self, entity):
        self._player = entity
        self.focus = entity

    @property
    def camera(self):
        return self._camera

    def spin(self, *args):
        if hasattr(self.focus, "energy"):
            while self.focus.energy < 0:
                self.processes()
                if not hasattr(self.focus, "energy"):
                    break

    def setup(self):
        kwargs = dict()
        kwargs["name"] = "Camera"
        kwargs["pos"] = 0, 0, 0
        camera = self.new_entity(**kwargs)
        self._camera = camera

    def clear(self):
        super().clear()
        self.setup()

world = World("name", "energy", "melee", "armor_class", "melee_bonus", "melee_damage", "max_hp", "damage",
              cell=Cell3Dict, pos=Pos3Dict, sprite=Sprite3Dict)


import self
from django import forms
from django.forms import ModelForm, Textarea
from django.http import request

from core import settings
from .models import Contracts, Attachments, Review, Document, LaborPosition, ClinReport, Employee

class DateInput(forms.DateInput):
    input_type = 'date'

class ContractForm(forms.ModelForm):
    class Meta:
        model = Contracts

        fields = ['contract_type', 'contract_status', 'tag_type', 'title', 'company_name', 'contract_number',
                  'office', 'division', 'short_description', 'effective_date',
                  'expiration_date', 'renewal_date', 'prime', 'prime_pm', 'sub', 'sub_pm', 'current_year',
                  'total_funding', 'current_funding', 'funded', 'city', 'state', 'zipcode', 'contact_email',
                  'main_logo',
                  'created_date']

        widgets = {
            'short_description': Textarea(
                attrs={'class': 'form-control', 'rows': 80, 'cols': 20}),
            'effective_date': DateInput(),
            'expiration_date': DateInput(),
            'renewal_date': DateInput(),
            'created_date': forms.TextInput(
                attrs={'class': 'form-control', 'readonly': 'readonly'}
            )
        }

class AttachmentForm(forms.ModelForm):
    class Meta:
        model = Attachments

        fields = ['contract_type', 'contracts_id', 'short_description', 'attach_file']

class ReviewForm(forms.ModelForm):
    class Meta:
        model = Review

        fields = ['text', 'atRisk']

        widgets = {
            'text': forms.Textarea(
                attrs={'class': 'form-control', 'rows': 4}
            ),
            'atRisk': forms.CheckboxInput(
                attrs={'class': 'form-check-input', 'title': 'At Risk'}
            )
        }

class DocumentForm(forms.ModelForm):
    class Meta:
        model = Document

        fields = ['document_type', 'title', 'tag', 'attach_file']

class LaborPositionForm(forms.ModelForm):
    class Meta:
        model = LaborPosition

        fields = ['sins_proposed', 'job_title', 'educational_level', 'years_experience', 'price_year_1',
                  'price_year_2', 'price_year_3', 'price_year_4', 'price_year_5']

class ClinReportForm(forms.ModelForm):
    class Meta:
        model = ClinReport

        fields = ['user', 'contracts', 'employees', 'item_number', 'supplies_services_description', 'qty', 'unit', 'unit_price',
                  'total_price', 'option_year', 'notes']

class EmployeeForm(forms.ModelForm):
    class Meta:
        model = Employee

        fields = ['user', 'contracts', 'last_name', 'first_name', 'middle_name']

class ContactForm(forms.Form):
    from_email = forms.EmailField(required=True)
    subject = forms.CharField(required=False, )
    message = forms.CharField(widget=forms.Textarea(attrs={'cols': 43, 'rows': 8}))
    name = forms.CharField(required=True)
    phone = forms.CharField(required=False)
    company = forms.CharField(required=False)

    def __init__(self, *args, **kwargs):
        super(ContactForm, self).__init__(*args, **kwargs)
        self.fields['from_email'].widget.attrs['placeholder'] = 'Email'
        self.fields['subject'].widget.attrs['placeholder'] = 'Subject'
        self.fields['message'].widget.attrs['placeholder'] = 'Message'
        self.fields['name'].widget.attrs['placeholder'] = 'Name'
        self.fields['phone'].widget.attrs['placeholder'] = 'Phone'
        self.fields['company'].widget.attrs['placeholder'] = 'Company'


import math
import random
#whole numbers only 199 or 11,717,555

def Commanizer(problemsets):
  questions = ""
  answersets= 0
  decimal_subtract = 0
  amount_commas = len(str(problemsets))
  period = str(problemsets).find(".")
  if period != -1:
    decimal_subtract = amount_commas - (period + 1)
  amount_commas = (amount_commas - 1) // 3
  questions = problemsets
  questions = list(str(questions))
  if amount_commas >= 1:
    questions.insert((-3 - decimal_subtract), ",")
  if amount_commas >= 2:
    questions.insert((-7 - decimal_subtract), ",")

  underbar = random.randint(1, len(str(problemsets)) + amount_commas)
  while questions[underbar * -1] == ",":
    underbar = random.randint(1, len(str(problemsets)) + amount_commas)
  # get underwcores. Run again id it lands on comma
  answersets = "".join(questions)
  return(answersets, underbar)

def Decimal_Places(seed):
  seedArr = [int(i) for i in seed.split(',')]
  j = 0
  i = 1
  temp_problem = []
  after_comma = []
  masterSeed = []

  for tried in range(3):
    for prob in range(seedArr[j]):
      problemsets = random.randint(seedArr[i], seedArr[i+1])
      after_comma = Commanizer(problemsets)
      temp_problem = list(str(after_comma[0]))
      temp_problem.insert((after_comma[1] * -1),  "\overline{")
      temp_problem = ''.join(temp_problem)
      temp_problem = "\(" + temp_problem + " \), " + str(after_comma[1])
      masterSeed.append(temp_problem)
    j = j + 3
    i = i + 3

  return(masterSeed)


import random

from torch import nn
import torch.nn.functional as F

from replay_memory import device

class DQN(nn.Module):

    def __init__(self, n_observations, n_actions):
        super(DQN, self).__init__()
        self.layer1 = nn.Linear(n_observations, 128)
        self.layer2 = nn.Linear(128, 128)
        self.layer3 = nn.Linear(128, n_actions)

    def forward(self, x):
        x = F.relu(self.layer1(x))
        x = F.relu(self.layer2(x))
        return self.layer3(x)

class FCN_QNet(nn.Module):
    """
    action value function, Q(S, a)
    produce the actions in parallel as output vector,
    and choose the max
    """
    def __init__(self, insize, outsize):
        """
        insize ==> input size
            == size of the observation space
        outsize ==> output size
            == number of actions
        """
        super(FCN_QNet, self).__init__()
        self.fc1 = nn.Linear(insize, 256)
        self.fc2 = nn.Linear(256, 84)
        self.fc3 = nn.Linear(84, outsize)

    def forward(self, x):
        """
        standard 3-layer fully connected NN
        """
        x = x.to(device)  # for CUDA
        # print("input x.size() = ", x.size())
        #x = x.view(x.size(0),-1)
        # may encounter view memory error
        # RuntimeError: view size is not compatible with input tensor's size and stride
        # (at least one dimension spans across two contiguous subspaces). Use .reshape(...) instead.
        x = x.reshape(x.size(0),-1)
        # print("after x.view ---> input x.size() = ", x.size())
        x = F.relu(self.fc1(x))
        x = F.relu(self.fc2(x))
        x = self.fc3(x)
        return x

    def sample_action(self, obs, epsilon):
        """
        greedy epsilon choose
        """
        coin = random.random()
        if coin < epsilon:
            # print("coin < epsilon", coin, epsilon)
            # for 3actionStateEnv use [0,1,2]
            value = random.randint(1, 4)
            return value
        else:
            # print("exploit")
            out = self.forward(obs)

            return out.argmax().item() + 1


#!/usr/bin/env python3
import math
from dataclasses import dataclass
from typing import cast, Dict, Set, Tuple, Union, Iterable

from aox.challenge import Debugger
from utils import BaseChallenge, Point2D, min_and_max_tuples

Neighbourhood = Tuple[bool, bool, bool, bool, bool, bool, bool, bool, bool]

class Challenge(BaseChallenge):
    def solve(self, _input: str, debugger: Debugger) -> Union[str, int]:
        """
        >>> Challenge().default_solve()
        42
        """
        algorithm, image = Image.from_mapping_and_image_text(_input)
        return image.step_many(algorithm, 2).light_pixel_count

@dataclass
class IEAlgorithm:
    mapping: Dict[Neighbourhood, bool]

    @classmethod
    def from_mapping_text(cls, mapping_text: str) -> "IEAlgorithm":
        """
        >>> algorithm = IEAlgorithm.from_mapping_text(
        ...     "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#...."
        ...     "#..#..##..###..######.###...####..#..#####..##..#.#####...##.#"
        ...     ".#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..####"
        ...     "#.....#.#....###..#.##......#.....#..#..#..##..#...##.######.#"
        ...     "###.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.#"
        ...     "#..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#...."
        ...     ".####.#..#..#.##.#....##..#.####....##...##..#...#......#.#..."
        ...     "....#.......##..####..#...#.#.#...##..#.#..###..#####........#"
        ...     "..####......#..#"
        ... )
        >>> list(map(algorithm.__getitem__, range(5)))
        [False, False, True, False, True]
        >>> list(map(algorithm.__getitem__, range(33, 36)))
        [False, True, True]
        >>> list(map(
        ...     algorithm.__getitem__,
        ...     map(IEAlgorithm.index_to_neighbourhood, range(5)),
        ... ))
        [False, False, True, False, True]
        >>> algorithm[(False,) * 9]
        False
        >>> algorithm[(False, True,) + (False,) * 7]
        True
        """
        # if mapping_text.strip()[0] == "#":
        #     raise Exception(
        #         f"Mapping text starts with # ({mapping_text[:15]}...)"
        #     )
        return cls(
            mapping={
                cls.index_to_neighbourhood(index): value == "#"
                for index, value in enumerate(mapping_text.strip())
            },
        )

    @classmethod
    def index_to_neighbourhood(cls, index: int) -> Neighbourhood:
        """
        >>> IEAlgorithm.index_to_neighbourhood(0)
        (False, False, False, False, False, False, False, False, False)
        >>> IEAlgorithm.index_to_neighbourhood(341)
        (True, False, True, False, True, False, True, False, True)
        >>> IEAlgorithm.index_to_neighbourhood(511)
        (True, True, True, True, True, True, True, True, True)
        """
        return cast(Neighbourhood, tuple(
            digit_str == "1"
            for digit_str in f"{index:09b}"
        ))

    def __post_init__(self) -> None:
        if len(self.mapping) != 512:
            raise Exception(
                f"Expected 512 mappings, but got {len(self.mapping)}"
            )
        if self[0] and self[511]:
            raise Exception(
                f"Cannot have both an empty and a full neighbourhood result to "
                f"a light"
            )

    def __getitem__(self, item: Union[int, Neighbourhood]) -> bool:
        if not isinstance(item, tuple):
            item = self.index_to_neighbourhood(item)

        return self.mapping[item]

@dataclass
class Area:
    min: Point2D
    max: Point2D

    @classmethod
    def from_points(cls, points: Iterable[Point2D]) -> "Area":
        _min, _max = min_and_max_tuples(points)
        return cls(
            min=Point2D(_min),
            max=Point2D(_max),
        )

    def __contains__(self, item: Point2D) -> bool:
        return (
            (self.min.x <= item.x <= self.max.x)
            and (self.min.y <= item.y <= self.max.y)
        )

    def pad(self, count: int) -> "Area":
        cls = type(self)
        # noinspection PyArgumentList
        return cls(
            min=Point2D(self.min.x - count, self.min.y + count),
            max=Point2D(self.max.x - count, self.max.y + count),
        )

    def __iter__(self) -> Iterable[Point2D]:
        return self.iter()

    def iter(self, pad_count: int = 0) -> Iterable[Point2D]:
        for y in self.ys(pad_count):
            for x in self.xs(pad_count):
                yield Point2D(x, y)

    def xs(self, pad_count: int = 0) -> Iterable[int]:
        yield from range(self.min.x - pad_count, self.max.x + pad_count + 1)

    def ys(self, pad_count: int = 0) -> Iterable[int]:
        yield from range(self.min.y - pad_count, self.max.y + pad_count + 1)

    @property
    def size(self) -> Point2D:
        return Point2D(self.max.x - self.min.x + 1, self.max.y - self.min.y + 1)

@dataclass
class Image:
    light_pixels: Set[Point2D]
    boundary: Area
    default_out_of_boundary: bool

    @classmethod
    def from_mapping_and_image_text(
        cls, text: str,
    ) -> Tuple[IEAlgorithm, "Image"]:
        mapping_text, image_text = text.strip().split("\n\n")
        return (
            IEAlgorithm.from_mapping_text(mapping_text),
            cls.from_image_text(image_text),
        )

    @classmethod
    def from_image_text(cls, image_text: str) -> "Image":
        """
        >>> print(":", Image.from_image_text('''
        ...     #..#.
        ...     #....
        ...     ##..#
        ...     ..#..
        ...     ..###
        ... '''))
        : .......
        .#..#..
        .#.....
        .##..#.
        ...#...
        ...###.
        .......
        """
        lines = filter(None, map(str.strip, image_text.splitlines()))
        light_pixels = {
            point
            for y, line in enumerate(lines)
            for x, character in enumerate(line)
            for point in [Point2D(x, y)]
            if character == "#"
        }
        return cls(
            light_pixels=light_pixels,
            boundary=Area.from_points(light_pixels),
            default_out_of_boundary=False,
        )

    def __str__(self) -> str:
        if not self.light_pixels:
            return ""

        return "\n".join(
            "".join(
                "#"
                if self[point] else
                "."
                for x in self.boundary.xs(1)
                for point in [Point2D(x, y)]
            )
            for y in self.boundary.ys(1)
        )

    def __getitem__(self, item: Point2D) -> bool:
        if item in self.boundary:
            return item in self.light_pixels
        else:
            return self.default_out_of_boundary

    def step_many(
        self, algorithm: IEAlgorithm, count: int,
        debugger: Debugger = Debugger(enabled=False),
    ) -> "Image":
        """
        >>> _algorithm = IEAlgorithm.from_mapping_text(
        ...     "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#...."
        ...     "#..#..##..###..######.###...####..#..#####..##..#.#####...##.#"
        ...     ".#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..####"
        ...     "#.....#.#....###..#.##......#.....#..#..#..##..#...##.######.#"
        ...     "###.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.#"
        ...     "#..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#...."
        ...     ".####.#..#..#.##.#....##..#.####....##...##..#...#......#.#..."
        ...     "....#.......##..####..#...#.#.#...##..#.#..###..#####........#"
        ...     "..####......#..#"
        ... )
        >>> image = Image.from_image_text('''
        ...     #..#.
        ...     #....
        ...     ##..#
        ...     ..#..
        ...     ..###
        ... ''')
        >>> print(":", image.step_many(_algorithm, 1))
        : .........
        ..##.##..
        .#..#.#..
        .##.#..#.
        .####..#.
        ..#..##..
        ...##..#.
        ....#.#..
        .........
        >>> print(":", image.step_many(_algorithm, 2))
        : ...........
        ........#..
        ..#..#.#...
        .#.#...###.
        .#...##.#..
        .#.....#.#.
        ..#.#####..
        ...#.#####.
        ....##.##..
        .....###...
        ...........
        """
        result = self
        debugger.default_report(f"Stepping {count} times")
        for step in debugger.stepping(range(count)):
            result = result.step(algorithm)
            debugger.default_report_if(
                f"Stepped {step}/{count} times, {result.light_pixel_count} "
                f"lights in result, size of {tuple(result.boundary.size)}"
            )

        return result

    def step(self, algorithm: IEAlgorithm) -> "Image":
        """
        >>> _algorithm = IEAlgorithm.from_mapping_text(
        ...     "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#...."
        ...     "#..#..##..###..######.###...####..#..#####..##..#.#####...##.#"
        ...     ".#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..####"
        ...     "#.....#.#....###..#.##......#.....#..#..#..##..#...##.######.#"
        ...     "###.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.#"
        ...     "#..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#...."
        ...     ".####.#..#..#.##.#....##..#.####....##...##..#...#......#.#..."
        ...     "....#.......##..####..#...#.#.#...##..#.#..###..#####........#"
        ...     "..####......#..#"
        ... )
        >>> image = Image.from_image_text('''
        ...     #..#.
        ...     #....
        ...     ##..#
        ...     ..#..
        ...     ..###
        ... ''')
        >>> print(":", image.step(_algorithm))
        : .........
        ..##.##..
        .#..#.#..
        .##.#..#.
        .####..#.
        ..#..##..
        ...##..#.
        ....#.#..
        .........
        """
        cls = type(self)
        light_pixels = {
            point
            for point in self.boundary.iter(1)
            if algorithm[self.get_neighbourhood(point)]
        }
        # noinspection PyArgumentList
        return cls(
            light_pixels=light_pixels,
            boundary=Area.from_points(light_pixels),
            default_out_of_boundary=(
                algorithm[511 if self.default_out_of_boundary else 0]
            ),
        )

    NEIGHBOURHOOD_OFFSETS = [
        Point2D(x, y)
        for y in [-1, 0, 1]
        for x in [-1, 0, 1]
    ]

    def get_neighbourhood(self, point: Point2D) -> Neighbourhood:
        return cast(Neighbourhood, tuple(
            self[point.offset(neighbour_offset)]
            for neighbour_offset in self.NEIGHBOURHOOD_OFFSETS
        ))

    @property
    def light_pixel_count(self) -> Union[int, float]:
        """
        >>> _algorithm = IEAlgorithm.from_mapping_text(
        ...     "..#.#..#####.#.#.#.###.##.....###.##.#..###.####..#####..#...."
        ...     "#..#..##..###..######.###...####..#..#####..##..#.#####...##.#"
        ...     ".#..#.##..#.#......#.###.######.###.####...#.##.##..#..#..####"
        ...     "#.....#.#....###..#.##......#.....#..#..#..##..#...##.######.#"
        ...     "###.####.#.#...#.......#..#.#.#...####.##.#......#..#...##.#.#"
        ...     "#..#...##.#.##..###.#......#.#.......#.#.#.####.###.##...#...."
        ...     ".####.#..#..#.##.#....##..#.####....##...##..#...#......#.#..."
        ...     "....#.......##..####..#...#.#.#...##..#.#..###..#####........#"
        ...     "..####......#..#"
        ... )
        >>> image = Image.from_image_text('''
        ...     #..#.
        ...     #....
        ...     ##..#
        ...     ..#..
        ...     ..###
        ... ''')
        >>> image.step_many(_algorithm, 2).light_pixel_count
        35
        """
        if self.default_out_of_boundary:
            return math.inf
        return len(self.light_pixels)

Challenge.main()
challenge = Challenge()


# ricochet_robots.py: Template para implementação do 1º projeto de Inteligência Artificial 2020/2021.
# Devem alterar as classes e funções neste ficheiro de acordo com as instruções do enunciado.
# Além das funções e classes já definidas, podem acrescentar outras que considerem pertinentes.

# Grupo 18:
# 93744 Nuno Carvalho
# 93739 Miguel Silva

from search import Problem, Node, astar_search, compare_searchers, iterative_deepening_search, recursive_best_first_search, bidirectional_search, depth_first_graph_search, depth_first_tree_search
from utils import manhattan_distance, euclidean_distance, hamming_distance
import sys
import math
import random
from timeit import default_timer as timer

from multiprocessing import Manager, cpu_count, Process

ROBOTS = ("Y", "B", "G", "R")

class RRState:
    state_id = 0
    def __init__(self, board):
        self.board = board
        self.id = RRState.state_id
        RRState.state_id += 1

    def __lt__(self, other):
        """ Este método é utilizado em caso de empate na gestão da lista
        de abertos nas procuras informadas.
        """
        return self.id < other.id

    def __hash__(self):
        return hash(self.board)

    def __eq__(self, other):
        return self.board == other.board

class Board:
    """ Representacao interna de um tabuleiro de Ricochet Robots. """
    def __init__(self, size, robots, target, barriers, parsed=False, simplified=False):
        self._size = size
        self._target = ()
        self._robots = {}
        self._barriers = {}
        self.simplified = simplified

        #append robots
        for pos in robots:
            self._set_robot(robots[pos], pos)

        #append target
        t, pos = target
        self._set_target(t, pos)

        #append barriers
        for pos in barriers:
            for b in barriers[pos]:
                self._set_barrier(b, pos)

                if parsed:
                    if b == "u":
                        self._set_barrier("d", self.__position_change(pos, "u"))
                    if b == "d":
                        self._set_barrier("u", self.__position_change(pos, "d"))
                    if b == "l":
                        self._set_barrier("r", self.__position_change(pos, "l"))
                    if b == "r":
                        self._set_barrier("l", self.__position_change(pos, "r"))

        if parsed:
            for x in range(1, size +  1):
                self._set_barrier("l", (x, 1))
                self._set_barrier("r", (x, self._size))

            for y in range(1, size + 1):
                self._set_barrier("u", (1, y))
                self._set_barrier("d", (self._size, y))

    def __hash__(self):
        if self.simplified:
            return hash((self.robot_position(self.get_target()[0])))
        else:
            return hash((self.robot_position(r) for r in ROBOTS))

    def __eq__(self, other):
        if self.simplified:
            r = self.get_target()[0]
            return self.robot_position(r) == other.robot_position(r)
        else:
            return all(self.robot_position(r) == other.robot_position(r) for r in ROBOTS)

    def __position_change(self, position, direction):
        """ Returns the translated position according to the input direction """
        if direction == "u":
            return (position[0] - 1, position[1])
        if direction == "d":
            return (position[0] + 1, position[1])
        if direction == "l":
            return (position[0], position[1] - 1)
        if direction == "r":
            return (position[0], position[1] + 1)

    #getters
    def _get_barriers(self, position):
        try:
            return self._barriers[position]
        except KeyError:
            return []

    def _get_robot(self, position):
        try:
            return self._robots[position]
        except KeyError:
            return None

    def _set_barrier(self, barrier, position):
        try:
            self._barriers[position].append(barrier)
        except KeyError:
            self._barriers[position] = []
            self._barriers[position].append(barrier)

    def get_target(self):
        return self._target

    #setters
    def _set_robot(self, robot, position):
        self._robots[position] = robot

    def _set_target(self, target, position):
        self._target = target, position

    def _reset_robot(self, position):
        del self._robots[position]

    def _valid_action(self, position, direction):
        aux_pos = self.__position_change(position, direction)
        if self.simplified:
            return direction not in self._get_barriers(position)
        else:
            return direction not in self._get_barriers(position) and \
                    self._get_robot(aux_pos) == None

    def robot_position(self, robot: str):
        """ Devolve a posição atual do robô passado como argumento. """
        for r in self._robots:
            if self._robots[r] == robot:
                return r

    def robot_valid_actions(self, robot):
        """ Devolve as possiveis ações do robô passado como argumento. """
        pos = self.robot_position(robot)

        pa = ["r", "d", "l", "u"]

        actions = [(robot, ac) for ac in pa if self._valid_action(pos, ac)]

        return actions

    def robot_action(self, action):
        robot, direction = action
        position = self.robot_position(robot)

        self._reset_robot(position)

        if self.simplified:
            if self._valid_action(position, direction):
                position = self.__position_change(position, direction)
        else:
            while self._valid_action(position, direction):
                position = self.__position_change(position, direction)

        self._set_robot(robot, position)

    def check_finish(self):
        tg = self.get_target()
        return tg[1] == self.robot_position(tg[0])

def parse_instance(filename: str) -> Board:
    """ Lê o ficheiro cujo caminho é passado como argumento e retorna
    uma instância da classe Board. """
    size = 0
    robots = {}
    barriers = {}
    target = None

    num_robots = 4

    with open(filename) as file:
        size = int(file.readline())

        #parse robots
        for _ in range(num_robots):
            line = file.readline().split(" ")
            robots[(int(line[1]), int(line[2]))] = line[0]

        #parse target
        line = file.readline().split(" ")
        target = (line[0], (int(line[1]), int(line[2]))) #(color, (x, y))

        #parse barriers
        for _ in range(int(file.readline())):
            line = file.readline().rsplit()
            try:
                barriers[(int(line[0]), int(line[1]))].append(line[2])
            except KeyError:
                barriers[(int(line[0]), int(line[1]))] = []
                barriers[(int(line[0]), int(line[1]))].append(line[2])

    return Board(size, robots, target, barriers, True)

class RicochetRobots(Problem):
    def __init__(self, board: Board):
        """ O construtor especifica o estado inicial. """
        self.initial = RRState(board)

    def actions(self, state: RRState):
        """ Retorna uma lista de ações que podem ser executadas a
        partir do estado passado como argumento. """

        actions = []
        for r in ROBOTS:
            actions += state.board.robot_valid_actions(r)

        return actions

    def result(self, state: RRState, action):
        """ Retorna o estado resultante de executar a 'action' sobre
        'state' passado como argumento. A ação retornada deve ser uma
        das presentes na lista obtida pela execução de
        self.actions(state). """
        b = state.board

        new_board = Board(b._size, b._robots, b._target, b._barriers)
        new_board.robot_action(action)

        return RRState(new_board)

    def goal_test(self, state: RRState):
        """ Retorna True se e só se o estado passado como argumento é
        um estado objetivo. Deve verificar se o alvo e o robô da
        mesma cor ocupam a mesma célula no tabuleiro. """

        return state.board.check_finish()

    def h(self, node: Node):
        """ Função heuristica utilizada para a procura A*. """
        b = node.state.board

        simplified_problem = SimplifiedRicochetRobots(b)
        solution_node = recursive_best_first_search(simplified_problem)

        aux = 0
        counter = 0

        if solution_node != None:
            for s in solution_node.solution():
                if s[1] != aux:
                    aux = s[1]
                    counter += 1
        else:
            counter = 3
        return counter

class SimplifiedRicochetRobots(RicochetRobots):
    def __init__(self, board: Board):
        self.initial = RRState(board)

    def actions(self, state: RRState):
        """ Retorna uma lista de ações que podem ser executadas a
        partir do estado passado como argumento. """
        robot = state.board.get_target()[0]

        actions = state.board.robot_valid_actions(robot)
        return actions

    def result(self, state: RRState, action):
        """ Retorna o estado resultante de executar a 'action' sobre
        'state' passado como argumento. A ação retornada deve ser uma
        das presentes na lista obtida pela execução de
        self.actions(state). """
        b = state.board

        new_board = Board(b._size, b._robots, b._target, b._barriers, simplified=True)
        new_board.robot_action(action)

        return RRState(new_board)

    def h(self, node: Node):
        b = node.state.board
        robot_pos = b.robot_position(b.get_target()[0])
        target_pos = b.get_target()[1]

        return manhattan_distance(robot_pos, target_pos)

def output(node):
    print(len(node.solution()))

    for s in node.solution():
        print(f"{s[0]} {s[1]}")

if __name__ == "__main__":
    # Ler o ficheiro de input de sys.argv[1],
    board = parse_instance(sys.argv[1])

    problem = RicochetRobots(board)

    solution_node = recursive_best_first_search(problem)

    output(solution_node)


from googleapiclient.discovery import build
from googleapiclient.errors import HttpError
from google.oauth2 import service_account
import re
import requests
# Disable warning for HTTPS requests being made without certificate verifiication
from urllib3.exceptions import InsecureRequestWarning
from urllib3 import disable_warnings
disable_warnings(InsecureRequestWarning)

def main(request):
    # --- Authentication and Authorization for service acct to google sheet --- #
    scopes = ['https://www.googleapis.com/auth/spreadsheets']
    service_account_file = 'service_acct_key.json'
    creds = service_account.Credentials.from_service_account_file(service_account_file, scopes=scopes)

    # --- Google sheet variables --- #
    spreadsheet_id = '1wv9tncWB4yF8PxxEcyvHXFHxdEDqMUiTbUCYA8A4KXE'
    range = 'codes'

    # --- Grab google sheet values --- #
    try:
        service = build('sheets', 'v4', credentials=creds)
        sheet = service.spreadsheets()
        result = sheet.values().get(spreadsheetId=spreadsheet_id,
                                    range=range).execute()
        values = result.get('values')
        values.pop(0) # Remove header of google sheet

    except HttpError as err:
        print(err)

    # --- Get refresh tokens and update google sheet --- #
    refresh_tokens = rw_gsheet(request, values, sheet, spreadsheet_id, range)

    # --- Prepare the data for post processing - remove duplicates and null values --- #
    refresh_tokens = flatten_list(refresh_tokens)
    refresh_tokens = list(dict.fromkeys(refresh_tokens)) # fromkeys() removes duplicates; using set() changes the order of the tokens
    refresh_tokens = list(filter(None, refresh_tokens))

    return(refresh_tokens)

def rw_gsheet(request, values, sheet, spreadsheet_id, range):
    row_num = 1 # offset for the header row in the sheet
    row_numbers = []
    new_refresh_tokens = []
    refresh_tokens = []

    # --- Check each "row" (value) if it contains a refresh token i.e. ['url', 'refresh_token'] --- #
    for value in values:
        row_num += 1
        if len(value) == 2:
            refresh_tokens.append(value[1])

        if len(value) == 1:
            row_numbers.append(row_num)
            refresh_token = get_refresh_token(request,value[0])
            new_refresh_tokens.append(refresh_token)
            refresh_tokens.append(refresh_token)

    # --- Update the google sheet --- #
    if len(new_refresh_tokens) > 0:
        post = sheet.values().append(spreadsheetId=spreadsheet_id, range=f'{range}!B'+str(row_numbers[0]),
                                        valueInputOption='RAW', body={'values':new_refresh_tokens}).execute()

    return refresh_tokens

def get_refresh_token(request,url):
    auth_url = 'https://www.strava.com/oauth/token'
    if re.findall('(?<=code=)(.*)(?=&scope)', url):
        code = re.findall('(?<=code=)(.*)(?=&scope)', url)
        payload = {
            'client_id': request['secrets']['client_id'],
            'client_secret': request['secrets']['client_secret'],
            'code': code,
            'grant_type': 'authorization_code',
            'f': 'json'
        }
        get_refresh_token = requests.post(auth_url, data=payload, verify=False)
        if get_refresh_token.status_code != 200:
            print(f'{get_refresh_token.status_code} Error: There was an issue with getting the refresh token using code {code} - check the POST request')
            return
        refresh_token = [get_refresh_token.json()['refresh_token']]
        return refresh_token

def flatten_list(_2d_list):
    flat_list = []
    for element in _2d_list:
        if type(element) is list:
            for item in element:
                flat_list.append(item)
        else:
            flat_list.append(element)
    return flat_list


"""Definition of main class - aubergine's public API."""
import importlib
import logging
from urllib.parse import urlparse
import falcon
from nadia.api import SchemaBuilder
import ymlref
from aubergine.extractors import ExtractorBuilder
from aubergine import utils

class Aubergine:
    """Main class for declaring an application.

    :param spec_dict: mapping defining OpeanAPI spefication.
    :type spec_dict: Mapping
    """

    def __init__(self, spec_dict):
        self.spec_dict = spec_dict

    def build_api(self, api_factory=falcon.API, **kwargs):
        """Build falcon API for this aubergine app.

        :param api_factory: a callable to obtain API instance from. Defaults to
         :py:class:`falcon.API`. Can be usefull if you wish to precreate API
          yourself (and for instance manipulate it somehow before it gets
          processed by Aubergine.
        :type api_factory: Callable
        :param kwargs: keyword arguments that can control api's creation. Usually
         not needed but here's the list of used `kwargs` anyway:
         -  'ex_factory': a :py:class:`aubergine.extractors.ExtractorBuilder` instance.
            If not provided, one will be created for you.
         - 'import_module': a callable that can be used to load modules/operations.
           More generally, it is a callable that will be called for every operation
           with argument being the part of operationId before the last dot. It should
           return any object with attribute matching part of operationId after the dot.
           For instance, for 'my.package.oper' operationId, import_module will be
           called with 'my.package' argument, and is supposed to return anything with
           `oper` atrribute (callable with argument matching the appropriate operation
           schema. Defaults to :py:func:`importlib.import_module`
        :returns: an API object
        :rtype: :py:class:`falcon.API`
        """

        logger = logging.getLogger('aubergine')
        self._greet()
        logger.info('Buidling app %s (version %s)',
                    self.spec_dict['info']['title'],
                    self.spec_dict['info']['version'])
        ex_factory = kwargs.get('ex_factory', ExtractorBuilder(SchemaBuilder.create()))
        import_module = kwargs.get('import_module', importlib.import_module)
        base_path = self._get_base_path()
        logger.info('Using base path %s', base_path)
        api = api_factory()
        for path, path_spec in self.spec_dict['paths'].items():
            logger.info('Creating handlers for path %s', path)
            handlers = {meth: utils.create_handler(path, op_spec, ex_factory, import_module)
                        for meth, op_spec in path_spec.items()}
            resource = utils.create_resource(handlers)
            api.add_route('/'.join((base_path.rstrip('/'), path.strip('/'))), resource)
        return api

    @classmethod
    def from_file(cls, path):
        """Shorthand for loading spec from given path and constructing Aubergine from it."""
        with open(path) as specfile:
            content = specfile.read()
            return cls(ymlref.load(content))

    def _get_base_path(self):
        base_paths = {urlparse(server['url']).path for server in self.spec_dict['servers']}
        if len(base_paths) != 1:
            raise ValueError("Base paths of servers differ, I don't know which one to use.")
        return next(iter(base_paths))

    def _greet(self):
        print(self.LOGO)

    LOGO = """
 █████╗ ██╗   ██╗██████╗ ███████╗██████╗  ██████╗ ██╗███╗   ██╗███████╗
██╔══██╗██║   ██║██╔══██╗██╔════╝██╔══██╗██╔════╝ ██║████╗  ██║██╔════╝
███████║██║   ██║██████╔╝█████╗  ██████╔╝██║  ███╗██║██╔██╗ ██║█████╗
██╔══██║██║   ██║██╔══██╗██╔══╝  ██╔══██╗██║   ██║██║██║╚██╗██║██╔══╝
██║  ██║╚██████╔╝██████╔╝███████╗██║  ██║╚██████╔╝██║██║ ╚████║███████╗
╚═╝  ╚═╝ ╚═════╝ ╚═════╝ ╚══════╝╚═╝  ╚═╝ ╚═════╝ ╚═╝╚═╝  ╚═══╝╚══════╝
                                                                       """


from synthesizer.utils.symbols import symbols
from synthesizer.utils import cleaners
import re

# Mappings from symbol to numeric ID and vice versa:
_symbol_to_id = {s: i for i, s in enumerate(symbols)}
_id_to_symbol = {i: s for i, s in enumerate(symbols)}

# Regular expression matching text enclosed in curly braces:
_curly_re = re.compile(r"(.*?)\{(.+?)\}(.*)")

def text_to_sequence(text, cleaner_names):
    """Converts a string of text to a sequence of IDs corresponding to the symbols in the text.

      The text can optionally have ARPAbet sequences enclosed in curly braces embedded
      in it. For example, "Turn left on {HH AW1 S S T AH0 N} Street."

      Args:
        text: string to convert to a sequence
        cleaner_names: names of the cleaner functions to run the text through

      Returns:
        List of integers corresponding to the symbols in the text
    """
    sequence = []

    # Check for curly braces and treat their contents as ARPAbet:
    while len(text):
        m = _curly_re.match(text)
        if not m:
            sequence += _symbols_to_sequence(_clean_text(text, cleaner_names))
            break
        sequence += _symbols_to_sequence(_clean_text(m.group(1), cleaner_names))
        sequence += _arpabet_to_sequence(m.group(2))
        text = m.group(3)

    # Append EOS token
    sequence.append(_symbol_to_id["~"])
    return sequence

def sequence_to_text(sequence):
    """Converts a sequence of IDs back to a string"""
    result = ""
    for symbol_id in sequence:
        if symbol_id in _id_to_symbol:
            s = _id_to_symbol[symbol_id]
            # Enclose ARPAbet back in curly braces:
            if len(s) > 1 and s[0] == "@":
                s = "{%s}" % s[1:]
            result += s
    return result.replace("}{", " ")

def _clean_text(text, cleaner_names):
    for name in cleaner_names:
        cleaner = getattr(cleaners, name)
        if not cleaner:
            raise Exception("Unknown cleaner: %s" % name)
        text = cleaner(text)
    return text

def _symbols_to_sequence(symbols):
    return [_symbol_to_id[s] for s in symbols if _should_keep_symbol(s)]

def _arpabet_to_sequence(text):
    return _symbols_to_sequence(["@" + s for s in text.split()])

def _should_keep_symbol(s):
    return s in _symbol_to_id and s not in ("_", "~")


import random
import config

def modexp(a,b,n):
    acc = 1
    while b > 0:
        if b % 2 == 1:
            acc = (acc*a) % n
        a = (a * a) % n
        b = b // 2

    return acc

#number -> string -> bytes
def encode_big(num):
    return str(num).encode('utf8')

#bytes -> string -> number
def decode_big(s):
    return int(s[0].decode('utf8'))

def dh(sock):
    #GENERO NUMERO A random.
    a = random.randint(1,config.P-2)
    print(f"N RANDOM {a}")

    #INVIO NUMERO GA
    ga = modexp(config.G, a, config.P)
    sock.sendall(encode_big(ga))     #encode_big --> encode della stringa
    print(f"GA INVIATO {ga}")

    #RICEVO NUMERO GB
    gb = decode_big(sock.recvfrom(4096))   #--> decode della stringa    #from_bytes(1, 'big')
    print(f"GB RICEVUTO {gb}")

    #CALCOLO NUMERO GAB
    gab = modexp(gb, a, config.P)
    print(f"GAB CALCOLATO {gab}")

    return gab


def on_received_string(receivedString):
    if receivedString == "heart":
        basic.show_leds("""
            . # . # .
                        # . # . #
                        # . . . #
                        . # . # .
                        . . # . .
        """)
    if receivedString == "smile":
        basic.show_leds("""
            . . . . .
                        . # . # .
                        . . . . .
                        # . . . #
                        . # # # .
        """)
    if receivedString == "diamond":
        basic.show_leds("""
            . . # . .
                        . # . # .
                        # . . . #
                        . # . # .
                        . . # . .
        """)
    if receivedString == "circle":
        basic.show_leds("""
            . # # # .
                        # . . . #
                        # . . . #
                        # . . . #
                        . # # # .
        """)
radio.on_received_string(on_received_string)

def on_received_value(name, value):
    global xValue, yValue, stop
    if name == "x":
        xValue = value
    if name == "y":
        yValue = value
    if name == "stop":
        stop = value
radio.on_received_value(on_received_value)

distance = 0
stop = 0
yValue = 0
xValue = 0
music.play_melody("C D E F G - - - ", 120)
radio.set_group(7)

def on_forever():
    global distance, stop
    distance = cuteBot.ultrasonic(cuteBot.SonarUnit.CENTIMETERS)
    if distance < 30 and stop == 0:
        cuteBot.stopcar()
        stop = 1
        music.play_melody("C5 B C5 B C5 B C5 B ", 200)
    cuteBot.motors(yValue + xValue, yValue - xValue)
basic.forever(on_forever)


#!/usr/bin/python3
# 12-pascal_triangle.py
"""Defines a Pascal's Triangle function."""

def pascal_triangle(n):
    """Represent Pascal's Triangle of size n.
    """
    if n <= 0:
        return []

    pascal = [[1]]
    while len(pascal) != n:
        pascal_tr = pascal[-1]
        tmp = [1]
        for i in range(len(pascal_tr) - 1):
            tmp.append(pascal_tr[i] + pascal_tr[i + 1])
        tmp.append(1)
        pascal.append(tmp)
    return pascal


# This script contains the code used for the illustration example

import matplotlib.pyplot as plt
import numpy as np
import os
import pandas as pd

# Set the seed
np.random.seed(42)

# Numerical and categorical variable lists
num_vars = ['AGE', 'PAREDINT', 'BMMJ1',
            'BFMJ2', 'HISEI', 'DURECEC', 'BSMJ', 'MMINS',
            'LMINS', 'SMINS', 'TMINS', 'FCFMLRTY', 'SCCHANGE', 'CHANGE', 'STUBMI',
            'ESCS', 'UNDREM', 'METASUM', 'METASPAM', 'ICTHOME', 'ICTSCH', 'HOMEPOS',
            'CULTPOSS', 'HEDRES', 'WEALTH', 'ICTRES', 'DISCLIMA', 'TEACHSUP',
            'DIRINS', 'PERFEED', 'EMOSUPS', 'STIMREAD', 'ADAPTIVITY', 'TEACHINT',
            'JOYREAD', 'SCREADCOMP', 'SCREADDIFF', 'PERCOMP', 'PERCOOP', 'ATTLNACT',
            'COMPETE', 'WORKMAST', 'GFOFAIL', 'EUDMO', 'SWBP', 'RESILIENCE',
            'MASTGOAL', 'GCSELFEFF', 'GCAWARE', 'ATTIMM', 'INTCULT', 'PERSPECT',
            'COGFLEX', 'RESPECT', 'AWACOM', 'GLOBMIND', 'DISCRIM', 'BELONG',
            'BEINGBULLIED', 'ENTUSE', 'HOMESCH', 'USESCH', 'INTICT', 'COMPICT',
            'AUTICT', 'SOIAICT', 'ICTCLASS', 'ICTOUTSIDE', 'INFOCAR', 'INFOJOB1',
            'INFOJOB2', 'FLCONFIN', 'FLCONICT', 'FLSCHOOL', 'FLFAMILY', 'BODYIMA',
            'SOCONPA']
cat_vars = ['ST004D01T', 'IMMIG', 'REPEAT']

# Load data
df = pd.read_csv('data/pisa_spain_sample_v2.csv')

# DATA PREPROCESSING
print('--- DATA PREPROCESSING ---')
from clearn.data_preprocessing import *

# Computre missing values
n_missing = df.isnull().sum().sum()
print('Missing values:', n_missing, f'({n_missing*100/df.size}%)')

# Generate missing values heat map
missing_values_heatmap(df, output_path=os.path.join("img", "missing_heatmap.jpg"))

# Impute missing values
df_imp = impute_missing_values(df, num_vars=num_vars, cat_vars=cat_vars)

# Plot the comparison between the distribution of a selection of variables before and after imputation
plot_imputation_distribution_assessment(df.loc[df_imp.index], df_imp, ['COMPICT', 'BODYIMA', 'PERCOOP', 'SOCONPA'],
                                        output_path=os.path.join("img", "imputation_distribution_assessment.jpg"))

# Remove outliers
df_, outliers = remove_outliers(df_imp, num_vars+cat_vars)

# DIMENSIONALITY REDUCTION
print('--- DIMENSIONALITY REDUCTION ---')
df_ = df_.reset_index(drop=True)
from clearn.dimensionality_reduction import DimensionalityReduction

# Instantiate class, project to a lower dimensionality with optimal number of components
dr = DimensionalityReduction(df_, num_vars=num_vars, cat_vars=cat_vars, num_algorithm='spca')
df_t = dr.transform(min_explained_variance_ratio=None)
print(dr.n_components_, len(dr.num_components_), len(dr.cat_components_))

# Explain fourth extracted component
print(dr.num_main_contributors(dim_idx=3))
dr.plot_num_main_contributors(dim_idx=3, output_path=os.path.join("img", "dim_red_main_contributors.jpg"))

# Explain component extracted from categorical variables
print(dr.cat_main_contributors_stats())
dr.plot_cat_main_contributor_distribution(dim_idx=0, output_path=os.path.join("img", "dim_red_cat_component.jpg"))

# Explained variance + elbow method
dr.plot_num_explained_variance(0.5, plots=['cumulative', 'normalized'],
                               output_path=os.path.join("img", "dim_red_explained_variance.jpg"))

# CLUSTERING
print('--- CLUSTERING ---')
from clearn.clustering import Clustering

# Instantiate class and compute clusters on projected space
cl = Clustering(df_t, algorithms=['kmeans', 'ward'], normalize=False)
cl.compute_clusters(max_clusters=21, prefix='STU')
print(cl.optimal_config_)

# Plot number of observations per cluster
cl.plot_clustercount(output_path=os.path.join("img", "cluster_count.jpg"))

# Plot normalized WSS curve for optimal k selection
cl.plot_optimal_components_normalized(output_path=os.path.join("img", "clustering_elbow_curve.jpg"))

# Compare intra-cluster means to global means
print(cl.compare_cluster_means_to_global_means())
cl.plot_cluster_means_to_global_means_comparison(xlabel='Principal Components', ylabel='Clusters',
                                                 levels=[-1, -0.67, -0.3, -0.15, 0.15, 0.3, 0.67, 1],
                                                 output_path=os.path.join("img", "clustering_intra_comparison.jpg"))

# Compare distributions of original variables by cluster
cl.plot_distribution_comparison_by_cluster(df_ext=df_[['ESCS', 'TEACHSUP']],
                                           output_path=os.path.join("img", "clustering_distribution_comparison.jpg"))

# 2-D plots
cl.plot_clusters_2D('dim_01', 'dim_02', output_path=os.path.join("img", "clustering_2d_plots.jpg"))

# Comparison of original categorical variable distribution by cluster
print(cl.describe_clusters_cat(df_['IMMIG'], cat_name='IMMIG', normalize=True))
cl.plot_cat_distribution_by_cluster(df_['IMMIG'], cat_label='IMMIG', cluster_label='Student clusters',
                                    output_path=os.path.join("img", "clustering_cat_comparison.jpg"))

# Assign clusters to data frame with original variables
df_['cluster'] = cl.df['cluster'].values
df_['cluster_cat'] = cl.df['cluster_cat'].values

# CLASSIFIER
print('--- CLASSIFIER ---')
from clearn.classifier import Classifier
np.random.seed(42)

# Instantiate the class with the original variables. As target, we set the clusters computed above
var_list = list(df_.columns[1:-3])
classifier = Classifier(df_, predictor_cols=var_list, target=df_['cluster'], num_cols=num_vars, cat_cols=cat_vars)

# Build a pipeline with feature selection, hyperparameter tuning, and model fitting. For feature selection, we make sure
# that variable ESCS (economic, social and cultural status) gets selected (features_to_keep=['ESCS']). Hyperparameter
# optimization is performed through exhaustive grid search for different values of the number of estimators
# (n_estimators=[30, 60]), eta (eta=[0.15, 0.25]), and maximum tree depth (max_depth=[3, 5, 7]). The algorithms used are
# those configured by default, i.e. random forest for feature selection and xgboost for the final classification model.
classifier.train_model(features_to_keep=['ESCS'], hyperparameter_tuning=True,
                       param_grid=dict(n_estimators=[30, 60], eta=[0.15, 0.25], max_depth=[3, 5, 7]))

# Get global feature importance of the five most important features
print(classifier.feature_importances.head())

# Plot global feature importance
classifier.plot_shap_importances(output_path=os.path.join("img", "classifier_global_feature_importance.jpg"))
plt.clf()

# Plot local importance for clusters with identifiers 1 and 2
classifier.plot_shap_importances_beeswarm(class_id=1,
                                          output_path=os.path.join("img", "classifier_local_importance_cl1.jpg"))
plt.clf()
classifier.plot_shap_importances_beeswarm(class_id=2,
                                          output_path=os.path.join("img", "classifier_local_importance_cl2.jpg"))
plt.clf()

# Performance assessment
# Hyperparameter tuning results
print(classifier.hyperparameter_tuning_metrics())

# Confusion matrix
classifier.plot_confusion_matrix(output_path=os.path.join("img", "classifier_confusion_matrix.jpg"))

# Classification report
print(classifier.classification_report())

# ROC curves
classifier.plot_roc_curves(output_path=os.path.join("img", "classifier_roc_curves.jpg"))


import argparse
import pig.evaluation

def get_args():
    parser = argparse.ArgumentParser()
    parser.add_argument("--versions", type=str, nargs="+")

    return parser.parse_args()

if __name__ == "__main__":
    args = get_args()
    pig.evaluation.full_run(gpus=1, versions=args.versions)


# Problem Set 4A
# Name: llewellyndm
# Collaborators:
# Time Spent: x:xx

def get_permutations(sequence):
    '''
    Enumerate all permutations of a given string

    sequence (string): an arbitrary string to permute. Assume that it is a
    non-empty string.

    You MUST use recursion for this part. Non-recursive solutions will not be
    accepted.

    Returns: a list of all permutations of sequence

    Example:
    >>> get_permutations('abc')
    ['abc', 'acb', 'bac', 'bca', 'cab', 'cba']

    Note: depending on your implementation, you may return the permutations in
    a different order than what is listed here.
    '''
    if len(sequence) == 1:
        return [sequence]
    else:
        previous_perms = get_permutations(sequence[1:]) #recursive step
        first_letter = sequence[0]
        permutations = []
        for perm in previous_perms:
            #for each character in that permutation
            for i in range(len(perm)):
                # make a new permutation of sequence by inserting the first
                # letter of sequence before that character
                new_perm = list(perm)
                new_perm.insert(i, first_letter)
                new_perm_joined = ''.join(new_perm)
                # ensure permutation is not a duplicate
                if new_perm_joined not in permutations:
                    permutations.append(new_perm_joined)
            # make an extra permutation by placing the first letter at the end
            # of the permutation
            extra_perm = list(perm)
            extra_perm.append(first_letter)
            extra_perm_joined = ''.join(extra_perm)
            if extra_perm_joined not in permutations:
                permutations.append(extra_perm_joined)
        return permutations

if __name__ == '__main__':
#    #EXAMPLE
#    example_input = 'abc'
#    print('Input:', example_input)
#    print('Expected Output:', ['abc', 'acb', 'bac', 'bca', 'cab', 'cba'])
#    print('Actual Output:', get_permutations(example_input))

#    # Put three example test cases here (for your sanity, limit your inputs
#    to be three characters or fewer as you will have n! permutations for a
#    sequence of length n)

    pass #delete this line and replace with your code here


import ujson

import ioc.locator as locator

class ConfigService:
    def __init__(self):
        self._log_service = locator.log_service

    def read_config(self, fileName):
        cfg = dict()
        f = None
        try:
            f = open(fileName, "r")
            json_content = f.read()
            cfg = ujson.loads(json_content)
        except Exception as e:
            self._log_service.log(
                "error reading config or config does not exists "
                + fileName
                + " "
                + str(e)
            )
        finally:
            if f != None:
                f.close()
        return cfg

    def write_config(self, fileName, config):
        f = None
        try:
            json_content = ujson.dumps(config)
            f = open(fileName, "w")
            f.write(json_content)
        except Exception as e:
            self._log_service.log("error writing config " + fileName + " " + str(e))
        finally:
            if f != None:
                f.close()


class Solution:
    def palindromePairs(self, words: List[str]) -> List[List[int]]:
        N = len(words)
        mp = {x : i for i, x in enumerate(words)}
        res = []

        for index, word in enumerate(words):
            for j in range(len(word) + 1):
                prefix = word[:j]
                suffix = word[j:]

                if prefix == prefix[::-1]:
                    suffixR = suffix[::-1]
                    if word != suffixR and suffixR in mp:
                        res.append([mp[suffixR], index])

                if j != len(word) and suffix == suffix[::-1]:
                    prefixR = prefix[::-1]
                    if word != prefixR and prefixR in mp:
                        res.append([index, mp[prefixR]])

        return res


from operator import index
from django.urls import path
from . import views

urlpatterns=[
    path('baabtrahome', views.baabtra_home),
    path('javascript', views.java_script),
    path('newjs', views.new_js),
    path('condjs', views.cond_js),
    path('loop', views.loop),
    path('largest', views.largest_among_two),
    path('largestofThree', views.largest_among_three),
    path('sumOfdigits', views.sum_of_digits),
    path('digitReverse', views.digit_reverse),
    path('palindrome', views.palindrome),
    path('greatestdigit', views.greatest_digit),
    path('numberOfdigit', views.numberOf_digit),
    path('hollowsquare', views.hollow_square),
    path('mirror_right_triangle', views.mirror_right_triangle),
    path('pyramid', views.pyramid_star),
    path('inv_pyramid', views.inverted_pyramid),
    path('right_triangle', views.right_triangle),
    path('half_diamond', views.half_diamond),
    path('inv_right_tri', views.inv_right_tri),
    path('do_while', views.do_while),
    path('dom', views.dom),
    path('dom_methods', views.dom_methods),
    path('simple_calculator', views.simple_calculator),
    path('todo', views.to_do),
    path('password', views.password),
    path('jquery', views.jQuery),
    path('addproduct', views.add_product),
    path('viewproduct', views.view_product),
    path('product', views.product),
    path('pTag', views.pjQuery),
    path('webdemo', views.webDemo),
    path('validation', views.validation),
]


#softpage
from Tkinter import *
import os
from datetime import datetime;
from PIL import Image,ImageTk
import urllib2
#creating instance of TK
root=Tk()

photo=ImageTk.PhotoImage(Image.open("C:\\Python27\\working face recg\\best2.jpg"))
root.configure(background='black')

root.geometry("1000x654")
def creator():

    os.system("dataSetCreator.py")

def train():

    os.system("trainner.py")

def takeattend():

    os.system("detector.py")

def close():

    root.destroy()

def attend():
    os.startfile("C:\\Python27\\working face recg\\attendance\\"+str(datetime.now().date())+'.xls')
def slide():
    os.startfile("C:\\Python27\\working face recg\\ppt_project"+'.pptx')

def send():
    c=urllib2.urlopen('http://localhost/FaceRecog_emailer/sendmailer.php')
    thpage=c.read()
    print(thpage)

root.title("FACE RECOGNITION ATTENDANCE SYSTEM")
Label(root,image=photo,bg='black',height=650,width=983).grid(row=0,rowspan=380,columnspan=100,sticky=N+E+W+S,padx=7,pady=7)

Button(root,text="DataSet Creator",font=("times new roman",20,'bold','italic'),bg="orange",fg='black',command=creator,height=0,width=18).grid(row=320,column=0,columnspan=2,sticky=W+E+N+S,padx=70,pady=7)

Button(root,text="Train Dataset",font=("times new roman",20,'bold','italic'),bg="orange",fg='black',command=train,height=1,width=18).grid(row=330,column=0,columnspan=2,sticky=N+E+W+S,padx=70,pady=7)

Button(root,text="Recognize + Attendance",font=('times new roman',20,'bold','italic'),bg="orange",fg="black",command=takeattend,height=1,width=18).grid(row=340,column=0,columnspan=2,sticky=N+E+W+S,padx=70,pady=7)

Button(root,text="View Attendance Sheet",font=('times new roman',20,'bold','italic'),bg="orange",fg="black",command=attend,height=1,width=18).grid(row=350,column=0,columnspan=2,sticky=N+E+W+S,padx=70,pady=7)

Button(root,text=" Exit  ",font=('times new roman',20,'bold','italic'),bg="orange",fg="black",command=close,height=1,width=18).grid(row=350,column=96,columnspan=4,sticky=N+E+W+S,padx=60,pady=7)

#Button(root,text="Developers",font=('times new roman',20,'bold','italic'),bg="orange",fg="black",command=close,height=1,width=18).grid(row=330,column=96,columnspan=4,sticky=N+E+W+S,padx=60,pady=7)
Button(root,text="Project Report",font=('times new roman',20,'bold','italic'),bg="orange",fg="black",command=slide,height=1,width=18).grid(row=340,column=96,columnspan=4,sticky=N+E+W+S,padx=60,pady=7)

Button(root,text="Send Attendance",font=('times new roman',20,'bold','italic'),bg="orange",fg="black",command=send,height=1,width=18).grid(row=330,column=96,columnspan=4,sticky=N+E+W+S,padx=60,pady=7)

root.mainloop()


import logging
import os
import plistlib
from typing import Optional

from mvt.common.module import DatabaseNotFoundError
from mvt.ios.versions import get_device_desc_from_id, is_ios_version_outdated

from ..base import IOSExtraction

class BackupInfo(IOSExtraction):
    """This module extracts information about the device and the backup."""

    def __init__(
        self,
        file_path: Optional[str] = None,
        target_path: Optional[str] = None,
        results_path: Optional[str] = None,
        module_options: Optional[dict] = None,
        log: logging.Logger = logging.getLogger(__name__),
        results: Optional[list] = None,
    ) -> None:
        super().__init__(
            file_path=file_path,
            target_path=target_path,
            results_path=results_path,
            module_options=module_options,
            log=log,
            results=results,
        )

        self.results = {}

    def run(self) -> None:
        info_path = os.path.join(self.target_path, "Info.plist")
        if not os.path.exists(info_path):
            raise DatabaseNotFoundError(
                "No Info.plist at backup path, unable to extract device " "information"
            )

        with open(info_path, "rb") as handle:
            info = plistlib.load(handle)

        fields = [
            "Build Version",
            "Device Name",
            "Display Name",
            "GUID",
            "ICCID",
            "IMEI",
            "MEID",
            "Installed Applications",
            "Last Backup Date",
            "Phone Number",
            "Product Name",
            "Product Type",
            "Product Version",
            "Serial Number",
            "Target Identifier",
            "Target Type",
            "Unique Identifier",
            "iTunes Version",
        ]

        for field in fields:
            value = info.get(field, None)

            if field == "Product Type" and value:
                product_name = get_device_desc_from_id(value)
                if product_name:
                    self.log.info("%s: %s (%s)", field, value, product_name)
                else:
                    self.log.info("%s: %s", field, value)
            else:
                self.log.info("%s: %s", field, value)

            self.results[field] = value

        if "Product Version" in info:
            is_ios_version_outdated(info["Product Version"], log=self.log)


# -*- coding: utf-8 -*-

import zipfile, io
import pandas as pd
import glob
from dnn_model import create_model_lstm, create_model_example2
from ai_check import evaluate_regression
from data_set import yahoo_reviews, tokenize_reviews

from keras.preprocessing import sequence
from keras.preprocessing.text import Tokenizer

from ex_word2vec import word2vec_model
import numpy as np

w2v_model = word2vec_model()

def create_sentence_vector(X, max_word_length, word_embedding_dim):
    """
    文章ベクトルを生成する
    中身的には分散表現した単語を配列に入れている感じ。
    """

    fill_vector = np.empty((0, max_word_length, word_embedding_dim))

    for words in X:
        vector = sentence_2D_vector(words, word_embedding_dim)
        vector_padding = padding_zero(vector, max_word_length, word_embedding_dim)
        vector_reshaped = vector_padding.reshape(1, max_word_length, word_embedding_dim)

        fill_vector = np.vstack((fill_vector, vector_reshaped))
    return fill_vector

def padding_zero(sentence_vector, max_sentence, word_embedding_dim):
    """
    文長の違いを吸収する。
    短い文は0ベクトルを入れ、末尾に実ベクトルを入れる。
    """
    zero_vector = np.zeros((max_sentence, word_embedding_dim))
    offset = max_sentence - sentence_vector.shape[0]
    zero_vector[offset:, :] = sentence_vector
    return zero_vector

def sentence_2D_vector(words, word_embedding_dim):
    """
    １文のベクトル表現を得る
    """
    sentence_vector = np.empty((0, word_embedding_dim)) # 50と言う数字は学習済みのword2vecが50次元でoutされているため。
    for word in words.split():
        # 単語の分散ベクトル
        try:
            word_vector = w2v_model.wv[word]
            sentence_vector = np.vstack((sentence_vector, word_vector))
        except Exception as e:
            pass

    return sentence_vector

def max_length_in_sentence_vectors(X):
    """
    最大文書長はいくつか？
    """
    sentence_vectors_length = [len(vectors.split()) for vectors in X]
    return np.max(sentence_vectors_length)

"""
example1では、学習データの中でトークン化を行い
学習させていた。

今回は、学習データにない単語を検証で使いたい場合にword2vecを使って文章ベクトルを作って学習させてみる。
"""

word_embedding_dim = 50

X, Y = yahoo_reviews()
max_length = max_length_in_sentence_vectors(X)

X_size = len(X) # 学習データ数
max_words_count = max_length + 10 # 最大文長

X_sentence = create_sentence_vector(X, max_words_count, word_embedding_dim)

model = create_model_example2(input_shape=(max_words_count, word_embedding_dim))
model.fit(X_sentence, Y, epochs=15, shuffle=True, validation_split=0.1)

y_pred = model.predict(X_sentence)

# AIと実際のデータにどれくらいの差があるか
evaluate_regression(Y, y_pred)


##
## Tempo de Jogo
## URI ONLINE - Problema 1046 - Iniciante
##
## Created by Jon on 15.03.2016
##

# -*- coding: utf-8 -*-

string = raw_input().split()

hIni = int(string[0])
hFim = int(string[1])

duracao = -(hIni-24) + hFim  ## Essa formula e baseada na observacao de que hIni = 16 e hFim = 2, entao a duracao e de 10h.

if duracao>24:
	print ("O JOGO DUROU %d HORA(S)"%(duracao-24))
else:
	print ("O JOGO DUROU %d HORA(S)"%(duracao))