import numpy as np
from scipy.stats import multivariate_normal
from scipy import ndimage
import cv2
from collections import *
import random
from itertools import *
from functools import *
import os

def points_to_gaussian_heatmap(centers, height, width, scale): # 将二维的坐标点转换为高斯热图
    gaussians = []
    for y, x in centers:
        s = np.eye(2) * scale
        g = multivariate_normal(mean=(x, y), cov=s)
        gaussians.append(g)
    x, y = np.arange(0, width), np.arange(0, height)
    xx, yy = np.meshgrid(x, y)
    xxyy = np.stack([xx.ravel(), yy.ravel()]).T
    zz = sum(g.pdf(xxyy) for g in gaussians)
    img = zz.reshape((height, width))

    return img / np.max(img)

@lru_cache
def point_prompt_cache_helper(label_path, local_mode=True, neg_range=20, area_limit=200):
    label = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE) / 255
    h, w = label.shape
    structure = ndimage.generate_binary_structure(2, 2)
    labeled_image, num_features = ndimage.label(label, structure=structure)
    label_cnts = Counter(labeled_image.flatten().tolist())
    filtered_labels = [k for k, v in label_cnts.items() if v >= area_limit and k]
    component = np.zeros((h, w), dtype=int)

    # if 'local' mode: choose a label
    if local_mode:
        choice_label = random.choice(filtered_labels)
        positive_points = np.argwhere(labeled_image==choice_label)
        component[labeled_image == choice_label] = 1
    else:
        positive_points = np.argwhere(labeled_image > 0)
        component[labeled_image > 0] = 1

    # dfs get adjacent negtive points:
    dq = deque([(x, y, 0) for x, y in positive_points])
    neg_s = set((x, y) for x, y in positive_points)
    while dq:
        x, y, d = dq.popleft()
        if d >= neg_range: continue
        for dx, dy in product(*[range(-1, 2)] * 2):
            nx, ny = x + dx, y + dy
            if 0 <= nx < h and 0 <= ny < w and (nx, ny) not in neg_s:
                neg_s.add((nx, ny))
                dq.append((nx, ny, d+1))
    negative_points = list(neg_s - set((x, y) for x, y in positive_points))

    # group
    positive_points_group = [positive_points] if local_mode \
        else [np.argwhere(labeled_image == k) for k in filtered_labels]

    return component, positive_points_group, negative_points

def label_to_point_prompt(label_path, positive_num=5, total_num=5, local_mode=True,
                          neg_range=20, area_limit=50, random_seed=None):
    if random_seed:
        random.seed(random_seed)
        np.random.seed(random_seed)

    component, positive_points_group, negative_points = \
        point_prompt_cache_helper(label_path, local_mode, neg_range, area_limit)
    prompt_points_pos, prompt_points_neg = [], []

    # positive points:
    for positive_points in positive_points_group:
        for _ in range(positive_num):
            i = np.random.choice(np.arange(len(positive_points)))
            y, x = positive_points[i] # pick up a positive point randomly
            prompt_points_pos.append([x, y])
    # negtive points:
    for _ in range(total_num-len(prompt_points_pos)):
        i = np.random.choice(np.arange(len(negative_points)))
        y, x = negative_points[i] # pick up a negative point randomly
        prompt_points_neg.append([x, y])

    return np.array([component], dtype=float), np.array(prompt_points_pos), np.array(prompt_points_neg)

if __name__=="__main__":
    mx = 0
    dir_path = "datasets/OCTA-500/OCTA_6M/GT_Artery"
    for label_path in os.listdir(dir_path):
        t = 0
        component, pos, neg = label_to_point_prompt("datasets/OCTA-500/OCTA_6M/GT_Artery/" + label_path, 1, 30, local_mode=False)
        t += len(pos)
        component, pos, neg = label_to_point_prompt("datasets/OCTA-500/OCTA_6M/GT_Vein/" + label_path, 1, 30, local_mode=False)
        t += len(pos)
        mx = max(mx, t)
    print("mx:", mx)
    # label_path = "datasets/OCTA-500/OCTA_3M/GT_Vein/10301.bmp"
    # label_img = cv2.imread(label_path, cv2.IMREAD_GRAYSCALE)
    # ori_img = cv2.imread("datasets/OCTA-500/OCTA_3M/ProjectionMaps/OCTA(ILM_OPL)/10301.bmp", cv2.IMREAD_GRAYSCALE)
    # for i in range(20):
    #     component, pos, neg = label_to_point_prompt(label_path, 3, 6)
    #     component = (255 * component[0]).astype(np.uint8)
    #     component = cv2.cvtColor(component, cv2.COLOR_GRAY2RGB)
    #     img = cv2.cvtColor(ori_img, cv2.COLOR_GRAY2RGB)
    #     label = cv2.cvtColor(label_img, cv2.COLOR_GRAY2RGB)
    #     cv2.imwrite("temp/complabel_{:0>4}.png".format(i), component)
    #     img = img * 0.4 + (0, 255, 255) * component * 0.2
    #     sz = 7
    #     for (x, y) in pos: cv2.circle(component, (x, y), sz, (0, 0, 255), -1)
    #     for (x, y) in neg: cv2.circle(component, (x, y), sz, (255, 0, 0), -1)
    #     cv2.imwrite("temp/comp_{:0>4}.png".format(i), component)
    #     for (x, y) in pos: cv2.circle(img, (x, y), sz, (0, 0, 255), -1)
    #     for (x, y) in neg: cv2.circle(img, (x, y), sz, (255, 0, 0), -1)
    #     cv2.imwrite("temp/img_{:0>4}.png".format(i), img)
    #     for (x, y) in pos: cv2.circle(label, (x, y), sz, (0, 0, 255), -1)
    #     for (x, y) in neg: cv2.circle(label, (x, y), sz, (255, 0, 0), -1)
    #     pt = 20
    #     try:
    #         for j, (x, y) in enumerate(pos): cv2.imwrite("temp/patch_{:0>4}_{:0>2}.png".format(i, j), label[y-pt:y+pt,x-pt:x+pt])
    #         for j, (x, y) in enumerate(neg): cv2.imwrite("temp/patch_{:0>4}_{:0>2}.png".format(i, j+len(pos)), label[y-pt:y+pt,x-pt:x+pt])
    #     except:
    #         pass
    #     cv2.imwrite("temp/label_{:0>4}.png".format(i), label)


from dataclasses import dataclass

@dataclass
class SelectOption:
    name: str
    id: str
    is_disabled: bool

def process_data(options: tuple[str | tuple[str, str], ...]) -> list[SelectOption]:
    """
    Processes the given `options` and returns the processed list

    :param options: A list of options to process
    :type options: tuple[str | tuple[str, str], ...]
    :raises TypeError: If an option is not processable, a `TypeError` will be raised
    :return: a list of `SelectOptions`
    :rtype: list[SelectOption]
    """
    processed_options: list[SelectOption] = []

    # process given options
    for option in options:
        if type(option) is str:
            processed_options.append(SelectOption(name=option, id=option, is_disabled=False))
        elif type(option) is tuple:
            processed_options.append(SelectOption(name=option[0], id=option[1], is_disabled=False))
        else:
            raise TypeError('Argument is not processable')

    return processed_options


import scapy.all as scapy
import socket

def scan(ip):
    # Create an ARP request packet to get MAC addresses for IP addresses
    arp_request = scapy.ARP(pdst=ip)

    # Create an Ethernet frame to send the ARP request
    ether = scapy.Ether(dst="ff:ff:ff:ff:ff:ff")  # Broadcast MAC address

    # Combine the Ethernet frame and ARP request
    arp_request_packet = ether / arp_request

    # Send the packet and receive responses
    answered = scapy.srp(arp_request_packet, timeout=1, verbose=False)[0]

    # Create a list of dictionaries to store IP and MAC addresses
    devices_list = []

    # Iterate through the responses and extract IP and MAC addresses
    for element in answered:
        device_info = {"ip": element[1].psrc, "mac": element[1].hwsrc}
        devices_list.append(device_info)

    return devices_list

def check_online(ip, port=80):
    try:
        socket.setdefaulttimeout(5)  # Adjust the timeout as needed
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect((ip, port))
        sock.close()
        return True
    except (socket.timeout, ConnectionRefusedError):
        return False

def main():
    target_ip_range = input("Enter the target IP range (e.g., 192.168.1.1/24): ")
    scanned_devices = scan(target_ip_range)

    print("Connected devices:")
    print("IP Address\t\tMAC Address")
    print("-----------------------------------------")
    for device in scanned_devices:
        ip_address = device["ip"]
        mac_address = device["mac"]
        if check_online(ip_address):
            print(f"{ip_address}\t\t{mac_address}")

if __name__ == "__main__":
    main()


# azureml-core of version 1.0.72 or higher is required
# azureml-dataprep[pandas] of version 1.1.34 or higher is required
from azureml.core import Workspace, Dataset
import pandas as pd
import numpy as np
import keras
from sklearn.model_selection import train_test_split
from azureml.core.model import Model

# df = pd.read_csv('tennis-result-2.csv')
df = pd.read_csv('0oiWTODB7i2iXjCIMw1H.csv')

def create_model():
      model = keras.models.Sequential()
      model.add(keras.layers.Dense(4, input_dim=4, activation='relu'))
      model.add(keras.layers.Dense(4, activation='relu'))
      model.add(keras.layers.Dense(1, activation='sigmoid'))
      model.compile(loss='binary_crossentropy', optimizer='adam',  metrics=['accuracy'])
      return model;

subscription_id = '76d03a17-20be-4175-bbed-ca6d7819c68f'
resource_group = 'machine-learning'
workspace_name = 'ml-ws'

workspace = Workspace(subscription_id, resource_group, workspace_name)

dataset = Dataset.get_by_name(workspace, name='tennis-result-less-columns')
dataset = dataset.to_pandas_dataframe()

properties = list(dataset.columns.values)

x = dataset[properties]
y = dataset['WinOrLose']

x = x.values
y = y.values

X_train, X_test, y_train, y_test = train_test_split(x, y, test_size=0.3, random_state=0)

training_model = create_model();
training_model.fit(X_train, y_train, epochs=50, batch_size=1)
test_loss, test_acc = training_model.evaluate(X_test, y_test)
print('Test accuracy:', test_acc)

x_predict = np.array([[25, 9, 0 ], [8, 33, 0]])

# serialize model to JSON
model_json = training_model.to_json()
with open("model/model.json", "w") as json_file:
    json_file.write(model_json)
# serialize weights to HDF5
training_model.save("model/model.h5")

training_model.save_weights("model/model_weight.h5")

# Register model weight
model = Model.register(workspace = workspace,
                        model_path ="model/model_weight.h5",
                        model_name = "ml-ws-keras-weight-ddd",
                        tags = {"for": "ddd night"},
                        description = "This is a sample Keras model trained by tennis result data for DDD night",)

## load model and score
# loaded_model = create_model();
# loaded_model.load_weights("model/model_weight.h5")
# result = loaded_model.predict_classes(x_predict)
# print(result)


from fastapi import APIRouter, HTTPException, status
from pydantic import BaseModel

class Trees (BaseModel):
    idtree: int
    name: str
    high: float

trees_list = [
    Trees(idtree=1, name="Evano", high=3.45),
    Trees(idtree=2, name="Roble", high=5.6),
    Trees(idtree=3, name="Arbusto", high=2.0),
    Trees(idtree=4, name="Pino", high=3.6),
    Trees(idtree=5, name="Palmera", high=3.0)
]

routerTrees = APIRouter()

@routerTrees.get("/5/", status_code=status.HTTP_200_OK)
async def treesclass():
    return trees_list

@routerTrees.get("/5/{id}", status_code=status.HTTP_200_OK)
async def treesclass(id: int):
    trees = filter(lambda tree: tree.idtree == id, trees_list)
    try:
        return list(trees)[0]
    except:
        raise HTTPException(status_code=status.HTTP_404_NOT_FOUND)


# patricia.danae.yero.py
# SPDX-License-Identifier: GPL-3.0-or-later
""" São Jerônimo.

.. codeauthor:: Carlo Oliveira <mail@local.tipo>

Changelog
---------
.. versionadded::    20.09
        Descreva o que você adicionou no código.

"""
from _spy.vitollino.main import Cena, Elemento, Texto, Sala, STYLE
from _spy.vitollino.main import INVENTARIO as inv
STYLE["width"]=900
STYLE["height"]='650px'

FLAVINHO ="https://i.imgur.com/fv9BZ54.jpeg"
FOTO_PRINCIPAL ="https://i.imgur.com/aisVckn.jpg"
DALMACIA ="https://imgur.com/dyLIQib.jpg"
JERONIMO_JOVEM ="blob:https://web.whatsapp.com/edad8633-e384-4538-b7f3-ddd4837df1c0"
JERONIMO_VELHO =""
CURIOSIDADE =""
LEAO ="blob:https://web.whatsapp.com/78b2c07d-57e3-4b38-92d1-2dfff506a540"
PAPA_LIBERIO="https://imgur.com/vlIrMHW.jpg"
BATIZADO_JERONIMO  =""
ROMA ="https://imgur.com/Zh5yUpP.jpg"
ORDDENACAO_SACERDOTAL ="https://imgur.com/qZ3zINX.jpg"
SONHO ="blob:https://web.whatsapp.com/2960c698-2f09-4eff-abd9-6ee5d4b7ee47"
JERONIMO_CAVERNA =""
BIBLIA ="https://imgur.com/QPELbOd.jpg"
VULGATA =""
BELEM ="https://imgur.com/sf5X4cr.jpg"
FOTO_DO_PADROEIRO ="https://imgur.com/Y0CZrXO.jpg"
FRASE =""
CENA_FINAL="https://imgur.com/kRQyK02.jpg"
PAPAD="https://imgur.com/a/bO5ZNKx.jpg"
CAVERNA="https://imgur.com/fBFCru5.jpg"
FOTO_PAROQUIA="https://imgur.com/zFwNqPr.jpg"
class kkkk():
    def __init__(self):
        self.ft= Cena(img=FOTO_PAROQUIA)
        self.leao= Cena(img=LEAO)
        self.padre= Elemento(img=FLAVINHO)
        self.f_t= Cena(img=FOTO_PRINCIPAL)
        self.dalmacia= Cena(img=DALMACIA)
        self.j_j= Elemento(img=JERONIMO_JOVEM)
        self.j_v= Elemento(img=JERONIMO_VELHO)
        self.batizado= Cena(img=BATIZADO_JERONIMO)
        self.roma= Cena(img=ROMA)
        self.ordenacao= Cena(img=ORDDENACAO_SACERDOTAL)
        self.sonho= Cena(img=SONHO)
        self.curiosidade= Cena(img=CURIOSIDADE)
        self.caverna= Cena(img=CAVERNA)
        #self.pl= Elemento(img=PAPA_LIBERIO)
        self.pl= Cena(img=PAPA_LIBERIO)
        self.j_c= Cena(img=JERONIMO_CAVERNA)
        self.biblia= Cena(img=BIBLIA)
        self.vulgata= Cena(img=VULGATA)
        self.belem= Cena(img=BELEM)
        self.f_p= Cena(img=FOTO_DO_PADROEIRO)
        self.frase= Cena(img=FRASE)
        self.cf= Cena(img=CENA_FINAL)
        self.pd= Elemento(img=PAPAD)
        self.f_t.vai()
        self.entrou_padre()

    def entrou_padre(self,*_):
        self.padre.entra(self.f_t)
        fala=Texto(self.f_t, "Eu sou o Padre Flávio e vou te ajudar nessa aventura sobre a história do nosso amado padroeiro", foi=self.entrou_1)
        self.padre.vai=Texto(self.f_t, "olá pessoal, certinho?", foi=fala.vai).vai

    def entrou_1(self,*_):
        def resposta(optou):
            respondeu = dict(
            A=Texto(self.dalmacia, "Ele foi para Roma depois, e não foi onde ele nasceu"),
            B=Texto(self.dalmacia, "São Jerônimo nasceu na Dalmácia no ano de 340.", foi=self.entrou_2),
            C=Texto(self.dalmacia, "Libério é o nome do Papa que vamos ficar conhecendo mais adiante"),
            )
            respondeu[optou].vai()
        self.dalmacia.vai()
        self.padre.entra(self.dalmacia)
        self.padre.vai=Texto(self.dalmacia, "Sabe onde Jerônimo nasceu?",
            foi=resposta, A="Roma", B="Dalmácia", C="Libério").vai

    def entrou_2(self,*_):
        self.roma.vai()
        self.padre.entra(self.roma)
        self.padre.vai=Texto(self.roma, "Após a morte de seus pais, Jerônimo foi para Roma estudar e durante sua permanencia teve um sonho muito importante para sua conversão.", foi=self.entrou_3).vai

    def entrou_3(self,*_):
        self.sonho.vai()
        self.padre.entra(self.sonho)
        self.padre.vai=Texto(self.sonho, "No sonho, Jerônimo apresentava-se como cristão e era repreedindo pelo próprio Cristo por estar faltando com a verdade.", foi=self.entrou_4).vai

    def entrou_4(self,*_):
        self.pl.vai()
        self.padre.entra(self.pl)
        self.padre.vai=Texto(self.pl, "Aos 25 anos de idade Jerônimo foi batizado pelo Papa Libério no fim de sua permanencia em Roma.", foi=self.entrou_5).vai
            #self.padre.vai=Texto(self.fp, "Aos 25 anos de idade Jerônimo foi batizado pelo Papa Libério no fim de sua permanencia em Roma.").vai

    def entrou_5(self,*_):
        self.ordenacao.vai()
        self.padre.entra(self.ordenacao)
        self.padre.vai=Texto(self.ordenacao, "Jerônimo foi ordenado sacerdote no ano de 379, retirando-se para dedicar-se ao estudo.", foi=self.entrou_curi).vai

    def entrou_curi(self,*_):
        self.curiosidade.vai()
        self.padre.entra(self.curiosidade)
        self.padre.vai=Texto(self.curiosidade,"Jerônimo foi ordenado sacerdote no ano de 379DC, retirando-se para dedicar-se ao estudo.", foi=self.entrou_6).vai

    def entrou_6(self,*_):
        self.caverna.vai()
        self.padre.entra(self.caverna)
        self.padre.vai=Texto(self.caverna,"Por ter aprendido as linguas originais para melhor compreender as escrituras, Nosso padroeiro pôde a pedido do Papa Damaso traduzir as escrituras para o Latim.", foi=self.entrou_7).vai

    def entrou_7(self,*_):
        self.biblia.vai()
        self.padre.entra(self.biblia)
        self.padre.vai=Texto(self.biblia, "A tradução da sagrada escritura recebeu o nome Vulgata.", foi=self.entrou_8).vai

    def entrou_8(self,*_):
        self.vulgata.vai()
        self.padre.entra(self.vulgata)
        self.padre.vai=Texto(self.vulgata, "A tradução da sagrada escritura recebeu o nome Vulgata.", foi=self.entrou_9).vai

    def entrou_9(self,*_):
        self.belem.vai()
        self.padre.entra(self.belem)
        self.padre.vai=Texto(self.belem, "Após traduzir a sagrada escritura , Jerônimo mudou-se para Belém a cidade onde nasceu nosso Salvador.", foi=self.entrou_10).vai

    def entrou_10(self,*_):
        self.f_p.vai()
        self.padre.entra(self.f_p)
        self.padre.vai=Texto(self.f_p, "Jerônimo morreu no dia 30 de setembro de 420 em Belém, por isso o dia da Biblía e de São Jerônimo são comemorados no dia 30 de setembro.", foi=self.entrou_11).vai

    def entrou_11(self,*_):
        self.leao.vai()
        self.padre.entra(self.leao)
        self.padre.vai=Texto(self.leao, "É um leão!!", foi=self.entrou_12).vai

    def entrou_12(self,*_):
        self.frase.vai()
        self.padre.entra(self.frase)
        self.padre.vai=Texto(self.frase, "A frase mais conhecida de nosso padroeiro é:  Ignorar as Escrituras Sagradas é ignorar a Cristo", foi=self.entrou_final).vai

    def entrou_final(self,*_):
        self.cf.vai()
        self.padre.entra(self.cf)
        fala=Texto(self.cf, " Até a próxima !!!")
        self.padre.vai=Texto(self.cf, "Espero que tenham gostado de aprender um pouco mais da história do nosso amdado padroeiro São Jerônimo!", foi=fala.vai).vai
#f_T.vai()
kkkk()


class Solution:
    def EntryNodeOfLoop(pHead):
        # write code here
        if not pHead or not pHead.next:
            return None
        fast,slow=pHead,pHead.next.next
        while(fast and slow and slow.next and fast!=slow):
            fast=fast.next
            slow=slow.next.next
        if not fast==slow:
            return None
        meet_pointer=fast
        count=1
        meet_pointer=meet_pointer.next
        while(meet_pointer!=fast):
            meet_pointer=meet_pointer.next
            count+=1
        #其中count是环中链表的长度
        head,post=pHead,pHead
        for _ in range(0,count):
            head=head.next
        while(post!=head.next):
            head=head.next
            post=post.next
        return post

offer/循环链表节点的入口.py

循环链表节点的入口.py


from sys import argv
from textdistance import damerau_levenshtein
import difflib
import ast

def create_pill(node, depth = 0):
    'recursively creates a pill that contains every node first letter and `depth` of a node'
    resulting_pill = ''
    resulting_pill += type(node).__name__[0] + str(depth) + '.'*len(node._fields)
    # print(f'visiting node {type(node).__name__} at depth {depth} with attributes {node._fields}')
    for child in ast.iter_child_nodes(node):
        resulting_pill += create_pill(child, depth + 1)
    return resulting_pill

txt1 = open(argv[1], 'r').read()
txt2 = open(argv[2], 'r').read()
pill1 = create_pill(ast.parse(txt1))
pill2 = create_pill(ast.parse(txt1))
if damerau_levenshtein.normalized_distance(pill1, pill2) <= 0.1:
    print('possible plagiarism')
    diff = difflib.HtmlDiff(tabsize=8, wrapcolumn=None, linejunk=None)
    stripped_text1 = ast.unparse(ast.parse(txt1))
    stripped_text2 = ast.unparse(ast.parse(txt2))
    diff_html = diff.make_file(stripped_text1.split('\n'), stripped_text2.split('\n'), argv[1], argv[2])
    with open('plagiarism.html', 'w') as html:
        html.write(diff_html)


#
# Logging class for PyBaMM
# Includes additional logging levels inspired by verboselogs
# https://pypi.org/project/verboselogs/#overview-of-logging-levels
#
# Implementation from stackoverflow
# https://stackoverflow.com/questions/2183233/how-to-add-a-custom-loglevel-to-pythons-logging-facility
#
import logging

def set_logging_level(level):
    logger.setLevel(level)

format = (
    "%(asctime)s - [%(levelname)s] %(module)s.%(funcName)s(%(lineno)d): "
    + "%(message)s"
)
logging.basicConfig(format=format)
logging.Formatter(datefmt="%Y-%m-%d %H:%M:%S", fmt="%(asctime)s.%(msecs)03d")

# Additional levels inspired by verboselogs
SPAM_LEVEL_NUM = 5
logging.addLevelName(SPAM_LEVEL_NUM, "SPAM")

VERBOSE_LEVEL_NUM = 15
logging.addLevelName(VERBOSE_LEVEL_NUM, "VERBOSE")

NOTICE_LEVEL_NUM = 25
logging.addLevelName(NOTICE_LEVEL_NUM, "NOTICE")

SUCCESS_LEVEL_NUM = 35
logging.addLevelName(SUCCESS_LEVEL_NUM, "SUCCESS")

def spam(self, message, *args, **kws):
    if self.isEnabledFor(SPAM_LEVEL_NUM):
        self._log(SPAM_LEVEL_NUM, message, args, **kws)

def verbose(self, message, *args, **kws):
    if self.isEnabledFor(VERBOSE_LEVEL_NUM):
        self._log(VERBOSE_LEVEL_NUM, message, args, **kws)

def notice(self, message, *args, **kws):
    if self.isEnabledFor(NOTICE_LEVEL_NUM):
        self._log(NOTICE_LEVEL_NUM, message, args, **kws)

def success(self, message, *args, **kws):
    if self.isEnabledFor(SUCCESS_LEVEL_NUM):
        self._log(SUCCESS_LEVEL_NUM, message, args, **kws)

logging.Logger.spam = spam
logging.Logger.verbose = verbose
logging.Logger.notice = notice
logging.Logger.success = success

# Create a custom logger
logger = logging.getLogger(__name__)
set_logging_level("WARNING")


class Solution:
    def deleteAndEarn(self, nums: List[int]) -> int:
        maxV = max(nums)
        points = [0 for _ in range(maxV + 1)]
        prev, cur = 0, 0
        for num in nums:
            points[num] += num

        for p in points:
            prev, cur = cur, max(prev + p, cur)
        return cur


'''
Given a binary tree, write a function to get the maximum width of the given tree. The width of a tree is the maximum width among all levels.
The binary tree has the same structure as a full binary tree, but some nodes are null.

The width of one level is defined as the length between the end-nodes (the leftmost and right most non-null nodes in the level,
where the null nodes between the end-nodes are also counted into the length calculation.

Example 1:
Input:
           1
         /   \
        3     2
       / \     \
      5   3     9
Output: 4
Explanation: The maximum width existing in the third level with the length 4 (5,3,null,9).

Example 2:
Input:
          1
         /
        3
       / \
      5   3
Output: 2
Explanation: The maximum width existing in the third level with the length 2 (5,3).

Example 3:
Input:
          1
         / \
        3   2
       /
      5
Output: 2
Explanation: The maximum width existing in the second level with the length 2 (3,2).

Example 4:
Input:
          1
         / \
        3   2
       /     \
      5       9
     /         \
    6           7
Output: 8
Explanation:The maximum width existing in the fourth level with the length 8 (6,null,null,null,null,null,null,7).

Note: Answer will in the range of 32-bit signed integer.
'''

#Solution: Using BFS
# Definition for a binary tree node.
# class TreeNode:
#     def __init__(self, val=0, left=None, right=None):
#         self.val = val
#         self.left = left
#         self.right = right
class Solution:
    def widthOfBinaryTree(self, root: TreeNode) -> int:
        queue = [(root, 0, 0)]
        cur_depth = left = ans = 0
        for n, d, p in queue:
            if n:
                queue.append((n.left, d+1, p*2))
                queue.append((n.right, d+1, p*2 + 1))
                if cur_depth != d:
                    cur_depth = d
                    left = p
                ans = max(p - left + 1, ans)

        return ans


# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.6.0
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

# +
import cv2
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import tqdm
import os

# %matplotlib inline
# -

ds_num = input("Dataset: #")
exp_dir = input("Experiment Directory: ")

if os.path.exists(exp_dir + "/artifacts/overlay"):
    print("Directory already exists")
    pass
else:
    os.mkdir(exp_dir + "/artifacts/overlay")

df = pd.read_csv(exp_dir + "/artifacts/pred_{:03d}_copy.csv".format(int(ds_num)))

def get_info(df, line):
    value = []
    params = ["e_trans3d", "e_trans2d", "e_orient", "e_phi", "e_gamma"]
    for param in params:
        value.append(str(round(df[param][line], 2)))

    return value

for i in tqdm.trange(100):
    orig = cv2.imread("../../Database/ds_021/val/img_{:05d}.jpg".format(i + 1))
    pred = cv2.imread(exp_dir + "/artifacts/pred_imgs/img_{:05d}.png".format(i + 1))
    orig = cv2.cvtColor(orig, cv2.COLOR_BGR2RGBA)
    pred = cv2.cvtColor(pred, cv2.COLOR_BGRA2RGBA)
    #     pred[:, :, 0:2] = 0
    blended = cv2.addWeighted(src1=orig, alpha=0.8, src2=pred, beta=0.4, gamma=0)
    im = cv2.cvtColor(blended, cv2.COLOR_RGBA2BGR)
    info = get_info(df, i)

    txt0 = "Error (3D):     {} mm".format(info[0])
    txt1 = "Error (2D):     {} px".format(info[1])
    txt2 = "Error (Orient): {} deg".format(info[2])
    txt3 = "Error (Joint):  {} deg".format(info[3])
    txt4 = "Error (Roll):   {} deg".format(info[4])

    im = cv2.putText(
        im,
        txt0,
        (50, 70),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (
            255,
            255,
            0,
        ),
        2,
    )
    im = cv2.putText(
        im,
        txt1,
        (50, 120),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (
            255,
            255,
            0,
        ),
        2,
    )
    im = cv2.putText(
        im,
        txt2,
        (50, 170),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (
            255,
            255,
            0,
        ),
        2,
    )
    im = cv2.putText(
        im,
        txt3,
        (50, 220),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (
            255,
            255,
            0,
        ),
        2,
    )
    im = cv2.putText(
        im,
        txt4,
        (50, 270),
        cv2.FONT_HERSHEY_SIMPLEX,
        1,
        (
            255,
            255,
            0,
        ),
        2,
    )
    cv2.imwrite(exp_dir + "/artifacts/overlay/img_{:05d}.png".format(i + 1), im)


import datetime

from django.shortcuts import render
from django.conf import settings
from django.http import HttpResponse

import requests
import redis

from .models import WeiboAccount

# Create your views here.
def weibo_login(request):
    app_key = settings.WEIBO_APP_KEY
    redirect_uri = settings.WEIBO_REDIRECT_URI
    return render(request, 'weibo/login.html', {
        'app_key': app_key,
        'redirect_uri': redirect_uri,
    })

def auth_complete(request):
    app_key = settings.WEIBO_APP_KEY
    app_secret = settings.WEIBO_APP_SECRET
    redirect_uri = settings.WEIBO_REDIRECT_URI
    code = request.GET['code']
    res = requests.post(
        settings.WEIBO_ACCESS_TOKEN_URL,
        data={
            "client_id": app_key, "client_secret": app_secret,
            "grant_type": "authorization_code", "code": code,
            "redirect_uri": redirect_uri,
        },
    )
    print(res.text)
    if res.status_code == requests.codes.ok:
        response_json = res.json()
        access_token = response_json['access_token']
        expire = response_json['expires_in']
        uid = response_json['uid']
        R = redis.StrictRedis()
        R.set(access_token, uid, ex=int(expire))
        request.session['access_token'] = access_token
        request.session.set_expiry(int(expire))
        user_info_res = requests.get(settings.WEIBO_USER_DETAIL_URL,
            params={'access_token': access_token, 'uid':uid}
        )
        if user_info_res.status_code == requests.codes.ok:
            user_info = user_info_res.json()
            wa, create = WeiboAccount.objects.get_or_create(
                access_token=access_token,
                defaults=dict(
                    uid=uid,
                    nickname=user_info['name'],
                    description=user_info['description'],
                    profile_image_url=user_info['profile_image_url'],
                    gender=user_info['gender'],
                    followers_count=user_info['followers_count'],
                    friends_count=user_info['friends_count'],
                    created=datetime.datetime.strptime(
                    user_info['created_at'], "%a %b %d %X %z %Y"),
                    allow_all_comment=user_info['allow_all_comment'],
                    avatar=user_info['avatar_hd'],
                    verified_reason=user_info['verified_reason'],
                    credit_score=user_info['credit_score'],
                    user_ability=user_info['user_ability'],
                )
            )
            return HttpResponse("OK!")
    return HttpResponse("FAIL!!!")


import copy

import torch

class ObservationQueue(list):
    """
    FIFO queue for storing observations. The queue is limited to a maximum length.
    """

    def __init__(self, maxlen=3):
        super().__init__()
        self.maxlen = maxlen

    def append(self, observation):
        if torch.cuda.is_available():
            observation = observation.cuda()
        super().append(copy.deepcopy(observation))
        if len(self) > self.maxlen:
            self.pop(0)


import pyspark
from operator import add

sc = pyspark.SparkContext()

def is_good_line(line):
    try:
        fields = line.split(',')
        if len(fields) == 7: # transactions
            str(fields[2]) # to_addr
            if int(fields[3]) == 0:
                return False
        elif len(fields) == 5: # contracts
            str(fields[0]) # contract addr
        else:
            return False
        return True
    except:
        return False

transactions = sc.textFile('/data/ethereum/transactions').filter(is_good_line)
contracts = sc.textFile('/data/ethereum/contracts').filter(is_good_line)

step1 = transactions.map(lambda l: (l.split(',')[2], int(l.split(',')[3])))
step2 = step1.reduceByKey(add)
step3 = step2.join(contracts.map(lambda x: (x.split(',')[0], 'contract')))
top10 = step3.takeOrdered(10, key = lambda x: -x[1][0])

for record in top10:
    print("{},{}".format(record[0], int(record[1][0]/1000000000000000000)))
#application_1649894236110_1689 15 minutes try 1
#application_1649894236110_1779 10 minutes
#application_1649894236110_1753 10 minutes


from svg.path import parse_path
from svg.path import Path, Line, Arc, CubicBezier, QuadraticBezier
from xml.dom import minidom
# from magenta.models.sketch_rnn import utils
from rdp import rdp
import matplotlib
import matplotlib.pyplot as plt
import matplotlib.gridspec as grdspc
import numpy as np
import os
import re
import sys
import ipdb

folder_p = '/home/kelvin/OgataLab/parse_svg/parse_svg/Sketchy_data_valid/airplane/'
dest_p = '/home/kelvin/OgataLab/magenta/magenta/models/sketch_rnn/sketchy_data/'
# f_name = 'n02139199_15837-7.svg'

def adjust_axes(axis):
    """ equalize the scales of x-axis and y-axis """
    x_lim = axis.get_xlim()
    y_lim = axis.get_ylim()
    new_lim = (min(x_lim[0], y_lim[0]), max(x_lim[1], y_lim[1]))
    axis.set_xlim(new_lim)
    axis.set_ylim(new_lim)
    axis.set_aspect('equal')

def rsvg_in_folderxy(f_path, no_of_files):
    '''reads all the svg files in from folder path, equal to the number of files specified. Then all path data in the svg files are converted into stroke-3 data and stored in a list'''
    c=0
    svg_data = []

    for file in os.listdir(f_path):

        if file.endswith(".svg"):
            # data = to_stroke3(svg2xyList(f_path + file))
            data = svg2xyList(f_path + file)
            if c<no_of_files:
                svg_data.append(data)
                c+=1
            else:
                break
    return svg_data

def rsvg_in_folders3(f_path, no_of_files):
    '''reads all the svg files in from folder path, equal to the number of files specified. Then all path data in the svg files are converted into stroke-3 data and stored in a list'''
    c=0
    svg_data = []

    for file in os.listdir(f_path):

        if file.endswith(".svg"):
            data = to_stroke3(svg2xyList(f_path + file))
            # data = arr_reduce(data,100)
            # data = svg2xyList(f_path + file)
            # ipdb.set_trace()
            if c<no_of_files:
                svg_data.append(data)
                c+=1
            else:
                break
    return svg_data

def check_max_seq(f_path):

    seq_len = []
    for file in os.listdir(f_path):
        if file.endswith(".svg"):
            N = to_stroke3(svg2xyList(f_path+file))
            seq_len.append((len(N),file))
    # ipdb.set_trace()
    return sorted(seq_len,key=lambda k:k[0])
    # print("Maximum sequence length is:",max[])

def svg2xyList(file, eps=3.0):
    '''converts path component "d" from svg file into xy-coordinate list'''
    line = []
    strt_pts = []
    ed_pts = []
    strt_pts1 = []
    ed_pts1 = []
    # print(file)
    all_path = minidom.parse(file).getElementsByTagName('path')
    # ipdb.set_trace()
    for i in range(len(all_path)):
        clr = all_path[i].getAttribute('stroke')
        clr1 = all_path[i].getAttribute('STROKE')
        if clr == '#000' or clr1 == '#000':
            d = all_path[i].getAttribute('d')
            P = parse_path(d)
            points = []

            if len(P)<1:
                pass
            else:
                for j in range(len(P)):
                    abs_ed = P[len(P)-1].end.real, P[len(P)-1].end.imag

                    if isinstance(P[j], CubicBezier) or isinstance(P[j], Line):
                        strt = P[j].start.real, P[j].start.imag
                        ed = P[j].end.real, P[j].end.imag

                        if j == 0:
                            abs_strt = strt
                            points.append(strt)
                            points.append(ed)
                        else:
                            points.append(ed)
                    else:
                        print("What?! th is "+P[j])

                points = rdp(points, epsilon=eps)
                line.append(points)
        else:
            pass
    return line

def to_stroke3(data, dtype=np.int16, vmax=500):
    '''converts xy-coordinate list into stroke-3 format numpy array'''

    buf = []

    init_d = np.array(data[0][0])
    l_d = np.array((0, 0))

    for d in data:
        # move to (0, 0)
        d_arr = np.array(d) - init_d

        # convert to relative points
        tmp = d_arr[1:, :] - d_arr[:-1, :]
        tmp = np.concatenate((
            np.expand_dims(d_arr[0, :] - l_d, 0),
            tmp
        ))

        # probability of the pen status
        p = np.zeros((tmp.shape[0], 1))
        p[-1, 0] = 1.0

        tmp = tmp
        buf.append(np.concatenate((tmp, p), axis=1))

        # for the next line
        l_d = d_arr[-1, :]

    buf = np.vstack(buf)

    # normalization to avoid lose of data in casting
    buf[:, :2] = buf[:, :2] / np.absolute(buf[:, :2]).max() * abs(vmax)
    buf = buf.astype(dtype)
    return buf

def get_colors(n, cmap='cool'):
    cmap = matplotlib.cm.get_cmap(cmap)
    idx = np.linspace(0.0, 1.0, n)
    return cmap(idx)

def view_stroke3(data, axis):
    '''view stroke-3 data as an image'''

    line = []
    x, y = 0, 0

    colors = get_colors(len(data))

    for point, color in zip(data, colors):
        _x, _y, v = point
        x += _x
        y += _y
        line.append([x, y])
        if v != 0:
            line = np.array(line)
            axis.plot(line[:,0],line[:,1]*-1.0, color=color)
            line = []
    adjust_axes(axis)

def svg_to_npz_ex(f_path, t, v, tst, max_seq):
    '''converts a number of svg files into npz format. npz file will contain 3 sections, train, validation and test. test, validation, and test parameters should be input as integers, and total shouldn't be greater than existing files.'''
    category = re.search('valid/(.+?)/', f_path).group(1)
    train_data = []
    validation_data = []
    test_data = []
    data1 = []
    c=0
    # print(category)
    for file in os.listdir(f_path):

        if file.endswith(".svg"):
            # print(file)
            data0 = svg2xyList(f_path + file)
            try:
                check = to_stroke3(data0)  # 2d matrix of stroke-3 format pic.
            except:
                print('This freakin file-->'+file)

            seq_len = len(check)
            if seq_len>max_seq:
                pass
            else:
                data1 = exp_w_order(data0)
                # ipdb.set_trace()
                for x in range(len(data1)):
                    data2 = to_stroke3(data1[x])
                    if c<t:
                        train_data.append(data2)
                        c += 1
                    elif c >=t and c<(t+v):
                        validation_data.append(data2)
                        c += 1
                    elif c>=(t+v) and c<(t+v+tst):
                        test_data.append(data2)
                        c += 1
                    else:
                        break
    # ipdb.set_trace()
    filename = os.path.join(dest_p, 'sketchy_'+category+'_exep2.npz')
    np.savez_compressed(filename, train=train_data, valid=validation_data, test=test_data)
    print('conversion succeeded.')

def svg_to_npz(f_path, t, v, tst, max_seq):
    '''converts a number of svg files into npz format. npz file will contain 3 sections, train, validation and test. test, validation, and test parameters should be input as integers, and total shouldn't be greater than existing files.'''
    category = re.search('valid/(.+?)/', f_path).group(1)
    train_data = []
    validation_data = []
    test_data = []
    c=0
    # print(category)
    for file in os.listdir(f_path):

        if file.endswith(".svg"):
            # print(file)
            data0 = svg2xyList(f_path + file)
            try:
                data1 = to_stroke3(data0)  # 2d matrix of stroke-3 format pic.
            except:
                print('This freakin file-->'+file)
            data1 = arr_reduce(data1,100)
            seq_len = len(data1)
            if seq_len>max_seq:
                pass
            else:
                if c<t:
                    train_data.append(data1)
                    c += 1
                elif c >=t and c<(t+v):
                    validation_data.append(data1)
                    c += 1
                elif c>=(t+v) and c<(t+v+tst):
                    test_data.append(data1)
                    c += 1
                else:
                    break
    # ipdb.set_trace()
    filename = os.path.join(dest_p, 'sketchy_'+category+'_ep3.npz')
    np.savez_compressed(filename, train=train_data, valid=validation_data, test=test_data)
    print('conversion succeeded.')

def view_xylist(data, axis):
    line = []
    # data = np.concatenate(np.array(data)).astype(np.float16)
    for point in data:
        # ipdb.set_trace()
        line = np.array(point)
        axis.plot(line[:,0],line[:,1]*-1.0)
        line = []
    axis.set_aspect('equal')

def svg_mix(data):
    '''recursively changes the starting stroke of the sketch sequence'''
    fin_lis = []
    for i in range(len(data)):
        tmp_lis = data
        tmp = [tmp_lis[x] for x in range(1,len(tmp_lis))]
        tmp.append(tmp_lis[0])
        data=tmp
        fin_lis.append(tmp)
        tmp=[]
    return fin_lis

def svg_reverse(data):
    '''reverses direction of stroke'''
    fin_lis = []
    for i in reversed(range(len(data))):
        fin_lis.append(data[i])
    return fin_lis

def exp_w_order(data):
    '''expands xy-coordinate data. Data produced from svg2xyList.'''
    data1 = svg_mix(data)
    reverse_lis = []
    for i in range(len(data)):
        inter =svg_reverse(data[i])
        reverse_lis.append(inter)
    data2 = svg_mix(reverse_lis)
    data_ex = data1+data2
    return data_ex

def arr_reduce(data, lim):
    '''used to reduce length of sequence. 'lim' refers to preferred sequence length.'''
    if len(data)>lim:
        a = [x for x in range(lim,len(data))]
        b = np.delete(data,a,0)
    else:
        b = data
    return b

# data0 = rsvg_in_folderxy(folder_p,500,250)

# data1 = exp_w_order(data0)

# svg_to_npz_ex(folder_p, 15000, 500, 500, 250)
# svg_to_npz(folder_p, 400, 50, 50, 100) #dataset less than 500

# svg2xyList(folder_p+f_name)
# svg_list = rsvg_in_folder(folder_p, 36)
# # ipdb.set_trace()
# c = 0
# row, col = 5, 4
# gs = grdspc.GridSpec(row, col)

# for i in range(row):
#     for j in range(col):
#         ax = plt.subplot(gs[i,j])
#         view_xylist(data1[c], ax)
#         ax.set_title(str(c+1))
#         c+=1

# for i in range(row):
#     for j in range(col):
#         ax = plt.subplot(gs[i,j])
#         view_stroke3(svg_list[c], ax)
#         ax.set_title(str(c+1))
#         c+=1

# plt.show()
# print(P)


"""
https://leetcode.com/problems/complement-of-base-10-integer/
"""

class Solution:
    def bitwiseComplement(self, N: int) -> int:
        """
        Handle the number bit by bit.
        """
        if not N:
            return 1

        curr, i, rslt = N, 0, 0
        while curr:
            rslt += ((curr & 1) ^ 1) << i
            i += 1
            curr >>= 1

        return rslt

    def bitwiseComplement2(self, N: int) -> int:
        """
        Suppose x's complement is y, then x ^ y = z, which contains all ones.
        So if we found the z, then x ^ z = x ^ x ^ y = y.
        """
        allOnes = 1
        while allOnes < N:
            allOnes = (allOnes << 1) + 1

        return allOnes ^ N

print(Solution().bitwiseComplement2(5))


from __future__ import absolute_import
import pandas as pd
import pytest
from nodebook.nodebookcore import ReferenceFinder, Nodebook, Node
from nodebook.pickledict import PickleDict
import ast

class TestReferenceFinder(object):
    @pytest.fixture()
    def rf(self):
        return ReferenceFinder()

    def test_assign(self, rf):
        code_tree = ast.parse("x = 3")
        rf.visit(code_tree)
        assert rf.inputs == set()
        assert rf.imports == set()
        assert rf.locals == {'x'}

    def test_augassign(self, rf):
        code_tree = ast.parse("x += 3")
        rf.visit(code_tree)
        assert rf.inputs == {'x'}
        assert rf.imports == set()
        assert rf.locals == {'x'}

    def test_import(self, rf):
        code_tree = ast.parse("import numpy as np")
        rf.visit(code_tree)
        assert rf.inputs == set()
        assert rf.imports == {'numpy'}
        assert rf.locals == {'np'}

    def test_multiline(self, rf):
        code_tree = ast.parse(
            "import pandas as pd\n"
            "y = pd.Series(x)\n"
        )
        rf.visit(code_tree)
        assert rf.inputs == {'x'}
        assert rf.locals == {'pd', 'y'}
        assert rf.imports == {'pandas'}

    def test_function(self, rf):
        code_tree = ast.parse(
            "def add(x,y):\n"
            "    return x+y\n"
        )
        rf.visit(code_tree)
        assert rf.inputs == set()
        assert rf.locals == {'add', 'x', 'y'}
        assert rf.imports == set()

class TestNodebook(object):
    @pytest.fixture()
    def nb(self):
        var_store = PickleDict()
        return Nodebook(var_store)

    def test_single_node(self, nb):
        node_id = '111'
        nb.insert_node_after(node_id, None)
        nb.update_code(node_id, "x = 42\nx")
        res, objs = nb.run_node(node_id)
        assert res == 42
        assert objs == {'x': 42}

    def test_node_chain(self, nb):
        # first node sets x to 42
        node_id1 = '111'
        nb.insert_node_after(node_id1, None)
        nb.update_code(node_id1, "x = 42")
        res, objs = nb.run_node(node_id1)
        assert res == None
        assert objs == {'x': 42}

        # second node increments x to 52
        node_id2 = '222'
        nb.insert_node_after(node_id2, node_id1)
        nb.update_code(node_id2, "x += 10")
        res, objs = nb.run_node(node_id2)
        assert res == None
        assert objs == {'x': 52}

        # running second node again should give same results
        res, objs = nb.run_node(node_id2)
        assert res == None
        assert objs == {'x': 52}

        # third node evaluates to x
        node_id3 = '333'
        nb.insert_node_after(node_id3, node_id2)
        nb.update_code(node_id3, "x")
        res, objs = nb.run_node(node_id3)
        assert res == 52
        assert objs == {}

        # fourth node inserted between first and second node, changing x to 1
        node_id4 = '444'
        nb.insert_node_after(node_id4, node_id1)
        nb.update_code(node_id4, "x=1")
        res, objs = nb.run_node(node_id4)
        assert res == None
        assert objs == {'x': 1}

        # re-running third node should now evaluate to 11
        res, objs = nb.run_node(node_id3)
        assert res == 11
        assert objs == {}


# coding: utf-8
import urllib.request
import json
import ssl
import constant

context = ssl.SSLContext(ssl.PROTOCOL_TLSv1)

class YoutubeAPI():
    def get_video_info(self, channel_id, page_token=None, published_after=None):
        url = 'https://www.googleapis.com/youtube/v3/search'
        params = {
            'key': constant.YOUTUBE_API_KEY,
            'part': 'id',
            'channelId': channel_id,
            'maxResults': 50,
            'order': 'date'
        }
        if page_token is not None:
            params['pageToken'] = page_token
        if published_after is not None:
            params['publishedAfter'] = published_after
        req = urllib.request.Request('{}?{}'.format(url, urllib.parse.urlencode(params)))
        with urllib.request.urlopen(req, context=context) as res:
            body = json.load(res)
            return body

    def get_video_details(self, video_ids):
        url = 'https://www.googleapis.com/youtube/v3/videos'
        params = {
            'key': constant.YOUTUBE_API_KEY,
            'part': 'snippet, liveStreamingDetails',
            'id': video_ids
        }

        req = urllib.request.Request('{}?{}'.format(url, urllib.parse.urlencode(params)))
        with urllib.request.urlopen(req, context=context) as res:
            body = json.load(res)
            return body


class Solution:
	def intersetcion(self, nums1, nums2):
		# using two pointers method
		result = []
		nums1, nums2 = sorted(nums1), sorted(nums2)
		i, j = 0

		while True:
			try:
				if nums1[i] > nums2[j]:
					j+=1
				elif nums1[i] < nums2[j]:
					i+=1
				else:
					result.append(nums1[i])
					i+=1
					j+=1
			except IndexError:
				break
		return result

''' solution 1
        for i in range(len(nums1)):
            if nums1[i] in nums2:
                nums2.remove(nums1[i])
                result.append(nums1[i])
        return result
'''
''' solution 2
		for i in range(len(nums1)):
			for j in range(len(nums2)):
				print(j)
				if nums1[i] == nums2[j]:
					temp.append(nums1[i])
					break
		return temp'''

def main():
	sol = Solution()

	print('Array 1 :{}'.format(array1))
	print('Array 2 :{}'.format(array2))

	if len(array1)<len(array2):
		result = sol.intersetcion(array1, array2)
	else:
		result = sol.intersetcion(array2, array1)

	print('intersetcion of two array is {}'.format(result))

if __name__ == '__main__':

	array1 = list(map(int, input().split()))
	array2 = list(map(int, input().split()))
	main()


from twisted.internet.defer import inlineCallbacks
from twisted.internet.task import Clock
from twisted.internet.error import ConnectionLost
from twisted.web.xmlrpc import Proxy

from vumi.message import TransportUserMessage
from vumi.tests.helpers import VumiTestCase
from vumi.tests.utils import LogCatcher
from vumi.transports.tests.helpers import TransportHelper
from vumi.transports.trueafrican.transport import TrueAfricanUssdTransport

class TestTrueAfricanUssdTransport(VumiTestCase):

    SESSION_INIT_BODY = {
        'session': '1',
        'msisdn': '+27724385170',
        'shortcode': '*23#'
    }

    @inlineCallbacks
    def setUp(self):
        self.tx_helper = self.add_helper(
            TransportHelper(TrueAfricanUssdTransport))
        self.clock = Clock()
        self.patch(TrueAfricanUssdTransport, 'get_clock', lambda _: self.clock)
        self.transport = yield self.tx_helper.get_transport({
            'interface': '127.0.0.1',
            'port': 0,
            'request_timeout': 10,
        })
        self.service_url = self.get_service_url(self.transport)

    def get_service_url(self, transport):
        """
        Get the URL for the HTTP resource. Requires the worker to be started.
        """
        addr = transport.web_resource.getHost()
        return "http://%s:%s/" % (addr.host, addr.port)

    def web_client(self):
        return Proxy(self.service_url)

    @inlineCallbacks
    def test_session_new(self):
        client = self.web_client()
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(msg, "Oh Hai!")

        # verify the transport -> application message
        self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
        self.assertEqual(msg['transport_type'], "ussd")
        self.assertEqual(msg['session_event'],
                         TransportUserMessage.SESSION_NEW)
        self.assertEqual(msg['from_addr'], '+27724385170')
        self.assertEqual(msg['to_addr'], '*23#')
        self.assertEqual(msg['content'], None)

        resp = yield resp_d
        self.assertEqual(
            resp,
            {
                'message': 'Oh Hai!',
                'session': '1',
                'type': 'cont'
            }
        )

    @inlineCallbacks
    def test_session_resume(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(msg, "pong")

        yield resp_d
        yield self.tx_helper.clear_dispatched_inbound()

        # resume session
        resp_d = client.callRemote(
            'USSD.CONT',
            {'session': '1',
             'response': 'pong'}
        )

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(msg, "ping")

        # verify the dispatched inbound message
        self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
        self.assertEqual(msg['transport_type'], "ussd")
        self.assertEqual(msg['session_event'],
                         TransportUserMessage.SESSION_RESUME)
        self.assertEqual(msg['from_addr'], '+27724385170')
        self.assertEqual(msg['to_addr'], '*23#')
        self.assertEqual(msg['content'], 'pong')

        resp = yield resp_d
        self.assertEqual(
            resp,
            {
                'message': 'ping',
                'session': '1',
                'type': 'cont'
            }
        )

    @inlineCallbacks
    def test_session_end_user_initiated(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(msg, "ping")

        yield resp_d
        yield self.tx_helper.clear_dispatched_inbound()

        # user initiated session termination
        resp_d = client.callRemote(
            'USSD.END',
            {'session': '1'}
        )

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.assertEqual(msg['transport_name'], self.tx_helper.transport_name)
        self.assertEqual(msg['transport_type'], "ussd")
        self.assertEqual(msg['session_event'],
                         TransportUserMessage.SESSION_CLOSE)
        self.assertEqual(msg['from_addr'], '+27724385170')
        self.assertEqual(msg['to_addr'], '*23#')
        self.assertEqual(msg['content'], None)

        resp = yield resp_d
        self.assertEqual(resp, {})

    @inlineCallbacks
    def test_session_end_application_initiated(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)
        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)

        self.tx_helper.make_dispatch_reply(msg, "ping")
        yield resp_d
        yield self.tx_helper.clear_dispatched_inbound()

        # end session
        resp_d = client.callRemote(
            'USSD.CONT',
            {'session': '1',
             'response': 'o rly?'}
        )

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(
            msg, "kthxbye", continue_session=False)

        resp = yield resp_d
        self.assertEqual(
            resp,
            {
                'message': 'kthxbye',
                'session': '1',
                'type': 'end'
            }
        )

    @inlineCallbacks
    def test_ack_for_outbound_message(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        # send response
        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        rep = yield self.tx_helper.make_dispatch_reply(msg, "ping")
        yield resp_d

        [ack] = yield self.tx_helper.wait_for_dispatched_events(1)
        self.assertEqual(ack['event_type'], 'ack')
        self.assertEqual(ack['user_message_id'], rep['message_id'])
        self.assertEqual(ack['sent_message_id'], rep['message_id'])

    @inlineCallbacks
    def test_nack_for_outbound_message(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)

        # cancel the request and mute the resulting error.
        request = self.transport._requests[msg['message_id']]
        request.http_request.connectionLost(ConnectionLost())
        resp_d.cancel()
        resp_d.addErrback(lambda f: None)

        # send response
        rep = yield self.tx_helper.make_dispatch_reply(msg, "ping")
        [nack] = yield self.tx_helper.wait_for_dispatched_events(1)

        self.assertEqual(nack['event_type'], 'nack')
        self.assertEqual(nack['user_message_id'], rep['message_id'])
        self.assertEqual(nack['sent_message_id'], rep['message_id'])
        self.assertTrue('HTTP client closed connection' in nack['nack_reason'])

    @inlineCallbacks
    def test_nack_for_request_timeout(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)

        self.clock.advance(10.1)  # .1 second after timeout

        rep = yield self.tx_helper.make_dispatch_reply(msg, "ping")
        yield resp_d

        [nack] = yield self.tx_helper.wait_for_dispatched_events(1)
        self.assertEqual(nack['event_type'], 'nack')
        self.assertEqual(nack['user_message_id'], rep['message_id'])
        self.assertEqual(nack['sent_message_id'], rep['message_id'])
        self.assertEqual(nack['nack_reason'], 'Exceeded request timeout')

    @inlineCallbacks
    def test_nack_for_invalid_outbound_message(self):
        msg = yield self.tx_helper.make_dispatch_outbound("outbound")
        [nack] = yield self.tx_helper.wait_for_dispatched_events(1)
        self.assertEqual(nack['user_message_id'], msg['message_id'])
        self.assertEqual(nack['sent_message_id'], msg['message_id'])
        self.assertEqual(nack['nack_reason'],
                         'Missing in_reply_to, content or session_id fields')

    @inlineCallbacks
    def test_timeout(self):
        client = self.web_client()

        # initiate session
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        with LogCatcher(message='Timing out') as lc:
            self.assertTrue(msg['message_id'] in self.transport._requests)
            self.clock.advance(10.1)  # .1 second after timeout
            self.assertFalse(msg['message_id'] in self.transport._requests)
            [warning] = lc.messages()
            self.assertEqual(warning,
                             'Timing out on response for +27724385170')
            resp = yield resp_d
            self.assertEqual(
                resp,
                {
                    'message': ('We encountered an error while processing'
                                ' your message'),
                    'type': 'end'
                }
            )

    @inlineCallbacks
    def test_request_tracking(self):
        """
        Verify that the transport cleans up after finishing a request
        """
        client = self.web_client()
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(msg, "pong")
        self.assertTrue(msg['message_id'] in self.transport._requests)
        yield resp_d
        self.assertFalse(msg['message_id'] in self.transport._requests)

    @inlineCallbacks
    def test_missing_session(self):
        """
        Verify that the transport handles missing session data in a
        graceful manner
        """
        client = self.web_client()
        resp_d = client.callRemote('USSD.INIT', self.SESSION_INIT_BODY)

        [msg] = yield self.tx_helper.wait_for_dispatched_inbound(1)
        self.tx_helper.make_dispatch_reply(msg, "pong")
        yield resp_d
        yield self.tx_helper.clear_dispatched_inbound()

        # simulate Redis falling over
        yield self.transport.session_manager.redis._purge_all()

        # resume
        resp_d = client.callRemote(
            'USSD.CONT',
            {'session': '1',
             'response': 'o rly?'}
        )

        resp = yield resp_d
        self.assertEqual(
            resp,
            {
                'message': ('We encountered an error while processing'
                            ' your message'),
                'type': 'end'
            }
        )


import pygame, random, sys
from pygame.locals import *
#Home Brewed Libraries:
import Drawing
from network import Network
pygame.init()

GameHeight = 500
GameWidth = 500

Window = pygame.display.set_mode((GameWidth, GameHeight),0, 16)
pygame.display.set_caption("Isaacs Very Nice Multiplayer Snek Game")

CELLSIZE=20
CellWidth = int(GameWidth / CELLSIZE)
CellHeight = int(GameHeight / CELLSIZE)

TicksPerSecCLOCK = pygame.time.Clock()

#             R    G    B
GRAY      = (100, 100, 100)
NAVYBLUE  = ( 60,  60, 100)
WHITE     = (255, 255, 255)
RED       = (255,   0,   0)
GREEN     = (  0, 255,   0)
BLUE      = (  0,   0, 255)
YELLOW    = (255, 255,   0)
ORANGE    = (255, 128,   0)
PURPLE    = (255,   0, 255)
CYAN      = (  0, 255, 255)
BLACK     = (  0,   0,   0)
DARKGREEN = (  0, 155,   0)
DARKGRAY  = ( 40,  40,  40)
RAINBOW   = [  0,  85, 170]

#Sound Objects:
#Eating apple sound effect:
EatingAppleSound = pygame.mixer.Sound("SNEK_Sounds/EatingApple.wav")
EatingAppleChannel = pygame.mixer.Channel(1)
#System music:
GameMusicSound = pygame.mixer.Sound("SNEK_Sounds/GameMusic.wav")
GameMusicChannel = pygame.mixer.Channel(2)
#Winner apple sould:
WinnerAppleSound = pygame.mixer.Sound("SNEK_Sounds/WinningApple.wav")

#Set game music volume lower:
GameMusicChannel.set_volume(0.2)

#System functions:
def terminate():
    pygame.quit()
    sys.exit()

Network = Network()
Player = Network.Connect()

while True:
    Sneks,game = Network.Get()
    if Player == 1:
        MeSnek = Sneks[1]
        OtherSnek = Sneks[0]
    else:
        MeSnek = Sneks[0]
        OtherSnek = Sneks[1]

    GameWidth,GameHeight = game.GetBoard()
    Window = pygame.display.set_mode((GameWidth, GameHeight),0,32)

    TicksPerSec = game.GetTPS()

    for event in pygame.event.get():
        if event.type == QUIT:
            break
        elif event.type == KEYDOWN:
            if not MeSnek.Ready:
                if (event.key == K_RETURN):
                    MeSnek.Ready = True
            if MeSnek.Ready and OtherSnek.Ready:
                if (event.key == K_a) and (MeSnek.Direction != "Right"):
                    MeSnek.Direction = "Left"
                elif (event.key == K_d) and (MeSnek.Direction != "Left"):
                    MeSnek.Direction = "Right"
                elif (event.key == K_w)  and (MeSnek.Direction != "Down"):
                    MeSnek.Direction = "Up"
                elif (event.key == K_s) and (MeSnek.Direction != "Up"):
                    MeSnek.Direction = "Down"

    if MeSnek.Ready and OtherSnek.Ready:
        if game.AppleLocation["Type"] == "Normal":
            AppleColor = RED
        elif game.AppleLocation["Type"] == "Speed":
            AppleColor = YELLOW
        elif game.AppleLocation["Type"] == "Board":
            AppleColor = BLUE

        Drawing.DrawGrid(Window,GameWidth,GameHeight)
        Drawing.DrawSNEK(Window,MeSnek.Cords,MeSnek.Color,MeSnek.InnerColor)
        Drawing.DrawSNEK(Window,OtherSnek.Cords,OtherSnek.Color,OtherSnek.InnerColor)
        Drawing.DrawScores(Window,"LEFT",MeSnek.Cords,GameWidth)
        Drawing.DrawScores(Window,"RIGHT",OtherSnek.Cords,GameWidth)
        Drawing.DrawApple(Window,game.AppleLocation,AppleColor)

        print(MeSnek.Direction,OtherSnek.Direction)

    elif not (MeSnek.Ready and OtherSnek.Ready):
        Drawing.DrawGrid(Window,GameWidth,GameHeight)
        Drawing.DrawStartScreen(Window,GameWidth,GameHeight,Player)

        Drawing.DrawSNEK(Window,MeSnek.Cords,MeSnek.Color,MeSnek.InnerColor)
        Drawing.DrawSNEK(Window,OtherSnek.Cords,OtherSnek.Color,OtherSnek.InnerColor)

    Network.Send(MeSnek.Direction,MeSnek.Ready,Player)
    pygame.display.update()
    TicksPerSecCLOCK.tick(TicksPerSec)


import gym
from gym import envs
import random
import time

#--------------------------------------------LunarLander
#Action space:
    #0: to nothing
    #1: fire right engine
    #2: fire main engine
    #3: fire left engine

 # state = [
 #            (pos.x - VIEWPORT_W/SCALE/2) / (VIEWPORT_W/SCALE/2),
 #            (pos.y - (self.helipad_y+LEG_DOWN/SCALE)) / (VIEWPORT_H/SCALE/2),
 #            vel.x*(VIEWPORT_W/SCALE/2)/FPS,
 #            vel.y*(VIEWPORT_H/SCALE/2)/FPS,
 #            self.lander.angle,
 #            20.0*self.lander.angularVelocity/FPS,
 #            1.0 if self.legs[0].ground_contact else 0.0,
 #            1.0 if self.legs[1].ground_contact else 0.0
 #            ]

#-------------------------------------------------------

#Our Agent
class Agent:
    def __init__(self, env):
        self.action_size = env.action_space.n

    def get_action_random(self):
        action = random.choice(range(self.action_size))
        return action

    def get_action_smart_decision(self, observation):
        angle = observation[4]*57
        #print("----y: ", observation[1])
        rotate_right = [2,3]
        rotate_left = [1,2]

        #fix height to be able to move
        if observation[1]<0.6 and (observation[0]<-0.15 or observation[0]>0.1):
            return 2

        if angle>2: #rotate right to fix
            action = random.choice(rotate_right)
            return action

        if angle<-2: #rotate left to fix
            action = random.choice(rotate_left)
            return action

        #fix x axis)
        if observation[0]<-0.1:
            return 3

        if observation[0]>0.1:
            return 1
        else:
            return 0

#Env creation
env = gym.make('LunarLander-v2')
print("Observation space: ", env.observation_space)
print("Action space: ", env.action_space)

#initiazlize Agent
agent = Agent(env)

nb_episodes = 10
total_reward = 0

for i in range(nb_episodes):
    observation = env.reset()

    #looping
    episode_reward = 0
    for i_episode in range(300):

        action = agent.get_action_smart_decision(observation)

        observation, reward, done, info = env.step(action)
        episode_reward += reward
        env.render()

        #sleep to be able to see
        time.sleep(0.025)

        if done:
            total_reward += reward
            break
    print("Reward for episode", i+1, "is", episode_reward)

env.close()

print("Nb of episodes ", nb_episodes)
print("Average reward : ", total_reward/nb_episodes)


__author__ = 'rorymiller'

import os
import re

# duplicate folder
# Traverse folders
# add YAML infromation to each .md file
# add index to each folder
# add index to first page
# make web friendly file names

# YAML for individual pages

## need to remove all non digit from yaml
## can perhaps try to add back in links.

def YAML(name):
    line1 = "-" * 3 + "\n"
    line2 = "title: " + name + "\n"
    line3 = 'layout: page \n'
    line4 = line1 + "\n"
    return line1 + line2 + line3 + line4

def YAMLi(name1):
    line1 = "-" * 3 + "\n"
    line2 = "title: " + name1 +'\n'
    line3 = 'layout: index' + '\n'
    line4 = 'content: \n'
    return line1+line2+line3+line4

# add YAML

def individual(rootdir):
    title = ""
    infile = ""
    outfile = ""

    for subdir, dirs, files in os.walk(rootdir):
        # open file

        for f in files:
            if ".md" in f and "_" not in f:
                infile = open(subdir + "/" + f, 'r')
                fl = f.lower()
                outfile = open(subdir + "/" + "a" + fl.replace(" ","_").replace('(',"_").replace(')',""), 'w') # can change subdir if needed

                # find title
                for line in infile:
                    if re.match("^[#]{1} [a-zA-Z]",line):
                        title = line
                        break
                title = title.replace('# ','').replace(":","")

                    #write YAML information to top of file
                outfile.write(YAML(title))

                    # copy rest of document into new file
                for line in infile:
                    outfile.write(line + '\n')

                print('done' , fl)
                    # close files
                infile.close()
                outfile.close()
    print("finished YAML individual files")

def index(rootdir): # must match outfile directory
    ind = "index.md"

    for subdir, dirs, files in os.walk(rootdir):

        indexfile = open(subdir + '/' + ind,"w")
        title = subdir.split('/')[-1]

        indexfile.write(YAMLi(title))

        if dirs:
            for f in dirs:
               print(f)
               indexfile.write("    - " + f + " " + "\n" + "")

        if files:
            for file in files:
                if ".md" in file and "index" not in file and file.islower() and " " not in file and file[0] == "a":
                    indexfile.write("    - " + file.replace(".md","") + "\n")

        indexfile.write("-" * 3 + "\n") # close YAML
    print("done with index files")

individual("/Users/rorymiller/Dropbox/GP_exp/notes")
index("/Users/rorymiller/Dropbox/GP_exp/notes")


# -*- coding: utf-8 -*-
"""
Created on Sat Aug 10 18:58:25 2019

@author: BHASKAR NEOGI
"""

p = input("Enter The Elements of Tuple :  ").split(',')
k = []
for i in p :

    i = int(i)
    k.append(i)

k = tuple(k)
x = (len(k)//2)
print (k[:x])
print (k[x:])


import os, fnmatch

images_folder = "./images/"
current_folder = "."

# перечень всех каталогов и файлов в текущем "." каталоге. Используем os.walk()
listOfFiles = os.listdir(images_folder)
pattern = "*.jpg"
for entry in listOfFiles:
    if fnmatch.fnmatch(entry, pattern):
        print(entry)


def stage(ctx):
    envs = [{
        # "system": "krakenci/ubuntu:20.04",
        # "agents_group": "docker",
        # "executor": "docker",
        "system": "ami-0967f290f3533e5a8", # my made by packer
        "agents_group": "aws-t2-micro",
        "config": "default"
    }]

    return {
        "parent": "Tarball",
        "triggers": {
            "parent": True
        },
        "parameters": [],
        "configs": [],
        "jobs": [{
            "name": "pylint agent",
            "steps": [{
                "tool": "shell",
                "cmd": "sudo apt update && sudo apt-get install -y --no-install-recommends python3-setuptools python3-wheel python3-pip gcc python3-dev || ps axf",
                "timeout": 300
            }, {
                "tool": "shell",
                "cmd": "sudo pip3 install pylint"
            }, {
                "tool": "artifacts",
                "action": "download",
                "source": "kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "tar -zxf kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "sudo pip3 install -r requirements.txt",
                "cwd": "kraken/agent"
            }, {
                "tool": "shell",
                "cmd": "cp server/kraken/server/logs.py server/kraken/server/consts.py agent/kraken/agent",
                "cwd": "kraken"
            }, {
                "tool": "pylint",
                "rcfile": "pylint.rc",
                "modules_or_packages": "agent",
                "cwd": "kraken"
            }],
            "environments": envs
        }, {
            "name": "pylint server + client",
            "steps": [{
                "tool": "shell",
                "cmd": "sudo apt update && sudo apt-get install -y --no-install-recommends python3-setuptools python3-wheel python3-pip gcc python3-dev libpq-dev python3-venv || ps axf",
                "timeout": 300
            }, {
                "tool": "artifacts",
                "action": "download",
                "source": "kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "tar -zxf kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "rake prepare_env",
                "cwd": "kraken",
                "timeout": 300
            }, {
                "tool": "pylint",
                "pylint_exe": "../venv/bin/poetry run pylint",
                "rcfile": "../pylint.rc",
                "modules_or_packages": "kraken",
                "cwd": "kraken/server",
                "timeout": 300
            }, {
                "tool": "shell",
                "cmd": "rake build_client",
                "cwd": "kraken/client",
                "timeout": 300
            }, {
                "tool": "pylint",
                "pylint_exe": "../venv/bin/poetry run pylint",
                "rcfile": "../pylint.rc",
                "modules_or_packages": "kraken",
                "cwd": "kraken/client",
                "timeout": 300
            }],
            "environments": [{
                "system": "ami-0967f290f3533e5a8", # my made by packer
                "agents_group": "aws-t3-micro",
                "config": "default"
            }]
        }, {
            "name": "ng lint",
            "steps": [{
                "tool": "shell",
                "cmd": "curl -fsSL https://deb.nodesource.com/setup_16.x | sudo -E bash - && sudo DEBIAN_FRONTEND=noninteractive apt-get install -yq --no-install-recommends git nodejs",
                "timeout": 300
            }, {
                "tool": "artifacts",
                "action": "download",
                "source": "kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "tar -zxf kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "npm install",
                "cwd": "kraken/ui",
                "timeout": 400
            }, {
                "tool": "nglint",
                "cwd": "kraken/ui"
            }],
            "environments": envs
        }, {
            "name": "cloc",
            "steps": [{
                "tool": "shell",
                "cmd": "sudo apt update && sudo apt-get install -y --no-install-recommends cloc || ps axf",
                "timeout": 300
            }, {
                "tool": "artifacts",
                "action": "download",
                "source": "kraken.tar.gz"
            }, {
                "tool": "shell",
                "cmd": "tar -zxf kraken.tar.gz"
            }, {
                "tool": "cloc",
                "not-match-f": "(package-lock.json|pylint.rc)",
                "exclude-dir": "docker-elk",
                "cwd": "kraken"
            }],
            "environments": envs
        }],
        "notification": {
            "slack": {"channel": "kk-results"},
            "email": "godfryd@gmail.com",
            "github": {"credentials": "#{KK_SECRET_SIMPLE_gh_status_creds}"}
        }
    }


import pyspark.sql.functions as f
from shared import extract_data, load_data
from pyspark.sql.window import Window

def analyse_data(raw_primary_person_use_df, raw_units_use_df):
    """
    This Function contain the main logic for the analysis
    :param raw_primary_person_use_df: Source Primary_person_use Dataframe
    :param raw_units_use_df: Source Units_Use Dataframe
    :return: Final Processed Dataframe
    """

    w = Window().partitionBy(f.lit('dummy')).orderBy(f.col('count').desc())

    return raw_primary_person_use_df.join(raw_units_use_df, on=["CRASH_ID", "UNIT_NBR"], how="inner") \
        .where(~f.col("PRSN_INJRY_SEV_ID").isin("NOT INJURED", "NA", "UNKNOWN")) \
        .groupBy("VEH_MAKE_ID").count() \
        .select(f.col('VEH_MAKE_ID'), f.col('count')) \
        .withColumn('row_num', f.row_number().over(w)) \
        .filter(f.col('row_num').between(5, 15)) \
        .select('VEH_MAKE_ID')

def run_job(spark, config):
    """
    This Function will execute the Analytics job 4
    :param spark: SparkSession Object
    :param config: Config file which contain all the parameters
    :return: None
    """
    load_data(
        analyse_data(
            extract_data(spark, f"{config.get('source_data_path')}/Primary_Person_use.csv"),
            extract_data(spark, f"{config.get('source_data_path')}/Units_use.csv")
        ), f"{config.get('target_data_path')}/job04")


import collections as co
import csv
import itertools as it
import math as m
import os
import re

# integer fields
class Int(co.namedtuple('Int', 'x')):
    __slots__ = ()
    def __new__(cls, x=0):
        if isinstance(x, Int):
            return x
        if isinstance(x, str):
            try:
                x = int(x, 0)
            except ValueError:
                # also accept +-∞ and +-inf
                if re.match('^\s*\+?\s*(?:∞|inf)\s*$', x):
                    x = m.inf
                elif re.match('^\s*-\s*(?:∞|inf)\s*$', x):
                    x = -m.inf
                else:
                    raise
        assert isinstance(x, int) or m.isinf(x), x
        return super().__new__(cls, x)

    def __str__(self):
        if self.x == m.inf:
            return '∞'
        elif self.x == -m.inf:
            return '-∞'
        else:
            return str(self.x)

    def __int__(self):
        assert not m.isinf(self.x)
        return self.x

    def __float__(self):
        return float(self.x)

    none = '%7s' % '-'
    def table(self):
        return '%7s' % (self,)

    diff_none = '%7s' % '-'
    diff_table = table

    def diff_diff(self, other):
        new = self.x if self else 0
        old = other.x if other else 0
        diff = new - old
        if diff == +m.inf:
            return '%7s' % '+∞'
        elif diff == -m.inf:
            return '%7s' % '-∞'
        else:
            return '%+7d' % diff

    def ratio(self, other):
        new = self.x if self else 0
        old = other.x if other else 0
        if m.isinf(new) and m.isinf(old):
            return 0.0
        elif m.isinf(new):
            return +m.inf
        elif m.isinf(old):
            return -m.inf
        elif not old and not new:
            return 0.0
        elif not old:
            return 1.0
        else:
            return (new-old) / old

    def __add__(self, other):
        return self.__class__(self.x + other.x)

    def __sub__(self, other):
        return self.__class__(self.x - other.x)

    def __mul__(self, other):
        return self.__class__(self.x * other.x)

# size results
class StackResult(co.namedtuple('StackResult', [
        'file', 'function', 'frame', 'limit', 'children'])):
    _by = ['file', 'function']
    _fields = ['frame', 'limit']
    _sort = ['limit', 'frame']
    _types = {'frame': Int, 'limit': Int}

    __slots__ = ()
    def __new__(cls, file='', function='',
            frame=0, limit=0, children=set()):
        return super().__new__(cls, file, function,
            Int(frame), Int(limit),
            children)

    def __add__(self, other):
        return StackResult(self.file, self.function,
            self.frame + other.frame,
            max(self.limit, other.limit),
            self.children | other.children)

def openio(path, mode='r', buffering=-1):
    # allow '-' for stdin/stdout
    if path == '-':
        if mode == 'r':
            return os.fdopen(os.dup(sys.stdin.fileno()), mode, buffering)
        else:
            return os.fdopen(os.dup(sys.stdout.fileno()), mode, buffering)
    else:
        return open(path, mode, buffering)

def collect(ci_paths, *,
        sources=None,
        everything=False,
        **args):
    # parse the vcg format
    k_pattern = re.compile('([a-z]+)\s*:', re.DOTALL)
    v_pattern = re.compile('(?:"(.*?)"|([a-z]+))', re.DOTALL)
    def parse_vcg(rest):
        def parse_vcg(rest):
            node = []
            while True:
                rest = rest.lstrip()
                m_ = k_pattern.match(rest)
                if not m_:
                    return (node, rest)
                k, rest = m_.group(1), rest[m_.end(0):]

                rest = rest.lstrip()
                if rest.startswith('{'):
                    v, rest = parse_vcg(rest[1:])
                    assert rest[0] == '}', "unexpected %r" % rest[0:1]
                    rest = rest[1:]
                    node.append((k, v))
                else:
                    m_ = v_pattern.match(rest)
                    assert m_, "unexpected %r" % rest[0:1]
                    v, rest = m_.group(1) or m_.group(2), rest[m_.end(0):]
                    node.append((k, v))

        node, rest = parse_vcg(rest)
        assert rest == '', "unexpected %r" % rest[0:1]
        return node

    # collect into functions
    callgraph = co.defaultdict(lambda: (None, None, 0, set()))
    f_pattern = re.compile(
        r'([^\\]*)\\n([^:]*)[^\\]*\\n([0-9]+) bytes \((.*)\)')
    for path in ci_paths:
        with open(path) as f:
            vcg = parse_vcg(f.read())
        for k, graph in vcg:
            if k != 'graph':
                continue
            for k, info in graph:
                if k == 'node':
                    info = dict(info)
                    m_ = f_pattern.match(info['label'])
                    if m_:
                        function, file, size, type = m_.groups()
                        if (not args.get('quiet')
                                and 'static' not in type
                                and 'bounded' not in type):
                            print("warning: "
                                "found non-static stack for %s (%s, %s)" % (
                                function, type, size))
                        _, _, _, targets = callgraph[info['title']]
                        callgraph[info['title']] = (
                            file, function, int(size), targets)
                elif k == 'edge':
                    info = dict(info)
                    _, _, _, targets = callgraph[info['sourcename']]
                    targets.add(info['targetname'])
                else:
                    continue

    callgraph_ = co.defaultdict(lambda: (None, None, 0, set()))
    for source, (s_file, s_function, frame, targets) in callgraph.items():
        # discard internal functions
        if not everything and s_function.startswith('__'):
            continue
        # ignore filtered sources
        if sources is not None:
            if not any(
                    os.path.abspath(s_file) == os.path.abspath(s)
                    for s in sources):
                continue
        else:
            # default to only cwd
            if not everything and not os.path.commonpath([
                    os.getcwd(),
                    os.path.abspath(s_file)]) == os.getcwd():
                continue

        # smiplify path
        if os.path.commonpath([
                os.getcwd(),
                os.path.abspath(s_file)]) == os.getcwd():
            s_file = os.path.relpath(s_file)
        else:
            s_file = os.path.abspath(s_file)

        callgraph_[source] = (s_file, s_function, frame, targets)
    callgraph = callgraph_

    if not everything:
        callgraph_ = co.defaultdict(lambda: (None, None, 0, set()))
        for source, (s_file, s_function, frame, targets) in callgraph.items():
            # discard filtered sources
            if sources is not None and not any(
                    os.path.abspath(s_file) == os.path.abspath(s)
                    for s in sources):
                continue
            # discard internal functions
            if s_function.startswith('__'):
                continue
            callgraph_[source] = (s_file, s_function, frame, targets)
        callgraph = callgraph_

    # find maximum stack size recursively, this requires also detecting cycles
    # (in case of recursion)
    def find_limit(source, seen=None):
        seen = seen or set()
        if source not in callgraph:
            return 0
        _, _, frame, targets = callgraph[source]

        limit = 0
        for target in targets:
            if target in seen:
                # found a cycle
                return m.inf
            limit_ = find_limit(target, seen | {target})
            limit = max(limit, limit_)

        return frame + limit

    def find_children(targets):
        children = set()
        for target in targets:
            if target in callgraph:
                t_file, t_function, _, _ = callgraph[target]
                children.add((t_file, t_function))
        return children

    # build results
    results = []
    for source, (s_file, s_function, frame, targets) in callgraph.items():
        limit = find_limit(source)
        children = find_children(targets)
        results.append(StackResult(s_file, s_function, frame, limit, children))

    return results

def fold(Result, results, *,
        by=None,
        defines=None,
        **_):
    if by is None:
        by = Result._by

    for k in it.chain(by or [], (k for k, _ in defines or [])):
        if k not in Result._by and k not in Result._fields:
            print("error: could not find field %r?" % k)
            sys.exit(-1)

    # filter by matching defines
    if defines is not None:
        results_ = []
        for r in results:
            if all(getattr(r, k) in vs for k, vs in defines):
                results_.append(r)
        results = results_

    # organize results into conflicts
    folding = co.OrderedDict()
    for r in results:
        name = tuple(getattr(r, k) for k in by)
        if name not in folding:
            folding[name] = []
        folding[name].append(r)

    # merge conflicts
    folded = []
    for name, rs in folding.items():
        folded.append(sum(rs[1:], start=rs[0]))

    return folded

def table(Result, results, diff_results=None, *,
        by=None,
        fields=None,
        sort=None,
        summary=False,
        all=False,
        percent=False,
        tree=False,
        depth=1,
        **_):
    all_, all = all, __builtins__.all

    if by is None:
        by = Result._by
    if fields is None:
        fields = Result._fields
    types = Result._types

    # fold again
    results = fold(Result, results, by=by)
    if diff_results is not None:
        diff_results = fold(Result, diff_results, by=by)

    # organize by name
    table = {
        ','.join(str(getattr(r, k) or '') for k in by): r
        for r in results}
    diff_table = {
        ','.join(str(getattr(r, k) or '') for k in by): r
        for r in diff_results or []}
    names = list(table.keys() | diff_table.keys())

    # sort again, now with diff info, note that python's sort is stable
    names.sort()
    if diff_results is not None:
        names.sort(key=lambda n: tuple(
            types[k].ratio(
                getattr(table.get(n), k, None),
                getattr(diff_table.get(n), k, None))
            for k in fields),
            reverse=True)
    if sort:
        for k, reverse in reversed(sort):
            names.sort(
                key=lambda n: tuple(
                    (getattr(table[n], k),)
                    if getattr(table.get(n), k, None) is not None else ()
                    for k in ([k] if k else [
                        k for k in Result._sort if k in fields])),
                reverse=reverse ^ (not k or k in Result._fields))

    # build up our lines
    lines = []

    # header
    header = []
    header.append('%s%s' % (
        ','.join(by),
        ' (%d added, %d removed)' % (
            sum(1 for n in table if n not in diff_table),
            sum(1 for n in diff_table if n not in table))
            if diff_results is not None and not percent else '')
        if not summary else '')
    if diff_results is None:
        for k in fields:
            header.append(k)
    elif percent:
        for k in fields:
            header.append(k)
    else:
        for k in fields:
            header.append('o'+k)
        for k in fields:
            header.append('n'+k)
        for k in fields:
            header.append('d'+k)
    header.append('')
    lines.append(header)

    def table_entry(name, r, diff_r=None, ratios=[]):
        entry = []
        entry.append(name)
        if diff_results is None:
            for k in fields:
                entry.append(getattr(r, k).table()
                    if getattr(r, k, None) is not None
                    else types[k].none)
        elif percent:
            for k in fields:
                entry.append(getattr(r, k).diff_table()
                    if getattr(r, k, None) is not None
                    else types[k].diff_none)
        else:
            for k in fields:
                entry.append(getattr(diff_r, k).diff_table()
                    if getattr(diff_r, k, None) is not None
                    else types[k].diff_none)
            for k in fields:
                entry.append(getattr(r, k).diff_table()
                    if getattr(r, k, None) is not None
                    else types[k].diff_none)
            for k in fields:
                entry.append(types[k].diff_diff(
                        getattr(r, k, None),
                        getattr(diff_r, k, None)))
        if diff_results is None:
            entry.append('')
        elif percent:
            entry.append(' (%s)' % ', '.join(
                '+∞%' if t == +m.inf
                else '-∞%' if t == -m.inf
                else '%+.1f%%' % (100*t)
                for t in ratios))
        else:
            entry.append(' (%s)' % ', '.join(
                    '+∞%' if t == +m.inf
                    else '-∞%' if t == -m.inf
                    else '%+.1f%%' % (100*t)
                    for t in ratios
                    if t)
                if any(ratios) else '')
        return entry

    # entries
    if not summary:
        for name in names:
            r = table.get(name)
            if diff_results is None:
                diff_r = None
                ratios = None
            else:
                diff_r = diff_table.get(name)
                ratios = [
                    types[k].ratio(
                        getattr(r, k, None),
                        getattr(diff_r, k, None))
                    for k in fields]
                if not all_ and not any(ratios):
                    continue
            lines.append(table_entry(name, r, diff_r, ratios))

    # total
    r = next(iter(fold(Result, results, by=[])), None)
    if diff_results is None:
        diff_r = None
        ratios = None
    else:
        diff_r = next(iter(fold(Result, diff_results, by=[])), None)
        ratios = [
            types[k].ratio(
                getattr(r, k, None),
                getattr(diff_r, k, None))
            for k in fields]
    lines.append(table_entry('TOTAL', r, diff_r, ratios))

    # find the best widths, note that column 0 contains the names and column -1
    # the ratios, so those are handled a bit differently
    widths = [
        ((max(it.chain([w], (len(l[i]) for l in lines)))+1+4-1)//4)*4-1
        for w, i in zip(
            it.chain([23], it.repeat(7)),
            range(len(lines[0])-1))]

    # adjust the name width based on the expected call depth, though
    # note this doesn't really work with unbounded recursion
    if not summary and not m.isinf(depth):
        widths[0] += 4*(depth-1)

    # print the tree recursively
    if not tree:
        print('%-*s  %s%s' % (
            widths[0], lines[0][0],
            ' '.join('%*s' % (w, x)
                for w, x in zip(widths[1:], lines[0][1:-1])),
            lines[0][-1]))

    if not summary:
        line_table = {n: l for n, l in zip(names, lines[1:-1])}

        def recurse(names_, depth_, prefixes=('', '', '', '')):
            for i, name in enumerate(names_):
                if name not in line_table:
                    continue
                line = line_table[name]
                is_last = (i == len(names_)-1)

                print('%s%-*s ' % (
                    prefixes[0+is_last],
                    widths[0] - (
                        len(prefixes[0+is_last])
                        if not m.isinf(depth) else 0),
                    line[0]),
                    end='')
                if not tree:
                    print(' %s%s' % (
                        ' '.join('%*s' % (w, x)
                            for w, x in zip(widths[1:], line[1:-1])),
                        line[-1]),
                        end='')
                print()

                # recurse?
                if name in table and depth_ > 1:
                    children = {
                        ','.join(str(getattr(Result(*c), k) or '') for k in by)
                        for c in table[name].children}
                    recurse(
                        # note we're maintaining sort order
                        [n for n in names if n in children],
                        depth_-1,
                        (prefixes[2+is_last] + "|-> ",
                         prefixes[2+is_last] + "'-> ",
                         prefixes[2+is_last] + "|   ",
                         prefixes[2+is_last] + "    "))

        recurse(names, depth)

    if not tree:
        print('%-*s  %s%s' % (
            widths[0], lines[-1][0],
            ' '.join('%*s' % (w, x)
                for w, x in zip(widths[1:], lines[-1][1:-1])),
            lines[-1][-1]))

def main(ci_paths,
        by=None,
        fields=None,
        defines=None,
        sort=None,
        **args):
    # it doesn't really make sense to not have a depth with tree,
    # so assume depth=inf if tree by default
    if args.get('depth') is None:
        args['depth'] = m.inf if args['tree'] else 1
    elif args.get('depth') == 0:
        args['depth'] = m.inf

    # find sizes
    if not args.get('use', None):
        results = collect(ci_paths, **args)
    else:
        results = []
        with openio(args['use']) as f:
            reader = csv.DictReader(f, restval='')
            for r in reader:
                if not any('stack_'+k in r and r['stack_'+k].strip()
                        for k in StackResult._fields):
                    continue
                try:
                    results.append(StackResult(
                        **{k: r[k] for k in StackResult._by
                            if k in r and r[k].strip()},
                        **{k: r['stack_'+k] for k in StackResult._fields
                            if 'stack_'+k in r and r['stack_'+k].strip()}))
                except TypeError:
                    pass

    # fold
    results = fold(StackResult, results, by=by, defines=defines)

    # sort, note that python's sort is stable
    results.sort()
    if sort:
        for k, reverse in reversed(sort):
            results.sort(
                key=lambda r: tuple(
                    (getattr(r, k),) if getattr(r, k) is not None else ()
                    for k in ([k] if k else StackResult._sort)),
                reverse=reverse ^ (not k or k in StackResult._fields))

    # write results to CSV
    if args.get('output'):
        with openio(args['output'], 'w') as f:
            writer = csv.DictWriter(f,
                (by if by is not None else StackResult._by)
                + ['stack_'+k for k in (
                    fields if fields is not None else StackResult._fields)])
            writer.writeheader()
            for r in results:
                writer.writerow(
                    {k: getattr(r, k) for k in (
                        by if by is not None else StackResult._by)}
                    | {'stack_'+k: getattr(r, k) for k in (
                        fields if fields is not None else StackResult._fields)})

    # find previous results?
    if args.get('diff'):
        diff_results = []
        try:
            with openio(args['diff']) as f:
                reader = csv.DictReader(f, restval='')
                for r in reader:
                    if not any('stack_'+k in r and r['stack_'+k].strip()
                            for k in StackResult._fields):
                        continue
                    try:
                        diff_results.append(StackResult(
                            **{k: r[k] for k in StackResult._by
                                if k in r and r[k].strip()},
                            **{k: r['stack_'+k] for k in StackResult._fields
                                if 'stack_'+k in r and r['stack_'+k].strip()}))
                    except TypeError:
                        raise
        except FileNotFoundError:
            pass

        # fold
        diff_results = fold(StackResult, diff_results, by=by, defines=defines)

    # print table
    if not args.get('quiet'):
        table(StackResult, results,
            diff_results if args.get('diff') else None,
            by=by if by is not None else ['function'],
            fields=fields,
            sort=sort,
            **args)

    # error on recursion
    if args.get('error_on_recursion') and any(
            m.isinf(float(r.limit)) for r in results):
        sys.exit(2)

if __name__ == "__main__":
    import argparse
    import sys
    parser = argparse.ArgumentParser(
        description="Find stack usage at the function level.",
        allow_abbrev=False)
    parser.add_argument(
        'ci_paths',
        nargs='*',
        help="Input *.ci files.")
    parser.add_argument(
        '-v', '--verbose',
        action='store_true',
        help="Output commands that run behind the scenes.")
    parser.add_argument(
        '-q', '--quiet',
        action='store_true',
        help="Don't show anything, useful with -o.")
    parser.add_argument(
        '-o', '--output',
        help="Specify CSV file to store results.")
    parser.add_argument(
        '-u', '--use',
        help="Don't parse anything, use this CSV file.")
    parser.add_argument(
        '-d', '--diff',
        help="Specify CSV file to diff against.")
    parser.add_argument(
        '-a', '--all',
        action='store_true',
        help="Show all, not just the ones that changed.")
    parser.add_argument(
        '-p', '--percent',
        action='store_true',
        help="Only show percentage change, not a full diff.")
    parser.add_argument(
        '-b', '--by',
        action='append',
        choices=StackResult._by,
        help="Group by this field.")
    parser.add_argument(
        '-f', '--field',
        dest='fields',
        action='append',
        choices=StackResult._fields,
        help="Show this field.")
    parser.add_argument(
        '-D', '--define',
        dest='defines',
        action='append',
        type=lambda x: (lambda k,v: (k, set(v.split(','))))(*x.split('=', 1)),
        help="Only include results where this field is this value.")
    class AppendSort(argparse.Action):
        def __call__(self, parser, namespace, value, option):
            if namespace.sort is None:
                namespace.sort = []
            namespace.sort.append((value, True if option == '-S' else False))
    parser.add_argument(
        '-s', '--sort',
        nargs='?',
        action=AppendSort,
        help="Sort by this field.")
    parser.add_argument(
        '-S', '--reverse-sort',
        nargs='?',
        action=AppendSort,
        help="Sort by this field, but backwards.")
    parser.add_argument(
        '-Y', '--summary',
        action='store_true',
        help="Only show the total.")
    parser.add_argument(
        '-F', '--source',
        dest='sources',
        action='append',
        help="Only consider definitions in this file. Defaults to anything "
            "in the current directory.")
    parser.add_argument(
        '--everything',
        action='store_true',
        help="Include builtin and libc specific symbols.")
    parser.add_argument(
        '--tree',
        action='store_true',
        help="Only show the function call tree.")
    parser.add_argument(
        '-Z', '--depth',
        nargs='?',
        type=lambda x: int(x, 0),
        const=0,
        help="Depth of function calls to show. 0 shows all calls but may not "
            "terminate!")
    parser.add_argument(
        '-e', '--error-on-recursion',
        action='store_true',
        help="Error if any functions are recursive.")
    sys.exit(main(**{k: v
        for k, v in vars(parser.parse_intermixed_args()).items()
        if v is not None}))