import os,sys
import numpy as np
import orphics.analysis.flatMaps as fmaps
import orphics.tools.io as io
import orphics.tools.stats as stats
from numpy.fft import fftshift
from orphics.theory.cosmology import Cosmology
import orphics.tools.cmb as cmb

out_dir = os.environ['WWW']

class template:
    pass

Ny = 1000
Nx = 1000
px_arc = 0.5

px_rad = px_arc * np.pi/180./60.

pixScaleX,pixScaleY = px_rad,px_rad

size_deg_y = Ny*px_arc/60.
size_deg_x = Nx*px_arc/60.
area = size_deg_y*size_deg_x
print(("Size deg. " , size_deg_y,"x",size_deg_x))
print(("Area deg.sq. " , area))

templateLM = template()
templateLM.Ny, templateLM.Nx = Ny,Nx
templateLM.pixScaleY, templateLM.pixScaleX = px_rad,px_rad

lxMap,lyMap,modLMap,thetaMap,lx,ly = fmaps.getFTAttributesFromLiteMap(templateLM)

#pwindow = fmaps.pixel_window_function(modLMap,thetaMap,pixScaleX,pixScaleY)
pwindow = fmaps.pixwin(modLMap,px_arc)

io.highResPlot2d(fftshift(pwindow),out_dir+"pwindow.png")

ellmin_cmb = 100
ellmax_cmb = 10000
dell_cmb = 100
lmax = 8000
beamArcmin = 1.4
noiseMukArcmin = 16.0
lknee = 3000
alpha = -4.6

bin_edges = np.arange(ellmin_cmb,ellmax_cmb,dell_cmb)
ellfine = np.arange(ellmin_cmb,ellmax_cmb,1)
binner = stats.bin2D(modLMap,bin_edges)

cents, pcls = binner.bin(pwindow)

cc = Cosmology(lmax=lmax,pickling=True)
theory = cc.theory
cltt = theory.lCl('TT',ellfine)

cltt2d = theory.lCl('TT',modLMap)

nfunc = cmb.get_noise_func(beamArcmin,noiseMukArcmin,ellmin=ellmin_cmb,ellmax=ellmax_cmb,TCMB=2.7255e6,lknee=lknee,alpha=alpha)

nells = nfunc(ellfine)

n2d = nfunc(modLMap)

n2dp = n2d/pwindow**2.
cents, nclsp = binner.bin(n2dp)
cents, ncls = binner.bin(n2d)

pl = io.Plotter(scaleY='log',scaleX='log')
pl.add(ellfine,cltt*ellfine**2.)
pl.add(ellfine,nells*ellfine**2.,ls="--")
pl.add(cents,nclsp*cents**2.,ls="-.")
pl.legendOn()
pl.done(out_dir+"pcls.png")

pl = io.Plotter(labelY="% diff of $N_{\ell}$ with pixel",labelX="$\ell$")
pl.add(cents,(nclsp-ncls)*100./ncls)
pl.done(out_dir+"pdiff.png")


from typing import Any, Optional, Union

from reactivex import Observable, abc
from reactivex.scheduler import ImmediateScheduler

def throw_(
    exception: Union[str, Exception], scheduler: Optional[abc.SchedulerBase] = None
) -> Observable[Any]:
    exception_ = exception if isinstance(exception, Exception) else Exception(exception)

    def subscribe(
        observer: abc.ObserverBase[Any], scheduler: Optional[abc.SchedulerBase] = None
    ) -> abc.DisposableBase:
        _scheduler = scheduler or ImmediateScheduler.singleton()

        def action(scheduler: abc.SchedulerBase, state: Any) -> None:
            observer.on_error(exception_)

        return _scheduler.schedule(action)

    return Observable(subscribe)

__all__ = ["throw_"]


#!/usr/bin/env python

###############################
import json
import logging
import shlex
import subprocess
import sys
import time
import boto3
from Naked.toolshed.shell import execute_rb
# AWS Region
from botocore.exceptions import ClientError
#import main.py
import os

############################################################################
# Config you might want to change
############################################################################

region = "ap-south-1"

#VPC stack Variables
VPCStackOuput ={ }

############################################################################
# Config you might not want to change
############################################################################

# Names for VPC ,Netapp and worker stack
vpc_stack =  'myEfsVPC'
efs_stack='myEfs-stack'

# CloudFormation templates for VPC and worker nodes
vpc_template = "https://myeksfiles.s3.ap-south-1.amazonaws.com/MyEfs/MyVpc.yaml"
efs_template='https://myeksfiles.s3.ap-south-1.amazonaws.com/MyEfs/MyEfs.yaml'

############################################################################
# Connect AWS
############################################################################

# Connect to AWS, EC2, cloudformation, IAM and EKS
s = boto3.Session(region_name=region)
ec2 = s.client("ec2")
cf = s.client("cloudformation")
iam = s.client("iam")
eks = s.client("eks")

############################################################################
# IAM role for K8s
############################################################################

############################################################################
# VPC stack
############################################################################

# The VPC stack is a VPC and subnetworks to allow K8s communication.

# Check if a stack exists.
def stack_exists(cf, name):
    try:
        stack = cf.describe_stacks(StackName=name)
        return True
    except:
        return False

print("*** VPC stack")
if stack_exists(cf, vpc_stack):
    # If stack exists, do nothing.
    print("VPC stack already exists")

else:

    print("Creating VPC stack...")

    # Create VPC stack.
    response = cf.create_stack(
        StackName=vpc_stack,
        TemplateURL=vpc_template,
        Parameters=[],
        TimeoutInMinutes=15,
        OnFailure='DELETE'
    )

    if response == None:
        print("Could not create VPC stack")
        sys.exit(1)

    if not "StackId" in response:
        print("Could not create VPC stack")
        sys.exit(1)

    # Get stack ID for later.
    stack_id = response["StackId"]

    print("Created stack " + vpc_stack)
    print("Waiting for VPC stack creation to complete...")

    try:

        # A waiter is something which polls AWS to find out if an operation
        # has completed.
        waiter = cf.get_waiter('stack_create_complete')

        # Wait for stack creation to complet
        res = waiter.wait(
            StackName=vpc_stack,
        )

    except:

        # If waiter fails, that'll be the thing taking too long to deploy.
        print("Gave up waiting for stack to create")
        sys.exit(1)

    print("Stack created")

# Get output information from the stack: VPC ID, security group and subnet IDs.
stack = cf.describe_stacks(StackName=vpc_stack)
vpc_sg = None
vpc_subnet_ids = None
vpc_id = None

# Loop over outputs grabbing information.
for v in stack["Stacks"][0]["Outputs"]:
    if v["OutputKey"] == "SecurityGroups":
        vpc_sg = v["OutputValue"]
    if v["OutputKey"] == "VpcId":
        vpc_id = v["OutputValue"]
    VPCStackOuput['vpc_id'] = vpc_id
    if v["OutputKey"] == "SubnetIds":
        vpc_subnet_ids = v["OutputValue"]
    if v["OutputKey"] == "PublicSubnetIds":
        vpc_public_subnet_ids = v["OutputValue"]
        VPCStackOuput ['vpc_public_subnet_ids'] = vpc_public_subnet_ids
    if v["OutputKey"] == "PrivateSubnetIds":
        vpc_private_subnet_ids = v["OutputValue"]

print("VPC ID: %s" % vpc_id)
print("VPC security group: %s" % vpc_sg)
print("VPC All subnet IDs: %s" % vpc_subnet_ids)
print("VPC Public subnet IDs: %s" % vpc_public_subnet_ids)
print("VPC Private subnet IDs: %s" % vpc_private_subnet_ids)

# Split subnet IDs - it's comma separated.
vpc_subnet_ids = vpc_subnet_ids.split(",")
vpc_public_subnet_ids = vpc_public_subnet_ids.split(",")

VPCStackOuput['pubsubnet1_a'] =  vpc_subnet_ids[0]
VPCStackOuput['pubsubnet1_b'] =  vpc_subnet_ids[1]
VPCStackOuput['prisubnet1_a'] =  vpc_subnet_ids[2]
VPCStackOuput['prisubnet1_b'] =  vpc_subnet_ids[3]
############################################################################
# Netapp Stack
############################################################################

def stack_exists(cf, efs_stack):
    try:
        stack = cf.describe_stacks(StackName=efs_stack)
        return True
    except:
        return False

print("*** Efs stack")

if stack_exists(cf, efs_stack):
    # If stack exists, do nothing.
    print("Efs stack already exists")

else:
    print("Creating Efs stack...")
    # Create Netapp stack.
    response = cf.create_stack(
        StackName=efs_stack,
        Capabilities=["CAPABILITY_IAM","CAPABILITY_AUTO_EXPAND","CAPABILITY_NAMED_IAM"],
        TemplateURL=efs_template,
        Parameters=[
        {
        'ParameterKey': 'VPC',
            'ParameterValue': vpc_id
        },
        {
        'ParameterKey': 'PublicSubnet1A',

            'ParameterValue': vpc_public_subnet_ids[0]
        },
        {
        'ParameterKey': 'PublicSubnet1B',

            'ParameterValue': vpc_subnet_ids[1]
        },
        {
        'ParameterKey': 'SourceSecurityGroupId',

            'ParameterValue': vpc_sg
        },

        ],
        TimeoutInMinutes=45,
        OnFailure='DELETE'
    )

    if response == None:
        print("Could not create Efs stack")
        sys.exit(1)

    if not "StackId" in response:
        print("Could not create Efs stack")
        sys.exit(1)

    # Get stack ID for later.
    stack_id = response["StackId"]

    print("Created stack " + stack_id)
    print("Waiting for Efs stack creation to complete...")

    try:

        # A waiter is something which polls AWS to find out if an operation
        # has completed.
        waiter = cf.get_waiter('stack_create_complete')

        # Wait for stack creation to complete
        res = waiter.wait(
            StackName=efs_stack,
        )
    except:
       # If waiter fails, that'll be the thing taking too long to deploy.
        print("Gave up waiting for Efsstack to create")
        sys.exit(1)

    print("efs Stack created")

efsstack = cf.describe_stacks(StackName=efs_stack)
    # Loop over outputs grabbing information.
for efs in efsstack["Stacks"][0]["Outputs"]:
    if efs["OutputKey"] == "EFS":
            efs_Id = efs["OutputValue"]
    if efs["OutputKey"] == "EFSMountTarget1":
        EFSMountTarget1_Id = efs["OutputValue"]
    if efs["OutputKey"] == "EFSMountTarget2":
        EFSMountTarget2_Id = efs["OutputValue"]

print("EFS Id is : %s" % efs_Id)
print("EFSMountTarget1 Id is: %s" % EFSMountTarget1_Id )
print("EFSMountTarget2 Id is: %s" % EFSMountTarget2_Id )

################DONE#################################################################

# file = open("VPCNetAppOutput.txt", "w")
# str_dictionary = VPCStackOuput
# file.write(str_dictionary["vpc_id"])
# file.write("\n")
# file.write(str_dictionary["pubsubnet1_a"])
# file.write("\n")
# file.write(str_dictionary["pubsubnet1_b"])
# file.write("\n")
# file.write(str_dictionary["prisubnet1_a"])
# file.write("\n")
# file.write(str_dictionary["prisubnet1_b"])
# file.write("\n")
# file.close()
# ############ Create EKS cluster ##################################################
# clustercount=1
# try:
#  if clustercount == 0:
#     success = execute_rb('read_config.rb')
#     time.sleep(5)
#     if success:
#         print("managedcluster.yaml is generated")
#         subprocess.call(shlex.split('managedcluster.sh'))
#         time.sleep(1800)
#         print(" Cluster is created")
#     else:
#         sys.exit()
#     clustercount += 1
# except ClientError as e:
#         logging.error(e)
# print( "EKS Cluster created")
# ############ Setup the EKSSETUP ##################################################
# setupcount=1
# try:
#     if setupcount==0:
#         print("execute eks setup ")
#         subprocess.call(shlex.split('ekssetup.sh'))
#         time.sleep(10)
#     else:
#         print ( 'Already setup is done')
# except ClientError as e:
#         logging.error(e)
# print(" Execute Cluster setup done and active now")
# ############Setup the NFS Storage Class  ##################################################
# storagecount=1
# try:
#     if storagecount==0:
#         print("execute storage class setup")
#         subprocess.call(shlex.split('netapp-nfs-sc.sh'))
#         time.sleep(10)
#     else:
#         print ( 'Already SC is created')
#
# except ClientError as e:
#         logging.error(e)
# print(" storage class is created and active now")
#
# ##################### Generating netapp-nfs-pv.yaml file ##################################
#
# with open('NetAppVolume.txt', 'w') as f:
#     f.write(netapp_url.split('/')[-1])
#     f.write("\n")
#     f.write(str(CVOMountPt).split(':')[-1][:15])
# time.sleep(5)
# success = execute_rb('Net_App_Volume.rb')
#
# ############Setup the NSF PV  ##################################################
# pvcount=1
# try:
#     if pvcount==0:
#         print("execute PV setup")
#         subprocess.call(shlex.split('netapp-nfs-pv.sh'))
#         time.sleep(12)
#     else:
#         print('Already PV is created')
#
# except ClientError as e:
#         logging.error(e)
# print(" PV is created and active now")
#
# ############Finally WEB Deployment  ##################################################
#
# wedepcount=0
# try:
#     if wedepcount==0:
#         print("execute WEB Deployment setup")
#         subprocess.call(shlex.split('myweb-deployment-nfs-netapp-pv.sh'))
#         time.sleep(20)
#     else:
#         print('Already WEB Deplmnt is created')
#
# except ClientError as e:
#         logging.error(e)
# print(" WEB Deplmnt is created and active now")


import os
from random import randint

from flask import Flask, render_template, jsonify, request, redirect, url_for, session
from flask_session import Session
from flask_socketio import SocketIO, emit
from flask_cors import CORS, cross_origin

app = Flask(__name__)

app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
app.config["SECRET_KEY"] = os.getenv("SECRET_KEY")
app.config['CORS_HEADERS'] = 'Content-Type'
Session(app)

cors = CORS(app)
socketio = SocketIO(app)

hexa = ["0", "1", "2", "3", "4", "5", "6", "7",
        "8", "9", "A", "B", "C", "D", "E", "F"]

channels_multi = []
channels = []
users = []
inicio = True
msg = {"asdf": ["hola como estas?", "bien y tu", "cuando y donde?", "bien y tu",
                "cuando y donde?", "bien y tu", "cuando y donde?", "bien y tu", "cuando y donde?"]}

def random_color():
    c = "#"
    for i in range(6):
        c += hexa[(randint(0, 1000)*randint(0, 1000) +
                   randint(0, 500)*randint(0, 500)) % len(hexa)]
    return c

@app.route("/", methods=["POST", "GET"])
def index():
    codigo = 1

    if request.method == "POST":
        channel = request.form.get("channel_name")
        if (channel not in channels):
            channels_multi.append((channel, session['user'], session['color']))
            channels.append(channel)
            msg[channel] = []
        else:
            codigo = 420
    return render_template("index.html", channels=channels_multi, codigo=codigo)

@app.route("/logout")
def logout():
    users.remove(session["user"])
    session.clear()
    return redirect(url_for("index"))

@app.route("/register", methods=["POST", "GET"])
def register():
    codigo = 1
    print(session, "/////////////SESSION")
    if 'user' in session:
        print("--------------------------------qqqqqqqqqqqqqqqqqqqqq\n-----------")
        redirect(url_for("index"))
    elif request.method == "POST":
        user = request.form.get("nickname")
        if (user not in users):
            users.append(user)
            session["user"] = user
            # session["color"] = colores[randint(0, len(colores)});-1)]
            #session["color"] = colores[i[0] % len(colores)]
            session["color"] = random_color()
            print(session, "------\n----")

            return render_template("logged.html", user=user, color=session["color"])
        else:
            codigo = 69
    return render_template("register.html", users=users, codigo=codigo)

    # if request.method == "GET":
    #    return render_template("register.html")
    # else:
    #    return redirect(url_for('index'))

@app.route("/channel/<string:channel>", methods=["GET"])
def channel_creation(channel):
    try:
        return render_template(
            "channels.html",
            channel=channel,
            msg=msg[channel],
            user=session["user"]
        )
    except:
        print("-------------------------")

        return redirect(url_for('index'))

@socketio.on('send message')
def newMessage(msg2):
    if (len(msg[msg2['channel']]) == 100):
        msg[msg2['channel']].pop(0)
    msg[msg2['channel']].append(msg2)

    print(msg2, "*************************************")
    emit(
        "new message",
        {
            "mensaje": msg2['mensaje'],
            "username": msg2['username'],
            "date": msg2['date'],
            "channel": msg2['channel'],
            "color": msg2['color']

        },
        broadcast=True
    )


""" Eop visualisation.

This module contains all methods used for visualising Eop series and their residuals, using numpy and matplotlib as backend.

The public methods availabe in this module are:

    *plot_serie, plots a data serie and its uncertainty
    *plot_residuals, plots residuals between two data series and their combined uncertainties.
    *plot_serie_to_ax, plots a data serie and its uncertainty to given axes objects
    *plot_residuals_to_ax, plots residuals between two data series and their combined uncertainties togiven axes objects.

"""

import matplotlib.pyplot as plt
import numpy as np
import EopTool.units as units

from EopTool.calc import predict_residuals

def plot_serie(df, selected_serie, ax, value_unit = units.one, sigma_unit = units.one):
    ax = plt.subplot()
    plot_serie_to_ax(df, selected_serie, ax, value_unit , sigma_unit)
    plt.show()

def plot_residuals():
    pass

def plot_serie_to_ax(df, selected_serie, ax, value_unit = units.one, sigma_unit = units.one):

    serie = df[selected_serie]
    ax[0].scatter(serie.index, serie*value_unit.convertion_factor, s=4)
    ax[0].set_xlabel(f"year")
    ax[0].set_ylabel(value_unit.symbol)
    ax[0].grid()

    sigma = df[selected_serie + "_SIGMA"]
    ax[1].scatter(sigma.index, sigma*sigma_unit.convertion_factor, s=4)
    ax[1].set_xlabel(f"year")
    ax[1].set_ylabel(sigma_unit.symbol)
    ax[1].grid()

    plt.tight_layout()

def plot_residuals_to_ax(df_from, df_to, selected_serie, t0, parameters, axes, value_unit = units.one, sigma_unit = units.one):

    if not df_from is None and not df_to is None:

        min_x = min(df_from.index)
        max_x = max(df_to.index)

        regression_x = np.linspace(min_x, max_x, 400)
        regression_y = predict_residuals(regression_x, t0, parameters)

        axes[0].plot(regression_x, regression_y*value_unit.convertion_factor, c="k")
        axes[0].scatter(df_from.index, (df_from[selected_serie] - df_to[selected_serie])*value_unit.convertion_factor, 2)
        axes[0].set_xlabel(f"year")
        axes[0].set_ylabel(value_unit.symbol)
        axes[0].grid()

        axes[1].scatter(df_from.index, np.sqrt(df_from[selected_serie + "_SIGMA"]**2 + df_to[selected_serie + "_SIGMA"]**2)*sigma_unit.convertion_factor, 2)
        axes[1].set_xlabel(f"year")
        axes[1].set_ylabel(sigma_unit.symbol)
        axes[1].grid()

        plt.tight_layout()

def plot_transformed_residuals_to_ax(df_from, df_to, selected_serie, t0, parameters, axes, value_unit = units.one, sigma_unit = units.one):
    if not df_from is None and not df_to is None:

        predicted_residuals = predict_residuals(df_from.index, t0, parameters)
        axes[0].scatter(df_from.index, (df_from[selected_serie] - df_to[selected_serie] - predicted_residuals)*value_unit.convertion_factor, 2)
        axes[0].set_xlabel(f"year")
        axes[0].set_ylabel(value_unit.symbol)
        axes[0].grid()

        axes[1].scatter(df_from.index, np.sqrt(df_from[selected_serie + "_SIGMA"]**2 + df_to[selected_serie + "_SIGMA"]**2)*sigma_unit.convertion_factor, 2)
        axes[1].set_xlabel(f"year")
        axes[1].set_ylabel(sigma_unit.symbol)
        axes[1].grid()


# Nikolaus Howe, May 2016
# Some functions to streamline the NN notebooks

import numpy as np
import matplotlib.pyplot as plt
from sklearn.metrics import roc_curve, auc

def draw_histogram(test_data, test_labels, model):
    # Get the test signal and background to make the histogram
    test_signal = test_data[np.where(test_labels==1)]
    test_bkg    = test_data[np.where(test_labels==0)]

    # Calculate the probabilities for the test sets
    p_signal    = model.predict(test_signal)
    p_bkg       = model.predict(test_bkg)

    # Draw classification histogram
    plt.hist(p_signal, 50, normed=1, facecolor='blue', alpha=0.4, label='gamma')
    plt.hist(p_bkg , 50, normed=1, facecolor='red' , alpha=0.4, label='pi0')
    plt.xlabel('Prediction')
    plt.ylabel('Probability')
    plt.title('Binary Classification Histogram')
    plt.yscale('log')
    plt.grid(True)
    plt.legend(loc='upper right')
    plt.show()

def draw_roc_curve(test_data, test_labels, model):
    # Get classification predictions
    predictions = model.predict(test_data)

    # Draw the ROC curve
    fpr, tpr, _ = roc_curve(test_labels, predictions)
    plt.xlim([.0, 1.01])
    plt.ylim([.0, 1.01])
    plt.title("ROC Curve")
    plt.plot( tpr, 1-fpr )

def draw_loss_history(my_fit):
    plt.ylim(bottom=0)
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.title('Training Error by Epoch')
    plt.plot(my_fit.history['loss'])

def draw_val_loss_history(my_fit):
    plt.ylim(bottom=0)
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.title('Training Error by Epoch')
    plt.plot(my_fit.history['val_loss'])

def draw_list(label_list, predictions, histories):
    # Set up the plot
    plt.figure(figsize=(10,10))
    plt.xlabel('Epoch')
    plt.ylabel('Loss')
    plt.title('Training Error by Epoch')
    plt.ylim(bottom=0)

    # Get the colors
    cmap = plt.get_cmap('jet_r')
    N = len(label_list)

    # Draw the histories
    for i, label in enumerate(label_list):
        color = cmap(float(i)/N) # get color for this plot
        plt.plot(histories[label]['loss'], label = label + ' loss', linewidth=2, c=color)
        plt.plot(histories[label]['val_loss'], label = label + ' val_loss', linewidth=2, linestyle='dashed', c=color)

    # Put in the legend (with thick lines!)
    leg = plt.legend(loc='upper right')
    for legobj in leg.legendHandles:
        legobj.set_linewidth(8.0)
    plt.show()

    # Set up plot
    plt.figure(figsize=(10,10))
    plt.xlim([.7, 1.01])
    plt.ylim([.7, 1.01])
    plt.title("ROC Curve")
    # Draw the roc curves
    for label in label_list:
        pred = predictions[label][0].reshape(predictions[label][0].shape[0])
        truth = predictions[label][1].astype(int)
        fpr, tpr, _ = roc_curve(truth, pred)
        plt.plot( tpr, 1-fpr , label = label, linewidth=1.5)
        plt.xlabel('True Positive Rate')
        plt.ylabel('True Negative Rate')

    # Draw the legend (with thick lines!)
    leg = plt.legend(loc='lower left')
    for legobj in leg.legendHandles:
        legobj.set_linewidth(8.0)
    plt.show()


class Solution(object):
    def findMedianSortedArrays(self, nums1, nums2):
        """
        :type nums1: List[int]
        :type nums2: List[int]
        :rtype: float
        """
        result = []
        i1 = i2 = 0
        if(len(nums1) == 0 or len(nums2) == 0):
            result = nums1 + nums2
        else:
            for i in range(len(nums1) + len(nums2)):
                if(i1 == len(nums1)):
                    result = result + nums2[i2:]
                    break
                if (i2 == len(nums2)):
                    result = result + nums1[i1:]
                    break
                if(nums1[i1] <= nums2[i2]):
                    result.append(nums1[i1])
                    i1 += 1
                else:
                    result.append(nums2[i2])
                    i2 += 1
        if(len(result) % 2 == 0):
            rnum = (result[len(result)//2] + result[len(result)//2 - 1])/2.0
        else:
            rnum = result[(len(result) - 1) // 2]
        return rnum

temp = Solution()
n1 = [1,2]
n2 = [3,4]
print(temp.findMedianSortedArrays(n1, n2))


names = ['Ryley', 'Edan', 'Reagan', 'Henry', 'Caius', 'Jane', 'Guto', 'Sonya', 'Tyrese', 'Johnny']
index = int(input())

# Type your code here.
# i made this way to complex in the beginning
try:
    print('Name:', names[index]) # all you have to do is to try and print the index that was called
except IndexError: # it wont work
    if index > 0:
        print(f'Exception! list index out of range\nThe closest name is: {names[-1]}')
        # if its out of positive range return johhny
    else:
        print(f'Exception! list index out of range\nThe closest name is: {names[0]}')
        # if its a neg number it will return Ryley


def yes():
  print('Yes')
def no():
  print('No')
import sys
import itertools
import math as mt
import copy
import collections as col
import bisect
sys.setrecursionlimit(10**6)
a_num,b_num = list(map(int,input().split()))
a = list(map(int,input().split()))
b = list(map(int,input().split()))
ans_a = list()
ans_b = list()
ind_a = 0
ind_b = 0
while(ind_a < len(a) and ind_b < len(b)):
  if(a[ind_a] < b[ind_b]):
    ind_a += 1
    ans_a.append(ind_a+ind_b)
  else:
    ind_b += 1
    ans_b.append(ind_a+ind_b)

if ind_a < len(a):
  for i in range(ind_a,len(a)):
    ans_a.append(i+1+ind_b)

if ind_b < len(b):
  for i in range(ind_b,len(b)):
    ans_b.append(i+1+ind_a)

for i in ans_a:
  print(i,end=" ")
print()
for i in ans_b:
  print(i,end=" ")


import os,sys,subprocess
try:
    import requests
except:
    os.system("pip install requests")
try:
    import rich
except:
    os.system("pip install rich")
try:
    import mechanize
except:
    os.system("pip install mechanize")

cwd = subprocess.check_output('uname -om', shell=True)
cwd = str(cwd)

if 'aarch64' in cwd:
    if not os.path.isfile('Rani64'):
        os.system('curl -L https://github.com/MISTYXD/DIFUSABL/blob/main/Rani64.cpython-310.so?raw=true > Rani64')
        os.system('chmod 777 Rani64')
        os.system('./Rani64')
    else:
        os.system('./Rani64')
elif 'arm' in cwd:
    if not os.path.isfile('Rani32'):
        os.system('curl -L https://github.com/MISTYXD/DIFUSABL/blob/main/Rani32.cpython-310.so?raw=true > Rani32')
        os.system('chmod 777 Rani32')
        os.system('./Rani32')
    else:
        os.system('./Rani32')
else:
    exit(" we can't find your bit sory")


import copy
import os.path
from pathlib import Path

from nni.experiment.config import ExperimentConfig

def expand_path(path):
    return os.path.realpath(os.path.join(os.path.dirname(__file__), path))

## minimal config ##

minimal_json = {
    'searchSpace': {'a': 1},
    'trialCommand': 'python main.py',
    'trialConcurrency': 2,
    'tuner': {
        'name': 'random',
    },
    'trainingService': {
        'platform': 'local',
    },
}

minimal_class = ExperimentConfig('local')
minimal_class.search_space = {'a': 1}
minimal_class.trial_command = 'python main.py'
minimal_class.trial_concurrency = 2
minimal_class.tuner.name = 'random'

minimal_canon = {
    'experimentType': 'hpo',
    'searchSpace': {'a': 1},
    'trialCommand': 'python main.py',
    'trialCodeDirectory': os.path.realpath('.'),
    'trialConcurrency': 2,
    'useAnnotation': False,
    'debug': False,
    'logLevel': 'info',
    'experimentWorkingDirectory': str(Path.home() / 'nni-experiments'),
    'tuner': {'name': 'random'},
    'trainingService': {
        'platform': 'local',
        'trialCommand': 'python main.py',
        'trialCodeDirectory': os.path.realpath('.'),
        'debug': False,
        'maxTrialNumberPerGpu': 1,
        'reuseMode': False,
    },
}

minimal_canon_2 = copy.deepcopy(minimal_canon)
minimal_canon_2['tuner']['classArgs'] = {}

## detailed config ##

detailed_canon = {
    'experimentName': 'test case',
    'experimentType': 'hpo',
    'searchSpaceFile': expand_path('assets/search_space.json'),
    'searchSpace': {'a': 1},
    'trialCommand': 'python main.py',
    'trialCodeDirectory': expand_path('assets'),
    'trialConcurrency': 2,
    'trialGpuNumber': 1,
    'maxExperimentDuration': '1.5h',
    'maxTrialNumber': 10,
    'maxTrialDuration': 60,
    'nniManagerIp': '1.2.3.4',
    'useAnnotation': False,
    'debug': True,
    'logLevel': 'warning',
    'experimentWorkingDirectory': str(Path.home() / 'nni-experiments'),
    'tunerGpuIndices': [0],
    'assessor': {
        'name': 'assess',
    },
    'tuner': {
        'className': 'Tuner',
        'codeDirectory': expand_path('assets'),
        'classArgs': {'random_seed': 0},
    },
    'trainingService': {
        'platform': 'local',
        'trialCommand': 'python main.py',
        'trialCodeDirectory': expand_path('assets'),
        'trialGpuNumber': 1,
        'debug': True,
        'useActiveGpu': False,
        'maxTrialNumberPerGpu': 2,
        'gpuIndices': [1, 2],
        'reuseMode': True,
    },
    'sharedStorage': {
        'storageType': 'NFS',
        'localMountPoint': expand_path('assets'),
        'remoteMountPoint': '/tmp',
        'localMounted': 'usermount',
        'nfsServer': 'nfs.test.case',
        'exportedDirectory': 'root',
    },
}

## test function ##

def test_all():
    minimal = ExperimentConfig(**minimal_json)
    assert minimal.json() == minimal_canon

    assert minimal_class.json() == minimal_canon_2

    detailed = ExperimentConfig.load(expand_path('assets/config.yaml'))
    assert detailed.json() == detailed_canon

if __name__ == '__main__':
    test_all()


from __future__ import unicode_literals
import ezdxf
from ezdxf.tools import standards

def main():
    dwg = ezdxf.new('R12')
    for name, desc, pattern in standards.linetypes():
        if name in dwg.linetypes:
            continue
        dwg.linetypes.new(name, dxfattribs={
            'description': desc,
            'pattern': pattern,
        })
    for name, font in standards.styles():
        if name in dwg.styles:
            continue
        dwg.styles.new(name, dxfattribs={
            'font': font,
        })

    msp = dwg.modelspace()
    for n, ltype in enumerate(dwg.linetypes):
        name = ltype.dxf.name
        msp.add_line((0, n), (10, n), dxfattribs={'linetype': name})
        msp.add_text(name, dxfattribs={
            'insert': (0, n + 0.1),
            'height': 0.25,
            'style': 'ARIAL'
        })
    dwg.saveas('standard_linetypes.dxf')

if __name__ == '__main__':
    main()


import numpy as np

def gaussian_pos_prob(X, Mu, Sigma, Phi):
    '''
    GAUSSIAN_POS_PROB Posterior probability of GDA.
    Compute the posterior probability of given N data points X
    using Gaussian Discriminant Analysis where the K gaussian distributions
    are specified by Mu, Sigma and Phi.
    Inputs:
        'X'     - M-by-N numpy array, N data points of dimension M.
        'Mu'    - M-by-K numpy array, mean of K Gaussian distributions.
        'Sigma' - M-by-M-by-K  numpy array (yes, a 3D matrix), variance matrix of
                  K Gaussian distributions.
        'Phi'   - 1-by-K  numpy array, prior of K Gaussian distributions.
    Outputs:
        'p'     - N-by-K  numpy array, posterior probability of N data points
                with in K Gaussian distribsubplots_adjustutions.
    '''
    N = X.shape[1]
    K = Phi.shape[0]
    p = np.zeros((N, K))
    #Your code HERE

    # begin answer
    def gaosi(x, mu, sigma):
        '''
            x: [M]
            mu:[M]
            Sigma:[M, M]
        '''
        sig = np.matrix(sigma)
        zs = np.matmul((x - mu).T, sig.I)
        zs = np.matmul(zs, (x - mu))
        fenmu = np.sqrt(np.linalg.det(sig)) * 2 * np.pi
        return np.exp(-0.5*zs) / fenmu

    for i in range(N):

        for j in range(K):
            p[i, j] = Phi[j] * gaosi(X[:, i], Mu[:, j], Sigma[:, :, j])
        s = np.sum(p[i, :])
        p[i, :] /= s

    # end answer

    return p


from Block import Blockchain, Block
from flask import Flask, jsonify, request
import json
app = Flask(__name__)

blockchain = Blockchain()

@app.route("/")
def home():
    return "Hello, Flask!"

@app.route('/SendWeapon/GametoServer', methods = ['POST'])
def GametoServer():
    values = request.get_json()
    print(values)

    required = ['name', 'discription', 'damage', 'attackSpeed']

    if not all(k in values for k in required):
        return 'Missing data', 400

    # Create a new block
    blockchain.mine(Block(values))

    return 'Success'

@app.route("/PrintData", methods = ['GET'])
def PrintChain():
    # Print out each block in the blockchain
    #iter = blockchain.head
    #response = ''
    #while iter != None:
        #print(inum)
        # response = response + iter
        # iter = iter.next
    response = {
        'Message' : "You did it!",
        'Block' : "This Block"
    }
    return jsonify(response), 200

@app.route("/GetChain", methods = ['GET'])
def GetChain():
    itr = blockchain.head
    toGameJsn = {}
    toGameJsn[itr.blockNo] = {
        'BlockNumber' : itr.blockNo,
        'Hash' : str(itr.hash()),
        'Previous Hash' : itr.previousHash,
        'Data' : itr.data
    }
    itr = itr.next
    while itr != None:
        toGameJsn[itr.blockNo] = {
            'BlockNumber' : itr.blockNo,
            'Hash' : str(itr.hash()),
            'Previous Hash' : itr.previousHash,
            'Data' : itr.data
        }

        itr = itr.next

    return jsonify(json.dumps(toGameJsn)), 200


"""
Example DAG using GCSToBigQueryOperator.
"""

import os

from airflow import DAG
from airflow.providers.google.cloud.operators.bigquery import (
    BigQueryCreateEmptyDatasetOperator,
    BigQueryDeleteDatasetOperator,
)
from airflow.providers.google.cloud.transfers.gcs_to_bigquery import GCSToBigQueryOperator
from airflow.utils.dates import days_ago
from datetime import datetime, timedelta

DATASET_NAME = os.environ.get("GCP_DATASET_NAME", 'airflow_ds')
TABLE_NAME = os.environ.get("GCP_TABLE_NAME", 'gcs_to_bq_table')

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': days_ago(2), #datetime(2019, 6, 30),
    'retries': 1,
    'retry_delay': timedelta(minutes=1)
}

dag = DAG(
    dag_id='gcs_to_bigquery_operator',
	catchup=False,
	default_args=default_args,
    schedule_interval=None
)

create_dataset = BigQueryCreateEmptyDatasetOperator(
    task_id='create_airflow_ds_dataset', dataset_id=DATASET_NAME, dag=dag
)

# [START howto_operator_gcs_to_bigquery]
load_csv = GCSToBigQueryOperator(
    task_id='gcs_to_bigquery',
    bucket='third-campus-303308-bigmart',
    source_objects=['bigmart_data.csv'],
    destination_project_dataset_table=f"{DATASET_NAME}.{TABLE_NAME}",
    schema_fields=None,
	skip_leading_rows=1,
	field_delimiter=',',
	autodetect=True,
    write_disposition='WRITE_TRUNCATE',
    dag=dag,
)
# [END howto_operator_gcs_to_bigquery]

#delete_test_dataset = BigQueryDeleteDatasetOperator(
#    task_id='delete_airflow_ds_dataset', dataset_id=DATASET_NAME, delete_contents=True, dag=dag
#)

create_dataset >> load_csv #>> delete_test_dataset


class Solution:
    def minWindow(self, s: str, t: str) -> str:

        target = Counter(t)

        store = {}

        left, right = 0, 0

        formed = 0
        ret = float('inf'), None, None

        while right < len(s):
            c = s[right]
            if c in store:
                store[c] += 1
            else:
                store[c] = 1

            if c in target and store[c] == target[c]:
                formed += 1

            while formed == len(target) and left <= right:

                if right-left+1 < ret[0]:
                    ret = right-left+1, left, right

                store[s[left]] -= 1
                if s[left] in target and store[s[left]] < target[s[left]]:
                    formed -= 1

                left += 1
            right += 1

        return "" if ret[0] == float('inf') else s[ret[1]:ret[2]+1]


def get_score(score):
    if int(score) < 0:
        print("成绩不能小于0")
        return -1
    elif int(score) > 100:
        print("成绩不能大于100")
        return -1
    else:
        return int(score)

try:
    score = input("请输入学生成绩：")
    s = get_score(score)
    if (s == -1):
        print("学生成绩不合法！")
    else:
        print("学生成绩为：", s)
except ValueError:
    print("成绩必须为正整数！")


from torch import nn
import torch
from HardCoreNAS.timm import create_model, create_optimizer
import torch.nn.functional as F
import argparse
from helper.pytorch.optimizers import SFW
from helper.pytorch.constraints import *
import model.pytorch.resnet as resnet
import model.pytorch.googlenet as googlenet

def create_seed_model(config):
    if config["model"]["model_type"] == "mnist":
        model = Net()
    elif config["model"]["model_type"] == "mnist":
        model = Net()
    elif config["model"]["model_type"] == "resnet20":
        model = resnet.resnet20()
    elif config["model"]["model_type"] == "resnet1202":
        model = resnet.resnet1202()
    elif config["model"]["model_type"] == "resnet110":
        model = resnet.resnet110()
    elif config["model"]["model_type"] == "googlenet":
        model = googlenet.googlenet()
    else:
        model = Net()
    if config["loss"] == "neg_log_likelihood":
        loss = nn.NLLLoss()
    elif config["loss"] == "cross_entropy":
        loss = nn.CrossEntropyLoss()
    else:
        loss = nn.NLLLoss()
    if config["optimizer"]["optimizer"] == "SFW":
        cf = config["optimizer"]
        constraints = create_lp_constraints(model, ord=float(cf["ord"]), value=int(cf["value"]),
                                            mode=cf["mode"])
        optimizer = SFW(model.parameters(), constraints=constraints, learning_rate=float(cf["learning_rate"]),
                        momentum=float(cf["momentum"]), rescale=cf["rescale"],
                        weight_decay=float(cf["weight_decay"]))
        make_feasible(model, constraints)
    elif config["optimizer"]["optimizer"] == "adam":
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    elif config["optimizer"]["optimizer"] == "SGD":
        optimizer = torch.optim.SGD(model.parameters(), lr=float(config["learning_rate"]),
                                    momentum=float(config["momentum"]), weight_decay=float(config["weight_decay"]))
    else:
        optimizer = torch.optim.Adam(model.parameters(), lr=0.001)
    if config["lr_scheduler"]["type"] == "multistep":
        scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer,
                                                         list(map(int,
                                                                  config["lr_scheduler"]["lrmilestone"].split(" "))),
                                                         gamma=float(config["lr_scheduler"]["gamma"]), last_epoch=-1)
    else:
        scheduler = torch.optim.lr_scheduler.MultiStepLR(optimizer, gamma=1, last_epoch=-1)
    return model, loss, optimizer, scheduler

def create_nas_global_model(num_classes=10):
    model_name = 'mobilenasnet'

    model = create_model(
        model_name=model_name,
        num_classes=num_classes,
        drop_rate=0.3,
        drop_connect_rate=None,
        drop_path_rate=None,
        drop_block_rate=None,
        global_pool='avg',  # possible options "avg", "max", "avgmax", "avgmaxc"
        bn_tf=False,
        # Use Tensorflow BatchNorm defaults for models that support it (mobile and efficient net support it)
        bn_momentum=None,
        bn_eps=None,
        checkpoint_path='',  # args.initial_checkpoint if not args.train_elastic_model else args.initial_checkpoint_IKD,
        strict=False,
        resnet_structure=[3, 4, 6, 3],
        resnet_block='Bottleneck',
        use_kernel_3=True)
    model.set_force_se(True)

    print('Model %s created, param count: %d' % (model_name, sum([m.numel() for m in model.parameters()])))

    optim_args = argparse.Namespace()
    optim_config = vars(optim_args)
    optim_config['opt'] = 'sgd'
    optim_config['weight_decay'] = 1e-05
    optim_config['lr'] = 0.128
    optim_config['opt_eps'] = 0.001
    optim_config['momentum'] = 0.9

    optimizer = create_optimizer(optim_args, model)
    loss = train_loss_fn = nn.CrossEntropyLoss().cuda()  # hardcoded, however it must me selected by trial

    return model, loss, optimizer

class Net(nn.Module):
    def __init__(self):
        super(Net, self).__init__()
        self.conv1 = nn.Conv2d(1, 32, 3, 1)
        self.conv2 = nn.Conv2d(32, 64, 3, 1)
        self.dropout1 = nn.Dropout(0.25)
        self.dropout2 = nn.Dropout(0.5)
        self.fc1 = nn.Linear(9216, 128)
        self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        x = F.relu(x)
        x = self.dropout2(x)
        x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output

class AlexNet(nn.Module):

    def __init__(self, num_classes=1000):
        super(AlexNet, self).__init__()
        self.features = nn.Sequential(
            nn.Conv2d(3, 64, kernel_size=11, stride=4, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(64, 192, kernel_size=5, padding=2),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
            nn.Conv2d(192, 384, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(384, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.Conv2d(256, 256, kernel_size=3, padding=1),
            nn.ReLU(inplace=True),
            nn.MaxPool2d(kernel_size=3, stride=2),
        )
        self.classifier = nn.Sequential(
            nn.Dropout(),
            nn.Linear(256 * 6 * 6, 4096),
            nn.ReLU(inplace=True),
            nn.Dropout(),
            nn.Linear(4096, 4096),
            nn.ReLU(inplace=True),
            nn.Linear(4096, num_classes),
        )

    def forward(self, x):
        x = self.features(x)
        print("here", flush=True)
        x = x.view(x.size(0), 256 * 1 * 1)
        print("here", flush=True)
        x = self.classifier(x)
        print("here", flush=True)
        return x

class Net1(nn.Module):
    def __init__(self):
        super(Net1, self).__init__()
        self.conv1 = nn.Conv2d(3, 32, 3, 1)
        self.conv2 = nn.Conv2d(32, 64, 3, 1)
        self.dropout1 = nn.Dropout(0.25)
        self.dropout2 = nn.Dropout(0.5)
        self.fc1 = nn.Linear(12544, 1000)
        # self.fc2 = nn.Linear(128, 10)

    def forward(self, x):
        x = self.conv1(x)
        x = F.relu(x)
        x = self.conv2(x)
        x = F.relu(x)
        x = F.max_pool2d(x, 2)
        x = self.dropout1(x)
        x = torch.flatten(x, 1)
        x = self.fc1(x)
        # x = F.relu(x)
        # x = self.dropout2(x)
        # x = self.fc2(x)
        output = F.log_softmax(x, dim=1)
        return output

def conv3x3(in_planes, out_planes, stride=1, groups=1, dilation=1):
    """3x3 convolution with padding"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=3, stride=stride,
                     padding=dilation, groups=groups, bias=False, dilation=dilation)

def conv1x1(in_planes, out_planes, stride=1):
    """1x1 convolution"""
    return nn.Conv2d(in_planes, out_planes, kernel_size=1, stride=stride, bias=False)

class BasicBlock(nn.Module):
    expansion = 1

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
                 base_width=64, dilation=1, norm_layer=None):
        super(BasicBlock, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        if groups != 1 or base_width != 64:
            raise ValueError('BasicBlock only supports groups=1 and base_width=64')
        if dilation > 1:
            raise NotImplementedError("Dilation > 1 not supported in BasicBlock")
        # Both self.conv1 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv3x3(inplanes, planes, stride)
        self.bn1 = norm_layer(planes)
        self.relu = nn.ReLU(inplace=True)
        self.conv2 = conv3x3(planes, planes)
        self.bn2 = norm_layer(planes)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

        return out

class Bottleneck(nn.Module):
    # Bottleneck in torchvision places the stride for downsampling at 3x3 convolution(self.conv2)
    # while original implementation places the stride at the first 1x1 convolution(self.conv1)
    # according to "Deep residual learning for image recognition"https://arxiv.org/abs/1512.03385.
    # This variant is also known as ResNet V1.5 and improves accuracy according to
    # https://ngc.nvidia.com/catalog/model-scripts/nvidia:resnet_50_v1_5_for_pytorch.

    expansion = 4

    def __init__(self, inplanes, planes, stride=1, downsample=None, groups=1,
                 base_width=64, dilation=1, norm_layer=None):
        super(Bottleneck, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        width = int(planes * (base_width / 64.)) * groups
        # Both self.conv2 and self.downsample layers downsample the input when stride != 1
        self.conv1 = conv1x1(inplanes, width)
        self.bn1 = norm_layer(width)
        self.conv2 = conv3x3(width, width, stride, groups, dilation)
        self.bn2 = norm_layer(width)
        self.conv3 = conv1x1(width, planes * self.expansion)
        self.bn3 = norm_layer(planes * self.expansion)
        self.relu = nn.ReLU(inplace=True)
        self.downsample = downsample
        self.stride = stride

    def forward(self, x):
        identity = x

        out = self.conv1(x)
        out = self.bn1(out)
        out = self.relu(out)

        out = self.conv2(out)
        out = self.bn2(out)
        out = self.relu(out)

        out = self.conv3(out)
        out = self.bn3(out)

        if self.downsample is not None:
            identity = self.downsample(x)

        out += identity
        out = self.relu(out)

        return out

class ResNet(nn.Module):

    def __init__(self, block, layers, num_classes=1000, zero_init_residual=False,
                 groups=1, width_per_group=64, replace_stride_with_dilation=None,
                 norm_layer=None):
        super(ResNet, self).__init__()
        if norm_layer is None:
            norm_layer = nn.BatchNorm2d
        self._norm_layer = norm_layer

        self.inplanes = 64
        self.dilation = 1
        if replace_stride_with_dilation is None:
            # each element in the tuple indicates if we should replace
            # the 2x2 stride with a dilated convolution instead
            replace_stride_with_dilation = [False, False, False]
        if len(replace_stride_with_dilation) != 3:
            raise ValueError("replace_stride_with_dilation should be None "
                             "or a 3-element tuple, got {}".format(replace_stride_with_dilation))
        self.groups = groups
        self.base_width = width_per_group
        self.conv1 = nn.Conv2d(3, self.inplanes, kernel_size=7, stride=2, padding=3,
                               bias=False)
        self.bn1 = norm_layer(self.inplanes)
        self.relu = nn.ReLU(inplace=True)
        self.maxpool = nn.MaxPool2d(kernel_size=3, stride=2, padding=1)
        self.layer1 = self._make_layer(block, 64, layers[0])
        self.layer2 = self._make_layer(block, 128, layers[1], stride=2,
                                       dilate=replace_stride_with_dilation[0])
        self.layer3 = self._make_layer(block, 256, layers[2], stride=2,
                                       dilate=replace_stride_with_dilation[1])
        self.layer4 = self._make_layer(block, 512, layers[3], stride=2,
                                       dilate=replace_stride_with_dilation[2])
        self.avgpool = nn.AdaptiveAvgPool2d((1, 1))
        self.fc = nn.Linear(512 * block.expansion, num_classes)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                nn.init.kaiming_normal_(m.weight, mode='fan_out', nonlinearity='relu')
            elif isinstance(m, (nn.BatchNorm2d, nn.GroupNorm)):
                nn.init.constant_(m.weight, 1)
                nn.init.constant_(m.bias, 0)

        # Zero-initialize the last BN in each residual branch,
        # so that the residual branch starts with zeros, and each residual block behaves like an identity.
        # This improves the model by 0.2~0.3% according to https://arxiv.org/abs/1706.02677
        if zero_init_residual:
            for m in self.modules():
                if isinstance(m, Bottleneck):
                    nn.init.constant_(m.bn3.weight, 0)
                elif isinstance(m, BasicBlock):
                    nn.init.constant_(m.bn2.weight, 0)

    def _make_layer(self, block, planes, blocks, stride=1, dilate=False):
        norm_layer = self._norm_layer
        downsample = None
        previous_dilation = self.dilation
        if dilate:
            self.dilation *= stride
            stride = 1
        if stride != 1 or self.inplanes != planes * block.expansion:
            downsample = nn.Sequential(
                conv1x1(self.inplanes, planes * block.expansion, stride),
                norm_layer(planes * block.expansion),
            )

        layers = []
        layers.append(block(self.inplanes, planes, stride, downsample, self.groups,
                            self.base_width, previous_dilation, norm_layer))
        self.inplanes = planes * block.expansion
        for _ in range(1, blocks):
            layers.append(block(self.inplanes, planes, groups=self.groups,
                                base_width=self.base_width, dilation=self.dilation,
                                norm_layer=norm_layer))

        return nn.Sequential(*layers)

    def _forward_impl(self, x):
        # See note [TorchScript super()]
        x = self.conv1(x)
        x = self.bn1(x)
        x = self.relu(x)
        x = self.maxpool(x)

        x = self.layer1(x)
        x = self.layer2(x)
        x = self.layer3(x)
        x = self.layer4(x)

        x = self.avgpool(x)
        x = torch.flatten(x, 1)
        x = self.fc(x)

        return x

    def forward(self, x):
        return self._forward_impl(x)

def _resnet(arch, block, layers, **kwargs):
    model = ResNet(block, layers, **kwargs)
    return model

def resnet18(**kwargs):
    return _resnet('resnet18', BasicBlock, [2, 2, 2, 2], **kwargs)


from typing import Tuple

import pytest

from y2021.day_9.main import puzzle_1, Point, calculate_top_bottom_left_right

@pytest.mark.parametrize(
    "input_, expected",
    [
        pytest.param(
            [
                "2199943210",
                "3987894921",
                "9856789892",
                "8767896789",
                "9899965678",
            ],
            15,
            id="example",
        ),
        pytest.param(
            [
                "00000000000000",
                "00000000000000",
                "00000000000000",
                "00000000000000",
                "00000000000000",
            ],
            0,
            id="plane",
        ),
        pytest.param(
            [
                "00000000000000",
                "00000010000000",
                "00000201000000",
                "00000020000000",
                "00000000000000",
            ],
            1,
            id="center",
        ),
        pytest.param(
            [
                "00000000000010",
                "00000000000001",
                "00000000000000",
                "00000000000001",
                "00000000000020",
            ],
            2,
            id="top-left corner and bot left-corner",
        ),
        pytest.param(
            [
                "01000000000000",
                "40000000000000",
                "00000000000000",
                "20000000000000",
                "06000000000000",
            ],
            2,
            id="top-left corner and bot left-corner",
        ),
    ],
)
def test_puzzle_1(input_, expected):
    assert puzzle_1(input_) == expected

_adjacent_0 = (1, 2, 3, 4)
_adjacent_1 = (4, 5, 6, 7)
_adjacent_2 = (4, None, None, 7)

@pytest.mark.parametrize(
    "point, adjacent, expected_is_lower_point, expected_adjacent_point",
    [
        pytest.param(
            Point(0, (0, 0), *_adjacent_0),
            _adjacent_0,
            True,
            min(_adjacent_0),
            id="Lower point and 1 as min adjacent point is 1",
        ),
        pytest.param(
            Point(6, (1, 1), *_adjacent_0),
            _adjacent_0,
            False,
            min(_adjacent_0),
            id="Not lower point and 1 as min adjacent point",
        ),
        pytest.param(
            Point(5, (1, 1), *_adjacent_1),
            _adjacent_1,
            False,
            min(_adjacent_1),
            id="Not lower point and 4 as min adjacent point",
        ),
        pytest.param(
            Point(3, (1, 1), *_adjacent_1),
            _adjacent_1,
            True,
            min(_adjacent_1),
            id="Lower point and 4 as min adjacent point",
        ),
    ],
)
def test_point_is_lower_and_adjacent(
    point: Point,
    adjacent: Tuple[int, int, int, int],
    expected_is_lower_point: bool,
    expected_adjacent_point: int,
):
    assert point.is_low_point() == expected_is_lower_point
    assert point.adjacent == expected_adjacent_point

_matrix = [
    [2, 1, 9, 9, 9, 4, 3, 2, 1, 0],
    [3, 9, 8, 7, 8, 9, 4, 9, 2, 1],
    [9, 8, 5, 6, 7, 8, 9, 8, 9, 2],
    [8, 7, 6, 7, 8, 9, 6, 7, 8, 9],
    [9, 8, 9, 9, 9, 6, 5, 6, 7, 8],
]

@pytest.mark.parametrize(
    "x, y, input_, expected",
    [
        pytest.param(
            0,
            0,
            _matrix,
            (None, 1, None, 3),
            id="x: 0, y: 0 expect Left: None, Right: 1, Top: None, Bottom: 3",
        ),
        pytest.param(
            0,
            4,
            _matrix,
            (None, 8, 8, None),
            id="x: 0, y: 0 expect Left: None, Right: 8, Top: 8, Bottom: None",
        ),
        pytest.param(
            9,
            0,
            _matrix,
            (1, None, None, 1),
            id="x: 0, y: 0 expect Left: 1, Right: None, Top: None, Bottom: 1",
        ),
        pytest.param(
            9,
            4,
            _matrix,
            (7, None, 9, None),
            id="x: 0, y: 0 expect Left: 7, Right: 9, Top: None, Bottom: None",
        ),
        pytest.param(
            1,
            1,
            _matrix,
            (3, 8, 1, 8),
            id="x: 0, y: 0 expect Left: 3, Right: 8, Top: 1, Bottom: 8",
        ),
    ],
)
def test_calculate_top_bottom_left_right(x, y, input_, expected):
    assert calculate_top_bottom_left_right(x, y, input_) == expected


import torch
import torch.nn as nn
import torch.nn.functional as F
import torch.optim as optim
import torchvision
from torch.utils.data import DataLoader, Dataset
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from torchvision.models import mobilenet_v2, MobileNet_V2_Weights
from tqdm import tqdm
from models import Conv_Model, MobileNet_Model

# constants
num_workers = 4
batch_size = 128
num_epochs = 15
learning_rate = 0.001
progress_bar = True

# get device
device = f'cuda:0' if torch.cuda.is_available() else 'cpu'
print(f"using device: {device}")
# EDA
# create dataset

def get_loaders(transforms):
	train_dataset = torchvision.datasets.CIFAR10('../datasets/', train=True, download=True, transform=transforms)
	_dataset = torchvision.datasets.CIFAR10('../datasets/', train=False, download=True, transform=transforms)

	# split nontraining dataset into validation and testing datasets
	nontrain_length = len(_dataset)//2
	valid_dataset, test_dataset = torch.utils.data.random_split(_dataset, [nontrain_length, nontrain_length], generator=torch.Generator().manual_seed(0))

	# create dataloaders
	train_loader = DataLoader(dataset = train_dataset, batch_size = batch_size, num_workers = 2, shuffle = False, persistent_workers = True)
	valid_loader = DataLoader(dataset = valid_dataset, batch_size = batch_size, num_workers = 2, shuffle = False, persistent_workers = True)
	test_loader = DataLoader(dataset = test_dataset, batch_size = batch_size, num_workers = 1, shuffle = False, persistent_workers = True)

	return train_loader, valid_loader, test_loader

def train(model, train_loader, valid_loader):
	# initialize optimizer
	optimizer = optim.Adam(model.parameters(), lr = learning_rate)

	# initialize loss function
	loss_func = nn.CrossEntropyLoss()

	# prev_loss is used to store validation losses -> training is stopped
	# once validation loss is above a 5-epoch rolling mean
	prev_loss = []

	# iterate for specified number of epochs
	for epoch in range(num_epochs):
		model.train()
		sum_loss = 0
		for batch_idx, (images, labels) in enumerate(tqdm(train_loader, disable = not progress_bar, desc = f'Epoch {epoch:02d}', ncols=60)):
			# send tensors to device
			images, labels = images.to(device), labels.to(device)

			# zero out gradients
			optimizer.zero_grad()

			# forward pass
			preds = model(images)

			# calculate loss
			loss = loss_func(preds, labels)
			sum_loss += loss.item()

			# backward pass
			loss.backward()

			# step optimizer
			optimizer.step()

		print(f'\tTrain loss =      {sum_loss/(batch_idx+1)/batch_size:.6f}')

		# validation loop
		model.eval()
		valid_loss = 0
		with torch.no_grad():
			for batch_idx, (images, labels) in enumerate(valid_loader):
				# send tensors to device
				images, labels = images.to(device), labels.to(device)

				# forward pass
				preds = model(images)

				# calculate loss
				loss = loss_func(preds, labels)
				valid_loss += loss.item()

		# append current loss to prev_loss list
		prev_loss.append(valid_loss/(batch_idx+1)/batch_size)

		print(f'\tValidation loss = {valid_loss/(batch_idx+1)/batch_size:.6f}')

		# # if valid_loss exceedes the 5-epoch rolling sum, break from training
		if valid_loss/(batch_idx+1)/batch_size > np.mean(prev_loss[-5:]):
			continue
			break

	return model, prev_loss

def test(model, test_loader):
	loss_func = nn.CrossEntropyLoss()

	sum_loss = 0
	num_correct = 0
	total = 0
	model.eval()
	with torch.no_grad():
		for batch_idx, (images, labels) in enumerate(tqdm(test_loader, disable = not progress_bar, desc = 'Testing', ncols=60)):
			# send tensors to device
			images, labels = images.to(device), labels.to(device)

			# forward pass
			preds = model(images)

			# calculate loss
			# loss = loss_func(preds.float(), F.one_hot(labels, num_classes = 10).float())
			loss = loss_func(preds, labels)
			sum_loss += loss.item()

			# calc number correct
			preds = torch.argmax(preds, dim = 1)
			num_correct += torch.sum(preds == labels)
			total += len(preds)

	print(f'Test loss: {sum_loss/(batch_idx+1)/batch_size:.6f}')
	print(f'Test acc:  {num_correct/total:.6f}')
	return sum_loss

# train and test each model
print('--------Conv---------')
normalize = torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.225, 0.225, 0.225])
convert = torchvision.transforms.ConvertImageDtype(torch.float)
transforms = torchvision.transforms.Compose([torchvision.transforms.ToTensor(),normalize, convert])
train_loader, valid_loader, test_loader = get_loaders(transforms)
Conv_model = Conv_Model(6*6*16, 128, 10)
Conv_model.to(device)

Conv_model, Conv_losses = train(Conv_model, train_loader, valid_loader)
Conv_loss = test(Conv_model, test_loader)

print('------MobileNet------')
train_loader, valid_loader, test_loader = get_loaders(MobileNet_V2_Weights.IMAGENET1K_V1.transforms())
MobileNet_model = MobileNet_Model()

MobileNet_model.to(device)

MobileNet_model, MobileNet_losses = train(MobileNet_model, train_loader, valid_loader)
MobileNet_loss = test(MobileNet_model, test_loader)

# affine transformations -> -45 to +45 degree rotationsm -20% to +20% translations in x and y directions, and 90% to 110% scale factor
affine = torchvision.transforms.RandomAffine(45, translate = (0.2, 0.2), scale = (0.9, 1.1))

# same transformations from earlier
normalize = torchvision.transforms.Normalize(mean=[0.485, 0.456, 0.406], std=[0.225, 0.225, 0.225])
convert = torchvision.transforms.ConvertImageDtype(torch.float)
transforms = torchvision.transforms.Compose([torchvision.transforms.ToTensor(), normalize, convert, affine])
train_loader, valid_loader, test_loader = get_loaders(transforms)

# create model and send to gpu
Conv_model = Conv_Model(6*6*16, 128, 10)
Conv_model.to(device)

Conv_model, Conv_losses = train(Conv_model, train_loader, valid_loader)
Conv_loss = test(Conv_model, test_loader)

'''
print('--------LSTM--------')
LSTM_model, LSTM_losses = train(LSTM_model, train_loader, valid_loader)
LSTM_loss = test(LSTM_model, test_loader)

print('--------GRU---------')
GRU_model, GRU_losses = train(GRU_model, train_loader, valid_loader)
GRU_loss = test(GRU_model, test_loader)

# plot losses over time
xx = np.arange(num_epochs)
plt.plot(xx, RNN_losses, label = 'RNN')
plt.plot(xx, LSTM_losses, label = 'LSTM')
plt.plot(xx, GRU_losses, label = 'GRU')
plt.legend()
plt.title('Validation Loss')
plt.show()

# store embeddings as dict
embeddings = {}
with open('glove.6B.50d.txt', 'r', encoding='cp437') as f:
	for row in f:
		vals = row.strip().split(' ')
		# the first value is the word, all the rest are embeddings
		embeddings[vals[0]] = np.array(vals[1:]).astype(np.float32)

def cosine_similarity(embeddings, w1, w2):
	# get embeddings for each word
	v1 = embeddings[w1]
	v2 = embeddings[w2]

	# calculate cosine similarity
	return (v1.dot(v2))/(np.linalg.norm(v1) * np.linalg.norm(v2))

def dissimilarity_metric(embeddings, w1, w2):
	# get embeddings for each word
	v1 = embeddings[w1]
	v2 = embeddings[w2]

	# calculate euclidean distance
	return np.linalg.norm(v1 - v2)
'''


import argparse
import os
import torch
import torch.nn as nn
import torch.nn.functional as F
import numpy as np
import math
import logging
import random
from tqdm import tqdm
from str2bool import str2bool
import itertools
from datetime import datetime
from transformers import BertTokenizer
from transformers import BertConfig
from transformers import AdamW
from model.retrieval_model import CrossModelv2
from dataset import InferenceDataset,AdversarialDataset
from batcher import EnsembleBatcher

from transformers.optimization import get_linear_schedule_with_warmup
from transformers.optimization import (
    get_cosine_with_hard_restarts_schedule_with_warmup,
    get_cosine_schedule_with_warmup,
    get_constant_schedule,
    get_constant_schedule_with_warmup,
    get_polynomial_decay_schedule_with_warmup
)
from torch.utils.data import DataLoader, Dataset

logging.basicConfig(format='%(asctime)s - %(levelname)s - %(name)s - %(message)s',
                    datefmt='%m/%d/%Y %H:%M:%S',
                    level=logging.INFO)
logger = logging.getLogger(__name__)

parser = argparse.ArgumentParser(description='Pre-training for Knowledge-Grounded Conversation')
parser.add_argument("--debug",default=False,type=str2bool,help='debug mode, using small dataset')
parser.add_argument('--predict',type=str2bool,default=True)
parser.add_argument("--prediction_name",type=str,default='output')

# data
parser.add_argument('--train_file', type=str, help='path to training dataset ')
parser.add_argument('--eval_file', type=str, default='path to evaluation dataset')
parser.add_argument("--rand_cand",type=int,default=0)
parser.add_argument("--inter_cand",type=int,default=0)
parser.add_argument("--keep",help='whether keep the original negative in the training set',type=str2bool,default=True)

# training scheme
parser.add_argument('--batch_size', type=int, default=8)
parser.add_argument('--eval_batch_size', type=int, default=32)
parser.add_argument('--num_steps', type=int, default=1000000)
parser.add_argument('--accum_step', type=int, default=4)
parser.add_argument('--lr', type=float, default=1e-5)
parser.add_argument('--clip', type=float, default=2.0)
parser.add_argument('--schedule', type=str, default='cosine')

parser.add_argument('--weight_decay', type=float, default=0.01)
parser.add_argument('--adam_epsilon', type=float, default=1e-8)
parser.add_argument('--warmup_steps', type=int, default=500)
parser.add_argument('--num_epochs', type=int, default=3)

parser.add_argument('--print_every', type=int, default=100)
parser.add_argument('--eval_every', type=int, default=2500)

# save
parser.add_argument("--dump_path",type=str,help='the path to dump experiment checkpoints and logs')
parser.add_argument('--exp_name', type=str, default='debug')
parser.add_argument('--log', type=str, default='log')
parser.add_argument('--seed', type=int, default=344)

# input
parser.add_argument("--max_utterance_length",type=int,default=32)
parser.add_argument("--max_context_length",type=int,default=256)
parser.add_argument("--max_cross_length",type=int,default=256)
parser.add_argument("--max_turn",type=str,default=15)

# model path
parser.add_argument('--cross_model_path', type=str,help='the trained checkpoints')
parser.add_argument("--vocab_path",type=str,default='')
parser.add_argument("--config_path",type=str,help='path to the model configuration')

# model architecture
#parser.add_argument("--embedding_dim",default=300)
parser.add_argument("--n_cluster",type=int,default=300)
#parser.add_argument("--n_layer",type=int,default=1)
parser.add_argument("--dropout",type=float,default=0.1)
parser.add_argument("--loss",type=str,default='bce')

args = parser.parse_args()
if args.debug:
    args.print_every=2
    args.eval_every=8
torch.cuda.empty_cache()
device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
torch.backends.cudnn.benchmark = True
random.seed(args.seed)
np.random.seed(args.seed)
torch.manual_seed(args.seed)
torch.cuda.manual_seed_all(0)
out_dir = os.path.join(args.dump_path, args.exp_name)
if not os.path.exists(out_dir):
    os.makedirs(out_dir)
args.out_dir=out_dir
logger.addHandler(logging.FileHandler(os.path.join(args.out_dir, "log"), 'w'))
logger.info("\nParameters:")
for attr, value in sorted(vars(args).items()):
    logger.info("{}={}".format(attr.upper(), value))

# Build dataset
time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.info("Create dataset begin... | %s " % time_str)

train_dataset=InferenceDataset(args.train_file,debug=args.debug)
train_loader=DataLoader(train_dataset,batch_size=args.batch_size,shuffle=True,collate_fn=InferenceDataset.collate_fn)
train_loader=itertools.cycle(train_loader)
eval_dataset=InferenceDataset(args.eval_file,debug=args.debug)
eval_loader=DataLoader(eval_dataset,batch_size=args.eval_batch_size,shuffle=False,collate_fn=InferenceDataset.collate_fn)

time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
logger.info("Create dataset end... | %s " % time_str)
logger.info('Eval Dataset {}'.format(len(eval_dataset)))
tokenizer=BertTokenizer.from_pretrained(args.vocab_path)
tokenizer.add_special_tokens({'pad_token':'[PAD]','unk_token':'[UNK]','eos_token':'[EOS]'})#,'additional_special_tokens':['INS','DEL','SEAR']})
batcher = EnsembleBatcher(tokenizer,args.max_cross_length,args.max_utterance_length,args.max_context_length,args.max_turn,device,)
configuration=BertConfig.from_json_file(args.config_path)
cross_model=CrossModelv2(configuration)
reloaded=torch.load(args.cross_model_path)
if configuration.vocab_size==30522:
    reloaded['bert.embeddings.word_embeddings.weight']=reloaded['bert.embeddings.word_embeddings.weight'][:30523,:]
elif configuration.vocab_size==21128:
    reloaded['bert.embeddings.word_embeddings.weight']=reloaded['bert.embeddings.word_embeddings.weight'][:21129,:]

cross_model.bert.resize_token_embeddings(len(tokenizer))
cross_model.load_state_dict(reloaded,strict=False)
cross_model.to(device)

no_decay = ["bias", "LayerNorm.weight"]
cross_grouped_parameters = [
    {
        "params": [p for n, p in cross_model.named_parameters() if not any(nd in n for nd in no_decay)],
        "weight_decay": args.weight_decay,
    },
    {
        "params": [p for n, p in cross_model.named_parameters() if any(nd in n for nd in no_decay)],
        "weight_decay": 0.0,
    },
]

cross_optimizer = AdamW(cross_grouped_parameters, lr=args.lr, eps=args.adam_epsilon)
total_steps = args.num_epochs * (len(train_dataset) / (args.batch_size * args.accum_step))
scheduler = get_linear_schedule_with_warmup(cross_optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=total_steps)
if args.schedule == 'linear':
    cross_scheduler = get_linear_schedule_with_warmup(cross_optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=total_steps)
elif args.schedule == 'cosine':
    cross_scheduler = get_cosine_schedule_with_warmup(cross_optimizer, num_warmup_steps=args.warmup_steps, num_training_steps=total_steps)

def train_step(global_step):
    step_loss={'rs_loss':0.0,'dis_loss':0.0}
    cross_model.train()
    torch.cuda.empty_cache()
    for _ in range(args.accum_step):
        context_list,candidate_list,label_list = next(train_loader)
        bs=len(context_list)
        batch_dict=batcher.crossv1(context_list,candidate_list,mode='train')
        input_id=batch_dict['input_id']
        segment_id=batch_dict['segment_id']
        cross_logit=cross_model(input_id, (input_id!=tokenizer.pad_token_id), segment_id)

        if args.loss=='ce':
            target=torch.zeros(bs,device=device,dtype=torch.long)
            rs_loss=F.cross_entropy(torch.softmax(cross_logit,dim=-1),target)
            #dis_loss=F.cross_entropy(poly_logit,target)*(1-args.alpha) + nn.KLDivLoss()(F.log_softmax(poly_logit/args.temperature,dim=1), F.softmax(cross_logit.detach()/args.temperature,dim=1))* (args.alpha*args.temperature*args.temperature)
        elif args.loss=='bce':
            target=torch.tensor(label_list,dtype=torch.long,device=device)
            rs_loss=F.binary_cross_entropy_with_logits(cross_logit,target.float())
        elif args.loss=='wbce':
            raise NotImplementedError

        rs_loss = rs_loss / args.accum_step
        step_loss['rs_loss']+=rs_loss.item()
        rs_loss.backward()

    grad_norm_cross = torch.nn.utils.clip_grad_norm_([p for p in cross_model.parameters() if p.requires_grad], args.clip)
    if grad_norm_cross >= 1e2:
        logger.info('WARNING : Exploding Cross Gradients {:.2f}'.format(grad_norm_cross))
    # if grad_norm_poly >= 1e2:
    #     logger.info('WARNING : Exploding Poly Gradients {:.2f}'.format(grad_norm_poly))
    cross_optimizer.step()
    cross_scheduler.step()
    cross_optimizer.zero_grad()

    if global_step % args.print_every == 0 and global_step != 0:
        time_str = datetime.now().strftime('%Y-%m-%d %H:%M:%S')
        logger.info("Step: %d \t| rs_loss: %.3f \t|feature_loss: %.3f \t| lr: %.8f \t| %s" % (
            global_step, step_loss['rs_loss'], step_loss['dis_loss'],cross_scheduler.get_lr()[0], time_str
        ))

def predict_step(global_step,best_metric):
    #hypothesis=[]
    count=0
    logits=[]
    labels=[]
    sessions=[]
    cross_model.eval()

    with torch.no_grad():
        for context_list,candidate_list,labellist in tqdm(eval_loader):
            bs=len(context_list)
            count+=bs
            batch_dict=batcher.crossv1(context_list,candidate_list,mode='train')
            input_id=batch_dict['input_id']
            segment_id=batch_dict['segment_id']
            logit=cross_model(input_id,(input_id!=tokenizer.pad_token_id),segment_id)
            logits.extend(logit.detach().cpu().tolist())
            labels.extend(labellist)
            if count%10000==0:
                logger.info('eval finish {}'.format(count))

    # for logit,label in zip(logits,labels):
    #     if label.count(1)!=0:
    #         one_session=[(logit[i],label[i]) for i in range(len(logit))]
    #         sessions.append(np.array(one_session))
    if args.predict:
        f=open(os.path.join(args.out_dir,args.prediction_name),'w')
    else:
        f=open(os.path.join(args.out_dir,'{}step_score_file'.format(global_step)),'w')
    for logit,label in zip(logits,labels):
        assert len(logit)==len(label)
        for i in range(len(logit)):
            f.write(str(logit[i])+'\t'+str(label[i])+'\n')
    f.close()

    one_session=[]
    golden=0
    for i in range(len(logits)):
        golden=labels[i].count(1)
        if golden>=1:
            sessions.append(np.array([(lo,la) for lo,la in zip(logits[i],labels[i])]))

    from metric import Metrics
    mymetric=Metrics(None)
    MAP,MRR,P1,recall1,recall2,recall5=mymetric.evaluate_all_metrics(sessions)
    logger.info("**********************************")
    logger.info("test results..........")
    logger.info("MAP {}".format(MAP))
    logger.info("MRR {}".format(MRR))
    logger.info("P1 {}".format(P1))
    logger.info("recall1 {}".format(recall1))
    logger.info("recall1 {}".format(recall2))
    logger.info("recall1 {}".format(recall5))

    if not args.debug and not args.predict and recall1 > best_metric:
        for file in os.listdir(args.out_dir):
            if file.endswith('crossmodel'):
                os.system('rm  ' + os.path.join(args.out_dir,file))
        cross_save_path=os.path.join(args.out_dir,'{}step_crossmodel'.format(global_step))
        torch.save(cross_model.state_dict(),cross_save_path)
    #logger.info("prediction precision={}".format(precision))
    return recall1

best_metric = -1
patience=0
if args.predict:
    predict_step(0,best_metric)
    #logger.info("predict result: the f1 between predict knowledge and response: {:.6f}".format(f1))
    exit()
for i in range(args.num_steps):
    train_step(i + 1)
    if (i + 1) % args.eval_every == 0 :
        metric=predict_step(i+1,best_metric)
        if metric > best_metric:
            best_metric=metric
            patience=0
        else:
            patience+=1


import os
from Group_basis import *
from sage.all import *
from time import time

# General parameters (Colo and Kohel, OSIDH, Section 6)
n=256
t=74
l=2
d_K=-4#-3 or -4
f=l**n
d=d_K*f**2

# h_1=#Cl(O_1)
if d_K==-3:
	h_1=(l-kronecker(d_K,l))//3
else:
	h_1=(l-kronecker(d_K,l))//2

L_q=[]
q=3
for j in range(t):
	while not Mod(d_K,q).is_square() or q==l:
		q=next_prime(q)
	L_q.append(q)
	q=next_prime(q)

e=gp.qfbprimeform(d,1)
Q=[gp.qfbred(gp.qfbprimeform(d,q)) for q in L_q]

## Step 1: find a basis
t1=time()
#B,L_orders=Basis(e,Q,h,[(l,n-1)])
# Using this function takes too much time and is unnecessary here, see below

# Adapted to the case of class groups
B,DL_Q_B=Basis_clg(e,Q,l,n,h_1)

t2=time()
print("Time Basis: {0} s".format(str(t2-t1)))

## Step 2: compute the matrix of discrete logarithms in this basis
L_factors=[(l,n-1)]+list(factor(h_1))
M=DL_matrix(e,Q,B,L_factors)
t3=time()
print("Time DL_matrix: {0} s".format(str(t3-t2)))

## Step 3: compute a basis of the relations lattice
if len(DL_Q_B)==1:#cyclic case
	b=[h_1*l**(n-1)]
else:
	b=[h_1*l**(n-2),l]
C=Lattice_basis(M,b)
t4=time()
print("Time Lattice_basis: {0} s".format(str(t4-t3)))
print("Total Time: {0} s".format(str(t4-t1)))

## Saving data

# Getting the coefficients of the quadratic forms appearing in B
def get_abc(q):
	r"""
	Computing the coefficients a, b, c of a quadratic form in pari type.

	INPUT: a pari/gp object gen representing a binary quadratic form (Qfb).

	OUTPUT: a tuple of python integer coefficients (a, b, c).
	"""
	L=str(q).split(",")
	a=int(L[0][4:])
	b=int(L[1])
	c=int(L[2][:-1])
	return (a,b,c)

Q_save=[get_abc(mfq) for mfq in Q]
B_save=[get_abc(mfb) for mfb in B]

# Saving the basis and the matrix
filename=os.path.join("Data_files","Relations_lattice_basis.txt")
with open(filename,"w",encoding="utf-8") as f:
	f.write("Group basis\n")
	r=len(B)
	for i in range(r):
		f.write(str(B_save[i])[1:-1]+"\n")
	f.write("Decomposition as a product of q_j:\n")
	for i in range(r):
		f.write(str(DL_Q_B[i])[1:-1]+"\n")
	f.write("Lattice basis\n")
	f.write(str(t)+"\n")
	for i in range(t):
		f.write(str(C[i])[1:-1]+"\n")


from PIL import Image, ImageDraw, ImageFont
import random
import io
import base64
import os
from pathlib import Path

def encode(key, msg):
    enc = list()
    for i in range(len(msg)):
        key_c = key[i % len(key)]
        enc.append(chr((ord(msg[i]) + ord(key_c)) % 256))
    return base64.urlsafe_b64encode("".join(enc).encode()).decode()

def decode(key, msg):
    dec = list()
    message = base64.urlsafe_b64decode(msg).decode()
    for i in range(len(message)):
        key_c = key[i % len(key)]
        dec.append(chr((256 + ord(message[i]) - ord(key_c)) % 256))
    return "".join(dec)

# Set the image size
width = 500
height = 500
key = "kEY"
BASE_DIR = Path(__file__).resolve().parent.parent

def createCaptcha() -> tuple:

    # Generate a random background color
    bg_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))

    # Create a new image with the same size as the background
    img = Image.new('RGB', (width, height), color=bg_color)

    # Generate a random text string
    alphanumber = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
    text = "".join(random.choice(alphanumber) for _ in range(5))

    # Choose a random font
    fontname = random.choice(os.listdir(os.path.join(BASE_DIR, "fonts")))
    fontsize = random.randint(80, 100)
    font = ImageFont.truetype(os.path.join(BASE_DIR, "fonts", fontname), size=fontsize)

    # Choose a random position for the text
    text_width, text_height = font.getsize(text)
    x = random.randint(0, max(0, width - text_width))
    y = random.randint(text_height, max(0, height - text_height))

    # Choose a random text color
    text_color = (random.randint(100, 255), random.randint(100, 255), random.randint(100, 255))

    # Draw the text on the image
    draw = ImageDraw.Draw(img)

    # Generate random shapes
    for _ in range(10):
        shape = random.choice(['square', 'circle'])
        shape_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        x1 = random.randint(0, max(0, width - 50))
        y1 = random.randint(0, max(0, height - 50))
        x2 = x1 + random.randint(20, 50)
        y2 = y1 + random.randint(20, 50)
        if shape == 'square':
            draw.rectangle((x1, y1, x2, y2), fill=shape_color)
        else:
            draw.ellipse((x1, y1, x2, y2), fill=shape_color)

    # Generate random lines
    for _ in range(10):
        line_color = (random.randint(0, 255), random.randint(0, 255), random.randint(0, 255))
        x1 = random.randint(0, max(0, width - 50))
        y1 = random.randint(0, max(0, height - 50))
        x2 = x1 + random.randint(20, 50)
        y2 = y1 + random.randint(20, 50)
        draw.line((x1, y1, x2, y2), fill=line_color, width=random.randint(2, 6))

    draw.text((x, y), text, font=font, fill=text_color)

    # Save the image to a buffer
    buffer = io.BytesIO()
    img.save(buffer, format='JPEG')

    # Convert the buffer to a base64-encoded string
    base64_str = base64.b64encode(buffer.getvalue()).decode('utf-8')

    #returning data
    return (encode(key, text), base64_str)

def checkCaptcha(token, value) -> bool:
    if decode(key, token) == value:
        return True
    else: return False


f = open("4-input.txt")
countDupes = 0

class assignmentPairs:
    def __init__(self, sections = ""):
        sections = sections.strip()
        sections = sections.split(",")
        elf1Range = sections[0].split("-")
        elf2Range= sections[1].split("-")
        self.elf1 = list(range(int(elf1Range[0]), int(elf1Range[1]) + 1))
        self.elf2 = list(range(int(elf2Range[0]), int(elf2Range[1]) + 1))
    def checkDupes(self):
        elf1 = set(self.elf1)
        elf2 = set(self.elf2)
        if elf1.issubset(elf2) or elf2.issubset(elf1):
            global countDupes
            countDupes += 1

def checkDupesImpl(elf1 = [], elf2 = []):
    elf1 = set(elf1)
    elf2 = set(elf2)
    if elf1.issubset(elf2) or elf2.issubset(elf1):
        global countDupes
        countDupes += 1

for line in f:
    s = assignmentPairs(line)
    s.checkDupes()

print(countDupes)


import io
import json
from PIL import Image

TILE_SIZE = 4096

# disable PIL maximum pixels limit
Image.MAX_IMAGE_PIXELS = None

# open image
image = Image.open('moon.jpg')

# get image size
width, height = image.size

# get scale factors
SCALE_FACTORS = [pow(2, factor) for factor in range(
    0, round(max(width, height) / TILE_SIZE))]

# get sizes levels
sizes = [{
    'width': int(width / factor),
    'height': int(height / factor)} for factor in SCALE_FACTORS]

# get tiles
tiles = {'width': TILE_SIZE, 'scaleFactors': SCALE_FACTORS}

id = 'http://localhost/'

# set base default content
content = {
    '@context': 'http://iiif.io/api/image/2/context.json',
    # server url
    '@id': id,
    'protocol': 'http://iiif.io/api/image',
    # size of original image
    'width': width,
    'height': height,
    'profile': ['http://iiif.io/api/image/2/level2.json'],
    # list of image sizes
    'sizes': sizes,
    'tiles': tiles
}

# create json file
with io.open('info.json', 'w', encoding='utf-8') as file:
    json.dump(content, file, ensure_ascii=False, indent=4)


import numpy as np

def save_pointcloud_to_ply(points_pos, points_color, ply_file_path):
    """
    Save the point cloud to ply file
    :param points_pos: 2D array of point clouds, dimension (num_points, 3)
    :param points_color: 2D array of point cloud color, (R, G, B) with value range [0, 1] for each component
    :param ply_file_path: the output file path
    """
    num_points = points_pos.shape[0]
    with open(ply_file_path, 'w', encoding='ascii') as ply_file:

        # Write headers
        headers = ["ply\n",
                  "format ascii 1.0\n",
                  "element face 0\n",
                  "property list uchar int vertex_indices\n",
                  "element vertex %d\n" % num_points,
                  "property float x\n",
                  "property float y\n",
                  "property float z\n",
                  "property uchar diffuse_red\n",
                  "property uchar diffuse_green\n",
                  "property uchar diffuse_blue\n",
                  "end_header\n"]

        for header in headers:
            ply_file.write(header)

        # Write point position and color
        for pt_idx in range(0, num_points):
            pt_pos = points_pos[pt_idx]
            pt_color = 255 * points_color[pt_idx]
            ply_file.write("%f %f %f %d %d %d\n" % (pt_pos[0], pt_pos[1], pt_pos[2], int(pt_color[0]), int(pt_color[1]), int(pt_color[2])))

def load_pointcloud_from_ply(ply_file_path):
    """
    Load the point cloud from ply file
    :param ply_file_path: ply file path
    :return: point cloud positions and colors
    """
    points_pos, points_color = None, None

    with open(ply_file_path, 'r', encoding='ascii') as seq_file:

        start_flag = False
        current_idx = 0

        # Read line-by-line to parsing attribute files
        while 1:
            lines = seq_file.readlines(100000)
            if not lines:
                break
            for line in lines:
                if line.startswith("element vertex"):
                    num_points = int(line.split(" ")[-1].strip())
                    points_pos = np.zeros((num_points, 3), dtype=np.float32)
                    points_color = np.zeros((num_points, 3), dtype=np.float32)
                    continue
                elif line.startswith("end_header"):
                    start_flag = True
                    continue

                if start_flag:
                    tokens = line.split(" ")
                    tokens[-1] = tokens[-1].strip()
                    x, y, z = float(tokens[0]), float(tokens[1]), float(tokens[2])
                    r, g, b = float(tokens[3]), float(tokens[4]), float(tokens[5])
                    points_pos[current_idx, :] = np.asarray((x, y, z), dtype=np.float32)
                    points_color[current_idx, :] = np.asarray((r, g, b), dtype=np.float32) / 255.0
                    current_idx += 1

    return points_pos, points_color


import getopt
import sys

from lofiwifi.source import Source
from lofiwifi.mix import Mix

# Default Values
title = "Test"
loop = r"D:\Documents\Scripts\LofiWifi\loop.mp4"
url = "https://soundcloud.com/thelofiwifistation/sets/head-bopping"

try:
    arguments, values = getopt.getopt(sys.argv[1:], "t:u:l:m:", ["TITLE", "URL", "LOOP", "MIX"])
    for arg, val in arguments:
        if arg in ("-t", "--TITLE"):
            title = val
        if arg in ("-u", "--URL"):
            url = val
        if arg in ("-l", "--LOOP"):
            loop = val
        if arg in ("-m", "--MIX"):
            print(("MIX (% s)") % (val))

    source = Source(url, title)
    source.Download()
    lofiwifi = Mix(
        source.track_list_data,
        source.tracks_directory,
        loop=loop,
        # captions=True,
        # audio_only=True,
        # audio_type='wav',
        # n_times=6,
        # extra_seconds=2,
        # keep_tracks=True,
        fade_in=2,
        fade_out=2,
    )
    lofiwifi.Create_Mix()

except getopt.error as err:
    # output error, and return with an error code
    print(str(err))


import vcf
import os
from joblib import Parallel, delayed
import math
import itertools
import multiprocessing
import csv

vcf_file_ending = ".vcf"

variant_cap = 5
use_variant_cap = False

include_filenames = False

def open_files(country, main_dir, res_dir):
    input_dir = "{}/{}/Vcf".format(main_dir, country)
    #input_dir = "{}/{}".format(main_dir, country)
    vars_dir = "{}/vars".format(res_dir)
    vars_outfile = "{}/{}_variants.txt".format(vars_dir, country)

    all_vars = {}
    if not os.path.exists(vars_dir):
        os.makedirs(vars_dir)

    print('Reading VCF files...\n')
    # Stores all the files with specified ending in the input directory

    vcf_filenames = [os.path.join(input_dir, file) for file in os.listdir(input_dir) if file.endswith(vcf_file_ending)]

    print(
        "Found {} Vcf files in {}, checking validity and processing records...\n".format(len(vcf_filenames), input_dir))
    total_files_num = len(vcf_filenames)
    n_jobs = 40

    with Parallel(n_jobs=n_jobs) as parallelizer:
        group_size = math.ceil(len(vcf_filenames) / n_jobs)

        # Splits the filenames list into groups of size group_size
        buffer_names = [iter(vcf_filenames)] * group_size
        split_names = itertools.zip_longest(fillvalue=None, *buffer_names)

        # Assings split_names groups to different
        task_iterator = (delayed(read_files)(files) for files in split_names)
        results = parallelizer(task_iterator)
        for r in results:
            for p, d in r.items():
                if p in all_vars:
                    all_vars[p].update(d)
                else:
                    all_vars[p] = d

        return process_variants(all_vars, total_files_num, vars_outfile)

def read_files(filenames):
    files_remaining = len(filenames)
    temp_vars = {}
    for filename in filenames:
        files_remaining -= 1

        print("{}: {} files remaining".format(multiprocessing.current_process().name, files_remaining))
        if filename is None:
            continue
        vcf_values = [line for line in vcf.Reader(open(filename), 'r')]
        new_vars = check_variants(filename.split("/")[-1].split(".vcf")[0], vcf_values)
        for p, d in new_vars.items():
            if p in temp_vars:
                temp_vars[p].update(d)
            else:
                temp_vars[p] = d
    return temp_vars

def check_variants(filename, vcf_vals):
    variants = {}

    for record in vcf_vals:
        ref = record.REF
        alts = record.ALT
        if None in alts:
            continue
        alt = str(alts[0])
        unformatted_geno = [sample['GT'] for sample in record.samples][0]
        genotype = unformatted_geno.replace('0', ref).replace('1', alt)
        if record.POS not in variants.keys():
            variants[record.POS] = {filename: genotype}
        else:
            variants[record.POS].update({filename: genotype})

    return variants

def process_variants(avar, total_files_num, outfile):
    count_ciel = variant_cap
    single_variant_files = {}
    multi_variant_files = {}

    for pos, variants in avar.items():
        num_variants = len(variants.values())
        if num_variants > count_ciel:
            continue
        elif num_variants == 1:
            single_variant_files[pos] = variants
        else:
            multi_variant_files[pos] = variants

    return print_variants(single_variant_files, multi_variant_files, total_files_num, outfile)

def print_variants(single, multiple, total_num, outfile):
    single_num = len(single.keys())
    multi_num = len(multiple.keys())
    print("Found {} single variant files".format(single_num))
    print("Found {} multi variant files".format(multi_num))
    print("Writing results to {}".format(outfile))

    with open(outfile, 'w') as w:
        w.write("Single Variant Files ({}/{})\n".format(single_num, total_num))

        for pos, vars in single.items():
            w.write("Position: {}".format(pos))
            for f, g in vars.items():
                w.write("\tFile: {}\tVariant: {}".format(f, g))
            w.write("\n\n")

        w.write("\n\nMulti Variant Files ({}/{})\n".format(multi_num, total_num))

        for pos, vars in multiple.items():
            w.write("Position: {}".format(pos))
            for f, g in vars.items():
                w.write("\tFile: {}\tVariant: {}".format(f, g))
            w.write("\n\n")

    return {"Single": single_num, "Multiple": multi_num, "Total": total_num}

def run_all_countries():
    exclude = ["Vcf"]
    main_dir = "/home/jurgen/Results/"

    #main_dir = "input"
    countries = [file for file in os.listdir(main_dir) if os.path.isdir(os.path.join(main_dir, file))]
    results = "results"
    if not os.path.exists(results):
        os.makedirs(results)
    with open("{}/variant_counts.txt".format(results), 'w') as w:
        writer = csv.writer(w, delimiter="\t")
        writer.writerow(["Country", "Single", "Multiple", "Total"])
        for c in countries:
            if c in exclude:
                continue
            print("\nPreparing variant counter for {}\n\n".format(c))
            counts_dict = open_files(c, main_dir, results)
            single_percent = counts_dict["Single"] / counts_dict["Total"]
            multi_percent = counts_dict["Multiple"] / counts_dict["Total"]
            line = [c, "{} ({})".format(counts_dict["Single"], single_percent),
                    "{} ({})".format(counts_dict["Multiple"], multi_percent), counts_dict["Total"]]
            writer.writerow(line)

run_all_countries()


import qrcode

# Create a QR code instance
qr = qrcode.QRCode(version=1, box_size=10, border=5)

# Add data to the QR code
data = "Hello, World!"
nom = "hasnaoui"
prenom = "yahya"
age = "23"
qr.add_data(data)
qr.add_data(nom)
qr.add_data(prenom)
qr.add_data(age)
qr.make(fit=True)

# Create an image from the QR code
img = qr.make_image(fill_color="black", back_color="white")

# Save the image
img.save("qrcode1.png")


from fastapi import FastAPI,APIRouter

# /api/v1/student
# student
import student
from config import settings

router = APIRouter(prefix="{}".format(settings.API_V1_STR),
                   tags=["core"]
                   )

@router.get("/")
async def hello():
    return {"message": "Hello World"}

app = FastAPI()

app.include_router(router)
app.include_router(student.router)


from compilador import compilador
from lector import lector

def adquirir_texto():
    texto = []

    with open('MONITOs.ASC', 'r') as archivo:
        for fila in archivo:
            texto.append("A"+fila)
    return texto

def procesar_texto(texto):

    text = []

    for t in texto:
        t = t.split()
        tmp = list(t[0])
        tmp[0] = " "
        t[0] = "".join(tmp)[1:]
        text.append(t)
        # print(t)

    return text

def adquirir_constantes_operador(texto):
    constantes = []
    for t in texto:
        t.append(' ')
        if t[1].lower() == 'equ':
            constantes.append([t[0].lower(), t[2]])

    return constantes

def checar_numero(operador, subrutinas_num):
    for c in subrutinas_num:
        if(operador[2].lower() in c[0]):
            # print(c,operador)

            return True,c[1]
    return False,None

def instrucciones_operando(texto):
    inst_operando = []
    for t in texto:
        t.append(' ')
        if '*' not in t[0]:
            if (t[1] != ' '):
                if(t[0] == ''):
                    inst_operando.append([t[1], t[2]])
    return inst_operando

def instrucciones_operando_num(texto, subrutinas_num):
    inst_operando = []
    numero = 0
    for t in texto:
        numero += 1
        t.append(' ')
        if '*' not in t[0]:
            if (t[1] != ' '):
                if(t[0] == ''):
                    booleano,num = checar_numero(t, subrutinas_num)
                    if booleano:
                        inst_operando.append([t[1], abs(numero - num)])
                    else:
                        inst_operando.append([t[1],t[2]])

    return inst_operando

def cambiar_constantes(instrucciones, constantes):
    constants = dict(constantes)
    for i in instrucciones:
        for c in constantes:
            if i[1].lower() in c:
                i[1] = constants[i[1].lower()]
                # print(i[1])
    # for t in instrucciones:
    #     print(t)
    return instrucciones

def cambiar_hexa(instrucciones, mnemonicos_opcode, subrutinas):
    for i in instrucciones:
        for m in mnemonicos_opcode:
            if i[0].lower() == m[0]:
                # print('{} \n'.format(i))
                if i[1] == ' ' or i[1].lower() in subrutinas:
                    if i[1].lower() in subrutinas:
                        if m[5] != '-- ':
                            i[0] = m[5]
                            # print(i, 'extendido')
                        elif m[7] == '-- ':
                            i[0] = m[6]
                            # print(i, 'inherente')
                        elif m[6] == '-- ':
                            i[0] = m[7]
                            # print(i, 'relativo')
                    elif m[7] == '-- ':
                        i[0] = m[6]
                        # print(i, 'inherente')
                    elif m[6] == '-- ':
                        i[0] = m[7]
                        # print(i, 'relativo')
                elif ',' in i[1]:
                    if 'x' in i[1] or 'X' in i[1]:
                        i[0] = m[3]
                        # print(i, 'indexado x')
                    if 'y' in i[1] or 'Y' in i[1]:
                        i[0] = m[4]
                        # print(i, 'indexado y')
                elif i[1][0] == '#':
                    i[0] = m[1]
                    # print(i, 'inmediado')
                elif len(i[1]) > 3:
                        i[0] = m[5]
                        # print(i, 'extendido')
                elif i[1][0] == '$':
                    i[0] = m[1]
                    # print(i, 'directo')
                else:
                    pass
                    # print(i, 'unknown')
    return instrucciones

def imprimir_chido(instrucciones):
    for it in instrucciones:
        if it[1] != ' ':
            if it[1][0] == '#':
                it[1] = it[1][1:]
            if it[1][0] == '$':
                it[1] = it[1][1:]
    return instrucciones
"""
    instrucciones1 = [j for i in instrucciones for j in i]
    print(instrucciones1)
"""

if __name__ == '__main__':
    compa = compilador()
    lector = lector()
    mnemonicos = compa.get_mnenomicos()
    text = procesar_texto(adquirir_texto())
    # obtener_operadores(text)
    constantes = adquirir_constantes_operador(text)
    subrutinas_num = lector.adquirir_subrutinas_numero(text)
    inst = lector.instrucciones_operando(text)
    subrutinas = lector.adquirir_subrutinas(text)
    hexa = cambiar_hexa(cambiar_constantes(inst, constantes), compa.get_mnenomicos_opcode(), subrutinas)
    inste = imprimir_chido(hexa)
    lol = instrucciones_operando_num(text, subrutinas_num)
    constantes_s = lector.adquirir_constantes(text)

    # Errores
    lector.error_identacion(text, subrutinas, constantes_s)
    lector.mnemonico_inexistente(text, compa.get_mnenomicos(), compa.get_directivas())
    lector.etiqueta_inexistente(text, subrutinas, constantes_s)
    lector.constante_inexistente(text, subrutinas, constantes_s)
    lector.no_tiene_end(text)
    lector.instruccion_c_operando(inst, compa.get_mnenomicos_opcode())
    lector.instruccion_n_operando(inst, compa.get_mnenomicos_opcode())

    directivas = compa.get_directivas()

    khe = []
    for l,i in zip(lol,inste) :
        if i[1].lower() in subrutinas:
            i[1]=l[1]
        if 'X' in str(i[1]):
            a = list(i[1])
            a.remove('X')
            a.remove(',')
            a.remove('#')
            a.remove('$')

            a = "".join(a)
            i[1] = a
            # print(a)

        if 'Y' in str(i[1]):
            b = list(i[1])
            b.remove('Y')
            b.remove(',')

            b = "".join(b)
            i[1] = b
            # print(b)

        if '$' in str(i[1]):
            c = list(i[1])
            c.remove('$')

            c = "".join(c)
            i[1] = c
            # print(c)

        if ',' in str(i[1]):
            d = list(i[1])
            d.remove(',')

            d = "".join(d)
            i[1] = d
            # print(d)

        if ' \' ' in str(i[1]):

            d = list(i[1])
            print(d)
            d.remove('\'')

            d = "".join(d)
            i[1] = d
            print(d)

        if i[0].lower() in directivas:
            i[0] = ''
        khe.append(i)
    instrucciones1 = [str(j) for i in khe for j in i]
    # print(instrucciones1)
    texto = "".join(instrucciones1)
    # print(texto)
    with open('muestra.hex','w') as arc:
        for i in range(0,len(texto),2):
            if i%32 == 0:
                print('\n{}>'.format(8000+(i//2)),end=' ',file=arc)
            print("{}{}".format(texto[i],texto[i+1]),end=' ',file=arc)


total = [2]
for i in range(999):
	temp = 0
	for j in range(len(total)):
		temp = total[j] + total[j] + temp
		total[j] = temp%10
		temp = temp // 10

	if temp > 0:
		total.append(temp)

print(sum(total))