'''
Given a list accounts, each element accounts[i] is a list of strings, where the first element accounts[i][0] is a name, and the rest of the elements are emails representing emails of the account.

Now, we would like to merge these accounts. Two accounts definitely belong to the same person if there is some email that is common to both accounts. Note that even if two accounts have the same name, they may belong to different people as people could have the same name. A person can have any number of accounts initially, but all of their accounts definitely have the same name.

After merging the accounts, return the accounts in the following format: the first element of each account is the name, and the rest of the elements are emails in sorted order. The accounts themselves can be returned in any order.

Example 1:
Input:
accounts = [["John", "johnsmith@mail.com", "john00@mail.com"], ["John", "johnnybravo@mail.com"], ["John", "johnsmith@mail.com", "john_newyork@mail.com"], ["Mary", "mary@mail.com"]]
Output: [["John", 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com'],  ["John", "johnnybravo@mail.com"], ["Mary", "mary@mail.com"]]
Explanation:
The first and third John's are the same person as they have the common email "johnsmith@mail.com".
The second John and Mary are different people as none of their email addresses are used by other accounts.
We could return these lists in any order, for example the answer [['Mary', 'mary@mail.com'], ['John', 'johnnybravo@mail.com'],
['John', 'john00@mail.com', 'john_newyork@mail.com', 'johnsmith@mail.com']] would still be accepted.
Note:

The length of accounts will be in the range [1, 1000].
The length of accounts[i] will be in the range [1, 10].
The length of accounts[i][j] will be in the range [1, 30].
'''

from collections import defaultdict

class Solution:
    def accountsMerge(self, accounts: List[List[str]]) -> List[List[str]]:
        email_to_name = {}
        graph = defaultdict(set)
        for account in accounts:
            name = account[0]
            first_email = account[1]
            email_to_name[first_email] = name
            for email in account[1:]:
                graph[first_email].add(email)
                graph[email].add(first_email)
                email_to_name[email] = name

        seen = set()
        ans = []
        for email in graph:
            if email not in seen:
                seen.add(email)
                stack = [email]
                component = []
                while stack:
                    node = stack.pop()
                    component.append(node)
                    for nei in graph[node]:
                        if nei not in seen:
                            seen.add(nei)
                            stack.append(nei)
                ans.append([email_to_name[email]] + sorted(component))

        return ans


import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from sklearn.linear_model import LinearRegression

if __name__ == '__main__':
    # 将csv数据读取为pandas对象
    fund = pd.read_csv('./csv/001112.csv', dtype={'fcode': str})
    # 转化时间字符串为时间
    fund['fdate'] = pd.to_datetime(fund['fdate'])
    # 设置时间列为索引，并升序排列
    fund = fund.set_index('fdate').sort_index(ascending=False)
    # x轴为数据2017后的索引 y为2017后的NAV
    x = fund.loc['2017'].index
    y = fund.loc['2017']['NAV']
    # 将xy数据转化为矩阵:
    # 将时间转化为时间戳
    x = x.astype(np.int64)
    # 将时间戳转化为1列多行的二维数组
    x = x.values.reshape(-1, 1)
    y = y.values.reshape(-1, 1)

    # 放入数据开始训练
    lr = LinearRegression()
    lr.fit(x, y)
    # 构建一个时间戳，来预测y轴的值
    test_x = pd.to_datetime(np.array(['2017-9-30', '2017-10-1'])).astype(np.int64).values.reshape(-1, 1)
    # 预测到Y轴的值 [[1.41483561]
    #  [1.41626252]]
    new_y = lr.predict(test_x)
    # 把拟合线画出来：如果y为训练预测出的值，则线条为直线拟合线
    x_date = fund.loc['2017'].index
    # 走势点图
    plt.scatter(x_date, fund.loc['2017']['NAV'])
    plt.plot(x_date, lr.predict(x), 'r')
    plt.show()
    print(new_y)


import random
from models.storage import *

##########################
# Monte-Carlo Prediction #
##########################
# Source : Reinforcement Learning : an introcuction, Sutton & Barto, p.92

class MCPrediction:
    def __init__(self, game, gamma=0.8, default_q=999):
        self.V = {}
        self.Returns = StoreAverage()
        self.gamma = gamma
        self.game = game
        self.name = "Monte-Carlo prediction"

    def argmax(self, s):
        actions = self.game.possible_actions(s)
        if len(actions) == 0: return None

        random.shuffle(actions)
        scores = []
        for a in actions:
            sp = self.game.step(s, a)
            scores.append((self.V.Get(sp), a))

        return max(scores)[1]

    def make_game(self, initial_state, smart=False):
        s = initial_state
        states = [s]
        results = [self.game.reward(s)]
        while not self.game.is_terminal(s):
            if not smart: a = self.game.random_action(s)
            else: a = self.choice(s, True)

            s = self.game.step(s, a)
            states.append(s)
            results.append(self.game.reward(s))
        return states, results

    def train(self, n=1000, smart=False):
        initial_state = self.game.initial_state()
        for i in range(n):
            G = 0
            S, R = self.make_game(initial_state, smart)
            S, R = reversed(S), reversed(R)
            for s, r in zip(S, R):
                G = self.gamma * G + r
                self.Returns.Add(s, G)
                self.V.Set(s, self.Returns.Average(s))

################################
# Monte-Carlo Exploring Starts #
################################
# Source : Reinforcement Learning : an introcuction, Sutton & Barto, p.99

class MCES:
    def __init__(self, game, gamma=0.8, defaultq=0):
        self.P = {}
        self.Returns = StoreAverage()
        self.Q = StoreValue(defaultq)
        self.gamma = gamma
        self.game = game
        self.name = "Monte-Carlo Exploring Starts"

    def argmax(self, s):
        actions = self.game.possible_actions(s)
        if len(actions) == 0: return None

        random.shuffle(actions)
        scores = []
        for a in actions:
            scores.append((self.Q.Get((a, s)), a))

        return max(scores, key=lambda x: x[0])[1]

    def make_game(self, initial_state, epsilon=0.5):
        s = initial_state
        states = [s]
        rewards = [0]
        actions = []
        while not self.game.is_terminal(s):
            rand = random.uniform(0, 1)
            if rand < epsilon: a = self.game.random_action(s)
            else: a = self.argmax(s)

            actions.append(a)
            s = self.game.step(s, a)
            states.append(s)
            rewards.append(self.game.reward(s))
        actions.append(None)

        return states, rewards, actions

    def train(self, n=1000, epsilon=0.5):
        initial_state = self.game.initial_state()
        for i in range(n):
            G = 0
            S, R, A = self.make_game(initial_state, epsilon)
            S, R, A = reversed(S), reversed(R), reversed(A)
            for s, r, a in zip(S, R, A):
                G = self.gamma * G + r
                self.Returns.Add((a,s), G)
                self.Q.Set((a, s), self.Returns.Average((a,s)))
                self.P[s] = self.argmax(s)

##########################
#       Q-Learning       #
##########################
# Source : Reinforcement Learning : an introcuction, Sutton & Barto, p.131

class QLearning:
    def __init__(self, game, alpha=0.3, gamma=0.8, defaultq=0):
        self.Q = StoreValue(defaultq)
        self.alpha = alpha
        self.gamma = gamma
        self.game = game
        self.name = "Q-Learning"

    def best_Q(self, s):
        scores = []
        A = self.game.possible_actions(s)
        random.shuffle(A)
        A.append(None)
        for a in A:
            scores.append(self.Q.Get((a, s)))

        return max(scores)

    def argmax(self, s):
        actions = self.game.possible_actions(s)
        if len(actions) == 0: return None

        random.shuffle(actions)
        scores = []
        for a in actions:
            scores.append((self.Q.Get((a, s)), a))

        return max(scores, key=lambda x: (x[0], -x[1]))[1]

    def make_game(self, initial_state, epsilon=0.5):
        s = initial_state
        actions = []
        while not self.game.is_terminal(s):
            rand = random.uniform(0, 1)
            if rand < epsilon: a = self.game.random_action(s)
            else: a = self.argmax(s)
            sp = self.game.step(s, a)
            r = self.game.reward(s)

            self.Q.Set((a, s), self.Q.Get((a, s)) + self.alpha * (r + self.gamma * self.best_Q(sp) - self.Q.Get((a, s))))
            s = sp

        r = self.game.reward(s)
        self.Q.Set((None, s), self.Q.Get((None, s)) + self.alpha * r)

        return actions

    def train(self, n=1000, epsilon=0.5):
        initial_state = self.game.initial_state()
        for i in range(n):
            s = initial_state
            A = self.make_game(initial_state, epsilon)

##############################
#       Bandit Problem       #
##############################
# Source : Reinforcement Learning : an introcuction, Sutton & Barto, p.32

class Bandit:
    def __init__(self, game, alpha=0.3, gamma=0.8):
        self.Q = StoreValue(9999)
        self.N = StoreValue()
        self.alpha = alpha
        self.gamma = gamma
        self.game = game
        self.name = "Bandit problem"

    def argmax(self, s):
        actions = self.game.possible_actions(s)
        if len(actions) == 0: return None

        random.shuffle(actions)
        scores = []
        for a in actions:
            scores.append((self.Q.Get((a, s)), a))

        return max(scores, key=lambda x: (x[0]))[1]

    def make_game(self, initial_state, epsilon=0.5):
        s = initial_state
        actions = []
        while not self.game.is_terminal(s):
            rand = random.uniform(0, 1)
            if rand < epsilon: a = self.game.random_action(s)
            else: a = self.argmax(s)

            sp = self.game.step(s, a)
            r = self.game.reward(s)

            self.N.Set((a, s), self.N.Get((a, s))+1)
            Qa = self.Q.Get((a, s))
            self.Q.Set((a, s), Qa + (r-Qa) / self.N.Get((a, s)))
            s = sp

        r = self.game.reward(s)
        Qa = self.Q.Get((None, s))
        self.N.Set((None, s), self.N.Get((None, s))+1)
        self.Q.Set((None, s), Qa + (r-Qa) / self.N.Get((None, s)))

        return actions

    def train(self, n=1000, epsilon=0.5):
        initial_state = self.game.initial_state()
        for i in range(n):
            s = initial_state
            A = self.make_game(initial_state, epsilon)

##############################
#        n-step SARSA        #
##############################
# Source : Reinforcement Learning : an introcuction, Sutton & Barto, p.147
import numpy as np

class NStepSarsa:
    def __init__(self, game, n=5, alpha=0.5, gamma=0.8):
        self.Q = StoreValue(9999)
        self.alpha = alpha
        self.gamma = gamma
        self.game = game
        self.n = n
        self.name = "n-Step SARSA"

    def argmax(self, s):
        actions = self.game.possible_actions(s)
        if len(actions) == 0: return None

        random.shuffle(actions)
        scores = []
        for a in actions:
            scores.append((self.Q.Get((a, s)), a))

        return max(scores, key=lambda x: (x[0]))[1]

    def choose_action(self, s, epsilon):
        rand = random.uniform(0, 1)
        if rand < epsilon: a = self.game.random_action(s)
        else: a = self.argmax(s)
        return a

    def make_game(self, initial_state, epsilon=0.5):
        t = 0
        T = 999999999

        s = initial_state
        a = self.choose_action(s, epsilon)
        actions = [a]
        states = [s]
        rewards = [0]

        while True:
            if t < T:
                s = self.game.step(s, a)
                r = self.game.reward(s)

                states.append(s)
                rewards.append(r)

                if self.game.is_terminal(s): T = t+1
                else:
                    a = self.choose_action(s, epsilon)
                    actions.append(a)

            tau = t-self.n+1
            if tau >= 0:
                G = 0
                for i in range(tau+1, min(tau+self.n+1, T+1)):
                    G += np.power(self.gamma, i-tau-1) * rewards[i]
                if tau + self.n < T:
                    S,A = states[tau+self.n], actions[tau+self.n]
                    G += np.power(self.gamma, self.n) * self.Q.Get((A,S))

                S,A = states[tau], actions[tau]
                QSA = self.Q.Get((A,S))
                self.Q.Set((A,S), QSA + self.alpha * (G - QSA))

            if tau == T - 1: break
            t += 1

        return actions

    def train(self, n=1000, epsilon=0.5):
        initial_state = self.game.initial_state()
        for i in range(n):
            s = initial_state
            A = self.make_game(initial_state, epsilon)


import json
from pathlib import Path
import numpy as np
import torch
import torch.utils.data

from PIL import Image
from panopticapi.utils import rgb2id

from utils.utils import masks_to_boxes
from dataset.utils import make_coco_transforms

city2int = {
    "aachen": 0,
    "bremen": 1,
    "darmstadt": 2,
    "erfurt": 3,
    "hanover": 4,
    "krefeld": 5,
    "strasbourg": 6,
    "tubingen": 7,
    "weimar": 8,
    "bochum": 9,
    "cologne": 10,
    "dusseldorf": 11,
    "hamburg": 12,
    "jena": 13,
    "monchengladbach": 14,
    "stuttgart": 15,
    "ulm": 16,
    "zurich": 17,
    "frankfurt": 18,
    "lindau": 19,
    "munster": 20,
    "berlin": 21,
    "bielefeld": 22,
    "bonn": 23,
    "leverkusen": 24,
    "mainz": 25,
    "munich": 26,
}

int2city = {v: k for k, v in city2int.items()}

def imgid2int(id):
    city, f, s = id.split("_")
    return int(int(s) + int(f) * 1e6 + city2int[city] * 1e12)

def int2imgid(num):
    cityn = num // int(1e12)
    f = (num - int(cityn * 1e12)) // int(1e6)
    s = num % int(1e6)
    return int2city[cityn] + "_" + str(f).zfill(6) + "_" + str(s).zfill(6)

class CityscapesPanoptic:
    def __init__(
        self, img_folder, ann_folder, ann_file, transforms=None, return_masks=True
    ):
        with open(ann_file, "r") as f:
            self.coco = json.load(f)

        # sort 'images' field so that they are aligned with 'annotations'
        # i.e., in alphabetical order
        self.coco["images"] = sorted(self.coco["images"], key=lambda x: x["id"])

        self.img_folder = img_folder
        self.ann_folder = ann_folder
        self.ann_file = ann_file
        self.transforms = transforms
        self.return_masks = return_masks

    def __getitem__(self, idx):
        ann_info = (
            self.coco["annotations"][idx]
            if "annotations" in self.coco
            else self.coco["images"][idx]
        )
        city = ann_info["image_id"].split("_")[0]
        img_path = (
            Path(self.img_folder) / city / (ann_info["image_id"] + "_leftImg8bit.png")
        )
        ann_path = Path(self.ann_folder) / ann_info["file_name"]

        img = Image.open(img_path).convert("RGB")
        w, h = img.size
        if "segments_info" in ann_info:
            masks = np.asarray(Image.open(ann_path), dtype=np.uint32)
            masks = rgb2id(masks)

            ids = np.array([ann["id"] for ann in ann_info["segments_info"]])
            masks = masks == ids[:, None, None]

            masks = torch.as_tensor(masks, dtype=torch.uint8)
            labels = torch.tensor(
                [ann["category_id"] for ann in ann_info["segments_info"]],
                dtype=torch.int64,
            )

        target = {}
        target["image_id"] = torch.tensor(
            [
                imgid2int(
                    ann_info["image_id"] if "image_id" in ann_info else ann_info["id"]
                )
            ]
        )
        if self.return_masks:
            target["masks"] = masks
        target["labels"] = labels

        target["boxes"] = masks_to_boxes(masks)

        target["size"] = torch.as_tensor([int(h), int(w)])
        target["orig_size"] = torch.as_tensor([int(h), int(w)])
        if "segments_info" in ann_info:
            for name in ["iscrowd", "area"]:
                target[name] = torch.tensor(
                    [ann[name] for ann in ann_info["segments_info"]]
                )

        if self.transforms is not None:
            img, target = self.transforms(img, target)

        return img, target

    def __len__(self):
        return len(self.coco["images"])

    def get_height_and_width(self, idx):
        img_info = self.coco["images"][idx]
        height = img_info["height"]
        width = img_info["width"]
        return height, width

def build_cityscapes_panoptic(image_set, args):
    img_folder_root = Path(args.coco_path)
    ann_folder_root = Path(args.coco_panoptic_path)
    assert img_folder_root.exists(), f"provided path {img_folder_root} does not exist"
    assert ann_folder_root.exists(), f"provided path {ann_folder_root} does not exist"

    ann_file = {
        "train": "/content/drive/MyDrive/cityscapes/gtFine/cityscapes_panoptic_train.json",
        "val": "/content/drive/MyDrive/cityscapes/gtFine/cityscapes_panoptic_val.json",
    }

    img_folder_path = {
        "train": "/content/drive/MyDrive/cityscapes/leftImg8bit/train",
        "val": "/content/drive/MyDrive/cityscapes/leftImg8bit/val",
    }

    ann_folder = {
        "train": "/content/drive/MyDrive/cityscapes/gtFine/cityscapes_panoptic_train",
        "val": "/content/drive/MyDrive/cityscapes/gtFine/cityscapes_panoptic_val",
    }

    dataset = CityscapesPanoptic(
        img_folder_path[image_set],
        ann_folder[image_set],
        ann_file[image_set],
        transforms=make_coco_transforms(image_set),
        return_masks=args.masks,
    )

    return dataset

def build_dataset(image_set, args):
    if args.dataset_file == "coco_panoptic":
        # to avoid making panopticapi required for coco
        return build_cityscapes_panoptic(image_set, args)
    raise ValueError(f"dataset {args.dataset_file} not supported")


import string, itertools

ascii_lowercases = list(string.ascii_lowercase)

MAX_WORD_LENGTH = 5

for i in range(1, MAX_WORD_LENGTH + 1):
    charlist = [[x for x in ascii_lowercases]] * i

    for combinations in itertools.product(*charlist):
        combinations = "".join(combinations)
        with open("../wordlist.txt", "a") as file:
            file.write(combinations + "\n")
    print("finished!", i)


#!/usr/bin/env python
# -*- coding: UTF-8 -*-
# File: cifar-convnet.py
# Author: Yuxin Wu <ppwwyyxxc@gmail.com>
import tensorflow as tf
import argparse
import numpy as np
import os

from tensorpack import *
import tensorpack.tfutils.symbolic_functions as symbf
from tensorpack.tfutils.summary import *
from tensorpack.utils.gpu import get_nr_gpu

"""
A small convnet model for Cifar10 or Cifar100 dataset.

Cifar10:
    91% accuracy after 50k step.
    19.3 step/s on Tesla M40

Not a good model for Cifar100, just for demonstration.
"""

class Model(ModelDesc):
    def __init__(self, cifar_classnum):
        super(Model, self).__init__()
        self.cifar_classnum = cifar_classnum

    def _get_input_vars(self):
        return [InputVar(tf.float32, [None, 30, 30, 3], 'input'),
                InputVar(tf.int32, [None], 'label')
               ]

    def _build_graph(self, input_vars):
        image, label = input_vars
        is_training = get_current_tower_context().is_training
        keep_prob = tf.constant(0.5 if is_training else 1.0)

        if is_training:
            tf.image_summary("train_image", image, 10)

        image = image / 4.0     # just to make range smaller
        with argscope(Conv2D, nl=BNReLU, use_bias=False, kernel_shape=3):
            logits = LinearWrap(image) \
                    .Conv2D('conv1.1', out_channel=64) \
                    .Conv2D('conv1.2', out_channel=64) \
                    .MaxPooling('pool1', 3, stride=2, padding='SAME') \
                    .Conv2D('conv2.1', out_channel=128) \
                    .Conv2D('conv2.2', out_channel=128) \
                    .MaxPooling('pool2', 3, stride=2, padding='SAME') \
                    .Conv2D('conv3.1', out_channel=128, padding='VALID') \
                    .Conv2D('conv3.2', out_channel=128, padding='VALID') \
                    .FullyConnected('fc0', 1024 + 512, nl=tf.nn.relu) \
                    .tf.nn.dropout(keep_prob) \
                    .FullyConnected('fc1', 512, nl=tf.nn.relu) \
                    .FullyConnected('linear', out_dim=self.cifar_classnum, nl=tf.identity)()

        cost = tf.nn.sparse_softmax_cross_entropy_with_logits(logits, label)
        cost = tf.reduce_mean(cost, name='cross_entropy_loss')

        wrong = symbf.prediction_incorrect(logits, label)
        # monitor training error
        add_moving_summary(tf.reduce_mean(wrong, name='train_error'))

        # weight decay on all W of fc layers
        wd_cost = tf.mul(0.0004,
                         regularize_cost('fc.*/W', tf.nn.l2_loss),
                         name='regularize_loss')
        add_moving_summary(cost, wd_cost)

        add_param_summary([('.*/W', ['histogram'])])   # monitor W
        self.cost = tf.add_n([cost, wd_cost], name='cost')

def get_data(train_or_test, cifar_classnum):
    isTrain = train_or_test == 'train'
    if cifar_classnum == 10:
        ds = dataset.Cifar10(train_or_test)
    else:
        ds = dataset.Cifar100(train_or_test)
    if isTrain:
        augmentors = [
            imgaug.RandomCrop((30, 30)),
            imgaug.Flip(horiz=True),
            imgaug.Brightness(63),
            imgaug.Contrast((0.2,1.8)),
            imgaug.GaussianDeform(
                [(0.2, 0.2), (0.2, 0.8), (0.8,0.8), (0.8,0.2)],
                (30,30), 0.2, 3),
            imgaug.MeanVarianceNormalize(all_channel=True)
        ]
    else:
        augmentors = [
            imgaug.CenterCrop((30, 30)),
            imgaug.MeanVarianceNormalize(all_channel=True)
        ]
    ds = AugmentImageComponent(ds, augmentors)
    ds = BatchData(ds, 128, remainder=not isTrain)
    if isTrain:
        ds = PrefetchData(ds, 3, 2)
    return ds

def get_config(cifar_classnum):
    logger.auto_set_dir()

    # prepare dataset
    dataset_train = get_data('train', cifar_classnum)
    step_per_epoch = dataset_train.size()
    dataset_test = get_data('test', cifar_classnum)

    sess_config = get_default_sess_config(0.5)

    lr = symbf.get_scalar_var('learning_rate', 1e-2, summary=True)
    def lr_func(lr):
        if lr < 3e-5:
            raise StopTraining()
        return lr * 0.31

    return TrainConfig(
        dataset=dataset_train,
        optimizer=tf.train.AdamOptimizer(lr, epsilon=1e-3),
        callbacks=Callbacks([
            StatPrinter(), ModelSaver(),
            InferenceRunner(dataset_test, ClassificationError()),
            StatMonitorParamSetter('learning_rate', 'val_error', lr_func,
                threshold=0.001, last_k=10),
        ]),
        session_config=sess_config,
        model=Model(cifar_classnum),
        step_per_epoch=step_per_epoch,
        max_epoch=150,
    )

if __name__ == '__main__':
    parser = argparse.ArgumentParser()
    parser.add_argument('--gpu', help='comma separated list of GPU(s) to use.')
    parser.add_argument('--load', help='load model')
    parser.add_argument('--classnum', help='10 for cifar10 or 100 for cifar100',
                        type=int, default=10)
    args = parser.parse_args()

    if args.gpu:
        os.environ['CUDA_VISIBLE_DEVICES'] = args.gpu
    else:
        os.environ['CUDA_VISIBLE_DEVICES'] = '0'

    with tf.Graph().as_default():
        config = get_config(args.classnum)
        if args.load:
            config.session_init = SaverRestore(args.load)

        if args.gpu:
            config.nr_tower = len(args.gpu.split(','))
        nr_gpu = get_nr_gpu()
        if nr_gpu == 1:
            QueueInputTrainer(config).train()
        else:
            SyncMultiGPUTrainer(config).train()


# This program reads data from a CD catalog data set
# and builds arrays for the catalog items
# Each array contains the title, artist, country, price, and year
# At the bottom, the total number of items and average price is
# displayed
# References: datacamp.com, stackoverflow.com
# References: datacamp.com, stackoverflow.com
# References: educba.com, guru99.com, nanonets.com
# References: realpython.com, edureka.co, stackabuse.com
# References: superfastpython.com, https://developer.rhino3d.com/

import sys

# Need a function to read the XML file and return arrays
# of all the titles, artists, countries, prices, and years

def read_file(xml_file):
    with open(xml_file, 'r') as file:
        data = file.read()
    title1 = "<TITLE>"
    title2 = "</TITLE>"
    artist1 = "<ARTIST>"
    artist2 = "</ARTIST>"
    country1 = "<COUNTRY>"
    country2 = "</COUNTRY>"
    price1 = "<PRICE>"
    price2 = "</PRICE>"
    year1 = "<YEAR>"
    year2 = "</YEAR>"

    title_array = []
    artist_array = []
    country_array = []
    price_array = []
    year_array = []

    while True:
        start = data.find("<CD>")
        end = data.find("</CD>")
        if start == -1 or end == -1:
            break
        catalog = data[start:end]

        title_count = catalog.find(title1)
        title = catalog[title_count + len(title1): catalog.find(title2)]
        title_array.append(title)

        artist_count = catalog.find(artist1)
        artist = catalog[artist_count + len(artist1): catalog.find(artist2)]
        artist_array.append(artist)

        country_count = catalog.find(country1)
        country = catalog[country_count + len(country1):catalog.find(country2)]
        country_array.append(country)

        price_count = catalog.find(price1)
        price = catalog[price_count + len(price1): catalog.find(price2)]
        price_array.append(price)

        year_count = catalog.find(year1)
        year = catalog[year_count + len(year1): catalog.find(year2)]
        year_array.append(year)

        data = data[end + len("</CD>"):]

    # check for empty file
    if len(title_array) == 0:
        print("Error: Missing or bad data.")
        sys.exit()

    return title_array, artist_array, country_array, price_array, year_array

# Find the average price
def calculate_average(xml_file):
    title_array, artist_array, \
    country_array, price_array, \
    year_array = read_file(xml_file)
    items = len(title_array)
    average = sum([float(price) for price in price_array]) / items
    return average

# Calculate total number of items
def calculate_total(xml_file):
    title_array = read_file(xml_file)
    total = len(title_array)
    return total

# Display arrays

def display_result(xml_file):
    title_array, artist_array, \
    country_array, price_array, \
    year_array = read_file(xml_file)
    if len(title_array) == 0:
        print("Error: Missing or bad data")
        return
    for i in range(len(title_array)):
        print(f"{title_array[i]} - {artist_array[i]} - {country_array[i]} - {price_array[i]} - {year_array[i]}")

def display_result2(xml_file):
    title_array, artist_array, \
    country_array, price_array, \
    year_array = read_file(xml_file)
    if len(title_array) == 0:
        print("Error: Missing or bad data")
        return
    num_items = len(title_array)
    avg_price = sum([float(price) for price in price_array]) / num_items
    print("Number of items: " + str(num_items))
    print("Average price: " + str(round(avg_price, 2)))

# Main function and call
def main():
    xml_file = "cd_catalog.xml"
    display_result(xml_file)
    display_result2(xml_file)

main()


import cv2
import math
import monta
import numpy as np
import matcompat
from scipy import signal
import matplotlib.pyplot as plt

lammbda=6
pi = math.pi
theta = np.arange(0, (np.pi-np.pi/8)+(np.pi/8), np.pi/8)
psi = 0
gamma = np.linspace(.4,1,4)
gamma = np.arange(.4, 1.2, .2)
b = 4
sigma = (1/pi)*math.sqrt((math.log(2)/2))*((2**b+1)/(2**b-1))*lammbda
l = int(12/2)
gt = 0
imagen0 = np.float32(cv2.imread('images/NegroyYo4.jpg'))
imagen0 = cv2.cvtColor(imagen0,cv2.COLOR_BGR2RGB)
imagen0 = cv2.resize(imagen0, (320, 240))
imagen1 = (imagen0-128)/127
imagen = np.zeros((240,320,4))
imagen[:,:,0]=(imagen1[:,:,0]-imagen1[:,:,1])/2
imagen[:,:,1]=(imagen1[:,:,0]+imagen1[:,:,1]-2*imagen1[:,:,2])/4
imagen[:,:,2]=(imagen1[:,:,0]+imagen1[:,:,1]+imagen1[:,:,2])/3
s = matcompat.size(imagen0)
for i in np.arange(1., (s[0])+1):
    for j in np.arange(1., (s[1])+1):
        imagen[int(i)-1,int(j)-1,3] = ((imagen1[int(i)-1,int(j)-1,:]).max()-(imagen1[int(i)-1,int(j)-1,:]).min())/2
contador = 0
g = np.zeros((13,13))
imagenSalida=np.zeros((240,320,4,32))
for i in range(len(theta)):
	for f in range(len(gamma)):
		for j in range(-l,l+1):
			for k in range(-l,l+1):
				x = j*math.cos(theta[i])+k*math.sin(theta[i])
				y = k*math.cos(theta[i])-j*math.sin(theta[i])
				g[j+l,k+l]=math.exp(-(x**2 + (gamma[f]**2)*(y**2))/(2*sigma**2))*math.cos((2*pi*x/lammbda)+psi)
		imagenSalida[:,:,0,contador] = signal.convolve2d(imagen[:,:,0], g,  boundary='symm', mode='same')
		imagenSalida[:,:,1,contador] = signal.convolve2d(imagen[:,:,1], g,  boundary='symm', mode='same')
		imagenSalida[:,:,2,contador] = signal.convolve2d(imagen[:,:,2], g,  boundary='symm', mode='same')
		imagenSalida[:,:,3,contador] = signal.convolve2d(imagen[:,:,3], g,  boundary='symm', mode='same')
		contador = contador + 1
s = matcompat.size(imagenSalida)
FM = np.zeros(s)
area = []
for i in range(s[3]):
	alpha = .6
	m1 = alpha*imagenSalida[:,:,0,k].max().max()
	m2 = alpha*imagenSalida[:,:,1,k].max().max()
	m3 = alpha*imagenSalida[:,:,2,k].max().max()
	m4 = alpha*imagenSalida[:,:,3,k].max().max()
	for i in range(s[0]):
		for j in range(s[1]):
			if imagenSalida[i,j,0,k]>m1:
				FM[i,j,0,k] = 1
			if imagenSalida[i,j,1,k]>m2:
				FM[i,j,1,k]=1
			if imagenSalida[i,j,2,k]>m3:
				FM[i,j,2,k]=1
			if imagenSalida[i,j,3,k]>m4:
				FM[i,j,3,k]=1
	[area,num] = monta.monta(FM[:,:,0,k])

cv2.imshow("input",  imagen)
cv2.waitKey(0)
cv2.destroyAllWindows()

#theta = [round(float(i)/10000000,4) for i in range(0,int((pi-pi/8)*10000000),int((pi/8)*10000000))]
#gamma = [float(i)/10 for i in range(4,11,2)]


"""Timezones lookup."""

import concurrent.futures
import os
import shutil
import subprocess
import sys
import time
import traceback
from datetime import datetime
from multiprocessing import cpu_count
from pathlib import Path

import pytz
import requests
import tzlocal
from fuzzywuzzy import process
import pycountry

import albert as v0

__title__ = "Timezones lookup"
__version__ = "0.4.0"
__triggers__ = "tz "
__authors__ = "Nikos Koukis"
__homepage__ = (
    "https://github.com/bergercookie/awesome-albert-plugins/blob/master/plugins/timezones"
)
__py_deps__ = ["pycountry", "fuzzywuzzy",
               "tzlocal", "requests", "traceback", "pytz"]

icon_path = str(Path(__file__).parent / "timezones")

cache_path = Path(v0.cacheLocation()) / "timezones"
config_path = Path(v0.configLocation()) / "timezones"
data_path = Path(v0.dataLocation()) / "timezones"
country_logos_path = data_path / "logos"
dev_mode = False

# country code -> cities
code_to_cities = dict({k: v for k, v in pytz.country_timezones.items()})
codes = list(code_to_cities.keys())
city_to_code = {vi: k for k, v in pytz.country_timezones.items() for vi in v}
cities = list(city_to_code.keys())
country_to_code = {
    c.name: c.alpha_2 for c in pycountry.countries if c.alpha_2 in codes}
country_to_cities = {
    country: [code_to_cities[code]] for country, code in country_to_code.items()
}
countries = list(country_to_code.keys())

local_tz_str = tzlocal.get_localzone().zone

def download_logo_for_code(code: str) -> bytes:
    """
    Download the logo of the given code.

    .. raises:: KeyError if given code is invalid.
    """
    # ret = requests.get(f"https://www.countryflags.io/{code}/flat/64.png")
    ret = requests.get(f"file:///64/{code}.png")
    if not ret.ok:
        print(f"[E] Couldn't download logo for code {code}")
    return ret.content

def get_logo_path_for_code(code: str) -> Path:
    """Return the path to the cached country logo"""
    return country_logos_path / f"{code}.png"

def save_logo_for_code(code: str, data: bytes):
    with open(get_logo_path_for_code(code), "wb") as f:
        f.write(data)

def download_and_save_logo_for_code(code):
    save_logo_for_code(code, download_logo_for_code(code))

def download_all_logos():
    with concurrent.futures.ThreadPoolExecutor(max_workers=cpu_count()) as executor:
        future_to_code = {
            executor.submit(download_and_save_logo_for_code, code): code for code in codes
        }
        for future in concurrent.futures.as_completed(future_to_code):
            code = future_to_code[future]
            try:
                future.result()
            except Exception as exc:
                print(
                    f"[W] Fetching logo for {code} generated an exception: {exc}")

# plugin main functions -----------------------------------------------------------------------

def initialize():
    """Called when the extension is loaded (ticked in the settings) - blocking."""

    # create plugin locations
    for p in (cache_path, config_path, data_path):
        p.mkdir(parents=False, exist_ok=True)

    # fetch all logos at startup
    country_logos_path.mkdir(exist_ok=True)
    if not list(country_logos_path.iterdir()):
        print("Downloading country logos")
        t = time.time()
        download_all_logos()
        print(f"Downloaded country logos - Took {time.time() - t} seconds")

def finalize():
    pass

def get_uniq_elements(seq):
    """Return only the unique elements off the list - Preserve the order.

    .. ref:: https://stackoverflow.com/questions/480214/how-do-you-remove-duplicates-from-a-list-whilst-preserving-order
    """
    seen = set()
    seen_add = seen.add
    return [x for x in seq if not (x in seen or seen_add(x))]

def handleQuery(query) -> list:
    """Hook that is called by albert with *every new keypress*."""  # noqa
    results = []

    if query.isTriggered:
        try:
            query.disableSort()

            results_setup = setup(query)
            if results_setup:
                return results_setup

            query_str = query.string.strip()

            matched = [
                elem[0] for elem in process.extract(query_str, [*cities, *countries], limit=8)
            ]

            matched2 = []
            # replace country names with its cities
            for m in matched:
                if m in countries:
                    matched2.extend(*country_to_cities[m])
                else:
                    matched2.append(m)
            matched2 = get_uniq_elements(matched2)

            # add own timezone:
            if local_tz_str in matched2:
                matched2.remove(local_tz_str)

            matched2.insert(0, local_tz_str)
            results.extend([get_as_item(m) for m in matched2])

        except Exception:  # user to report error
            if dev_mode:  # let exceptions fly!
                print(traceback.format_exc())
                raise

            results.insert(
                0,
                v0.Item(
                    id=__title__,
                    icon=icon_path,
                    text="Something went wrong! Press [ENTER] to copy error and report it",
                    actions=[
                        v0.ClipAction(
                            f"Copy error - report it to {__homepage__[8:]}",
                            f"{traceback.format_exc()}",
                        )
                    ],
                ),
            )

    return results

# supplementary functions ---------------------------------------------------------------------

def get_as_item(city: str):
    """Return an item - ready to be appended to the items list and be rendered by Albert."""
    code = city_to_code[city]

    icon = str(get_logo_path_for_code(code))
    utc_dt = pytz.utc.localize(datetime.utcnow())
    dst_tz = pytz.timezone(city)
    dst_dt = utc_dt.astimezone(dst_tz)

    text = f"{str(dst_dt)}"
    subtext = f"[{code}] | {city}"

    return v0.Item(
        id=__title__,
        icon=icon,
        text=text,
        subtext=subtext,
        completion=city,
        actions=[
            v0.UrlAction(
                "Open in zeitverschiebung.net",
                f'https://www.zeitverschiebung.net/en/timezone/{city.replace("/", "--").lower()}',
            ),
        ],
    )

def sanitize_string(s: str) -> str:
    return s.replace("<", "<")

def get_as_subtext_field(field, field_title=None) -> str:
    """Get a certain variable as part of the subtext, along with a title for that variable."""
    s = ""
    if field:
        s = f"{field} | "
    else:
        return ""

    if field_title:
        s = f"{field_title}: " + s

    return s

def save_data(data: str, data_name: str):
    """Save a piece of data in the configuration directory."""
    with open(config_path / data_name, "w") as f:
        f.write(data)

def load_data(data_name) -> str:
    """Load a piece of data from the configuration directory."""
    with open(config_path / data_name, "r") as f:
        data = f.readline().strip().split()[0]

    return data

def setup(query):
    """Setup is successful if an empty list is returned.

    Use this function if you need the user to provide you data
    """

    results = []
    return results


#!/usr/bin/python3

# Codeforces - Round 640
# Problem A - Sum of Round Numbers

def read_int():
	n = int(input())
	return n

def read_ints():
	ints = [int(x) for x in input().split(" ")]
	return ints

#---

def solve(n):
	s = str(n)
	l = len(s)
	sol = []
	for i in range(l):
		val = s[i]
		if val != '0':
			si = val + '0' * (l - i - 1)
			sol.append(si)

	return sol

# Main
t = read_int()
for case in range(t):
	n = read_int()
	sol = solve(n)

	print (len(sol))
	print (" ".join(sol))


import numpy as np
import cv2
import time

def my_padding(src, filter):
    (h, w) = src.shape
    if isinstance(filter, tuple):
        (h_pad, w_pad) = filter
    else:
        (h_pad, w_pad) = filter.shape
    h_pad = h_pad // 2
    w_pad = w_pad // 2
    padding_img = np.zeros((h+h_pad*2, w+w_pad*2))
    padding_img[h_pad:h+h_pad, w_pad:w+w_pad] = src

    # repetition padding
    # up
    padding_img[:h_pad, w_pad:w_pad + w] = src[0, :]
    # down
    padding_img[h_pad + h:, w_pad:w_pad + w] = src[h - 1, :]
    # left
    padding_img[:, :w_pad] = padding_img[:, w_pad:w_pad + 1]
    # right
    padding_img[:, w_pad + w:] = padding_img[:, w_pad + w - 1:w_pad + w]

    return padding_img

def my_filtering(src, filter):
    (h, w) = src.shape
    (f_h, f_w) = filter.shape

    #filter 확인
    #print('<filter>')
    #print(filter)

    # 직접 구현한 my_padding 함수를 이용
    pad_img = my_padding(src, filter)

    dst = np.zeros((h, w))
    for row in range(h):
        for col in range(w):
            dst[row, col] = np.sum(pad_img[row:row + f_h, col:col + f_w] * filter)

    return dst

def get_my_sobel():
    sobel_x = np.dot(np.array([[1], [2], [1]]), np.array([[-1, 0, 1]]))
    sobel_y = np.dot(np.array([[-1], [0], [1]]), np.array([[1, 2, 1]]))
    return sobel_x, sobel_y

def calc_derivatives(src):
    # calculate Ix, Iy
    sobel_x, sobel_y = get_my_sobel()
    Ix = my_filtering(src, sobel_x)
    Iy = my_filtering(src, sobel_y)
    return Ix, Iy

def find_local_maxima(src, ksize):
    (h, w) = src.shape
    pad_img = np.zeros((h+ksize, w+ksize))
    pad_img[ksize//2:h+ksize//2, ksize//2:w+ksize//2] = src
    dst = np.zeros((h, w))

    for row in range(h):
        for col in range(w):
            max_val = np.max(pad_img[row : row+ksize, col:col+ksize])
            if max_val == 0:
                continue
            if src[row, col] == max_val:
                dst[row, col] = src[row, col]

    return dst

def get_integral_image(src):
    assert len(src.shape) == 2
    h, w = src.shape
    dst = np.zeros(src.shape)
    ##############################
    # ToDo
    # dst는 integral image
    # dst 알아서 채우기
    ##############################
    integral_image = dst
    for row in range(0, h):
        summation = 0
        for col in range(0, w):
            summation += src[row][col]
            integral_image[row][col] = summation

            if row > 0:
                integral_image[row][col] += integral_image[row - 1][col]

    # dst = integral_image
    # return dst

    dst2 = np.zeros(src.shape)

    for y in range(h):
        for x in range(w):

            min_row, max_row = max(0, y - 1), min(h - 1, y + 1)
            min_col, max_col = max(0, x - 1), min(w - 1, x + 1)

            dst2[y][x] = integral_image[max_row][max_col]

            if min_row > 0:
                dst2[y][x] -= integral_image[min_row - 1][max_col]

            if min_col > 0:
                dst2[y][x] -= integral_image[max_row][min_col - 1]

            if min_col > 0 and min_row > 0:
                dst2[y][x] += integral_image[min_row - 1][min_col - 1]

    return dst2

def calc_M_harris(IxIx, IxIy, IyIy, fsize = 5):
    assert IxIx.shape == IxIy.shape and IxIx.shape == IyIy.shape
    h, w = IxIx.shape
    M = np.zeros((h, w, 2, 2))
    IxIx_pad = my_padding(IxIx, (fsize, fsize))
    IxIy_pad = my_padding(IxIy, (fsize, fsize))
    IyIy_pad = my_padding(IyIy, (fsize, fsize))

    # for row in range(h):
    #     for col in range(w):
    #         M[row, col, 0, 0] = np.sum(IxIx_pad[row:row+fsize, col:col+fsize])
    #         M[row, col, 0, 1] = np.sum(IxIy_pad[row:row+fsize, col:col+fsize])
    #         M[row, col, 1, 0] = M[row, col, 0, 1]
    #         M[row, col, 1, 1] = np.sum(IyIy_pad[row:row+fsize, col:col+fsize])

    for row in range(h):
        for col in range(w):
            ixix = 0
            ixiy = 0
            iyiy = 0
            for f_row in range(fsize):
                for f_col in range(fsize):
                    ixix = ixix + IxIx_pad[row + f_row][col + f_col]
                    ixiy = ixiy + IxIy_pad[row + f_row][col + f_col]
                    iyiy = iyiy + IyIy_pad[row + f_row][col + f_col]
            M[row, col, 0, 0] = ixix
            M[row, col, 0, 1] = ixiy
            M[row, col, 1, 0] = ixiy
            M[row, col, 1, 1] = iyiy

    return M

def harris_detector(src, k = 0.04, threshold_rate = 0.01, fsize=5):
    harris_img = src.copy()
    h, w, c = src.shape
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) / 255.
    # calculate Ix, Iy
    Ix, Iy = calc_derivatives(gray)

    # Square of derivatives
    IxIx = Ix**2
    IyIy = Iy**2
    IxIy = Ix * Iy

    start = time.perf_counter()  # 시간 측정 시작
    M_harris = calc_M_harris(IxIx, IxIy, IyIy, fsize)
    end = time.perf_counter()  # 시간 측정 끝
    print('M_harris time : ', end-start)

    R = np.zeros((h, w))
    for row in range(h):
        for col in range(w):
            ##########################################################################
            # ToDo
            # det_M 계산
            # trace_M 계산
            # R 계산 Harris & Stephens (1988), Nobel (1998) 어떤걸로 구현해도 상관없음
            ##########################################################################
            det_M = M_harris[row, col, 0, 0] * M_harris[row, col, 1, 1] - (M_harris[row, col, 0, 1] * M_harris[row, col, 1, 0])
            trace_M = M_harris[row, col, 0, 0] + M_harris[row, col, 1, 1]
            R[row, col] = det_M - k*trace_M*trace_M

    # thresholding
    R[R < threshold_rate * np.max(R)] = 0

    R = find_local_maxima(R, 21)
    R = cv2.dilate(R, None)

    harris_img[R != 0]=[0, 0, 255]

    return harris_img

def harris_detector_integral(src, k = 0.04, threshold_rate = 0.01, fsize=5):
    harris_img = src.copy()
    h, w, c = src.shape
    gray = cv2.cvtColor(src, cv2.COLOR_BGR2GRAY) / 255.
    # calculate Ix, Iy
    Ix, Iy = calc_derivatives(gray)

    # Square of derivatives
    IxIx = Ix**2
    IyIy = Iy**2
    IxIy = Ix * Iy

    start = time.perf_counter()  # 시간 측정 시작
    IxIx_integral = get_integral_image(IxIx)
    IxIy_integral = get_integral_image(IxIy)
    IyIy_integral = get_integral_image(IyIy)
    end = time.perf_counter()  # 시간 측정 끝

    print('make integral image time : ', end-start)

    start = time.perf_counter()  # 시간 측정 시작
    ##############################
    # ToDo
    # M_integral 완성시키기
    ##############################
    M_integral = calc_M_harris(IxIx_integral, IxIy_integral, IyIy_integral, fsize)
    end = time.perf_counter()  # 시간 측정 끝
    print('M_harris integral time : ', end-start)

    R = np.zeros((h, w))
    for row in range(h):
        for col in range(w):
            ##########################################################################
            # ToDo
            # det_M 계산
            # trace_M 계산
            # R 계산 Harris & Stephens (1988), Nobel (1998) 어떤걸로 구현해도 상관없음
            ##########################################################################
            det_M = M_integral[row, col, 0, 0] * M_integral[row, col, 1, 1] - (M_integral[row, col, 0, 1] * M_integral[row, col, 1, 0])
            trace_M = M_integral[row, col, 0, 0] + M_integral[row, col, 1, 1]
            R[row, col] = det_M - k * trace_M * trace_M

    # thresholding
    R[R < threshold_rate * np.max(R)] = 0

    R = find_local_maxima(R, 21)
    R = cv2.dilate(R, None)

    harris_img[R != 0]=[0, 0, 255]

    return harris_img

def main():
    src = cv2.imread('zebra.png') # shape : (552, 435, 3)
    print('start!')
    cv2.imshow('original ', src)
    harris_img = harris_detector(src)
    cv2.imshow('harris_img ' + '201402414', harris_img)

    harris_integral_img = harris_detector_integral(src)
    cv2.imshow('harris_integral_img ' + '201402414' , harris_integral_img)
    cv2.waitKey()
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()


from unittest.runner import TextTestRunner
import urllib.request
import unittest
from typing import TypeVar, Callable, List

T = TypeVar('T')
S = TypeVar('S')

#################################################################################
# EXERCISE 1
#################################################################################
def mysort(lst: List[T], compare: Callable[[T, T], int]) -> List[T]:
    """
    This method should sort input list lst of elements of some type T.

    Elements of the list are compared using function compare that takes two
    elements of type T as input and returns -1 if the left is smaller than the
    right element, 1 if the left is larger than the right, and 0 if the two
    elements are equal.
    """
    temp = lst
    switched = True
    while switched:
        switched = False
        for i in range(len(temp) - 1):
            if compare(temp[i], temp[i + 1]) == 1:
                temp[i], temp[i + 1] = temp[i + 1], temp[i]
                switched = True

    return temp

def mybinsearch(lst: List[T], elem: S, compare: Callable[[T, S], int]) -> int:
    """
    This method search for elem in lst using binary search.

    The elements of lst are compared using function compare. Returns the
    position of the first (leftmost) match for elem in lst. If elem does not
    exist in lst, then return -1.
    """
    def binsearch(ar, targ, start, end):
        if start > end:
            return -1

        mid = (start + end) // 2
        if compare(ar[mid], targ) == 0:
            return mid

        if compare(ar[mid], targ) == 1:
            return binsearch(ar, targ, start, mid-1)
        else:
            return binsearch(ar, targ, mid+1, end)

    return binsearch(lst, elem, 0, len(lst)-1)

class Student():
    """Custom class to test generic sorting and searching."""
    def __init__(self, name: str, gpa: float):
        self.name = name
        self.gpa = gpa

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

# 30 Points (total)
def test1():
    """Tests for generic sorting and binary search."""
    print(80 * "#" + "\nTests for generic sorting and binary search.")
    test1_1()
    test1_2()
    test1_3()
    test1_4()
    test1_5()

# 6 Points
def test1_1():
    """Sort ints."""
    print("\t-sort ints")
    tc = unittest.TestCase()
    ints = [ 4, 3, 7, 10, 9, 2 ]
    intcmp = lambda x,y:  0 if x == y else (-1 if x < y else 1)
    sortedints = mysort(ints, intcmp)
    tc.assertEqual(sortedints, [2, 3, 4, 7, 9, 10])

# 6 Points
def test1_2():
    """Sort strings based on their last character."""
    print("\t-sort strings on their last character")
    tc = unittest.TestCase()
    strs = [ 'abcd', 'aacz',  'zasa' ]
    suffixcmp = lambda x,y: 0 if x[-1] == y[-1] else (-1 if x[-1] < y[-1] else 1)
    sortedstrs = mysort(strs,suffixcmp)
    tc.assertEqual(sortedstrs, [ 'zasa', 'abcd', 'aacz' ])

# 6 Points
def test1_3():
    """Sort students based on their GPA."""
    print("\t-sort students on their GPA.")
    tc = unittest.TestCase()
    students = [ Student('Josh', 3.0), Student('Angela', 2.5), Student('Vinesh', 3.8),  Student('Jia',  3.5) ]
    sortedstudents = mysort(students, lambda x,y: 0 if x.gpa == y.gpa else (-1 if x.gpa < y.gpa else 1))
    expected = [ Student('Angela', 2.5), Student('Josh', 3.0), Student('Jia',  3.5), Student('Vinesh', 3.8) ]
    tc.assertEqual(sortedstudents, expected)

# 6 Points
def test1_4():
    """Binary search for ints."""
    print("\t-binsearch ints")
    tc = unittest.TestCase()
    ints = [ 4, 3, 7, 10, 9, 2 ]
    intcmp = lambda x,y:  0 if x == y else (-1 if x < y else 1)
    sortedints = mysort(ints, intcmp)
    tc.assertEqual(mybinsearch(sortedints, 3, intcmp), 1)
    tc.assertEqual(mybinsearch(sortedints, 10, intcmp), 5)
    tc.assertEqual(mybinsearch(sortedints, 11, intcmp), -1)

# 6 Points
def test1_5():
    """Binary search for students by gpa."""
    print("\t-binsearch students")
    tc = unittest.TestCase()
    students = [ Student('Josh', 3.0), Student('Angela', 2.5), Student('Vinesh', 3.8),  Student('Jia',  3.5) ]
    stcmp = lambda x,y: 0 if x.gpa == y.gpa else (-1 if x.gpa < y.gpa else 1)
    stbincmp = lambda x,y: 0 if x.gpa == y else (-1 if x.gpa < y else 1)
    sortedstudents = mysort(students, stcmp)
    tc.assertEqual(mybinsearch(sortedstudents, 3.5, stbincmp), 2)
    tc.assertEqual(mybinsearch(sortedstudents, 3.7, stbincmp), -1)

#################################################################################
# EXERCISE 2
#################################################################################
class PrefixSearcher():

    def __init__(self, document, k):
        """
        Initializes a prefix searcher using a document and a maximum
        search string length k.
        """
        self.strings = []
        for x in range(0, len(document) - 1):
            if x + k < len(document):
                self.strings.append(document[x: x + k])
            else:
                self.strings.append(document[x: len(document)])

        comp = lambda x,y:  0 if len(x) == len(y) else (-1 if len(x) > len(y) else 1)
        self.strings = mysort(self.strings, comp)

    def search(self, q):
        """
        Return true if the document contains search string q (of
        length up to n). If q is longer than n, then raise an
        Exception.
        """
        for x in self.strings:
            if q in x:
                return True

        return False

        pass

# 30 Points
def test2():
    print("#" * 80 + "\nSearch for substrings up to length n")
    test2_1()
    test2_2()

# 15Points
def test2_1():
    print("\t-search in hello world")
    tc = unittest.TestCase()
    p = PrefixSearcher("Hello World!", 1)
    tc.assertTrue(p.search("l"))
    tc.assertTrue(p.search("e"))
    tc.assertFalse(p.search("h"))
    tc.assertFalse(p.search("Z"))
    tc.assertFalse(p.search("Y"))
    p = PrefixSearcher("Hello World!", 2)
    tc.assertTrue(p.search("l"))
    tc.assertTrue(p.search("ll"))
    tc.assertFalse(p.search("lW"))

# 20 Points
def test2_2():
    print("\t-search in Moby Dick")
    tc = unittest.TestCase()
    md_url = 'https://www.gutenberg.org/files/2701/2701-0.txt'
    md_text = urllib.request.urlopen(md_url).read().decode()
    p = PrefixSearcher(md_text[0:1000],4)
    tc.assertTrue(p.search("Moby"))
    tc.assertTrue(p.search("Dick"))

#################################################################################
# EXERCISE 3
#################################################################################
class SuffixArray():

    def __init__(self, document: str):
        """
        Creates a suffix array for document (a string).
        """

        comp = lambda x,y:  0 if x == y else (-1 if x < y else 1)

        self.sa = mysort([document[i:] for i in range(len(document))], comp)
        pass

    def positions(self, searchstr: str):
        """
        Returns all the positions of searchstr in the documented indexed by the suffix array.
        """
        out = []
        for x in range(0, len(self.sa)):
            sub = self.sa[x]
            if searchstr == sub[0:len(searchstr)]:
                out.append(x)
                return out

        pass

    def contains(self, searchstr: str):
        """
        Returns true of searchstr is coontained in document.
        """
        for x in self.sa:
            if searchstr in x:
                return True
        pass

# 40 Points
def test3():
    """Test suffix arrays."""
    print(80 * "#" + "\nTest suffix arrays.")
    test3_1()
    test3_2()

# 20 Points
def test3_1():
    print("\t-suffixarray on Hello World!")
    tc = unittest.TestCase()
    s = SuffixArray("Hello World!")
    tc.assertTrue(s.contains("l"))
    tc.assertTrue(s.contains("e"))
    tc.assertFalse(s.contains("h"))
    tc.assertFalse(s.contains("Z"))
    tc.assertFalse(s.contains("Y"))
    tc.assertTrue(s.contains("ello Wo"))

# 20 Points
def test3_2():
    print("\t-suffixarray on Moby Dick!")
    tc = unittest.TestCase()
    md_url = 'https://www.gutenberg.org/files/2701/2701-0.txt'
    md_text = urllib.request.urlopen(md_url).read().decode()
    s = SuffixArray(md_text[0:1000])
    tc.assertTrue(s.contains("Moby-Dick"))
    tc.assertTrue(s.contains("Herman Melville"))
    posset = set(s.positions("Moby-Dick"))
    tc.assertEqual(posset, {355, 356})

#################################################################################
# TEST CASES
#################################################################################
def main():
    test1()
    test2()
    test3()

if __name__ == '__main__':
    main()


import pandas as pd
import pickle
df=pd.read_csv(r'C:/Users/SAIDHANUSH/spam-ham.csv')

df['Category'].replace('spam',0,inplace=True)
df['Category'].replace('ham',1,inplace=True)

x=df['Message']
y=df['Category']

from sklearn.feature_extraction.text import CountVectorizer
cv=CountVectorizer()

x=cv.fit_transform(x)

pickle.dump(cv,open('transform.pkl','wb'))

from sklearn.model_selection import train_test_split
x_train,x_test,y_train,y_test=train_test_split(x,y,random_state=4,test_size=0.2)

from sklearn.tree import DecisionTreeClassifier
clf=DecisionTreeClassifier()
clf.fit(x_train,y_train)
pr=clf.predict(x_test)
from sklearn.metrics import accuracy_score
print(accuracy_score(y_test,pr))

pickle.dump(clf,open('nlp_model1.pkl','wb'))


from django.shortcuts import render, redirect, get_object_or_404
from django.shortcuts import render, get_object_or_404
from .models import *
from .forms import *
from .models import Product
from .forms import ProductUpdateForm
from .models import Category
from django.http import JsonResponse
# libraries for Import Export
from import_export.formats import base_formats
from django.http import HttpResponse
from import_export.resources import modelresource_factory
from .resources import ProductResources
from tablib import Dataset
from reportlab.pdfgen import canvas
from import_export import resources
from import_export.resources import ModelResource

#===============================================================code for category========================================

#====== list and add category======================
def category_list(request):
    queryset = Category.objects.all()
    if request.method == "POST":       # Add Categories
        formbb = CategoryForm(request.POST or None)
        if formbb.is_valid():
            formbb.save()
        return redirect('category_list')
    else:
        formbb = CategoryForm()
    context = {
        "queryset":queryset,
        "formbb": formbb,
    }
    return render(request, "category_list.html", context)

#============= delete category===================
def delete_categorys(request, pk):
    queryset = get_object_or_404(Category, pk=pk) # used to get the product
    if request.method == 'POST':
        queryset.delete()
        return redirect('category_list')
    return redirect('category_list')

#===========================================================the for Products page==========================================

#=========== list and Add product ===========================
def product_list(request):
    queryset = Product.objects.all()
    if request.method == 'POST':    # Add Products
        formcc = ProductForm(request.POST)
        if formcc.is_valid():
            formcc.save()
            return redirect('product_list')
    else:
        formcc = ProductForm()

    queryset = Product.objects.all().order_by('product_name')
    context ={
        "queryset":queryset,
        "formcc":formcc,
    }
    return render(request, "product_list.html", context)

#====================================== Add product to main Store=======================================
def receive_products(request, code):
    queryset = Product.objects.get(code=code)
    formjj= ProductAmendForm(request.POST or None, instance=queryset)
    if formjj.is_valid():
        instance= formjj.save(commit=False)
        instance.shop_send_quantity = 0   # set the value of the "issue to shop" =0
        instance.factory_quantity += instance.receive_main_quantity   # add the received quantity from factory to the quantity in the store
        instance.first_add_main_quantity = instance.receive_main_quantity+instance.first_add_main_quantity # recording the products that are stored in main store from the first record of received itme until now
        instance.save()
        return redirect ("product_list")
    context = {
        "instance":queryset,
        "formjj":formjj,
    }
    return render(request, 'receive_products.html',context)

#===================== issue products from main store to shop store================================

def issue_products(request, code):
    queryset = Product.objects.get(code=code)
    formuu= ProductIssueForm(request.POST or None, instance=queryset)
    if formuu.is_valid():
        instance= formuu.save(commit=False)
        instance.receive_main_quantity=0
        instance.factory_quantity -= instance.shop_send_quantity
        instance.shop_receive_quantity = instance.shop_receive_quantity+ instance.shop_send_quantity
        instance.shop_remain_quantity +=instance.shop_send_quantity
        instance.save()
        return redirect ("product_list")
    context = {
        "instance":queryset,
        "formuu":formuu,
    }
    return render(request, 'issue_products.html',context)

#====================== delete products from the list====================

def delete_product(request, code):
    queryset = get_object_or_404(Product, code=code)
    if request.method == 'POST':
        queryset.delete()
        return redirect('product_list')
    return redirect('product_list')

#======================= Update the Products =================================
def update_products(request, code):
          queryset= Product.objects.get(code=code)
          formvv= ProductUpdateForm(instance = queryset)
          if request.method == 'POST':
                    formvv = ProductUpdateForm(request.POST,instance=queryset)
                    if formvv.is_valid():
                              formvv.save()
                              return redirect('product_list')
          context= {
              'formvv' : formvv
          }
          return render(request, 'update_products.html', context)

#=================================================================for the shop store =============================================
def shop_sell(request):
    return render(request, 'shop_sell.html')

def product_shop_list(request):
    queryset = Product.objects.all()
    queryset = Product.objects.all().order_by('product_name')
    context ={
        "queryset":queryset,
    }
    return render(request, "product_shop_list.html", context)

 #==================== code for the import and Export=========================================

class ProductResource(ModelResource):
    class Meta:
        model = Product

def export_pdf(request):
    response = HttpResponse(content_type='application/pdf')
    response['Content-Disposition'] = 'attachment; filename="products.pdf"'
    p = canvas.Canvas(response)

    product = Product.objects.all()
    for item in product:
        p.drawString(100, 700, f'Name: {item.product_name}')
        p.drawString(100, 680, f'Description: {item.description}')
        p.showPage()

    p.save()
    return response

def export_excel(request):
    product = ProductResource().export()
    response = HttpResponse(product.xls, content_type='application/ms-excel')
    response['Content-Disposition'] = 'attachment; filename="product.xls"'
    return response

def import_excel(request):
    if request.method == 'POST':
        dataset = Dataset()
        new_data = request.FILES['myfile']

        if not new_data.name.endswith('xls'):
            messages.info(request, 'Wrong format')
            return render(request, 'import_data.html')

        imported_data = dataset.load(new_data.read(), 'xls')
        result = ProductResource().import_data(dataset, dry_run=True)  # Check if the data is valid
        if not result.has_errors():
            ProductResource().import_data(dataset, dry_run=False)  # Import the actual data
            messages.success(request, 'Data imported successfully')

    return render(request, 'import_data.html')

def export_import(request):
    return render(request, 'product_list.html')


def for_P():
    for row in range(7):
        for col in range(6):
            if col==0 or (row%3==0 and col<5 and row<6) or (col==5 and row%3!=0 and row<3):
                print("*",end=" ")
            else:
                print(end="  ")
        print()

def while_P():
    i=0
    while i<7:
        j=0
        while j<6:
            if j==0 or (i%3==0 and j<5 and i<6) or (j==5 and i%3!=0 and i<3):
                print("*",end=" ")
            else:
                print(end="  ")
            j+=1
        i+=1
        print()


import datetime

def rest_sec_of_day():
    """
    :return: 截止到目前当日剩余时间
    """
    today = datetime.datetime.strptime(str(datetime.date.today()), "%Y-%m-%d")
    tomorrow = today + datetime.timedelta(days=1)
    nowTime = datetime.datetime.now()
    return (tomorrow - nowTime).seconds  # 获取秒


#!/usr/bin/env python3
"""
Name: bgp_neighbor_prefix_received.py
Description: NXAPI: display bgp neighbor summary info
"""
our_version = 109
script_name = "bgp_neighbor_prefix_received"
# standard libraries
import argparse
from concurrent.futures import ThreadPoolExecutor

# local libraries
from nxapi_netbox.args.args_cookie import ArgsCookie
from nxapi_netbox.args.args_nxapi_tools import ArgsNxapiTools
from nxapi_netbox.general.log import get_logger
from nxapi_netbox.netbox.netbox_session import netbox, get_device_mgmt_ip
from nxapi_netbox.vault.vault import get_vault
from nxapi_netbox.nxapi.nxapi_bgp_unicast_summary import (
    NxapiBgpUnicastSummaryIpv4,
    NxapiBgpUnicastSummaryIpv6,
)

def get_parser():
    help_afi = "address family to query. one of ipv4 or ipv6."
    help_nonzero = (
        "if specified, only display neighbors with non-zero prefixes received"
    )
    ex_prefix = "Example: "
    ex_afi = "{} --afi ipv6".format(ex_prefix)
    ex_nonzero = "{} --nonzero".format(ex_prefix)

    parser = argparse.ArgumentParser(
        description="DESCRIPTION: display bgp unicast summary info via NXAPI",
        parents=[ArgsCookie, ArgsNxapiTools],
    )
    default = parser.add_argument_group(title="DEFAULT SCRIPT ARGS")
    mandatory = parser.add_argument_group(title="MANDATORY SCRIPT ARGS")

    default.add_argument(
        "--afi",
        dest="afi",
        required=False,
        choices=["ipv4", "ipv6"],
        default="ipv4",
        help="{} {}".format(help_afi, ex_afi),
    )

    default.add_argument(
        "--nonzero",
        dest="nonzero",
        required=False,
        default=False,
        action="store_true",
        help="{} {}".format(help_nonzero, ex_nonzero),
    )

    parser.add_argument(
        "--version", action="version", version="{} v{}".format("%(prog)s", our_version)
    )
    return parser.parse_args()

def get_device_list():
    try:
        return cfg.devices.split(",")
    except:
        log.error(
            "exiting. Cannot parse --devices {}.  Example usage: --devices leaf_1,spine_2,leaf_2".format(
                cfg.devices
            )
        )
        exit(1)

def print_header():
    print(fmt.format("ip", "hostname", "neighbor", "prefix_rx"))

def print_output(futures):
    for future in futures:
        output = future.result()
        if output == None:
            continue
        for line in output:
            print(line)

def collect_prefix_rx(ip, bgp):
    lines = list()
    for neighbor in bgp.neighbor_info:
        bgp.neighbor = neighbor
        try:
            prefixreceived = int(bgp.prefixreceived)
        except:
            log.warning(
                "collect_prefix_rx. {} skipping neighbor {}. cannot convert bgp.prefixreceived {} to int()".format(
                    bgp.hostname, bgp.neighbor, bgp.prefixreceived
                )
            )
            continue
        if prefixreceived == 0 and cfg.nonzero == True:
            continue
        lines.append(fmt.format(ip, bgp.hostname, bgp.neighbor, bgp.prefixreceived))
    lines.append("")
    return lines

def get_instance(ip, vault):
    """
    return a list of NxapiBgpUnicastSummary*() instances based on cfg.afi
    """
    if cfg.afi == "ipv4":
        return NxapiBgpUnicastSummaryIpv4(
            vault.nxos_username, vault.nxos_password, ip, log
        )
    elif cfg.afi == "ipv6":
        return NxapiBgpUnicastSummaryIpv6(
            vault.nxos_username, vault.nxos_password, ip, log
        )
    else:
        log.error("exiting. Unknown afi {}".format(cfg.afi))
        exit(1)

def worker(device, vault):
    ip = get_device_mgmt_ip(nb, device)
    instance = get_instance(ip, vault)
    instance.nxapi_init(cfg)
    instance.vrf = cfg.vrf
    instance.refresh()
    return collect_prefix_rx(ip, instance)

def get_fmt():
    fmt_ipv6 = "{:<15} {:<18} {:<40} {:>9}"
    fmt_ipv4 = "{:<15} {:<18} {:<15} {:>9}"
    if cfg.afi == "ipv4":
        return fmt_ipv4
    else:
        return fmt_ipv6

cfg = get_parser()
log = get_logger(script_name, cfg.loglevel, "DEBUG")
vault = get_vault(cfg.vault)
vault.fetch_data()
nb = netbox(vault)

devices = get_device_list()

fmt = get_fmt()
print_header()

executor = ThreadPoolExecutor(max_workers=len(devices))
futures = list()
for device in devices:
    args = [device, vault]
    futures.append(executor.submit(worker, *args))
print_output(futures)


import tensorflow as tf

def cast_float(x, y):
    x = tf.cast(x, tf.float32)

    return x, y

def normalize(x, y):
    x = tf.reshape(x, (-1, 28, 28, 1))
    x = x / 255.0

    return x, y

def augment(x, y):
    x = tf.image.random_flip_left_right(x)
    x = tf.image.random_flip_up_down(x)

    return x, y

def to_one_hot(x, y):
    y = tf.one_hot(y, 10)

    return x, y

(x_train, y_train), (x_test, y_test) = tf.keras.datasets.mnist.load_data()

mnist_train = tf.data.Dataset.from_tensor_slices((x_train, y_train)).batch(32).shuffle(10000)
mnist_train = mnist_train.map(cast_float)
mnist_train = mnist_train.map(normalize)
mnist_train = mnist_train.map(augment)
mnist_train = mnist_train.map(to_one_hot)
mnist_train = mnist_train.repeat()

mnist_validation = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(1000).shuffle(10000)
mnist_validation = mnist_validation.map(cast_float)
mnist_validation = mnist_validation.map(normalize)
mnist_validation = mnist_validation.map(to_one_hot)
mnist_validation = mnist_validation.repeat()

mnist_test = tf.data.Dataset.from_tensor_slices((x_test, y_test)).batch(1000)
mnist_test = mnist_test.map(cast_float)
mnist_test = mnist_test.map(normalize)
mnist_test = mnist_test.map(to_one_hot)


import datetime
import table
import restaurant

class Restaurant:
  def __init__(self):
    self.tables = []
    self.name = "Restaurant Dingo"
    for i in range(8):
      self.tables.append(table.Table(i))

  def get_tables(self):
    return self.tables

  def print_tables(self):
    for i in range(8):
      print("Table " + str(i))

def loop_opening_hours(action):
  dt = datetime.datetime.now()
  newdate = dt.replace(hour=12, minute=0)
  for i in range(12, 20):
    newdate = dt.replace(hour=i, minute=0)
    action(newdate)


#!/usr/bin/python3

import os

filename = os.path.basename(__file__)
# 如果鲁迅也炒股
textStr = '我家门前有两棵树，一棵是绿枣树，另一棵也是绿枣树。'
voiceArr = ['YunjianNeural']

for v in voiceArr:
  shell = f'edge-tts --text {textStr} --voice zh-CN-{v} --write-media ./audios/{filename}.{v}.mp3'
  os.system(shell)


class Solution:
    def maxOnesRow(self,arr, n, m):
        i, j, ans = 0, m-1, -1

        while i < n:
            while j >= 0 and arr[i][j] == 1:
                ans = i
                j -= 1
            i += 1

        return ans

obj = Solution()
arr, n, m = [[0, 1, 1, 1], [0, 0, 1, 1], [1, 1, 1, 1], [0, 0, 0, 0]], 4, 4
ans = obj.maxOnesRow(arr, n, m)
print(ans)


class Poly:

    def __init__(self,*terms):
        # __str__ uses the name self.terms for the dictionary of terms
        # So __init__ should build this dictionary from terms
        self.terms = {}
        for i in terms:
            assert type(i[0]) in (int,float)
            assert type(i[1]) == int, float
            assert i[1] >= 0
            assert i[1] not in self.terms.keys()
            if i[0] != 0:
                self.terms.update({i[1]:i[0]})

        # Fill in the rest of this method, using *terms to intialize self.terms

    # I have written str(...) because it is used in the bsc.txt file and
    #   it is a bit subtle to get correct. Notice that it assumes that
    #   every Poly object stores a dict whose keys are powers and whose
    #   associated values are coefficients. This function does not depend
    #   on any other method in this class being written correctly.
    def __str__(self):
        def term(c,p,var):
            return (str(c) if p == 0 or c != 1 else '') +\
                   ('' if p == 0 else var+('^'+str(p) if p != 1 else ''))
        if len(self.terms) == 0:
            return '0'
        else:
            return ' + '.join([term(c,p,'x') for p,c in sorted(self.terms.items(),reverse=True)]).replace('+ -','- ')

    def __repr__(self):
        new_str = ''
        for i in self.terms.items():
            new_str += str('(' +str(i[1]) +','+ str(i[0]) + ')')
        return 'Poly({})'.format(new_str)

    def __len__(self):
        new_list = []
        for i in self.terms.keys():
            new_list.append(i)
        if len(self.terms.keys()) > 0:
            return max(new_list)
        else:
            return 0

    def __call__(self,arg):
        count = 0
        for i in self.terms.items():
            count += (arg**i[0])*i[1]
        return count

    def __iter__(self):
        for i in sorted(self.terms.items(), reverse = True):
            yield (i[1],i[0])

    def __getitem__(self,index):
        if type(index) != int:
            raise TypeError
        elif index < 0 :
            raise TypeError
        return self.terms[index]

    def __setitem__(self,index,value):
        if type(index) == int and index > 0:
            self.terms.update({index:value})
            for i in self.terms.keys():
                if self.terms[i] == 0:
                    del self.terms[i]
                    break
        else:
            raise TypeError

    def __delitem__(self,index):
        if type(index) == int and index >= 0:
            if index in self.terms.keys():
                del self.terms[index]
        else:
            raise TypeError

    def _add_term(self,c,p):
        if type(c) in (int,float) and type(p) == int:
            if p not in self.terms.keys() and c != 0:
                self.terms.update({p:c})
            elif p in self.terms.keys() and c != 0:
                self.terms.update({p:c + self.terms[p]})
            for i in self.terms.keys():
                if self.terms[i] == 0:
                    del self.terms[i]
                    break

    def __add__(self,right):
        pass
#         new_dict = {}
#         for i in right.terms.items():
#             if i[1] in self.terms.keys():
#                 new_dict.update({i[1]})

    def __radd__(self,left):
        pass

    def __mul__(self,right):
        pass

    def __rmul__(self,left):
        pass

    def __eq__(self,right):
        new_list = []
        if type(right) not in (int,str):
            for i,e in zip(self.terms.items(), right.terms.items()):
                if i[0] == e[0] and i[1] == e[1]:
                    new_list.append(True)
                else:
                    new_list.append(False)
            return all(new_list)
        elif type(right) == int:
            if 1 not in self.terms.keys():
                if right in self.terms.values():
                    return True
            else:
                return False
        else:
            raise TypeError

if __name__ == '__main__':
    # Some simple tests; you can comment them out and/or add your own before
    # the driver is called.
    print('Start simple tests')
    p = Poly((3,2),(-2,1), (4,0))
    print('  For Polynomial: 3x^2 - 2x + 4')
    print('  str(p):',p)
    print('  repr(p):',repr(p))
    print('  len(p):',len(p))
    print('  p(2):',p(2))
    print('  list collecting iterator results:',[t for t in p])
    print('  p+p:',p+p)
    print('  p+2:',p+2)
    print('  p*p:',p*p)
    print('  p*2:',p*2)
    print('End simple tests\n')

    import driver
    #driver.default_show_exception=True
    #driver.default_show_exception_message=True
    #driver.default_show_traceback=True
    driver.driver()

ics 33/solutions/ile2 solutions/Lab 6/VegaHector/poly.py


import matplotlib.pyplot as plt
import librosa
import librosa.display
import os
import torch
from torch.distributions.beta import Beta
import numpy as np
from pytorch_lightning.callbacks import Callback
import torch.nn as nn
from einops import rearrange
from tqdm import tqdm

from helpers import nessi

image_folder = "images"
os.makedirs(image_folder, exist_ok=True)

class MyStaticPostQuantizationCallback(Callback):
    def __init__(self, get_calibration_loader, calibration_batches=100):
        self.calibration_loader = get_calibration_loader()
        self.calibration_batches = calibration_batches

    def quantize_model(self, pl_module):
        print("*********** Before Quantization: ***********")
        if hasattr(pl_module, 'mel'):
            pl_module.mel.cpu()

        # get the shape of spectrograms
        sample = next(iter(self.calibration_loader))[0][0].unsqueeze(0)
        sample = sample[:, :, :sample.size(2) // 10]
        shape = pl_module.mel_forward(sample).size()

        # get original macs and params
        macc_orig, n_params_orig = nessi.get_model_size(pl_module.net, input_size=(1, shape[1], shape[2], shape[3]))
        print("macc_orig: ", macc_orig)
        print("n_params_orig: ", n_params_orig)

        # print size of model before quantization
        print_size_of_model(pl_module.net)
        pl_module.net.fuse_model()

        # get macs and params after fusing model
        macc, n_params = nessi.get_model_size(
            pl_module.net, input_size=(1, shape[1], shape[2], shape[3]))
        print("macc after fuse : ", macc)
        print("n_params after fuse: ", n_params)

        pl_module.net.qconfig = torch.quantization.get_default_qconfig('fbgemm')
        torch.quantization.prepare(pl_module.net, inplace=True)

        pl_module.net.cpu()
        if hasattr(pl_module, 'mel'):
            pl_module.mel.cpu()
        for i, batch in enumerate(tqdm(self.calibration_loader, total=self.calibration_batches)):
            x, files, y, device_indices, cities, indices = batch
            # split to 1-second pieces
            x = rearrange(x, 'b c (slices t) -> (b slices) c t', slices=10)
            x = x.cpu()
            if hasattr(pl_module, 'mel'):
                x = pl_module.mel_forward(x)

            with torch.no_grad():
                pl_module.net(x)
            # stop after a certain number of calibration samples
            if i == self.calibration_batches:
                break

        torch.quantization.convert(pl_module.net, inplace=True)
        print("*********** After Quantization: ***********")
        return dict(macc_orig=macc_orig, n_params_orig=n_params_orig,
          macc_fuse=macc, n_params_fuse=n_params, model_size_bytes=print_size_of_model(pl_module.net))

    def on_test_start(self, trainer, pl_module):
        self.quantize_model(pl_module)

def mixstyle(x, p=0.5, alpha=0.1, eps=1e-6):
    if np.random.rand() > p:
        return x
    batch_size = x.size(0)

    # changed from dim=[2,3] to dim=[1,3] from channel-wise statistics to frequency-wise statistics
    f_mu = x.mean(dim=[1, 3], keepdim=True)
    f_var = x.var(dim=[1, 3], keepdim=True)

    f_sig = (f_var + eps).sqrt()  # compute instance standard deviation
    f_mu, f_sig = f_mu.detach(), f_sig.detach()  # block gradients
    x_normed = (x - f_mu) / f_sig  # normalize input
    lmda = Beta(alpha, alpha).sample((batch_size, 1, 1, 1)).to(x.device)  # sample instance-wise convex weights
    perm = torch.randperm(batch_size).to(x.device)  # generate shuffling indices
    f_mu_perm, f_sig_perm = f_mu[perm], f_sig[perm]  # shuffling
    mu_mix = f_mu * lmda + f_mu_perm * (1 - lmda)  # generate mixed mean
    sig_mix = f_sig * lmda + f_sig_perm * (1 - lmda)  # generate mixed standard deviation
    return x_normed * sig_mix + mu_mix  # denormalize input using the mixed statistics

def print_size_of_model(model):
    torch.save(model.state_dict(), "temp.p")
    model_size_bytes = os.path.getsize("temp.p")
    print('Size (MB):', model_size_bytes/1e6)
    os.remove('temp.p')
    return model_size_bytes

def mixup(size, alpha):
    rn_indices = torch.randperm(size)
    lambd = np.random.beta(alpha, alpha, size).astype(np.float32)
    lambd = np.concatenate([lambd[:, None], 1 - lambd[:, None]], 1).max(1)
    lam = torch.FloatTensor(lambd)
    # data = data * lam + data2 * (1 - lam)
    # targets = targets * lam + targets2 * (1 - lam)
    return rn_indices, lam

def spawn_get(seedseq, n_entropy, dtype):
    child = seedseq.spawn(1)[0]
    state = child.generate_state(n_entropy, dtype=np.uint32)

    if dtype == np.ndarray:
        return state
    elif dtype == int:
        state_as_int = 0
        for shift, s in enumerate(state):
            state_as_int = state_as_int + int((2 ** (32 * shift) * s))
        return state_as_int
    else:
        raise ValueError(f'not a valid dtype "{dtype}"')


#!/usr/bin/env python3

"""
Example for Implied Volatility using the NAG Library for Python
Finds implied volatilities of the Black Scholes equation using specfun.opt_imp_vol
Data needs to be downloaded from:
http://www.cboe.com/delayedquote/QuoteTableDownload.aspx
Make sure to download data during CBOE Trading Hours.
Updated for NAG Library for Python Mark 27.1
"""

# pylint: disable=invalid-name,too-many-branches,too-many-locals,too-many-statements

try:
    import sys
    import pandas
    import numpy as np
    import matplotlib.pylab as plt
    import warnings
    from naginterfaces.library import specfun, fit
    from naginterfaces.base import utils
    from matplotlib import cm

except ImportError as e:
    print(
        "Could not import the following module. "
        "Do you have a working installation of the NAG Library for Python?"
    )
    print(e)
    sys.exit(1)

__author__ = "Edvin Hopkins, John Morrissey and Brian Spector"
__copyright__ = "Copyright 2021, The Numerical Algorithms Group Inc"
__email__ = "support@nag.co.uk"

# Set to hold expiration dates
dates = []

cumulative_month = {'Jan': 31, 'Feb': 57, 'Mar': 90,
                    'Apr': 120, 'May': 151, 'Jun': 181,
                    'Jul': 212, 'Aug': 243, 'Sep': 273,
                    'Oct': 304, 'Nov': 334, 'Dec': 365}

def main(): # pylint: disable=missing-function-docstring

    try:
        if len(sys.argv)>1:
            QuoteData = sys.argv[1]
        else:
            QuoteData = 'QuoteData.dat'

        qd = open(QuoteData, 'r')
        qd_head = []
        qd_head.append(qd.readline())
        qd_head.append(qd.readline())
        qd.close()
    except: # pylint: disable=bare-except
        sys.stderr.write("Usage: implied_volatility.py QuoteData.dat\n")
        sys.stderr.write("Couldn't read QuoteData\n")
        sys.exit(1)

    print("Implied Volatility for %s %s" % (qd_head[0].strip(), qd_head[1]))

    # Parse the header information in QuotaData
    first = qd_head[0].split(',')
    second = qd_head[1].split()
    qd_date = qd_head[1].split(',')[0]

    company = first[0]
    underlyingprice = float(first[1])
    month, day = second[:2]
    today = cumulative_month[month] + int(day) - 30
    current_year = int(second[2])

    def getExpiration(x):
        monthday = x.split()
        adate = monthday[0] + ' ' + monthday[1]
        if adate not in dates:
            dates.append(adate)
        return (int(monthday[0]) - (current_year % 2000)) * 365 + cumulative_month[monthday[1]]

    def getStrike(x):
        monthday = x.split()
        return float(monthday[2])

    data = pandas.io.parsers.read_csv(QuoteData, sep=',', header=2, na_values=' ')

    # Need to fill the NA values in dataframe
    data = data.fillna(0.0)

    # Let's look at data where there was a recent sale
    data = data[(data['Last Sale'] > 0) | (data['Last Sale.1'] > 0)]

    # Get the Options Expiration Date
    exp = data.Calls.apply(getExpiration)
    exp.name = 'Expiration'

    # Get the Strike Prices
    strike = data.Calls.apply(getStrike)
    strike.name = 'Strike'

    data = data.join(exp).join(strike)

    print("Number of data points found: {}\n".format(len(data.index)))

    print('Calculating Implied Vol of Calls...')
    r = np.zeros(len(data.index))
    t = (data.Expiration - today)/365.0
    s0 = np.full(len(data.index),underlyingprice)
    pCall= (data.Bid + data.Ask) / 2

    # A lot of the data is incomplete or extreme so we tell the NAG routine
    # not to worry about warning us about data points it can't work with
    warnings.simplefilter('ignore',utils.NagAlgorithmicWarning)
    sigmaCall = specfun.opt_imp_vol('C',pCall,data.Strike, s0,t,r,mode = 1).sigma
    impvolcall = pandas.Series(sigmaCall,index=data.index, name='impvolCall')

    data = data.join(impvolcall)

    print('Calculating Implied Vol of Puts...')
    pPut= (data['Bid.1'] + data['Ask.1']) / 2
    sigmaPut = specfun.opt_imp_vol('P',pPut,data.Strike, s0,t,r,mode = 1).sigma
    impvolput = pandas.Series(sigmaPut,index=data.index, name='impvolPut')

    data = data.join(impvolput)
    fig = plt.figure(1)
    fig.subplots_adjust(hspace=.4, wspace=.3)

    # Plot the Volatility Curves
    # Encode graph layout: 3 rows, 3 columns, 1 is first graph.
    num = 331
    max_xticks = 4

    for date in dates:
        # add each subplot to the figure
        plot_year, plot_month = date.split()
        plot_date = (int(plot_year) - (current_year % 2000)) * 365 + cumulative_month[plot_month]
        plot_call = data[(data.impvolCall > .01) &
                       (data.Expiration == plot_date) &
                       (data['Last Sale'] > 0)]
        plot_put = data[(data.impvolPut > .01) &
                        (data.Expiration == plot_date) &
                        (data['Last Sale.1'] > 0)]

        myfig = fig.add_subplot(num)
        xloc = plt.MaxNLocator(max_xticks)
        myfig.xaxis.set_major_locator(xloc)
        myfig.set_title('Expiry: %s 20%s' % (plot_month, plot_year))
        myfig.plot(plot_call.Strike, plot_call.impvolCall, 'pr', label='call',markersize=0.5)
        myfig.plot(plot_put.Strike, plot_put.impvolPut, 'p', label='put',markersize=0.5)
        myfig.legend(loc=1, numpoints=1, prop={'size': 10})
        myfig.set_ylim([0,1])
        myfig.set_xlabel('Strike Price')
        myfig.set_ylabel('Implied Volatility')
        num += 1

    plt.suptitle('Implied Volatility for %s Current Price: %s Date: %s' %
                 (company, underlyingprice, qd_date))

    print("\nPlotting Volatility Curves/Surface")

    # The code below will plot the Volatility Surface
    # It uses fit.dim2_cheb_lines to fit with a polynomial and
    # fit.dim2_cheb_eval to evaluate at intermediate points

    m = np.empty(len(dates), dtype=np.int32)
    y = np.empty(len(dates), dtype=np.double)
    xmin = np.empty(len(dates), dtype=np.double)
    xmax = np.empty(len(dates), dtype=np.double)

    data = data.sort_values(by=['Strike']) # Need to sort for NAG Algorithm

    k = 3   # this is the degree of polynomial for x-axis (Strike Price)
    l = 3   # this is the degree of polynomial for y-axis (Expiration Date)

    i = 0

    for date in dates:
        plot_year, plot_month = date.split()
        plot_date = (int(plot_year) - (current_year % 2000)) * 365 + cumulative_month[plot_month]

        call_data = data[(data.Expiration == plot_date) &
				(data.impvolPut > .01) &
				(data.impvolPut < 1) &
                                (data['Last Sale.1'] > 0)]

        exp_sizes = call_data.Expiration.size
        if exp_sizes > 0:
            m[i] = exp_sizes

            if i == 0:
                x = np.array(call_data.Strike)
                call = np.array(call_data.impvolPut)
                xmin[0] = x.min()
                xmax[0] = x.max()
            else:
                x2 = np.array(call_data.Strike)
                x = np.append(x,x2)
                call2 = np.array(call_data.impvolPut)
                call = np.append(call,call2)
                xmin[i] = x2.min()
                xmax[i] = x2.max()
            y[i] = plot_date-today
            i+=1
    nux = np.zeros(1,dtype=np.double)
    nuy = np.zeros(1,dtype=np.double)

    if len(dates) != i:
        print(
            "Error with data: the CBOE may not be open for trading "
            "or one expiration date has null data"
        )
        return 0
    weight = np.ones(call.size, dtype=np.double)

    #Call the NAG Chebyshev fitting function
    output_coef = fit.dim2_cheb_lines(m,k,l,x,y,call,weight,(k + 1) * (l + 1),xmin,xmax,nux,nuy)

    # Now that we have fit the function,
    # we use fit.dim2_cheb_eval to evaluate at different strikes/expirations

    nStrikes = 100 # number of Strikes to evaluate
    spacing = 20 # number of Expirations to evaluate
    for i in range(spacing):
        mfirst = 1
        xmin = data.Strike.min()
        xmax = data.Strike.max()

        x = np.linspace(xmin, xmax, nStrikes)

        ymin = data.Expiration.min() - today
        ymax = data.Expiration.max() - today

        y = (ymin) + i * np.floor((ymax - ymin) / spacing)

        fx=np.empty(nStrikes)

        fx=fit.dim2_cheb_eval(mfirst,k,l,x,xmin,xmax,y,ymin,ymax,output_coef)

        if 'xaxis' in locals():
            xaxis = np.append(xaxis, x)
            temp = np.empty(len(x))
            temp.fill(y)
            yaxis = np.append(yaxis, temp)
            for j in range(len(x)):
                zaxis.append(fx[j])
        else:
            xaxis = x
            yaxis = np.empty(len(x), dtype=np.double)
            yaxis.fill(y)
            zaxis = []
            for j in range(len(x)):
                zaxis.append(fx[j])

    fig = plt.figure(2)
    ax = fig.add_subplot(111, projection='3d')

    # A try-except block for Matplotlib
    try:
        ax.plot_trisurf(xaxis, yaxis, zaxis, cmap=cm.jet)
    except AttributeError:
        print ("Your version of Matplotlib does not support plot_trisurf")
        print ("...plotting wireframe instead")
        ax.plot(xaxis, yaxis, zaxis)

    ax.set_xlabel('Strike Price')
    ax.set_ylabel('Days to Expiration')
    ax.set_zlabel('Implied Volatility for Put Options')
    plt.suptitle('Implied Volatility Surface for %s Current Price: %s Date: %s' %
                 (company, underlyingprice, qd_date))

    plt.show()

if __name__ == "__main__":
    main()


import numpy as np

class StaticFns:

    @staticmethod
    def termination_fn(obs, act, next_obs):
        assert len(obs.shape) == len(next_obs.shape) == len(act.shape) == 2

        height = next_obs[:, 0]
        angle = next_obs[:, 1]
        not_done = (height > 0.8) \
                   * (height < 2.0) \
                   * (angle > -1.0) \
                   * (angle < 1.0)
        done = ~not_done
        done = done[:, None]
        return done

    @staticmethod
    def reward_fn(obs, act, next_obs):
        reward_ctrl = -0.1 * np.sum(np.square(act), axis=1)
        reward_run = obs[:, 8]
        reward_height = -3.0 * np.square(obs[:, 0] - 1.3)
        reward = reward_run + reward_ctrl + reward_height + 1.0
        return -reward


'''Проверка гипотезы Сиракуз'''
# Гипотеза Сиракуз гласит, что любое натуральное число сводится к единице при следующих
# действиях над ним: а) если число четное, то разделить его пополам, б) если число нечетное,
# то умножить его на 3, прибавить 1 и результат разделить на 2. Над вновь полученным
# числом вновь повторить действия a) или б) в зависимости от четности числа. Рано или
# поздно число станет равным 1.
def syracuse(n: int):
    print(n)
    while n != 1:
        if n % 2 == 0:
            n = n // 2
        else:
            n = (3 * n + 1) // 2
        print(n, end=' ')

syracuse(56)


from pymongo.collection import Collection
from bson.objectid import ObjectId

def insert_object(obj: dict, collection: Collection):
    """Вставка объекта в коллекцию"""
    obj['fields'] = list(obj['fields'].items())
    return collection.insert_one(obj).inserted_id

def delete_object(object_id: str, collection: Collection):
    """Удаление объекта из коллекции"""
    collection.delete_one({"_id": ObjectId(object_id)})

def get_object(object_id: str, collection: Collection):
    """Получение объекта из коллекции по id"""
    obj = collection.find_one({"_id": ObjectId(object_id)})
    if obj is not None:
        obj['fields'] = dict(obj['fields'])
    return obj

def get_objects(
        page_size: int,
        page_number: int,
        collection: Collection
) -> list[dict]:
    """
    Получение объектов из коллекции
    :param page_size: Размер страницы
    :param page_number: Номер страницы
    :param collection: Коллекция MongoDB
    :return: Список объектов
    """
    result = []
    for obj in collection.find({}).limit(page_size).skip((page_number - 1) * page_size):
        obj['fields'] = dict(obj['fields'])
        result.append(obj)
    return result