import numpy as np
import pandas as pd
import scipy.spatial
from sklearn.preprocessing import normalize

# read data
raw_data = pd.read_csv('data/tracks.csv')
data = np.array(raw_data[['id', 'name', 'artists', 'popularity', 'duration_ms', 'danceability', 'instrumentalness', 'energy', 'valence', 'acousticness', 'tempo', 'liveness', 'loudness', 'speechiness']])

def normalize_data(data_in):
    for i in range(3, np.shape(data_in)[1]):
        data_min = np.min(data_in[:,i])
        data_max = np.max(data_in[:,i])
        first_part = data_in[:,:i]
        second_part = np.array([( data_in[:,i] + data_min ) / (np.max(data_in[:,i]) + data_min)]).T
        data_a = np.concatenate((first_part, second_part), axis=1)
        data_in = np.concatenate((data_a, data_in[:,i+1:]), axis=1)
    return data_in

data = normalize_data(data)
data = np.concatenate((data[:,:3], data[:,3:]), axis=1)
song_names_lower = np.char.lower(np.array(data[:,1], dtype=str))
visited = set()

# Given a song ID, get the index of the song in data
def getIndexFromId(newData, songId):
    return np.argwhere(newData[:,0] == songId)[0][0]

# Given a song name, get the index of the song in data
def getIndexFromName(newData, songName):
    return np.argwhere(newData[:,1] == songName)[0][0]

# Given a song name, get the id of the song in data
def getIdFromName(songName):
    return data[getIndexFromName(data, songName)][0]

# Given a song ID, get the k most similar songs
def getKSimilarSongs(songName, k, filters):
    songIndex = getIndexFromName(data, songName)
    songId, artists = data[songIndex][0], data[songIndex][2].strip('][\'').split(', ')
    root = [[songId, songName, artists, 0.0, None]]
    visited.clear()
    visited.add(songId)
    newData = data
    for i in range(len(filters)):
        low, high = getRange(filters[i])
        if low is not None:
            newData = newData[low <= newData[:,i+3]]
            newData = newData[newData[:,i+3] <= high]
    return root + getKSimilarSongsHelper(songId, k, 0, newData)

def getRange(filter_val):
    if filter_val == "Any":
        return [None,None]
    elif filter_val == "Low" or filter_val == "Short":
        return [0.0,0.33]
    elif filter_val == "Medium":
        return [0.33,0.67]
    elif filter_val == "High" or filter_val == "Long":
        return [0.67,1.0]

def getKSimilarSongsHelper(songId, k, similarity, newData):
    # create new array with input song
    inputSong = np.expand_dims(data[getIndexFromId(data, songId)], axis=0)

    # get distance between input song and all other songs
    # note that the first column (id) is cut off since it is a string
    diff = scipy.spatial.distance.cdist(inputSong[:,3:], newData[:,3:], metric='euclidean')[0]

    # get k most similar indexes of songs
    kMostSimlarIndexes = np.argpartition(diff, k)[1:k+1]

    # return array containing k pairs of [songId, songName, artists, similarityRating, parentId]
    songs = (np.column_stack((newData[kMostSimlarIndexes][:,0],
                              newData[kMostSimlarIndexes][:,1],
                              newData[kMostSimlarIndexes][:,2],
                              diff[kMostSimlarIndexes],
                              [songId]*len(kMostSimlarIndexes)))).tolist()

    newSongs = []
    for song in songs:
        if song[3] != 0.0 and song[0] not in visited: # if similarity is 0, it's the same song so filter it out
            visited.add(song[0])
            song[3] += similarity # add parents similarity to root
            song[2] = song[2].strip('][\'').split(', ') # parse string representation of list of artists
            newSongs.append(song)

            # recursively add more songs with half as many songs at each step
            newK = k // 2
            if newK > 1:
                newSongs += getKSimilarSongsHelper(song[0], newK, song[3], newData) # add children

    return newSongs

def finishSongName(partialName):
    partialName = partialName.lower()
    length = len(partialName)
    sliced_data = slicer_vectorized(song_names_lower, 0, length)
    indices = np.argwhere(sliced_data == partialName)
    song_names = data[indices][:,0][:,1][:8]
    return song_names.tolist()

def slicer_vectorized(a,start,end):
    b = a.view((str,1)).reshape(len(a),-1)[:,start:end]
    return np.array(b).view((str,end-start)).flatten()

# print(finishSongName("car"))
# sample input:
# print(getKSimilarSongs("Easy Living (with Teddy Wilson & His Orchestra)", 4))


import os
import re
import torch
import math
import numpy as np

import torchvision.utils as vutils

from PIL import Image, ImageDraw, ImageFont
from utils.checkpoints import Checkpoint

try: # backward compatibility
    from tensorboardX import SummaryWriter
except ImportError:
    from torch.utils.tensorboard import SummaryWriter

from core.config import cfg, cfg_from_file, cfg_from_list

class BaseTrainer(object):

    def __del__(self):
        # commented out, because hangs on exit
        # (presumably some bug with threading in TensorboardX)
        """
        if not self.quiet:
            self.writer.close()
            self.writer_val.close()
        """
        pass

    def __init__(self, args, quiet=False):
        self.args = args
        self.quiet = quiet

        # config
        # Reading the config
        if type(args.cfg_file) is str \
                and os.path.isfile(args.cfg_file):

            cfg_from_file(args.cfg_file)
            if args.set_cfgs is not None:
                cfg_from_list(args.set_cfgs)

        self.start_epoch = 0
        self.best_score = -1e16
        self.checkpoint = Checkpoint(args.snapshot_dir, max_n = 5)

        if not quiet:
            #self.model_id = "%s" % args.run
            logdir = os.path.join(args.logdir, 'train')
            logdir_val = os.path.join(args.logdir, 'val')

            self.writer = SummaryWriter(logdir)
            self.writer_val = SummaryWriter(logdir_val)

    def _define_checkpoint(self, name, model, optim):
        self.checkpoint.add_model(name, model, optim)

    def _load_checkpoint(self, suffix):
        if self.checkpoint.load(suffix):
            # loading the epoch and the best score
            tmpl = re.compile("^e(\d+)Xs([\.\d+\-]+)$")
            match = tmpl.match(suffix)
            if not match:
                print("Warning: epoch and score could not be recovered")
                return
            else:
                epoch, score = match.groups()
                self.start_epoch = int(epoch) + 1
                self.best_score = float(score)

    def checkpoint_epoch(self, score, epoch):

        if score > self.best_score:
            self.best_score = score

        print(">>> Saving checkpoint with score {:3.2e}, epoch {}".format(score, epoch))
        suffix = "e{:03d}Xs{:4.3f}".format(epoch, score)
        self.checkpoint.checkpoint(suffix)

        return True

    def checkpoint_best(self, score, epoch):

        if score > self.best_score:
            print(">>> Saving checkpoint with score {:3.2e}, epoch {}".format(score, epoch))
            self.best_score= score

            suffix = "e{:03d}Xs{:4.3f}".format(epoch, score)
            self.checkpoint.checkpoint(suffix)

            return True

        return False

    @staticmethod
    def get_optim(params, cfg):

        if not hasattr(torch.optim, cfg.OPT):
            print("Optimiser {} not supported".format(cfg.OPT))
            raise NotImplementedError

        optim = getattr(torch.optim, cfg.OPT)

        if cfg.OPT == 'Adam':
            upd = torch.optim.Adam(params, lr=cfg.LR, \
                                   betas=(cfg.BETA1, 0.999), \
                                   weight_decay=cfg.WEIGHT_DECAY)
        elif cfg.OPT == 'SGD':
            print("Using SGD >>> learning rate = {:4.3e}, momentum = {:4.3e}, weight decay = {:4.3e}".format(cfg.LR, cfg.MOMENTUM, cfg.WEIGHT_DECAY))
            upd = torch.optim.SGD(params, lr=cfg.LR, \
                                  momentum=cfg.MOMENTUM, \
                                  weight_decay=cfg.WEIGHT_DECAY)

        else:
            upd = optim(params, lr=cfg.LR)

        upd.zero_grad()

        return upd

    @staticmethod
    def set_lr(optim, lr):
        for param_group in optim.param_groups:
            param_group['lr'] = lr

    def _visualise_grid(self, x_all, labels, t, ious=None, tag="visualisation", scores=None):

        # adding the labels to images
        bs, ch, h, w = x_all.size()
        x_all_new = torch.zeros(bs, ch, h + 16, w)
        _, y_labels_idx = torch.max(labels, -1)
        classNamesOffset = len(self.classNames) - labels.size(1)
        classNames = self.classNames[classNamesOffset:]
        for b in range(bs):
            label_idx = labels[b]
            label_names = [name for i,name in enumerate(classNames) if label_idx[i].item()]
            label_name = ", ".join(label_names)

            ndarr = x_all[b].mul(255).clamp(0, 255).byte().permute(1, 2, 0).cpu().numpy()
            arr = np.zeros((16, w, ch), dtype=ndarr.dtype)
            ndarr = np.concatenate((arr, ndarr), 0)
            im = Image.fromarray(ndarr)
            draw = ImageDraw.Draw(im)

            font = ImageFont.truetype("fonts/UbuntuMono-R.ttf", 12)

            # draw.text((x, y),"Sample Text",(r,g,b))
            draw.text((5, 1), label_name, (255,255,255), font=font)
            im_np = np.array(im).astype(np.float)
            x_all_new[b] = (torch.from_numpy(im_np)/255.0).permute(2,0,1)

        summary_grid = vutils.make_grid(x_all_new, nrow=1, padding=8, pad_value=0.9)
        self.writer.add_image(tag, summary_grid, t)

    def _apply_cmap(self, mask_idx, mask_conf):
        palette = self.trainloader.dataset.get_palette()

        masks = []
        col = Colorize()
        mask_conf = mask_conf.float() / 255.0
        for mask, conf in zip(mask_idx.split(1), mask_conf.split(1)):
            m = col(mask).float()
            m = m * conf
            masks.append(m[None, ...])

        return torch.cat(masks, 0)

    def _mask_rgb(self, masks, image_norm, alpha=0.3):
        # visualising masks
        masks_conf, masks_idx = torch.max(masks, 1)
        masks_conf = masks_conf - F.relu(masks_conf - 1, 0)

        masks_idx_rgb = self._apply_cmap(masks_idx.cpu(), masks_conf.cpu())
        return alpha * image_norm + (1 - alpha) * masks_idx_rgb

    def _init_norm(self):
        self.trainloader.dataset.set_norm(self.enc.normalize)
        self.valloader.dataset.set_norm(self.enc.normalize)
        self.trainloader_val.dataset.set_norm(self.enc.normalize)


import contextlib
import itertools
import sqlite3
from contextlib import closing
from flask import g

DATABASE = "users.db"

def init_db():
    tasks = [
        "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER, gender TEXT)",
        "CREATE TABLE IF NOT EXISTS steps (id INTEGER PRIMARY KEY, user_id INTEGER, day INTEGER, value INTEGER, "
        "UNIQUE(user_id, day) ON CONFLICT REPLACE, FOREIGN KEY (user_id) REFERENCES users(id))",
        "CREATE TABLE IF NOT EXISTS anxieties (id INTEGER PRIMARY KEY, user_id INTEGER, timestamp INTEGER, value INTEGER, "
        "UNIQUE(user_id, timestamp) ON CONFLICT REPLACE, FOREIGN KEY (user_id) REFERENCES users(id))",
        "CREATE TABLE IF NOT EXISTS heartrates (id INTEGER PRIMARY KEY, user_id INTEGER, timestamp INTEGER, value INTEGER, "
        "UNIQUE(user_id, timestamp) ON CONFLICT REPLACE, FOREIGN KEY (user_id) REFERENCES users(id))",
    ]

    with closing(sqlite3.connect(DATABASE)) as db:
        execute_multiple_tasks(db, tasks, itertools.repeat((), len(tasks)))

def get_db():
    db = getattr(g, "_database", None)
    if db is None:
        db = g._database = sqlite3.connect(DATABASE)
    return db

# INSERT
def execute_single_task_no_return(db, command, param):
    execute_multiple_tasks_no_return(db, [command], [param])

def execute_multiple_tasks_no_return(db, commands, params):
    with contextlib.closing(db.cursor()) as cursor:
        for command, param in zip(commands, params):
            cursor.execute(
                command,
                param,
            )
        db.commit()

# SELECT
def execute_single_task(db, command, param):
    return execute_multiple_tasks(db, [command], [param])[0]

def execute_multiple_tasks(db, commands, params):
    with contextlib.closing(db.cursor()) as cursor:
        return list(
            map(
                lambda command_param_pair: cursor.execute(
                    command_param_pair[0], command_param_pair[1]
                ).fetchall(),
                zip(commands, params),
            )
        )


#Write a program which contains filter(), map() and reduce() in it.
# Python application which contains one list of numbers. List contains the numbers which are accepted from user.
# Filter should filter out all such numbers which greater than or equal to 70 and less than or equal to 90.
# Map function will increase each number by 10.
# Reduce will return product of all that numbers.

from functools import reduce

def Greater(no):
    if no>=70 and no<=90:
        return True
    else:
        return False
def Addition(no):
    return no+10
def Summation(no1,no2):
    return no1+no2

def main():
    arr=[]
    size=int(input("Enter the number of elements:"))
    for i in range(size):
        no=int(input(f"Enter the element {i+1}:"))
        arr.append(no)
    print("The list is:",arr)

    FilterList=list(filter(Greater,arr))
    print("The output after filter is:",FilterList)

    MapList = list(map(Addition, FilterList))
    print("The output after map is:", MapList)

    output = reduce(Summation, MapList)
    print("The output after reduce is:", output)

if __name__=="__main__":
    main()

4 Python_Programs/Programs to demo python concepts/filter_map_reuduce_ass1.py


from __future__ import print_function, division

import importlib
import os
import sys
from argparse import ArgumentParser

import settings
import regression

def parseArguments(argv=None):
    default_msg = ' [default %(default)s]'

    parser = ArgumentParser(
        description='Help run regression tests with given testplan name and test level(s)'
    )
    parser.add_argument(
        'testplan',
        help='testplan name'
    )
    parser.add_argument(
        'levels',
        type=str,
        help='test level(s), eg. 0 or 0,1,3'
    )
    parser.add_argument(
        '-P', '--project',
        type=str, default='dla',
        help='Set project name' + default_msg
    )
    parser.add_argument(
        '--os',
        choices=['linux'], default='linux',
        help='Choose device OS' + default_msg
    )
    parser.add_argument(
        '--target',
        choices=['sim', 'ufpga'], default='sim',
        help='Set the target' + default_msg
    )
    parser.add_argument(
        '--noclean',
        action='store_true', default=False,
        help="Don't clean output directory when test completes [default to clean]"
    )
    parser.add_argument(
        '--show',
        action='store_true', default=False,
        help="Fork test output to stdout" + default_msg
    )
    parser.add_argument(
        '-d', '--debug',
        type=int, default=0,
        help="Set debug level" + default_msg
    )
    parser.add_argument(
        '-l', '--list',
        action='store_true', default=False,
        help="Print tests in testlist"
    )
    parser.add_argument(
        '--force', dest='forceTests',
        action='store_true', default=False,
        help="Force running staged tests"
    )
    parser.add_argument(
        '--testhome',
        type=str, default=settings.TEST_HOME,
        help="Path to find test files" + default_msg
    )
    parser.add_argument(
        '--goldhome',
        type=str, default=settings.GOLD_HOME,
        help="Path to find gold files" + default_msg
    )

    filter_group = parser.add_argument_group(
        title='Filter options',
        description='Options to help run tests selectively'
    )
    filter_group.add_argument(
        '-f', '--filter',
        type=str, default=None,
        help="Apply a match string to filter tests"
    )
    filter_group.add_argument(
        '-r', '--rfilter',
        type=str, default=None,
        help="Apply a regular expression to filter tests"
    )

    save_group = parser.add_argument_group()
    save_group.add_argument(
        '--savelead',
        action='store_true', default=False,
        help='Update lead files with test results if they mismatch'
    )
    save_group.add_argument(
        '--savegold',
        action='store_true', default=False,
        help='Update gold files with test results if they mismatch'
    )

    options = parser.parse_args(argv)
    return options

def setup_env(options):
    if 'DLA_TOP' not in os.environ:
        os.environ['DLA_TOP'] = settings.DLA_HOME
    sys.path.insert(0, settings.TESTPLAN_HOME)

    if 'OUT' not in os.environ:
        os.environ['OUT'] = settings.OUTPUT_DIR

def list_tests(options, testplan):
    """ List tests in given testplan with given level num"""
    for level in options.levels.split(','):
        testlist = testplan.get_testlist(
            level,
            options.target,
            options.filter,
            options.rfilter
        )
        regression.list_tests(testlist, options)

def deploy_tests(options):
    """Resolve testing dependency on target running env

    KUM/firmware: always run in host mode
    UMD: from device or host
    """
    pass

def run_tests(options, testplan):
    """Run all given tests, and list result stats"""
    deploy_tests(options)

    target = options.target

    for level in options.levels.split(','):
        print("Running testplan[%s] level[%s] [%s] tests"
              % (testplan.name, level, target))
        testlist = testplan.get_testlist(
            level,
            target,
            options.filter,
            options.rfilter
        )

        # Execute testlist
        results = regression.run_tests(testlist, options)

        # List result stats
        written = testplan.num_written(level, target)
        total = testplan.num_total(level, target)
        regression.list_results(results, written, total)

def main():
    options = parseArguments()

    # set environment vars
    setup_env(options)

    # obtain testplan object
    mod = importlib.import_module(options.testplan)
    testplan = mod.Module()

    testplan.register_tests()

    if options.list:
        list_tests(options, testplan)
    else:
        run_tests(options, testplan)

if __name__ == "__main__":
    main()


from odoo import api, fields, models

from odoo.addons.website.models import ir_http

class ProductTemplate(models.Model):
    _inherit = "product.template"

    website_attachment_ids = fields.Many2many(
        string="Website attachments",
        comodel_name="ir.attachment",
        context={"default_public": True, "hide_attachment_products": True},
        domain=lambda self, *args, **kwargs: (
            self._domain_website_attachment_ids(*args, **kwargs)
        ),
        help="Files publicly downloadable from the product eCommerce page.",
    )

    @api.model
    def _domain_website_attachment_ids(self):
        """Get domain for website attachments."""
        domain = [
            # Only use public attachments
            ("public", "=", True),
            # Exclude Odoo asset files to avoid confusing the user
            "!",
            ("name", "=ilike", "%.assets%.js"),
            "!",
            ("name", "=ilike", "%.assets%.css"),
            "!",
            ("name", "=ilike", "web_editor%"),
            "!",
            ("name", "=ilike", "/web/content/%.assets%.js"),
            "!",
            ("name", "=ilike", "/web/content/%.assets%.css"),
            "!",
            ("name", "=ilike", r"/web/content/%/web\_editor.summernote%.js"),
            "!",
            ("name", "=ilike", r"/web/content/%/web\_editor.summernote%.css"),
        ]
        # Filter by website domain in frontend
        if ir_http.get_request_website():
            website = self.env["website"].get_current_website()
            domain += website.website_domain()
        return domain


from typing import Optional, Sequence

from linebot.v3 import messaging

from ..utils import find_indexes
from .emoji import Emoji

class TextMessage(messaging.TextMessage):
    """
    https://developers.line.biz/en/reference/messaging-api/#text-message
    """

    def __init__(
        self,
        text: str,
        *,
        quick_reply: Optional[messaging.QuickReply] = None,
        emojis: Optional[Sequence[Emoji]] = None,
        quote_token: Optional[str] = None,
    ) -> None:
        if len(text) > 5000:
            raise ValueError("text must be less than or equal to 5000 characters")

        line_emojis: Optional[Sequence[messaging.Emoji]] = None
        if emojis:
            line_emojis = []
            indexes = find_indexes(text, "$")
            for i, emoji in enumerate(emojis):
                line_emojis.append(
                    messaging.Emoji(
                        index=indexes[i],
                        productId=emoji.product_id,
                        emojiId=emoji.emoji_id,
                    )
                )

        super().__init__(
            text=text,
            quickReply=quick_reply,
            emojis=line_emojis,
            quoteToken=quote_token,
        )

class TemplateMessage(messaging.TemplateMessage):
    """
    https://developers.line.biz/en/reference/messaging-api/#template-messages
    """

    def __init__(
        self,
        alt_text: str,
        *,
        template: messaging.Template,
        quick_reply: Optional[messaging.QuickReply] = None,
    ) -> None:
        if len(alt_text) > 400:
            raise ValueError("altText must be less than or equal to 400 characters")

        super().__init__(altText=alt_text, template=template, quickReply=quick_reply)  # type: ignore

class ImageMessage(messaging.ImageMessage):
    """
    https://developers.line.biz/en/reference/messaging-api/#image-message
    """

    def __init__(
        self,
        original_content_url: str,
        preview_image_url: str,
        *,
        quick_reply: Optional[messaging.QuickReply] = None,
    ) -> None:
        if len(original_content_url) > 2000:
            raise ValueError(
                "originalContentUrl must be less than or equal to 2000 characters"
            )
        if len(preview_image_url) > 2000:
            raise ValueError(
                "previewImageUrl must be less than or equal to 2000 characters"
            )

        super().__init__(
            originalContentUrl=original_content_url,
            previewImageUrl=preview_image_url,
            quickReply=quick_reply,
        )


from dodge.components import Position, Projectile, Actor, RetaliatoryDeath, DamageBonus, Attacker, Renderable
from dodge.components.component import Component
from dodge.event import Event
from dodge.constants import ComponentType, EventType, EventParam
from dodge.entity import Entity
import uuid

class Weapon(Component):
    def __init__(self, event_stack, projectile_name, path, power, speed, targeting_radius, render_info, cooldown=0):
        super().__init__(component_type=ComponentType.WEAPON,
                         target_events=[EventType.FIRE_ALL],
                         emittable_events=[EventType.ADD_TO_LEVEL],
                         event_stack=event_stack)
        self.projectile_name = projectile_name
        self.path = path
        self.power = power
        self.speed = speed
        self.targeting_range = targeting_radius
        self.render_info = render_info
        self.cooldown = cooldown

    def _target_nearest(self, position, target_faction, level):
        nearby_entities = level.get_entities_in_radius(position.x, position.y, self.targeting_range)
        targets = []
        for entity in nearby_entities:
            if entity.has_component(ComponentType.FACTION) and \
                            entity.get_component(ComponentType.FACTION).faction == target_faction and \
                            entity.has_component(ComponentType.DESTRUCTIBLE):
                targets.append(entity)
        targets.sort(key=position.distance_to)

        if targets:
            target_pos = targets[0].get_component(ComponentType.POSITION)
            return target_pos.x, target_pos.y
        else:
            return None, None

    def _build_projectile(self, shooter_pos, tx, ty):
        path = self.path.build_path(shooter_pos.x, shooter_pos.y, tx, ty)
        projectile = Entity(uuid.uuid4(), self.projectile_name,
                            [RetaliatoryDeath(self._event_stack),
                             Actor(self._event_stack, self.speed),
                             DamageBonus(self.power),
                             Projectile(path, self._event_stack),
                             Position(self._event_stack, shooter_pos.x, shooter_pos.y, False),
                             Attacker(self._event_stack),
                             Renderable.build_from_info(self.render_info)])
        return projectile

    def _handle_event(self, event: Event):
        shooter = event[EventParam.HANDLER]
        shooter_pos = shooter.get_component(ComponentType.POSITION)
        level = event[EventParam.LEVEL]

        (tx, ty) = self._target_nearest(shooter_pos, event[EventParam.FACTION], level)
        if tx is None:
            return event
        else:
            projectile = self._build_projectile(shooter_pos, tx, ty)
            self.emit_event(Event(EventType.ADD_TO_LEVEL, {EventParam.LEVEL: level,
                                                           EventParam.TARGET: projectile,
                                                           EventParam.TAKES_TURN_IMMEDIATELY: True}))
            return event


'''
https://programmers.co.kr/learn/courses/30/lessons/60059
'''
import copy

def ins(key, lock, i, j):
    lock = copy.deepcopy(lock)
    # print(key, lock, i, j)
    for y in range(len(lock)):
        for x in range(len(lock)):
            if len(lock) > y + i >= 0 and len(lock) > x + j >= 0 and len(key) > y >= 0 and len(key) > x >= 0:
                lock[y+i][x+j] ^= key[y][x]
                if lock[y+i][x+j] == 0:
                    return False

    total = sum([sum(l) for l in lock])
    if total == len(lock)**2:
        return True
    # print(key, lock, i, j)
    # print('-------------------')

    return False

def solution(key, lock):
    fr = - len(key) + 1
    to = len(lock)
    for d in range(4):
        for i in range(fr, to):
            for j in range(fr, to):
                if ins(key[:], lock[:], i, j):
                    print(d,i,j)
                    return True
        key = list(zip(*key[::-1]))

    return False

# print(solution([[0, 0, 0], [1, 0, 0], [0, 1, 1]], [[1, 1, 1], [1, 1, 0], [1, 0, 1]]), True)
# print(solution([[0, 0, 0], [1, 0, 0], [0, 1, 1]], [[1, 1, 1, 1], [1, 1, 1, 1], [1, 1, 1, 0], [1, 1, 1, 1]]), True)


import os
from collections import Counter

class Utilities:

    def is_card_in_hand(self, guessing_hand, target_hand, guess, deck_list, deck, current_player, correct_guess):
        indices = [i for i, x in enumerate(target_hand) if x.card_number == guess]

        # Draw card if guess doesn't match
        if not indices:
            print('Go Fish!')
            hand, deck_list = deck.draw_cards(deck_list, guessing_hand, 1)
            correct_guess = False
        # Remove all matching cards from hand
        else:
            print('You guessed it!')
            for index in sorted(indices, reverse=True):
                guessing_hand.append(target_hand.pop(index))
                correct_guess = True

        return guessing_hand, target_hand, deck_list, correct_guess

    def scoring_cycle(self, hand, player_name, player_pts):
        counts = Counter(hand)
        match = False
        for ele in counts:
            if (counts[ele] == 4):
                print("Theres 4 of this card:")
                print(ele)
                # Remove element from hand and increment players score by 1.
                hand.remove(ele)
                print(player_name + str(player_pts))
                match = True
        if match:
            player_pts += 1

        return hand, player_pts

    def print_hand(self, hand, player_name):
        print(player_name + ":")
        hand.sort(key=lambda x: x.card_number)
        print(hand)

    def end_player_cycle(self):
        input("Press enter when ready...")
        os.system('clear')
        input("Please pass the game to the next player...")


# This is where we test on a real chain (Rinkeby in this case)

from time import time
import pytest
from brownie import network
from scripts.deploy_lottery import deploy_lottery
from scripts.helpful_scripts import (
    LOCAL_BLOCKCHAIN_ENVIRONMENTS,
    fund_with_link,
    get_account,
)

def test_can_pick_winner():
    if network.show_active() in LOCAL_BLOCKCHAIN_ENVIRONMENTS:
        pytest.skip()
    lottery = deploy_lottery()
    account = get_account()
    lottery.startLottery({"from": account})
    lottery.enter(
        {"from": account, "value": lottery.getEntranceFee()}
    )  # We could add + 1000 Wei to make sure we match the Entrance Fee
    lottery.enter({"from": account, "value": lottery.getEntranceFee()})
    fund_with_link(lottery)
    lottery.endLottery({"from": account})
    time.sleep(60)
    assert lottery.recentWinner() == account
    assert lottery.balance() == 0


# Implemeting a Array

class Array(object):

    def __init__(self):
        self.length = 0
        self.data = {}

    def get_index(self,indx):
        return self.data[indx]

    def append(self,num):
        self.data[self.length] = num
        self.length += 1
        return self.length

    def pop(self):
        del self.data[self.length-1]
        self.length -= 1
        return self.data

    def delete(self,num):
        item = self.data[num]
        self.indexitems(num)
        return item

    def indexitems(self,num):
        for i in range(num,self.length-1):
            self.data[i] = self.data[i+1]
        del self.data[self.length-1]
        self.length -= 1
        return self.data

    def insert(self,indx,value):
        self.data[self.length] = self.data[self.length-1]
        for i in range(self.length-1,indx-1,-1):
            print(i,"i",self.data[i])
            self.data[i+1] = self.data[i]
        self.data[indx] = value
        self.length += 1

    def print_list(self):
        cur = self.data
        ar = []
        for i in cur:
            ar.append(cur[i])
        return ar

newarr = Array()
newarr.append(1) # 0
newarr.append(2) # 1
newarr.append(3) # 2
newarr.append(4) # 3
newarr.append(99) # 4
# print("before del",newarr.data)
# newarr.delete(1)
print("newarr aft del",newarr.print_list())
newarr.insert(2,945)
print("newarr aft insert 2",newarr.print_list())


from tools.general import load_input_list

def resolve_bsp(bsp_code, low_char, high_char):

    lower = 0
    upper = 2 ** len(bsp_code) - 1

    for c in bsp_code:
        mid = (lower + upper) // 2
        if c == high_char:
            lower = mid + 1
        elif c == low_char:
            upper = mid
        else:
            raise ValueError(f"Invalid character '{c}'")

    return lower

def seat_id(bp_code):

    row = resolve_bsp(bp_code[:7], 'F', 'B')
    col = resolve_bsp(bp_code[7:], 'L', 'R')

    return (8 * row) + col

taken_seats = sorted(seat_id(s) for s in load_input_list("day5.txt").split('\n'))

print(f"Part 1 => {taken_seats[-1]}")

prev = taken_seats[0]
for s in taken_seats[1:]:
    if s - prev == 2:
        print(f"Part 2 => {s - 1}")
        break
    prev = s


from aiogram import Bot, Dispatcher, executor
from aiogram.types import InputFile, InlineKeyboardMarkup, InlineKeyboardButton
import matplotlib.pyplot as plt
from datetime import datetime

from config import BOT_TOKEN, BOTNAME
import db
import kbds
import usefull

bot = Bot(token=BOT_TOKEN)
dp = Dispatcher(bot)
hello_msg = """
Это бот для слежения за своими тратами.
После любой своей покупки всегда добавляй её так:
(сумма) (категория) (описание)
Пример: 100 такси до дома
Команды:
/familycreate (familyname) - Создание семьи
/familylink - Получение ссылки на семью
/familyremove (username) - Удаление аккаунта из семьи
"""

def dbuserexists(fn):
    """Decorator for check existing user in database before function execution"""
    async def wrapper(message):
        if not db.user_in_db(message.from_user.id):
            db.add_new_user(message.from_user.id, message.from_user.username)
        await fn(message)
    return wrapper

@dp.message_handler(commands=["start"])
@dbuserexists
async def start(message):
    ref_part = usefull.parse_command(message.text)
    if ref_part is None:
        await message.answer(hello_msg, reply_markup=kbds.base_keyboard)
    else:
        db.link_user_to_family(message.from_user.id, ref_part)
        await message.answer(hello_msg, reply_markup=kbds.base_keyboard)
        await message.answer("Your account linked to family successfully")

@dp.message_handler(commands=["familylink"])
@dbuserexists
async def familylink(message):
    """Prints family's link to connect account to family"""
    link = usefull.generate_link(message.from_user.id)
    if link is None:
        await message.answer("You is not in family")
    else:
        await message.answer(f"Your link: t.me/{BOTNAME}?start={link}")

@dp.message_handler(commands=["familyremove"])
@dbuserexists
async def familyremove(message):
    """Removes account from family"""
    username = usefull.parse_command(message.text)
    if username is None:
        await message.answer("You need to specify nickname")
    else:
        user_id = db.get_userid_by_nickname(username)
        if user_id is None:
            await message.answer("Incorrect nickname")
            return
        status = db.remove_user_from_family(user_id,
                                            db.get_family_by_admin_id(message.from_user.id))
        if status == "OK":
            await message.answer("account removed successfully")
        elif status == "NOTINFAMILY":
            await message.answer("You are not admin in user's family")
        elif status == "USERISADMIN":
            await message.answer("Can't remove account if it is admin")
        else:
            await message.answer("Can't remove account from this family")

@dp.message_handler(commands=["familycreate"])
@dbuserexists
async def familycreate(message):
    """Creates family"""
    familyname = usefull.parse_command(message.text)
    if familyname is None:
        await message.answer("You need to specify family name like this:\n/familycreate FamilyName")
    elif len(familyname) > 100:
        await message.answer("Family name can't be longer than 100 chars")
        return
    else:
        status = db.create_family(message.from_user.id, familyname)
        if status == "OK":
            await message.answer("Family created succesfully")
        elif status == "EXISTS":
            await message.answer("You have already created family")
        else:
            await message.answer("Error while creating family")

@dp.message_handler(lambda message: message.text == "Траты за неделю")
@dbuserexists
async def week_costs(message):
    msg, picture_name = usefull.getcosts_message_builder(message.from_user.id,
                                                         start_date=message.date,
                                                         weeks=1, keyword="неделю")
    await message.answer(msg)

@dp.message_handler(lambda message: message.text == "Траты за месяц")
@dbuserexists
async def week_costs(message):
    msg, picture_name = usefull.getcosts_message_builder(message.from_user.id,
                                                         start_date=message.date,
                                                         months=1, keyword="месяц")

    if picture_name:
        await message.answer_photo(photo=InputFile(picture_name), caption=msg)
    else:
        await message.answer(msg)

@dp.message_handler(lambda message: message.text == "Траты за год")
@dbuserexists
async def week_costs(message):
    msg, picture_name = usefull.getcosts_message_builder(message.from_user.id,
                                                         start_date=message.date,
                                                         months=1, keyword="год")

    if picture_name:
        await message.answer_photo(photo=InputFile(picture_name), caption=msg)
    else:
        await message.answer(msg)

@dp.message_handler(lambda message: message.text == "Траты семьи")
@dbuserexists
async def family_costs(message):
    family_id = db.get_user_family(message.from_user.id)

    if family_id is None:
        await message.answer("У вас нет семьи. Создайте её с помощью комманды /familycreate или привяжитесь к другой с помощью линк-ссылки.")
    else:
        msg, picture_name = usefull.family_users_getstatistic(family_id)

        if picture_name:
            await message.answer_photo(photo=InputFile(picture_name), caption=msg)
        else:
            await message.answer(msg)

@dp.message_handler()
@dbuserexists
async def parser(message):
    """Parses message for adding costs"""
    message_type, parsed_msg = usefull.parse_message(message.text)
    if message_type == "add_cost":
        if len(parsed_msg["buy_type"]) > 30:
            await message.answer("Type of cost can't be longer than 30 chars")
            return
        if len(parsed_msg["description"]) > 255:
            await message.answer("Description of cost can't be longer than 255 chars")
            return
        error = db.add_cost(message.from_user.id, parsed_msg["cost"],
                            parsed_msg["buy_type"], parsed_msg["description"],
                            str(message.date))
        if error == "OK":
            await message.answer("Чек успешно добавлен")
        else:
            await message.answer("Невозможно добавить чек")
    else:
        await message.answer("Неизвестная команда. \nПожалуйста, нажмите на одну из кнопок или введите покупку в формате (сумма) (категория) (описание)")

if __name__ == "__main__":
    executor.start_polling(dp)


from django.urls import re_path, reverse_lazy
from django.views.generic import RedirectView

from pages import views

urlpatterns = [
    re_path(r"^home/", views.HomePage.as_view(), name="home"),
    re_path(
        r"^msg/(?P<msg>.+)/$", views.SendMessageView.as_view(), name="send-message"
    ),
    re_path(r"", RedirectView.as_view(url=reverse_lazy("home"))),
]


#!/usr/bin/env python3

__license__ = 'MIT'

import argparse
import json
import hashlib
import os
import subprocess
import tempfile
import urllib.request
import shlex
from collections import OrderedDict

parser = argparse.ArgumentParser()
parser.add_argument('packages', nargs='+')
parser.add_argument('--python2', action='store_true',
                    help='Look for a Python 2 package')
parser.add_argument('--cleanup', choices=['scripts', 'all'],
                    help='Select what to clean up after build')
parser.add_argument('--build-only', action='store_const',
                    dest='cleanup', const='all',
                    help='Clean up all files after build')
parser.add_argument('--output',
                    help='Specify output file name')
opts = parser.parse_args()

def get_pypi_url(name: str, filename: str) -> str:
    url = 'https://pypi.python.org/pypi/{}/json'.format(name)
    print('Extracting download url for', name)
    with urllib.request.urlopen(url) as response:
        body = json.loads(response.read().decode('utf-8'))
        for release in body['releases'].values():
            for source in release:
                if source['filename'] == filename:
                    return source['url']
        else:
            raise Exception('Failed to extract url from {}'.format(url))

def get_file_hash(filename: str) -> str:
    sha = hashlib.sha256()
    print('Generating hash for', filename)
    with open(filename, 'rb') as f:
        while True:
            data = f.read(1024 * 1024 * 32)
            if not data:
                break
            sha.update(data)
        return sha.hexdigest()

package_name = 'python{}-{}'.format('2' if opts.python2 else '3',
                                    opts.packages[0])
tempdir_prefix = 'pip-generator-{}-'.format(package_name)

with tempfile.TemporaryDirectory(prefix=tempdir_prefix) as tempdir:
    if opts.python2:
        pip_executable = 'pip2'
        pip_install_prefix = '--install-option="--prefix=${FLATPAK_DEST}"'
    else:
        pip_executable = 'pip3'
        pip_install_prefix = '--prefix=${FLATPAK_DEST}'

    pip_download = [
        pip_executable,
        'download',
        '--dest',
        tempdir
    ]

    subprocess.run(pip_download + [
        '--no-binary', ':all:',
    ] + opts.packages, check=True)

    pip_command = [
        pip_executable,
        'install',
        '--no-index',
        '--find-links="file://${PWD}"',
        pip_install_prefix,
    ] + [
        shlex.quote(package)
        for package in opts.packages
    ]

    main_module = OrderedDict([
        ('name', package_name),
        ('buildsystem', 'simple'),
        ('build-commands', [' '.join(pip_command)]),
        ('sources', []),
    ])

    if opts.cleanup == 'all':
        main_module['cleanup'] = ['*']
    elif opts.cleanup == 'scripts':
        main_module['cleanup'] = ['/bin', '/share/man/man1']

    for filename in os.listdir(tempdir):
        name = filename.rsplit('-', 1)[0]
        if name == 'setuptools':  # Already installed
            continue
        sha256 = get_file_hash(os.path.join(tempdir, filename))
        url = get_pypi_url(name, filename)
        source = OrderedDict([
            ('type', 'file'),
            ('url', url),
            ('sha256', sha256),
        ])
        main_module['sources'].append(source)

output_filename = opts.output or package_name + '.json'
with open(output_filename, 'w') as output:
    output.write(json.dumps(main_module, indent=4))


#Se Jin Park   Section 201
comp = {
    "0": "0101010",
    "1": "0111111",
    "-1": "0111010",
    "D": "0001100",
    "A": "0110000",
    "!D": "0001101",
    "!A": "0110001",
    "-D": "0001111",
    "-A": "0110011",
    "D+1": "0011111",
    "A+1": "0110111",
    "D-1": "0001110",
    "A-1": "0110010",
    "D+A": "0000010",
    "D-A": "0010011",
    "A-D": "0000111",
    "D&A": "0000000",
    "D|A": "0010101",
    "M": "1110000",
    "!M": "1110001",
    "-M": "1110011",
    "M+1": "1110111",
    "M-1": "1110010",
    "D+M": "1000010",
    "D-M": "1010011",
    "M-D": "1000111",
    "D&M": "1000000",
    "D|M": "1010101"
    }

dest = {
    "null": "000",
    "M": "001",
    "D": "010",
    "A": "100",
    "MD": "011",
    "AM": "101",
    "AD": "110",
    "AMD": "111"
    }

jump = {
    "null": "000",
    "JGT": "001",
    "JEQ": "010",
    "JGE": "011",
    "JLT": "100",
    "JNE": "101",
    "JLE": "110",
    "JMP": "111"
    }

symbol = {
    "R0":"0",
    "R1":"1",
    "R2":"2",
    "R3":"3",
    "R4":"4",
    "R5":"5",
    "R6":"6",
    "R7":"7",
    "R8":"8",
    "R9":"9",
    "R10":"10",
    "R11":"11",
    "R12":"12",
    "R13":"13",
    "R14":"14",
    "R15":"15",
    "SCREEN":"16384",
    "KBD":"24576",
    "SP":"0",
    "LCL":"1",
    "ARG":"2",
    "THIS":"3",
    "THAT":"4",
    }

global default
global checker
checker = True
default = 16

list = []
flag = True

while(flag):
    val = str(input())
    if val != "EOF":
        list.append(val)
    else:
        flag = False

#gets rid of all empty spaces in strings
def whiteline(list):
    listy = []
    for string in list:
        a = string.replace(" ", "")
        b = a.replace("\t", "")
        listy.append(b)
    return listy

#gets rid of all the empty space in the list
def emptyspace(list):
    list = [i for i in list if i]
    return list

#gets rid of commment lines in the beginning
semicleanlist = emptyspace(whiteline(list))
scl = []
for string in semicleanlist:
        if string[0] == "/":
            pass
        else:
            scl.append(string)

#gets rid of comment lines after the code
scl2 = []
for string in scl:
    if "/" in string:
        scl2.append(string[0:(string.index("/"))])
    else:
        scl2.append(string)
#scl2 is final code only list

#decides which instruction to use and calls
def instruction(line, ind):

        if line[0] == "@":

            return astruction(line)
        if line[0] == "(":
            temp = line[1:-1]
            symbol[temp] = ind[0]
            ind[0] -= 1
            return ""
        else:
            other = cstruction(line)

            return "111" + other

def astruction(line):
        global default
        global checker
        if line[1].isalpha():
            temp = line[1:]
            value = symbol.get(temp)
            if value == None:
                if checker:
                    return
                symbol[temp] = default
                default += 1

            value = int(symbol.get(temp))
            b = bin(value)[2:].zfill(16)
            return b
        else: #if it's number so @20
            a = int(line[1:])
            b = bin(a)[2:].zfill(16)
            return b

def nullcase(line):
    line = line
    if not "=" in line:
        line = "null=" + line
    if not ";" in line:
        line = line + ";null"
    return line

def cstruction(line):
    temp1 = nullcase(line)
    temp = temp1.split("=")
    dcode = str(dest.get(temp[0]))
    temp2 = temp[1].split(";")
    ccode = str(comp.get(temp2[0]))
    jcode = str(jump.get(temp2[1]))
    result = ccode + dcode + jcode
    return result

ind = [0]
for line in range(0,len(scl2)):
    instruction(scl2[line], ind)
    ind[0] += 1
checker = False
ind = [0]
for line in range(0,len(scl2)):
    string1 = instruction(scl2[line], ind)
    if string1 != "":
        print(string1)
    ind[0] += 1


import pytest
from sos import execute_workflow
from sos.targets import file_target

def test_py_module(clear_now_and_after):
    '''Test target Py_Module'''
    clear_now_and_after('report.md')
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate', autoinstall=True)
from tabulate import tabulate
table = [["Sun",696000,1989100000],["Earth",6371,5973.6],
    ["Moon",1737,73.5],["Mars",3390,641.85]]
report: output='report.md', expand=True
    {tabulate(table)}
''')
    assert file_target('report.md').target_exists('target')
    with open('report.md') as rep:
        assert rep.read().strip() == '''
-----  ------  -------------
Sun    696000     1.9891e+09
Earth    6371  5973.6
Moon     1737    73.5
Mars     3390   641.85
-----  ------  -------------
'''.strip()

def test_py_module_with_version():
    '''Test target Py_Module'''
    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate', version='2.0', autoinstall=True)
''')

        #
    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate>=2.0')
''')
        #

    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate==20.0')
''')
        #
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate<2.0')
''')

def test_upgrade_py_module():
    '''Test upgrade py module #1246'''
    # first install tabulate == 0.7.5
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate==0.7.5', autoinstall=True)
''')
    # test should pass
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate'), Py_Module('tabulate==0.7.5')
''')
    # test for newer version should fail

    with pytest.raises(Exception):
        execute_workflow(r'''
[10]
depends: Py_Module('tabulate==0.8.3')
''')

    # auto install should work
    execute_workflow(r'''
[10]
depends: Py_Module('tabulate==0.8.3', autoinstall=True)
''')

    # test for old version should fail
    execute_workflow(r'''
        [10]
        depends: Py_Module('tabulate'), Py_Module('tabulate==0.8.3')
        ''')
    # test for old version should fail
    with pytest.raises(Exception):
        execute_workflow(r'''
        [10]
        depends: Py_Module('tabulate==0.7.5')
        ''')


# Realizar la carga de enteros por teclado. Preguntar después que ingresa el valor si desea cargar otro valor debiendo el operador ingresar la
# cadena 'si' o 'no' por teclado. Mostrar la suma de los valores ingresados.

'''contestacion="si"
numero=0
suma=0

if contestacion == "Si" or contestacion=="si" or contestacion=="s" or contestacion=="S":
    numero=int(input("Ingrese un número: "))
    suma=suma+numero
    contestacion=input("¿Quiere ingresar mas números? ")
else:
  print("El resultado de la suma de los números ingresados es: ", suma)'''

# Despues de muchas vueltas la solución es la siguiente:

contestacion = "si"
suma = 0

while contestacion == "Si" or contestacion == "si" or contestacion == "s" or contestacion == "S" or contestacion == "SI" or contestacion == "sI":
    valor = int(input("Ingrese un valor:"))
    suma = suma+valor
    contestacion = input("Desea cargar otro numero (si/no):")

print("La suma de valores ingresados es", suma)

'''Por fin despues de mucho mirar y otro fallo igual en otro ejercicio me di cuenta de que siempre hay que poner la variable y la comparación
no vasta con poner la variable una vez y luego los comparadores, es variable y comparas, variable y comparas y asi siempre.'''


from django.contrib import admin
from .models import UserTypeMaster,UserMaster

# class UserAdmin(admin.ModelAdmin):
# 	list_display=['UserName','UserPassword']
# 	list_filter=['UserName','UserEmail']
# 	search_fields=['UserPassword','UserEmail']
# 	ordering=['UserPassword','UserName']

ModelField= lambda model: type('Subclass'+model.__name__,(admin.ModelAdmin,),{
	'list_display':[x.name for x in model._meta.fields],
})

# Register your models here.

admin.site.register(UserTypeMaster,ModelField(UserTypeMaster))
admin.site.register(UserMaster,ModelField(UserMaster))


import os

import numpy as np
from tqdm import tqdm

from tools.common import read_json
from tools.data.semantic_kitti import image_height, image_width

def eval_voxel(output_path, restore_path):
    pred = np.load(output_path)
    gt = np.load(restore_path)
    Is = []
    Us = []
    for i in range(20):
        cur_pred = pred == i
        cur_gt = gt == i
        Is.append(np.sum(cur_pred & cur_gt))
        Us.append(np.sum(cur_pred | cur_gt))
    return Is, Us

def eval_voxels(output_dir, restore_dir):
    Iss, Uss = [], []
    for file in tqdm(os.listdir(restore_dir)):
        output_path = os.path.join(output_dir, file)
        restore_path = os.path.join(restore_dir, file)
        Is, Us = eval_voxel(output_path, restore_path)
        Iss.append(Is)
        Uss.append(Us)
    Iss = np.array(Iss).sum(axis=0)
    Uss = np.array(Uss).sum(axis=0) + 1e-6
    ious = Iss / Uss
    return ious

def calculate_iou(box1, box2):
    min_x1, min_y1, max_x1, max_y1 = box1
    min_x2, min_y2, max_x2, max_y2 = box2
    w1, h1 = max_x1 - min_x1, max_y1 - min_y1
    w2, h2 = max_x2 - min_x2, max_y2 - min_y2

    x_inter_left = max(min_x1, min_x2)
    y_inter_top = max(min_y1, min_y2)
    x_inter_right = min(max_x1, max_x2)
    y_inter_bottom = min(max_y1, max_y2)

    I = max(0, x_inter_right - x_inter_left) * max(0, y_inter_bottom - y_inter_top)
    U = w1 * h1 + w2 * h2 - I
    iou = I / U
    return iou

def calculate_ap(gt_boxes, pred_boxes, scroes, iou_threshold=0.5):
    sorted_indices = np.argsort(scroes)[::-1]
    pred_boxes = pred_boxes[sorted_indices]
    matches = np.zeros(len(pred_boxes), dtype=bool)
    gt_matches = np.zeros(len(gt_boxes), dtype=bool)
    for i, predicted_box in enumerate(pred_boxes):
        best_iou = 0.0
        best_match_index = -1
        for j, gt_box in enumerate(gt_boxes[~gt_matches]):
            iou = calculate_iou(predicted_box, gt_box)
            if iou > best_iou:
                best_iou = iou
                best_match_index = j

        if best_iou >= iou_threshold:
            matches[i] = True
            gt_matches[best_match_index] = True

    tp = np.sum(matches)
    fp = len(matches) - tp
    fn = len(gt_boxes) - tp
    return tp, fp, fn

def eval_2d_obj(output_path, gt_path, iou_threshold=0.5):
    result = read_json(output_path)
    classes = np.array(result["classes"])
    boxes = np.array(result["boxes"])
    scores = np.array(result["scores"])
    boxes[:, [0, 2]] *= image_width
    boxes[:, [1, 3]] *= image_height
    gt = read_json(gt_path)
    classes_gt = []
    boxes_gt = []
    for l in gt["labels"]:
        classes_gt.append(l["label"])
        boxes_gt.append(l["box2d"])
    classes_gt = np.array(classes_gt)
    boxes_gt = np.array(boxes_gt)
    sum_tp, sum_fp, sum_fn = 0, 0, 0
    for i in range(1, 20):
        cls_mask = classes == i
        cls_mask_gt = classes_gt == i
        tp, fp, fn = calculate_ap(boxes_gt[cls_mask_gt], boxes[cls_mask], scores[cls_mask], iou_threshold)
        sum_tp += tp
        sum_fp += fp
        sum_fn += fn
    return sum_tp, sum_fp, sum_fn

def eval_2d_objs(output_dir, preprocessed_dir, iou_threshold=0.5):
    sum_tp, sum_fp, sum_fn = 0, 0, 0
    for file in tqdm(os.listdir(preprocessed_dir)):
        output_path = os.path.join(output_dir, file)
        gt_path = os.path.join(preprocessed_dir, file)
        tp, fp, fn = eval_2d_obj(output_path, gt_path, iou_threshold)
        sum_tp += tp
        sum_fp += fp
        sum_fn += fn
    precision = sum_tp / (sum_tp + sum_fp)
    recall = sum_tp / (sum_tp + sum_fn)
    return precision, recall

def eval_2d_objs_during_training(preds, labels, iou_threshold=0.5):
    sum_tp, sum_fp, sum_fn = 0, 0, 0
    pred_logits = preds[2]
    classes, scores = pred_logits.argmax(-1), pred_logits.max(-1)
    boxes = np.zeros_like(preds[3])
    boxes[:, :2] = preds[3][:, :2] - preds[3][:, 2:] / 2
    boxes[:, 2:] = preds[3][:, :2] + preds[3][:, 2:] / 2
    gt_classes = labels[0]
    gt_boxes = np.zeros_like(labels[1])
    gt_boxes[:, :, :2] = labels[1][:, :, :2] - labels[1][:, :, 2:] / 2
    gt_boxes[:, :, 2:] = labels[1][:, :, :2] + labels[1][:, :, 2:] / 2
    bs = labels[0].shape[0]
    num_preds = preds[5]
    cur_i = 0
    for b in range(bs):
        b_classes = classes[cur_i : cur_i + num_preds[b]]
        b_scores = scores[cur_i : cur_i + num_preds[b]]
        b_boxes = boxes[cur_i : cur_i + num_preds[b]]
        cur_i += num_preds[b]
        b_gt_classes = gt_classes[b]
        b_gt_boxes = gt_boxes[b]
        for i in range(1, 20):
            cls_mask = b_classes == i
            cls_mask_gt = b_gt_classes == i
            tp, fp, fn = calculate_ap(b_gt_boxes[cls_mask_gt], b_boxes[cls_mask], b_scores[cls_mask], iou_threshold)
            sum_tp += tp
            sum_fp += fp
            sum_fn += fn
    precision = sum_tp / (sum_tp + sum_fp + 1e-6)
    recall = sum_tp / (sum_tp + sum_fn)
    return {"accuracy": precision, "recall": recall}


import re
import subprocess
from pathlib import Path

EXEC = re.compile(r', \[.*\]')
IP = re.compile(r'inet_addr\(\".+\"\)')
PORT = re.compile(r'htons\([0-9]+\)')
KNOWN_CONNECT = {
    '127.0.0.53:53', # DNS Lookup
    '151.101.0.223:443', # PyPI
    '151.101.128.223:443', # PyPI
    '151.101.64.223:443', # PyPI
    '151.101.188.223:443', # PyPI
    '151.101.192.223:443', # PyPI
    '151.101.0.223:443', # PyPI
    '151.101.189.63:443', # PyPI
    '151.101.197.63:443', # PyPI
    '192.30.255.113:9418', # Github
    '192.30.255.112:9418', # Github
}
ENV_LOCATIONS = {
    '/etc/environment',
    '/etc/profile',
    '/etc/bashrc',
    '~/.bash_profile',
    '~/.bashrc',
    '~/.profile',
    '~/.cshrc',
    '~/.zshrc',
    '~/.tcshrc',
}
BAD_COMMANDS = {
    '"set"',
    '"env"',
}

def check_path(pkg, syscall):
    path = syscall.split('"')[1]
    for loc in ENV_LOCATIONS:
        if Path(loc).expanduser().as_posix() == path:
            print(f'\033[93m{pkg} tried to access sensitive environment location [{path}] during installation.\033[0m')

def check_cmd(pkg, syscall):
    args = EXEC.search(syscall)
    match_str = args.group()
    if any(cmd in match_str for cmd in BAD_COMMANDS):
        print(f'\033[91m{pkg} tried to access environment variables by executing {match_str} command during installation.\033[0m')

def check_connect(pkg, syscall):
    ipo = IP.search(syscall)
    porto = PORT.search(syscall)
    ip_addr = ipo.group().replace('inet_addr(', '').replace('"', '').replace(')', '')
    port = porto.group().replace('htons(', '').replace(')', '')
    loc = f'{ip_addr}:{port}'
    if loc in KNOWN_CONNECT:
        return
    print(f'\033[94m{pkg} tried to connect to [{loc}] during installation.\033[0m')

def lookup_env(pkg, syscalls):
    """Check syscalls for malicious activities."""
    calls = syscalls.splitlines()
    for i in calls:
        if 'openat(' in i:
            check_path(pkg, i)
        elif 'execve(' in i:
            check_cmd(pkg, i)
        elif 'connect(' in i and 'sin_addr=' in i:
            check_connect(pkg, i)

def collect_syscalls(pkg):
    print(f'Analyzing: {pkg}')
    """Collect sensitive system calls during installation."""
    args = [
        'strace', '-s', '2000', '-fqqe',
        'trace=openat,execve,connect','--seccomp-bpf',
        'pip', 'install', '--no-cache'] + pkg.split()
    return subprocess.check_output(args, stderr=subprocess.STDOUT).decode('utf-8', 'ignore')

def check_packages():
    pkgs = Path('./requirements.txt').read_text().splitlines()
    for pkg in pkgs:
        # Handle comments in requirements file.
        if pkg.startswith('#'):
            continue
        if '# ' in pkg:
            pkg = pkg.split('# ')[0]
        syscalls = collect_syscalls(pkg)
        if '#egg=' in pkg:
            pkg = pkg.split('#egg=')[1]
        lookup_env(pkg, syscalls)

if __name__ == "__main__":
    check_packages()


import numpy as np
import os
import gzip
import random
# Killing optional CPU driver warnings
os.environ['TF_CPP_MIN_LOG_LEVEL'] = '2'

class Model:
    """
        This model class provides the data structures for your NN,
        and has functions to test the three main aspects of the training.
        The data structures should not change after each step of training.
        You can add to the class, but do not change the
        stencil provided except for the blanks and pass statements.
        Make sure that these functions work with a loop to call them multiple times,
        instead of implementing training over multiple steps in the function

        Arguments:
        train_images - NumPy array of training images
        train_labels - NumPy array of labels
    """
    def __init__(self, train_images, train_labels):
        self.input_size, self.num_classes, self.batchSz, self.learning_rate = 784, 10, 1, 0.5
        self.train_images = train_images
        self.train_labels = train_labels
        # sets up weights and biases...
        self.W = np.zeros((self.input_size, self.num_classes))
        self.b = np.zeros((1, self.num_classes))

    def run(self):
        """
        Does the forward pass, loss calculation, and back propagation
        for this model for one step

        Args: None
        Return: None
        """
        delta_w = np.zeros((self.input_size, self.num_classes))
        delta_b = np.zeros((1, self.num_classes))
        for i in range(self.batchSz):
            r = random.randint(0, self.train_images.shape[0] - 1)
            l = self.b + np.dot(self.train_images[r],  self.W)
            e_x = np.exp(l - np.max(l))
            p = e_x / e_x.sum()
            loss = -np.log(p)
            p[0, self.train_labels[r]] -= 1
            delta_w += -self.learning_rate * np.dot(self.train_images[r].reshape(-1, 1), p)
            delta_b += -self.learning_rate * p
        self.W += delta_w
        self.b += delta_b

    def accuracy_function(self, test_images, test_labels):
        """
        Calculates the accuracy of the model against test images and labels

        DO NOT EDIT
        Arguments
        test_images: a normalized NumPy array
        test_labels: a NumPy array of ints
        """
        scores = np.dot(test_images, self.W) + self.b
        predicted_classes = np.argmax(scores, axis=1)
        return np.mean(predicted_classes == test_labels)

def main():
    # TO-DO: import MNIST test data
    with open('train-images-idx3-ubyte.gz', 'rb') as f, gzip.GzipFile(fileobj=f) as bytestream:
        buf1 = bytestream.read(16 + 60000 * 28 * 28)
        train_images = np.frombuffer(buf1, dtype='uint8', offset=16).reshape(60000, 28 * 28)
        train_images = train_images[:10000] / 255.0
    with open('train-labels-idx1-ubyte.gz', 'rb') as f, gzip.GzipFile(fileobj=f) as bytestream:
        buf2 = bytestream.read(8 + 60000)
        train_labels = np.frombuffer(buf2, dtype='uint8', offset=8)
        train_labels = train_labels[:10000]

    with open('t10k-images-idx3-ubyte.gz', 'rb') as f, gzip.GzipFile(fileobj=f) as bytestream:
        buf3 = bytestream.read(16 + 10000 * 28 * 28)
        test_images = np.frombuffer(buf3, dtype='uint8', offset=16).reshape(10000, 28 * 28) / 255.0
    with open('t10k-labels-idx1-ubyte.gz', 'rb') as f, gzip.GzipFile(fileobj=f) as bytestream:
        buf4 = bytestream.read(8 + 10000)
        test_labels = np.frombuffer(buf4, dtype='uint8', offset=8)

    # TO-DO: Create Model with training data arrays that sets up the weights and biases.
    m = Model(train_images, train_labels)

    # TO-DO: Run model for number of steps by calling run() each step
    steps = 10000
    for i in range(steps):
        m.run()

    # TO-DO: test the accuracy by calling accuracy_function with the test data
    print(m.accuracy_function(test_images, test_labels))

if __name__ == '__main__':
    main()


from tkinter import *
w=Tk()
c=Canvas(w,width=520,height=420,bg='white')
c.pack()

def drawPicture(x1,y1,m1,x2,y2,m2,k,n):
    c.create_line(x1,y1,x2,y2)
    c.create_text(x1,y1,text=round(m1,2),anchor=S)
    if k==n:
        c.create_text(x2,y2,text=round(m2,2),anchor=S)

def drawPoint(a,i,n):
    maxmoney=a*((1+i)**n)
    dy=maxmoney/380
    dx=500/n
    m1=a*(1+i)
    x1=500/(2*n)
    y1=420-m1/dy
    for k in range(2,n+1):
        m2=a*((1+i)**k)
        x2=x1+dx
        y2=420-m2/dy
        drawPicture(x1,y1,m1,x2,y2,m2,k,n)
        x1=x2
        y1=y2
        m1=m2

def main():
    m=eval(input("Please input the money:"))
    r=eval(input("Please input the rate:"))
    y=eval(input("Please input the year:"))
    drawPoint(m,r,y)

main()


import logging as log

import networkx as nx
import numpy as np

from mo.front.common.layout import get_features_dim, get_height_dim, get_width_dim
from mo.front.common.partial_infer.utils import int64_array
from mo.middle.replacement import MiddleReplacementPattern
from mo.ops.reshape import Reshape

class FeatureShuffleReshape(MiddleReplacementPattern):
    """
    This pass finds patterns like in shufflenet topology (Reshape->Transpose->Reshape) and will change attributes for
    first Reshape and Transpose operations to preserve original semantics.
    """

    enabled = True

    def pattern(self):
        return dict(
            nodes=[
                ('reshape1', dict(kind='op', type='Reshape')),
                ('reshape1_data', dict(kind='data')),
                ('transpose', dict(kind='op', type='Permute')),
                ('transpose_data', dict(kind='data')),
                ('reshape2', dict(kind='op', type='Reshape')),
                ('reshape2_data', dict(kind='data')),
            ],
            edges=[('reshape1', 'reshape1_data'),
                   ('reshape1_data', 'transpose'),
                   ('transpose', 'transpose_data'),
                   ('transpose_data', 'reshape2'),
                   ('reshape2', 'reshape2_data'),
                   ]
        )

    def replace_pattern(self, graph: nx.MultiDiGraph, match: dict):
        reshape1 = match['reshape1']
        reshape2 = match['reshape2']
        transpose = match['transpose']

        # Check that Reshape->Transpose->Reshape shuffle only feature channel
        input_shape = np.array(reshape1.in_node(0).shape)
        reshape1_shape = np.array(reshape1.out_node().shape)
        output_shape = np.array(reshape2.out_node().shape)

        # Check that input shape is 4D
        if len(input_shape) != 4:
            log.warning('Can\'t convert Reshape->Transpose({})->Reshape sequence due to input shape should be 4D '
                        '(instead of {}D)'.format(transpose.name, len(input_shape)))
            return

        # Check that output shape the same as input shape
        if not np.prod(input_shape) == np.prod(output_shape):
            log.warning('Can\'t convert Reshape->Transpose({})->Reshape sequence due to output shape should be equal '
                        'to input shape: {} and {}'.format(transpose.name, input_shape, output_shape))
            return

        # Input shapes can be either NCHW or NHWC, so in case of channel split, feature channel can be splited as
        # follows in comments below
        # So feature_dims_split list contains possible dims responsible for feature dim
        layout = graph.graph['layout']
        feature_dim = get_features_dim(layout, len(input_shape))
        spatial_dims = [get_height_dim(layout, len(input_shape)), get_width_dim(layout, len(input_shape))]
        if layout == 'NCHW':
            # NC1C2HW or NC1C2(H*W)
            feature_dims_split = np.array([feature_dim, feature_dim + 1])
        else:
            # NHWC1C2 or N(H*W)C1C2 or (N*H*W)C1C2
            feature_dims_split = np.array([len(reshape1_shape) - 2, len(reshape1_shape) - 1])

        # Check that feature_dims_split suits reshape layer shape
        for dim in feature_dims_split:
            if dim < 0 or dim >= len(reshape1_shape):
                log.warning('Can\'t convert Reshape({}:{})->Transpose->Reshape sequence. Can\'t detect feature shuffle.'
                            ''.format(reshape1.shape, reshape1_shape))
                return

        if not np.prod(np.delete(reshape1_shape, feature_dims_split)) == np.prod(np.delete(input_shape, feature_dim)):
            log.warning('Can\'t convert Reshape->Transpose->Reshape sequence. Can\'t detect feature shuffle. {} '
                        'should be equal to {}'.format(np.prod(np.delete(reshape1_shape, feature_dims_split)),
                                                       np.prod(np.delete(input_shape, feature_dim))))
            return

        # Check transpose order
        if not np.array_equal(feature_dims_split[::-1], transpose.order[feature_dims_split]):
            log.warning('Can\'t convert Reshape->Transpose({})->Reshape sequence. Transpose operation should witch '
                        'feature order (given order: {})'.format(transpose.name, transpose.order))
            return

        # Now we are sure that Reshape->Transpose->Reshape shuffle feature dims
        # So, then we change Reshape and Transpose attrs to suite NCHW layout

        # The resulting shape for Reshape1 layer : [N,C1,C2,(H*W)]
        new_reshape1_shape = np.concatenate((np.array([input_shape[0]]),
                                             np.array(reshape1_shape[feature_dims_split]),
                                             np.array([np.prod(input_shape[spatial_dims])])))

        new_transpose_order = np.array([0, 2, 1, 3])
        new_transpose_shape = np.array(new_reshape1_shape[new_transpose_order])

        reshape1.out_node().shape = new_reshape1_shape
        transpose.order = new_transpose_order
        transpose.out_node().shape = new_transpose_shape

        # Preserve layers from conversion to NCHW (in case of NHWC topology layout)
        reshape1['nchw_layout'] = True
        reshape1.out_node()['nchw_layout'] = True
        transpose['nchw_layout'] = True
        transpose.out_node()['nchw_layout'] = True

class ReshapeSoftmaxReshape(MiddleReplacementPattern):
    """
    In case of NHWC this pass finds patterns Reshape(-1,C) -> Softmax and changes first Reshape dims for NCHW format.
    This transformation is necessary because after conversion to NCHW this sequence will have wrong interpretation.
    There is no need to permute data before reshape because the Softmax will be performed over the features dimension
    so the output will be in a correct layout.
    """

    enabled = True

    def pattern(self):
        return dict(
            nodes=[
                ('reshape1', dict(kind='op', type='Reshape')),
                ('reshape1_data', dict(kind='data')),
                ('softmax', dict(kind='op', type='SoftMax')),
                ('softmax_data', dict(kind='data')),
            ],
            edges=[('reshape1', 'reshape1_data'),
                   ('reshape1_data', 'softmax'),
                   ('softmax', 'softmax_data'),
                   ])

    def replace_pattern(self, graph: nx.MultiDiGraph, match: dict):
        layout = graph.graph['layout']
        if layout != 'NHWC':
            return

        reshape1 = match['reshape1']
        softmax = match['softmax']

        # Check that Reshape->Softmax->Reshape shuffle only feature channel
        input_shape = np.array(reshape1.in_node(0).shape)
        reshape1_shape = np.array(reshape1.out_node().shape)

        # Check that input shape is 4D
        if len(input_shape) != 4:
            log.warning('Can\'t convert Reshape({})->Softmax->Reshape sequence due to input shape should be 4D '
                        '(instead of {}D {})'.format(reshape1.name, len(input_shape), input_shape))
            return

        if len(reshape1_shape) != 2:
            log.warning('This pass expect 2D output tensor for first Reshape {} layer (given shape: {})'
                        ''.format(reshape1.name, reshape1_shape))
            return

        # Define feature dim
        feature_dim = get_features_dim(layout, len(input_shape))
        spatial_dims = [get_height_dim(layout, len(input_shape)), get_width_dim(layout, len(input_shape))]

        # Skip transform in case if spatial dims in input shape are equal to [1,1]
        if np.array_equal(input_shape[spatial_dims], np.array([1, 1])):
            log.info('Skip this transformation due to spatial dims are [1,1]')
            return

        # Check that Reshape1 has out dims [-1, feature_dims]
        if not (reshape1_shape[-1] == input_shape[-1] and reshape1_shape[0] == np.prod(
                np.delete(input_shape, feature_dim))):
            log.warning('Output shape for Reshape operation should be [{},{}] instead of {}'.format(
                np.prod(np.delete(input_shape, feature_dim)), input_shape[-1], reshape1_shape))
            return

        # Now we are sure that Reshape->Softmax suits for this transformation

        # The resulting shape for Reshape1 layer : [N,C,(H*W)]
        new_reshape1_shape = np.concatenate((np.array([input_shape[0]]),
                                             np.array([reshape1_shape[-1]]),
                                             np.array([np.prod(input_shape[spatial_dims])])))

        # update 'dim' attribute but preserve batch dimension size which could be -1
        reshape1.dim = int64_array([reshape1.dim[0], *new_reshape1_shape[1:]])

        old_shape = np.array(reshape1.out_node().shape)
        reshape1.out_node().shape = new_reshape1_shape
        softmax.out_node().shape = new_reshape1_shape

        # Preserve layers from conversion to NCHW (in case of NHWC topology layout)
        reshape1['nchw_layout'] = True
        reshape1.out_node()['nchw_layout'] = True
        softmax['nchw_layout'] = True
        softmax.out_node()['nchw_layout'] = True

        # Create final Reshape to keep original shape for softmax output if softmax is not the last node
        softmax_out_data = softmax.out_node()
        if len(softmax_out_data.out_nodes()) != 0:
            next_operation = softmax_out_data.out_node()
            # Save edge attributes & remove edge
            edge_attrs = graph.get_edge_data(softmax_out_data.id, next_operation.id)[0]
            graph.remove_edge(softmax_out_data.id, next_operation.id)
            reshape_op = Reshape(graph, dict(name=softmax.id + "/Reshape", dim=np.array(old_shape), nchw_layout=True))
            reshape_out_data = reshape_op.create_node_with_data(inputs=[softmax_out_data])
            graph.add_edges_from([(reshape_out_data.id, next_operation.id, edge_attrs)])


from src.aoc_2_corruption_checksum import parse_input, compute_checksum, compute_evenly_divisible

def test_parse_input():
    provided_input = """179	2358	5197	867\n2637	136	3222	591"""
    expected_output = [
        [179, 2358, 5197, 867],
        [2637, 136, 3222, 591],
    ]
    output = parse_input(provided_input)
    assert output == expected_output

def test_corruption_checksum():
    provided_input = [
        [5, 1, 9, 5],
        [7, 5, 3],
        [2, 4, 6, 8]
    ]
    expected_output = 18
    output = compute_checksum(provided_input)
    assert output == expected_output

def test_evenly_divisible():
    provided_input = [
        [5, 9, 2, 8],
        [9, 4, 7, 3],
        [3, 8, 6, 5]
    ]
    expected_output = 9
    output = compute_evenly_divisible(provided_input)
    assert output == expected_output


"""
Tests for functions in the ``rand`` module.

Due to the stochastic nature of many of these functions,
this is not absolute proof that the functions are working as expected.
False failures, while highly unlikely, are possible.

If something fails and you aren't sure why, try re-running
the tests a few times before going bug-hunting.
"""

from __future__ import division

import unittest
import math
import random

from blur import rand

class TestRand(unittest.TestCase):
    """Tests for all methods in the ``rand`` module."""
    def test__linear_interp(self):
        # Positive integer slope with and without rounding
        self.assertAlmostEqual(
            rand._linear_interp([(0, 0), (2, 2)], 1, round_result=False),
            1)
        self.assertAlmostEqual(
            rand._linear_interp([(0, 0), (2, 2)], 1, round_result=True),
            1)
        # Negative integer slope with and without rounding
        self.assertAlmostEqual(
            rand._linear_interp([(-2, -2), (0, 0)], -1, round_result=False),
            -1)
        self.assertAlmostEqual(
            rand._linear_interp([(-2, -2), (0, 0)], -1, round_result=True),
            -1)
        # Positive non-integer slope with and without rounding
        self.assertAlmostEqual(
            rand._linear_interp([(0, 0), (4, 1)], 1, round_result=True),
            0)
        self.assertAlmostEqual(
            rand._linear_interp([(0, 0), (4, 1)], 1, round_result=False),
            0.25)
        # Negative non-integer slope with and without rounding
        self.assertAlmostEqual(
            rand._linear_interp([(-4, -1), (0, 0)], -1, round_result=True),
            0)
        self.assertAlmostEqual(
            rand._linear_interp([(-4, -1), (0, 0)], -1, round_result=False),
            -0.25)
        # Three points in curve with and without rounding
        self.assertAlmostEqual(
            rand._linear_interp([(0, 0), (2, 2), (18, 7)],
                                1, round_result=False), 1)
        self.assertAlmostEqual(
            rand._linear_interp([(0, 0), (2, 2), (18, 7)],
                                1, round_result=True), 1)
        # test_x out of the domain of the curve
        with self.assertRaises(rand.ProbabilityUndefinedError):
            rand._linear_interp([(0, 0), (2, 2)], -1, round_result=False)

    def test__point_under_curve(self):
        # Build several random curves and test points below the minimum
        # and above the maximum
        MIN_X = -100
        MIN_Y = -100
        MAX_X = 100
        MAX_Y = 100
        curve = [(random.randint(-100, 100), random.randint(-100, 100))
                 for i in range(30)]
        # Attach points to domain bounds at MIN_Y
        curve.append((MIN_X, MIN_Y))
        curve.append((MAX_X, MIN_Y))
        # Sort the points in the curve as this is a requirement of the function
        curve.sort(key=lambda p: p[0])

        # Point guaranteed to be under the random curve
        point_under = ((MIN_X + MAX_X) / 2, MIN_Y - 1)
        self.assertTrue(rand._point_under_curve(curve, point_under))
        # Point guaranteed to be over the random curve
        point_over = ((MIN_X + MAX_X) / 2, MAX_Y + 1)
        self.assertFalse(rand._point_under_curve(curve, point_over))
        # Points outside the x domain of the random curve
        point_out_of_lower_x_bound = (MIN_X - 1, MIN_Y - 1)
        self.assertFalse(rand._point_under_curve(
            curve, point_out_of_lower_x_bound))
        point_out_of_upper_x_bound = (MAX_X + 1, MIN_Y - 1)
        self.assertFalse(rand._point_under_curve(
            curve, point_out_of_upper_x_bound))
        # Points under the curve but on the curve bounds
        point_on_lower_x_bound = (MIN_X, MIN_Y - 1)
        self.assertTrue(rand._point_under_curve(
            curve, point_on_lower_x_bound))
        point_on_upper_x_bound = (MAX_X, MIN_Y - 1)
        self.assertTrue(rand._point_under_curve(
            curve, point_on_upper_x_bound))

    def test_clamp_value_between_min_and_max_returns_value(self):
        self.assertEqual(rand._clamp_value(value=5, minimum=4, maximum=6), 5)

    def test_clamp_value_on_min_returns_value(self):
        self.assertEqual(rand._clamp_value(value=5, minimum=5, maximum=6), 5)

    def test_clamp_value_on_max_returns_value(self):
        self.assertEqual(rand._clamp_value(value=6, minimum=4, maximum=6), 6)

    def test_clamp_value_below_min_returns_min(self):
        self.assertEqual(rand._clamp_value(value=3, minimum=4, maximum=6), 4)

    def test_clamp_value_above_max_returns_max(self):
        self.assertEqual(rand._clamp_value(value=7, minimum=4, maximum=6), 6)

    def test_clamp_value_with_max_under_min_raises_ValueError(self):
        with self.assertRaises(ValueError):
            rand._clamp_value(value=5, minimum=3, maximum=2)

    def test_bound_weights_with_bad_bounds_raises_ValueError(self):
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        with self.assertRaises(ValueError):
            rand.bound_weights(weights, 10, 5)

    def test_bound_weights_without_bounds_returns_unmodified(self):
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        self.assertEqual(weights, rand.bound_weights(weights))

    def test_bound_weights_doesnt_modify_input(self):
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        original_weights = weights[:]
        rand.bound_weights(weights, 2, 4)
        self.assertEqual(weights, original_weights)

    def test_bound_weights_with_weights_already_inside_bounds(self):
        # When all weights are already inside bounds,
        # rand.bound_weights() should have no effect
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        modified = rand.bound_weights(weights, -2, 8)
        self.assertEqual(weights, modified)

    def test_bound_weights_clipping_on_existing_weights(self):
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        self.assertEqual(rand.bound_weights(weights, 2, 4),
                         [(2, 2), (4, 2)])

    def test_bound_weights_clipping_between_existing_weights(self):
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        self.assertEqual(rand.bound_weights(weights, 1, 5),
                         [(1, 1), (2, 2), (4, 2), (5, 1)])

    def test_bound_weights_with_only_one_bound(self):
        weights = [(0, 0), (2, 2), (4, 2), (6, 0)]
        # With only minimum
        self.assertEqual(rand.bound_weights(weights, minimum=1),
                         [(1, 1), (2, 2), (4, 2), (6, 0)])
        # With only maximum
        self.assertEqual(rand.bound_weights(weights, maximum=5),
                         [(0, 0), (2, 2), (4, 2), (5, 1)])

    def test_normal_distribution(self):
        """
        Test the accuracy of ``rand.normal_distribution()``.

        Use the curve it creates to generate a large number of samples,
        and then calculate the real variance and mean of the resulting
        sample group and compare the two within a comfortable margin.
        """
        MEAN = -12
        VARIANCE = 2.5
        STANDARD_DEVIATION = math.sqrt(VARIANCE)
        SAMPLE_COUNT = 1000
        curve = rand.normal_distribution(MEAN, VARIANCE, weight_count=30)
        samples = [rand.weighted_rand(curve) for i in range(SAMPLE_COUNT)]
        samples_mean = sum(samples) / len(samples)
        samples_variance = (
            sum((s - samples_mean) ** 2 for s in samples) /
            len(samples)
        )
        mean_diff = abs(MEAN - samples_mean)
        variance_diff = abs(VARIANCE - samples_variance)
        self.assertLess(mean_diff, abs(MEAN / 4))
        self.assertLess(variance_diff, abs(VARIANCE / 4))

    def test_normal_distribution_with_bounds(self):
        MEAN = -12
        VARIANCE = 20
        MIN_X = -15
        MAX_X = 5
        curve = rand.normal_distribution(MEAN, VARIANCE, MIN_X, MAX_X)
        self.assertEqual(min(weight[0] for weight in curve), MIN_X)
        self.assertEqual(max(weight[0] for weight in curve), MAX_X)

    def test_is_valid_options_weights_list_with_valid_args(self):
        try:
            options = [('Option 1', 5), ('Option 2', 3), ('Option 3', 1)]
            rand._is_valid_options_weights_list(options)
        except TypeError:
            self.fail()

    def test_is_valid_options_weights_list_with_empty_list(self):
        self.assertFalse(rand._is_valid_options_weights_list([]))

    def test_is_valid_options_weights_list_with_non_list(self):
        self.assertFalse(rand._is_valid_options_weights_list(42))

    def test_is_valid_options_weights_list_with_non_tuple_in_list(self):
        options = [('Option 1', 5), 'this is not a tuple']
        self.assertFalse(rand._is_valid_options_weights_list(options))

    def test_is_valid_options_weights_list_with_wrong_tuple_lengths(self):
        options = [('Option 1', 5, 'one extra')]
        self.assertFalse(rand._is_valid_options_weights_list(options))
        options = [('one too few',)]
        self.assertFalse(rand._is_valid_options_weights_list(options))
        options = [()]  # Empty tuple
        self.assertFalse(rand._is_valid_options_weights_list(options))

    def test_is_valid_options_weights_list_with_invalid_weight_strength(self):
        options = [('Option 1', 'not a number for strength')]
        self.assertFalse(rand._is_valid_options_weights_list(options))

    def test_is_valid_numerical_weights_list_with_valid(self):
        weights = [(1, 2), (3, 4.0), (5.6, 7)]
        self.assertTrue(rand._is_valid_numerical_weights_list(weights))

    def test_is_valid_numerical_weights_list_with_nonlist(self):
        weights = 'not a list'
        self.assertFalse(rand._is_valid_numerical_weights_list(weights))

    def test_is_valid_numerical_weights_list_with_empty_list(self):
        self.assertFalse(rand._is_valid_numerical_weights_list([]))

    def test_is_valid_numerical_weights_list_with_nontuples(self):
        weights = ['items', 'are', 'not', 'tuples']
        self.assertFalse(rand._is_valid_numerical_weights_list(weights))

    def test_is_valid_numerical_weights_list_with_short_tuples(self):
        weights = [(1)]  # tuple wrong length
        self.assertFalse(rand._is_valid_numerical_weights_list(weights))

    def test_is_valid_numerical_weights_list_with_long_tuples(self):
        weights = [(1, 2, 3)]
        self.assertFalse(rand._is_valid_numerical_weights_list(weights))

    def test_is_valid_numerical_weights_list_with_wrong_types_in_tuples(self):
        weights = [(1, 'wrong type')]
        self.assertFalse(rand._is_valid_numerical_weights_list(weights))
        weights = [('wrong type', 2)]
        self.assertFalse(rand._is_valid_numerical_weights_list(weights))

    def test_prob_bool(self):
        # Test guaranteed outcomes
        self.assertTrue(rand.prob_bool(100))
        self.assertTrue(rand.prob_bool(1))
        self.assertFalse(rand.prob_bool(0))
        self.assertFalse(rand.prob_bool(-100))
        # Test that probabilties are working as expected
        true_count = 0
        for i in range(1000):
            if rand.prob_bool(0.2):
                true_count += 1
        self.assertTrue(5 <= true_count <= 400)

    def test_percent_possible(self):
        # Test guaranteed outcomes
        self.assertTrue(rand.prob_bool(1000))
        self.assertTrue(rand.prob_bool(100))
        self.assertFalse(rand.prob_bool(0))
        self.assertFalse(rand.prob_bool(-1000))
        # Test that probabilties are working as expected
        true_count = 0
        for i in range(1000):
            if rand.percent_possible(20):
                true_count += 1
        self.assertTrue(5 <= true_count <= 400)

    def test_pos_or_neg_with_given_prob(self):
        # Test guaranteed outcomes
        self.assertEqual(rand.pos_or_neg(5, 10), 5)
        self.assertEqual(rand.pos_or_neg(5, 1), 5)
        self.assertEqual(rand.pos_or_neg(5, 0), -5)
        self.assertEqual(rand.pos_or_neg(5, -10), -5)
        # Test that probabilties are working as expected
        pos_count = 0
        for i in range(1000):
            if rand.pos_or_neg(5, 0.2) == 5:
                pos_count += 1
        self.assertTrue(5 <= pos_count <= 400)

    def test_pos_or_neg_without_given_prob(self):
        # Test that probabilties are working as expected
        pos_count = 0
        for i in range(1000):
            if rand.pos_or_neg(5) == 5:
                pos_count += 1
        self.assertTrue(300 <= pos_count <= 700)

    def test_pos_or_neg_1_with_given_prob(self):
        # Test guaranteed outcomes
        self.assertEqual(rand.pos_or_neg_1(10), 1)
        self.assertEqual(rand.pos_or_neg_1(1), 1)
        self.assertEqual(rand.pos_or_neg_1(0), -1)
        self.assertEqual(rand.pos_or_neg_1(-10), -1)
        # Test that probabilties are working as expected
        pos_count = 0
        for i in range(1000):
            if rand.pos_or_neg_1(0.2) == 1:
                pos_count += 1
        self.assertTrue(5 <= pos_count <= 400)

    def test_pos_or_neg_1_without_given_prob(self):
        # Test that probabilties are working as expected
        pos_count = 0
        for i in range(1000):
            if rand.pos_or_neg_1() == 1:
                pos_count += 1
        self.assertTrue(300 <= pos_count <= 700)

    def test_weighted_choice(self):
        options = [(0, 1), (5, 2), (10, 5)]
        zero_count = 0
        five_count = 0
        ten_count = 0
        for i in range(1000):
            result = rand.weighted_choice(options)
            if result == 0:
                zero_count += 1
            elif result == 5:
                five_count += 1
            elif result == 10:
                ten_count += 1
            else:
                self.fail('Unexpected weighted_choice'
                          'result {0}'.format(result))
        self.assertTrue(25 <= zero_count <= 250)
        self.assertTrue(50 <= five_count <= 600)
        self.assertTrue(300 <= ten_count <= 900)

    def test_weighted_choice_as_tuple(self):
        options = [(1, 0), (5, -1), (10, 5), (19, 1)]
        for i in range(25):
            result = rand.weighted_choice(options, True)
            expected_index = next(
                i for i, option in enumerate(options)
                if option[0] == result[1])
            self.assertEqual(expected_index, result[0])

    def test_weighted_choice_with_empty_list(self):
        with self.assertRaises(ValueError):
            foo = rand.weighted_choice([])

    def test_weighted_choice_with_all_non_pos_weights(self):
        with self.assertRaises(rand.ProbabilityUndefinedError):
            foo = rand.weighted_choice([(1, 0), (5, 0), (10, 0)])

    def test_weighted_choice_with_mixed_pos_neg_weights(self):
        options = [(1, 0), (5, -1), (10, 5), (19, 1)]
        for i in range(25):
            self.assertIn(rand.weighted_choice(options),
                          [10, 19])

    def test_weighted_choice_with_one_weight_returns_it(self):
        weight_list = [('The Only Weight', 2)]
        expected_result = weight_list[0][0]
        self.assertEqual(rand.weighted_choice(weight_list), expected_result)

    def test_weighted_rand_with_one_weight_returns_it(self):
        weight_list = [('The Only Weight', 2)]
        expected_result = weight_list[0][0]
        self.assertEqual(rand.weighted_rand(weight_list), expected_result)

    def test_weighted_rand_and_choice_with_one_weight_equivalent(self):
        weight_list = [('The Only Weight', 2)]
        self.assertEqual(rand.weighted_rand(weight_list),
                         rand.weighted_choice(weight_list))

    def test_weighted_rand_with_arbitrary_curve(self):
        """
        Test ``rand.weighted_rand()``.

        Find a large number of points from a randomly built weight distribution
        and comparing the distribution against the expectation using a crude
        histogram model.
        """
        MIN_X = -1000
        MIN_Y = 0
        MAX_X = 1000
        MAX_Y = 1000
        curve = [(random.randint(MIN_X, MAX_X), random.randint(MIN_Y, MAX_Y))
                 for i in range(30)]
        # Attach points to domain bounds at MIN_Y
        curve.append((MIN_X, MIN_Y))
        curve.append((MAX_X, MIN_Y))
        # Sort the points in the curve as this is a
        # requirement of _linear_interp()
        curve.sort(key=lambda p: p[0])

        BIN_WIDTH = 1
        bins = {b: 0 for b in range(MIN_X, MAX_X, BIN_WIDTH)}
        TEST_COUNT = 1000
        for i in range(TEST_COUNT):
            point = rand.weighted_rand(curve, round_result=False)
            # Match the found point to the closest bin to the left
            bins[int(math.floor(point / BIN_WIDTH) * BIN_WIDTH)] += 1
        # Make sure the binning is working as expected
        self.assertEqual(sum(values for i, values in bins.items()), TEST_COUNT,
                         msg='This test itself is broken! '
                             'Not all rolled points were matched to a bin.')
        sum_probability = 0
        for bin_x in bins.keys():
            sum_probability += rand._linear_interp(curve, bin_x)
        for bin_x, count in bins.items():
            bin_probability = rand._linear_interp(curve, bin_x)
            expected_count = (bin_probability / sum_probability) * TEST_COUNT
            self.assertLess(abs(count - expected_count), TEST_COUNT / 10)

    def test_weighted_order_doesnt_modify_original(self):
        original_list = [('Something', 10),
                         ('Another',   5),
                         ('Again',     1),
                         ('And',       700),
                         ('More',      5)]
        input_list = original_list[:]
        shuffled = rand.weighted_order(input_list)
        self.assertEqual(input_list, original_list)

    def test_weighted_order_output_type(self):
        original_list = [('Something', 10),
                         ('Another',   5),
                         ('Again',     1),
                         ('And',       700),
                         ('More',      5)]
        shuffled = rand.weighted_order(original_list)
        for item in shuffled:
            self.assertIsInstance(item, str)

    def test_weighted_order_doesnt_lose_or_add_items(self):
        original_list = [('Something', 10),
                         ('Another',   5),
                         ('Again',     1),
                         ('And',       700),
                         ('More',      5)]
        shuffled = rand.weighted_order(original_list)
        self.assertEqual(len(original_list), len(shuffled))
        for item in original_list:
            self.assertIn(item[0], shuffled)

    def test_weighted_order_with_extremely_strong_weight(self):
        original_list = [('Something', 10),
                         ('Another',   5),
                         ('Again',     1),
                         ('And',       7000),
                         ('More',      5)]
        success_count = 0
        TRIALS = 500
        for i in range(TRIALS):
            shuffled = rand.weighted_order(original_list)
            if shuffled.index('And') == 0:
                success_count += 1
        self.assertGreater(success_count, TRIALS * 0.75)

    def test_weighted_order_with_all_1_weights(self):
        # When every weight is 0, the probability spread should be uniform
        original_list = [('Something', 1),
                         ('Another',   1),
                         ('Again',     1),
                         ('And',       1),
                         ('More',      1)]
        landing_positions = {'Something': [],
                             'Another': [],
                             'Again': [],
                             'And': [],
                             'More': []}
        for i in range(1000):
            shuffled_list = rand.weighted_order(original_list)
            for index, value in enumerate(shuffled_list):
                landing_positions[value].append(index)
        # With an even distribution, landing indices should average near 2.5
        for positions in landing_positions.values():
            average = sum(positions) / len(positions)
            self.assertLess(abs(average - 2.5), 1)

    def test_weighted_order_with_invalid_weight(self):
        with self.assertRaises(rand.ProbabilityUndefinedError):
            foo = rand.weighted_order([('bar', 0), ('baz', 5), ('buzz', -3)])

    def test_weighted_order_with_empty_list_returns_empty_list(self):
        self.assertEqual(rand.weighted_order([]), [])


# Definition for a binary tree node.
# class TreeNode(object):
#     def __init__(self, x):
#         self.val = x
#         self.left = None
#         self.right = None

class Solution(object):
    def findTilt(self, root):
        """
        :type root: TreeNode
        :rtype: int
        """
        result = []
        def getvalue(root):
            if root is None:
                return 0
            left = getvalue(root.left)
            right = getvalue(root.right)
            result.append(abs(left - right))
            return root.val + left + right
        getvalue(root)
        return sum(result)

Binary Tree Tilt/Binary Tree Tilt(AC).py


"""
This Codes are to find the statistics of token length and number of elements in
factual description in train data. Longest token length is required to pre-
determine the pad-size of input data before training.
"""

import json

def find_longest(json_data, key):
    with open(json_data) as f:
        longest = []
        for idx, each_line in enumerate(f):
            list_of_strings = json.loads(each_line)[key]
            length = len(list_of_strings)
            print(idx, length)

            if not longest:
                longest.append(length)
            elif longest[-1] < length:
                longest[-1] = length
            elif longest[-1] >= length:
                continue
        print(longest[-1])
        return longest

def find_shortest(json_data, key):
    with open(json_data) as f:
        shortest = []
        for idx, each_line in enumerate(f):
            list_of_strings = json.loads(each_line)[key]
            length = len(list_of_strings)
            print(idx, length)

            if not shortest:
                shortest.append(length)
            elif shortest[-1] > length:
                shortest[-1] = length
            elif shortest[-1] <= length:
                continue
        print(shortest[-1])
        return shortest

def find_words_set():
    collector = set()
    with open("Train.json") as f:
        for each_line in f:
            list_of_strings = json.loads(each_line)["features_content"]
            voca_set = set(list_of_strings)
            # print(voca_set)
            collector = collector.union(voca_set)

    print(len(collector))
    # dump_pkl(collector, "words_in_factual.pkl")
    return collector

if __name__ == "__main__":
    data_path = \
        "/home/zachary/projects/DeepWTO/models/citability_w_Article/data" \
        "/prep/entire_one_label.json"
    find_longest(data_path, 'art')

    pass