#!/usr/bin/env python3

# Version 1.1
# Author Alexis Blanchet-Cohen
# Date: 13/04/2014

import argparse
import configparser
import glob
import os
import os.path
import subprocess
import sys
import util

parser = argparse.ArgumentParser(description="Separates BAM files by strand.")
parser.add_argument("-s", "--scriptsDirectory", help="Scripts directory. DEFAULT=separatebamsbystrand", default="separatebamsbystrand")
parser.add_argument("-i", "--inputDirectory", help="Input directory with BAM files. DEFAULT=../results/star", default="../results/star")
parser.add_argument("-o", "--outputDirectory", help="Output directory with bedgraph files. DEFAULT=../results/star", default="../results/star")
parser.add_argument("-e", "--extension", help="Extension to identify BAM files to sort. DEFAULT=.bam", default=".bam")
parser.add_argument("-q", "--submitJobsToQueue", help="Submit jobs to queue immediately.", choices=["yes", "no", "y", "n"], default="no")
args = parser.parse_args()

# If not in the main scripts directory, cd to the main scripts directory, if it exists.
util.cdMainScriptsDirectory()

# Read samples files
samples = util.getSamples()

# Process command line arguments.
scriptsDirectory = os.path.abspath(args.scriptsDirectory)
inputDirectory = os.path.abspath(args.inputDirectory)
outputDirectory = os.path.abspath(args.outputDirectory)
extension = args.extension

# Check if the inputDirectory exists, and is a directory.
util.checkInputDirectory(inputDirectory)

# Create script and output directories, if they do not exist yet.
util.makeDirectory(outputDirectory)
util.makeDirectory(scriptsDirectory)

# Read configuration files
config = util.readConfigurationFiles()

header = config.getboolean("server", "PBS_header")

# cd to scripts directory
os.chdir(scriptsDirectory)

# Function to check BAM file names.
# Create a symbolic link to accepted_hits.bam named sampleName.bam. Useful for TopHat output, which is named accepted_hits.bam, by default
#def createSymbolicLinks():
#    for sample in samples:
#        command = "ln -fs " + os.path.join(inputDirectory, sample, "accepted_hits.bam") + " " + os.path.join(inputDirectory, sample, sample + ".bam")
#        print(command)
#        subprocess.call(command, shell=True)

#if (args.symbolicLinks.lower() == "yes") | (args.symbolicLinks.lower() == "y"):
#    createSymbolicLinks()

strands = ["positive", "negative"]

# Write script for each sample and each strand
for sample in samples:
    for strand in strands:
        # Create script file.
        scriptName = "separatebamsbystrand_" + sample + "_" + strand + ".sh"
        script = open(scriptName, "w")
        if header:
            util.writeHeader(script, config, "separatebambystrand")
        # BAM to bedgraph
        script.write("separatebambystrand.py" + " \\\n")
        script.write("--input_bam " + os.path.relpath(os.path.join(inputDirectory, sample, sample + ".bam")) + " \\\n")
        script.write("--strand " + strand + " \\\n")
        script.write("&> " + scriptName + ".log")

if (args.submitJobsToQueue.lower() == "yes") | (args.submitJobsToQueue.lower() == "y"):
    subprocess.call("submitJobs.py", shell=True)


import requests
import simplejson as js
from datetime import datetime
import pystrava
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

print('Start!')

# Load settings --------------------------------------------------------------------------------------------------------
with open('setup.ini') as fid:
    initial_settings = js.load(fid)
saving_path = initial_settings['saving_path']

# Connect to strava ----------------------------------------------------------------------------------------------------
stcon = pystrava.strava_user(base_url = 'https://www.strava.com/api/v3',client_id=initial_settings['client_id'],
                                      client_secret=initial_settings['client_secret'],
                                      code=initial_settings['code'])
# Get token ------------------------------------------------------------------------------------------------------------
stcon.get_token()

# Download all activity into a JSON ------------------------------------------------------------------------------------
download_all_enabled = False
if download_all_enabled:
    act_list = stcon.get_activity_list()
    with open(saving_path + '/activity_list.json','w+') as fid:
        js.dump(act_list,fp=fid)
else:
    with open(saving_path + '/activity_list.json','r') as fid:
        act_list = js.load(fp=fid)

# Create a DataFrame from the JSON
act_data = pd.DataFrame(act_list)
act_data['start_date_utc'] = act_data.start_date.apply(lambda x: datetime.strptime(x, '%Y-%m-%dT%H:%M:%SZ').timestamp())

# Get activity details -------------------------------------------------------------------------------------------------
act_ids = [2041949158,2006283792,1990887172]
act_streams_dict = stcon.get_activity_streams(act_ids)

# Create dict of DF from activity details
act_streams_dict_df = dict()
for k,v in act_streams_dict.items():
    temp_dict = dict()
    for df_k, df_v in v.items():
        temp_dict[df_k] = df_v['data']

    act_streams_dict_df[k] = pd.DataFrame(temp_dict)

id_sel = str(2041949158)
ax = act_streams_dict_df[id_sel].plot(x='time',y='heartrate',color='C1')
ax.legend(['heartrate'], loc=2)
ax2 = ax.twinx()
act_streams_dict_df[id_sel].plot(x='time',y='altitude',ax=ax2,color='C2')
ax2.legend(['altitude'], loc=1)
ax.grid(True,which='major')
plt.show(block=False)

# Get all preferred segments -------------------------------------------------------------------------------------------
seg_list = stcon.get_starred_segments()
with open(saving_path + '/star_seg_list.json', 'w+') as fid:
    js.dump(seg_list, fp=fid)

print('Finish!')


import jwt
import datetime
from app import app
from models.Article import ArticleModel

class jwtclass:

 def encode_auth_token(self,user_id):
     try:
        payload = {
            'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=5),
            'iat': datetime.datetime.utcnow(),
            'sub': user_id
        }
        return jwt.encode(
            payload,
            app.config.get('SECRET_KEY'),
            algorithm='HS256'
        )
     except Exception as e:
        return e

 def decode_auth_token(self,auth_token):
    """
    Decodes the auth token
    :param auth_token:
    :return: integer|string
    """

    try:
        payload = jwt.decode(auth_token, app.config.get('SECRET_KEY'))
        return payload['sub']
    except jwt.ExpiredSignatureError:
        return 'Signature expired. Please log in again.'
    except jwt.InvalidTokenError:
        return 'Invalid token. Please log in again.'


import tensorflow as tf
import latticex.rosetta as rst

X = tf.Variable([[1.0, 2.0], [3.0, 4.0 ], [5.0, 6.0]],  name='x')
Y = tf.Variable([[7.0, 8.0], [9.0, 10.0], [11.0, 12.0]],  name='y')
Z = tf.matmul(X, Y, transpose_a=True)

Y1 = tf.Variable([[7.0, 8.0]], name="y1")
Z1 = tf.matmul(X, Y1, transpose_b=True)

Y2 = tf.Variable([[7.0, 8.0, 9.0]], name="y2")
Z2 = tf.matmul(X, Y2, transpose_a=True, transpose_b=True)

try:
    sess = tf.Session()
    sess.run(tf.global_variables_initializer())
    print(sess.run(Z))
    print(sess.run(Z1))
    print(sess.run(Z2))
    print("Pass")
except Exception:
    print("Fail")

# try:
#     train_step = tf.train.GradientDescentOptimizer(0.01).minimize(Z)
#     print("Pass")
# except Exception:
#     print("Fail")

Writer = tf.summary.FileWriter("log/matmul2", tf.get_default_graph())
Writer.close()
rst.deactivate()


class Word:

    def __init__(self, text, part):
        self.text=text
        self.part=part

class Sentence:

    def show(self, list):
        str=''

        for index in self.content:
            if index <= len(list):
                str += list[index].text + ' '

        print(str)

    def show_parts(self, list):
        arr=[]
        for index in self.content:
            if index <= len(list):
                arr.append(list[index].part)

        print(set(arr))

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

words = [["собака", "сущ"],["ела", "глагол"], ["колбасу", "сущ"], ["вечером", "наречие"]]
list=[]
for item in words:
    list.append(Word(item[0], item[1]))

dog = Sentence([0,2,6,1])
dog.show(list)
dog.show_parts(list)


import sqlite3

def setup():
    # Connect to or create the database
    conn = sqlite3.connect('example.db')

    # Create a cursor
    cursor = conn.cursor()

    # Create a tablex
    cursor.execute('''
        CREATE TABLE IF NOT EXISTS replay (
            id INTEGER PRIMARY KEY,
            nodeTraversal TEXT,
            ksuid CHAR(100) NOT NULL
        )
    ''')
    return conn

def insert(nodeTraversal, ksu_id):
    # Insert values into the table
    conn = setup()
    cursor = conn.cursor()
    # rows = fetchallRows()
    rows = select(ksu_id);
    if(len(rows) > 0):
        query = 'Update replay set nodeTraversal =?  where ksuid = ?'
        cursor.execute(query, (nodeTraversal, ksu_id))
    else:
        cursor.execute('INSERT INTO replay (nodeTraversal, ksuid) VALUES (?, ?)', (nodeTraversal, ksu_id))
    conn.commit()

    # Close the connection
    conn.close()

def select(ksu_id):
    conn = setup()
    cursor = conn.cursor()
    query = 'SELECT nodeTraversal FROM replay where ksuid =?'
    cursor.execute(query, [ksu_id])
    rows = cursor.fetchall()

    # Close the connection
    conn.close()
    return rows

def fetchallRows():
    conn = setup()
    cursor = conn.cursor()
    query = 'SELECT ksuid, nodeTraversal FROM replay'
    cursor.execute(query)
    rows = cursor.fetchall()
    print(rows)
    # Close the connection
    conn.close()
    return rows


import itertools
import logging
import locale
import re
from typing import List
import json
import os
from ordered_set import OrderedSet

from spacy.lang.lex_attrs import is_digit, like_num
from spacy.tokens import Token

from .number_lookup import maps, prefixes
from .unit_classifier.unit_disambiguator import unit_disambiguator

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

from pathlib import Path
def get_project_root() -> Path:
    return Path(__file__).parent

root_path=str(get_project_root())
if not os.path.exists(os.path.join(root_path+"/unit_models")):
    import zipfile
    with zipfile.ZipFile(os.path.join(root_path+"/unit_models.zip"), 'r') as zip_ref:
        zip_ref.extractall(os.path.join(root_path))
disambiguator = unit_disambiguator()

AMBIGUOUS_FORMS = ["c", "¥", "kn", "p", "R", "b", "'", "′", '"', '″', "C", "F", "kt", "B", "P", "dram", "pound", "pounds", "a"]
AMBIGUOUS_UNITS = ["celsius", "pound sterling", "japanese yen", "pixel", "poise", "barn", "bel", "dram", "minute", "south african rand",
                   "bit", "cent", "knot", "penny", "chinese yuan", "croatian kuna", "inch", "kilobyte", "acre", "second", "armenian dram",
                   "year", "kiloton", "kibibyte", "coulomb", "farad", "roentgen", "byte", "fahrenheit", "foot", "pound-mass", "point"]

def get_project_root() -> Path:
    return Path(__file__).parent

class NormalizeException(Exception):
    pass

def is_num(s):
    try:
        locale.atoi(s)
    except ValueError:
        try:
            locale.atof(s)
        except ValueError:
            return False
    return True

def preprocess_number(text: List):
    remove_words = ["a"]
    for word in remove_words:
        if word in text:
            text.remove(word)
    res = []
    # a fraction can't be a scale if its at the first position. e.g. halve a million
    if len(text) > 1:
        res.append(str(maps["fractions"].get(text[0], text[0])))
        res.extend(text[1:])

    elif text[0] in maps["fractions"].keys(): # e.g. half turn (the fractions from number_lookup.py)
        res.append("".join(str(maps["fractions"][text[0]])))

    elif re.findall(r"([0-9,.]*[0-9]+)×?([e|E])([+|−]?)(\d*)\b", text[0]): # e.g. 6.022E20, 6.022E+20, 6.022E−20 etc.
        res.extend([re.sub(r"([0-9,.]*[0-9]+)×?([e|E])([+|−]?)(\d*)\b", r"\1×e\3\4", text[0])])

    elif re.findall(r"([0-9,.]*[0-9]+)×(10)([+|−]+)(\d*)\b", text[0]): # e.g. 1.06×10−10
        res.extend([re.sub(r"([0-9,.]*[0-9]+)×(10)([+|−]+)(\d*)\b", r"\1×e\3\4", text[0])])
    else:
        res.extend(text)
    return res

def normalize_number(text: List):
    text = preprocess_number(text)

    if "×" in text[0]: # scientific notation e.g. 5.067×10−4
        number_1 = locale.atof(text[0][:text[0].index("×")])
        if text[0][text[0].index("×")+1:] in maps["scientific_notation"]:
            number_2 = maps["scientific_notation"][text[0][text[0].index("×")+1:]]
        elif "e−" in text[0][text[0].index("×")+1:]: # 6.022×e-20
            number_2 = 10**locale.atof("-"+text[0][text[0].index("×")+3:])
        elif "e" in text[0][text[0].index("×")+1:]:  # 6.022×e+20
            number_2 = 10**locale.atof(text[0][text[0].index("×")+2:])
        elif "^−" in text[0][text[0].index("×")+1:]:  # 1.06×10^−10
            number_2 = 10**locale.atof("-"+text[0][text[0].index("−")+1:])
        elif "^" in text[0]: # 1.06×10^10
            number_2 = 10**locale.atof(text[0][text[0].index("^")+1:])
        else: # 1.06×10
            number_2 = locale.atof(text[0][text[0].index("×")+1:])
        return number_1*number_2

    if len(text) == 1 and is_num(text[0]):
        return locale.atof(text[0])

    if len(text) == 1 and not is_digit(text[0]):
        if text[0] in maps["string_num_map"]:
            # return maps["string_num_map"][text[0]]
            return locale.atof(str(maps["string_num_map"][text[0]]))
        if any(char in ('⁄', '/') for char in text[0]): # 1/16 of a US pint, 1⁄32 of a US quart, and 1⁄128 of a US gallon
            if '⁄' in text[0]:
                return int(text[0][:text[0].index("⁄")])/int(text[0][text[0].index("⁄")+1:])
            return int(text[0][:text[0].index("/")])/int(text[0][text[0].index("/")+1:])
        return locale.atof(str(maps["scales"][text[0]]))

    if len(text) == 2 and like_num(text[0]) and not like_num(text[1]):
        if text[1] == "m":
            return locale.atof(text[0]) * maps["scales"]["million"]
        return locale.atof(text[0]) * maps["scales"][text[1]]

    if len(text) == 2 and not is_digit(text[0]) and like_num(text[1]): # five, 20
        if text[0] in maps["string_num_map"]:
            return [locale.atof(str(maps["string_num_map"][text[0]])), locale.atof(text[1])]

    if len(text) == 2 and like_num(text[0]) and like_num(text[1]): # e.g. [66.5, 107] from '66.5 x 107 mm'
        return [locale.atof(text[0]), locale.atof(text[1])]

    current = 0.0
    result = 0.0
    for i, word in enumerate(text):
        if is_digit(word) and i != 0:
            raise NormalizeException("Mixing words and numbers (e.g. four 100 thousand) is not supported")
        if word not in (maps["scales"].keys() | maps["string_num_map"].keys() | maps["fractions"].keys()) and not is_digit(word.replace(".","")):
            raise NormalizeException(f"Number lookup for {word} failed.")
        if word in maps["scales"].keys() | maps["fractions"].keys():
            if word in maps["scales"]:
                scale = maps["scales"][word]
            else:
                scale = maps["fractions"][word]
            incr = 0
        else:
            scale = 1
            if word in maps["string_num_map"]:
                incr = maps["string_num_map"][word]
            elif is_digit(word.replace(".","")):
                incr = locale.atof(word)
        current = incr + current * scale
        if scale > 100:
            result += current
            current = 0
    return result + current

def normalize_number_token(tokens: List[Token]):
    lemmatized_text = [token.lemma_ for token in tokens]
    if any(token._.one_of_number and token in maps["string_num_map"] for token in tokens):
        lemmatized_text = [tokens[0].lemma_]
    try:
        normalized = normalize_number(lemmatized_text)
        return normalized
    except:
        logging.warning('\033[93m' + f"Cant normalize {lemmatized_text}" + '\033[0m')
        return None

def normalize_bound_token(tokens: List[Token]):
    # if not specified assume equality
    bound = "="
    if len(tokens) > 1 and " ".join(token.lemma_.lower() for token in tokens) in maps["bounds"]:
        bound = maps["bounds"][" ".join(token.lemma_ for token in tokens)] # e.g. "up to"
    else:
        for token in tokens:
            if token.lemma_.lower() in maps["bounds"]:
                bound = maps["bounds"][token.lemma_.lower()]
    return bound

def match_text_to_unit(unit_text, units_dict):
    if any(unit_text==key for key in units_dict):
        return unit_text, [unit_text]

    for key in units_dict.keys():
        if units_dict[key].get("surfaces") is not None and unit_text in units_dict[key].get("surfaces"):
            return key, [key]
        if units_dict[key].get("symbols") is not None and unit_text in units_dict[key].get("symbols"):
            return key, [key]
        if units_dict[key].get("prefixes") is not None:
            for symbol in units_dict[key].get("symbols"):
                if unit_text in [(prefix+symbol) for prefix in units_dict[key].get("prefixes")]:
                    index_prefix = [(unit_text in (prefix+symbol) ) for prefix in units_dict[key].get("prefixes")].index(True)
                    return prefixes[units_dict[key].get("prefixes")[index_prefix]]+key, [key]

    return unit_text, []

def check_ambiguous_unit(text, norm_unit, unit):
    """"use BERT classifier to diambiguate unit if it is ambiguous"""
    if unit=="pounds": # for pounds and pound, use the surface form "pound" in both cases to do disambiguation
        unit="pound"
    if norm_unit in AMBIGUOUS_UNITS and unit in AMBIGUOUS_FORMS:
        return disambiguator.disambiguate(text, unit)
    return norm_unit

def normalize_unit_token(tokens: List[Token], text, recursive=False): # return normalized unit + index of the token + whether in the unit.json (i.e scientific) + list of keys
    if not tokens or (len(tokens)==1 and tokens[0].text=="of"): # unit-less quantity
        return "-", None, False, [], False

    if len(set(token.text for token in tokens)) != 1 and len(tokens) > 1:
        tokens = [token for i,token in enumerate(tokens) if token.text != tokens[(i+1)%len(tokens)].text]

    if any(token._.one_of_noun for token in tokens):
        i = [token._.one_of_noun for token in tokens].index(True)
        def accept_token(token):
            return not token.is_punct and not (token.text in ["this","that","which","what","who","whose","whom"]) or token.text in ["/", "-"]
        return " ".join([t.lemma_ for t in list(itertools.takewhile(accept_token, list(tokens[i].subtree))) if not t.is_stop]), -1, False, [], False

    norm_units = ""
    index = None
    in_unit_list = True
    path = get_project_root()
    file_name = os.path.join(path, "unit.json")
    with open(file_name, "r", encoding="utf8") as f:
        units_dict = json.load(f)

    if len(tokens) > 1: # first look if the tokens as whole are present in the units_dict
        index = -1 # whole list is unit
        token_text = " ".join(list(OrderedSet(token.lemma_.upper() if token.text.isupper() else token.lemma_ for token in tokens))) # if not token.is_punct)))
    else:
        index = 0 # single token
        token_text = tokens[0].text # use text because of e.g. 'A/cm2 (a/cm2)

    if "/" in token_text: # e.g. 'kV/cm'
        connector = token_text.index("/")
        first_unit, first_key = match_text_to_unit(token_text[:connector], units_dict)
        second_unit, second_key = match_text_to_unit(token_text[connector+1:], units_dict)

        return first_unit+" per "+second_unit, index, in_unit_list, [first_key,second_key], False

    if any(token_text==key for key in units_dict):
        return token_text, index, in_unit_list, [token_text], False # lemmatized unit is already normalized, e.g. 'mile' -> 'mile'

    for key in units_dict:
        if units_dict[key].get("surfaces") is not None and token_text in units_dict[key].get("surfaces"):
            return check_ambiguous_unit(text, key, token_text), index, in_unit_list, [key], False # e.g. 'light year' -> 'light-year'
        if units_dict[key].get("symbols") is not None and token_text in units_dict[key].get("symbols"):
            return check_ambiguous_unit(text, key, token_text), index, in_unit_list, [key], False # e.g. '%' -> 'percentage'

    if any(token.text in ["per"] for token in tokens): # '$ per square foot' -> 'dollar per square foot'
        token_text = [token.text for token in tokens]
        i = token_text.index("per")
        token_text[:i] = [normalize_unit_token(tokens[:i], text)[0]] # C$ -> canadian dollar
        first_key = normalize_unit_token(tokens[:i], text)[-2]
        j = token_text.index("per")
        _, index_slice, _, _, slice_unit = normalize_unit_token(tokens[i+1:], text, True)
        token_text[j+1:] = [normalize_unit_token(tokens[i+1:], text, True)[0]]
        second_key = normalize_unit_token(tokens[i+1:], text, True)[-2]

        if slice_unit:
            return " ".join(token_text), index_slice+j+1, in_unit_list, [first_key,second_key], True
        if index_slice is not None:
            if index_slice+j+1==len(tokens):
                return " ".join(token_text), index, in_unit_list, [first_key,second_key], False
            return " ".join(token_text), -1, in_unit_list, [first_key,second_key], False
        return " ".join(token_text[:j]), -1, in_unit_list, [first_key,second_key], False

    if any(token.text in ["a", "an"] for token in tokens): # 'barrels a day', '£ an hour'
        token_text = [token.text for token in tokens]
        i = token_text.index("a") if "a" in token_text else token_text.index("an")
        token_text[:i] = [normalize_unit_token(tokens[:i], text)[0]] # C$ -> canadian dollar
        first_key = normalize_unit_token(tokens[:i], text)[-2]
        j = token_text.index("a") if "a" in token_text else token_text.index("an")
        token_text[j] = "per"
        _, index_slice, _, _, slice_unit = normalize_unit_token(tokens[i+1:], text, True) # [$, a, month, mortgage]
        token_text[j+1:] = [normalize_unit_token(tokens[i+1:], text, True)[0]]
        second_key = normalize_unit_token(tokens[i+1:], text, True)[-2]

        if slice_unit:
            return " ".join(token_text), index_slice+j+1, in_unit_list, [first_key,second_key], True
        if index_slice is not None:
            if index_slice+j+1==len(tokens):
                return " ".join(token_text), index, in_unit_list, [first_key,second_key], False
            return " ".join(token_text), -1, in_unit_list, [first_key,second_key], False
        return " ".join(token_text[:j]), -1, in_unit_list, [first_key,second_key], False

    if len(tokens) > 2:
        index = 1
        token_text = " ".join([token.lemma_ for token in tokens[:2] if token.text not in ["-"]]) # e.g. [US, $, contract] -> US $

        if token_text in units_dict.keys():
            return token_text, index, in_unit_list, [token_text], True

        for key in units_dict.keys():
            if units_dict[key].get("surfaces") is not None and token_text in units_dict[key].get("surfaces"):
                return check_ambiguous_unit(text, key, token_text), index, in_unit_list, [key], True
            if units_dict[key].get("symbols") is not None and token_text in units_dict[key].get("symbols"):
                return check_ambiguous_unit(text, key, token_text), index, in_unit_list, [key], True

    for key in units_dict.keys():
        if units_dict[key].get("symbols") is not None:
            if any(token.text in units_dict[key].get("symbols") for token in tokens):
                index = [token.text in units_dict[key].get("symbols") for token in tokens].index(True)
                return check_ambiguous_unit(text, key, tokens[index].text), index, in_unit_list, [key], False # e.g. [G, data, month] -> generation wireless
            if units_dict[key].get("prefixes") is not None:
                for i, token in enumerate(tokens):
                    for symbol in units_dict[key].get("symbols")+units_dict[key].get("surfaces"):
                        if token.text in [(prefix+symbol) for prefix in units_dict[key].get("prefixes")]:
                            index_prefix = [(token.text in (prefix+symbol)) for prefix in units_dict[key].get("prefixes")].index(True)
                            return prefixes[units_dict[key].get("prefixes")[index_prefix]]+key, i, in_unit_list, [key], False # e.g. GHz -> gigahertz
                        if token.text in [(prefix+symbol) for prefix in prefixes.values()]:
                            index_prefix = [(token.text in (prefix+symbol)) for prefix in prefixes.values()].index(True)
                            return list(prefixes.values())[index_prefix]+key, i, in_unit_list, [key], False # e.g. gigawatts -> gigawatt
                        if token.lemma_ in [(prefix+symbol) for prefix in prefixes.values()]:
                            index_prefix = [(token.lemma_ in (prefix+symbol)) for prefix in prefixes.values()].index(True)
                            return list(prefixes.values())[index_prefix]+key, i, in_unit_list, [key], False # e.g. gigawatt -> gigawatt

    # cases like '40 yard line' where both 'yard' and 'line' are in unit.json
    index_loop = len(tokens)
    key_token = None
    for key in units_dict.keys():
        if units_dict[key].get("surfaces") is not None and any(token.lemma_ in units_dict[key].get("surfaces") for token in tokens):
            index_new = [token.lemma_ in units_dict[key].get("surfaces") for token in tokens].index(True)
            if index_new < index_loop:
                index_loop = index_new
                key_token = key
    if key_token:
        return key_token, index_loop, in_unit_list, [key_token], bool(len(tokens)!=index_loop+1)

    # check if some of the tokens is present as a key in the unit.json file
    norm_units = [token.lemma_.upper() if token.text.isupper() else token.lemma_ for token in tokens if token.lemma_ in units_dict.keys()]

    if not norm_units:
        in_unit_list = False

    if not norm_units and any(token.pos_ in ["NOUN", "PROPN"] or token._.unit for token in tokens): # skip e.g. "cheaper", extract parts per million
        if not recursive:
            norm_units = [token.lemma_.upper() if token.text.isupper() else token.lemma_ for token in tokens if token.pos_ in ["NOUN", "PROPN", "ADJ"]]
            if norm_units: # e.g. 'students' -> 'student'
                if len(tokens) == 1:
                    index = 0
                else:
                    index = -1 # whole list is unit
        else: # e.g. $ 70 - a - share price
            norm_units = [token.lemma_.upper() if token.text.isupper() else token.lemma_ for token in tokens if token.pos_ in ["NOUN", "PROPN"] or token._.unit]
            if norm_units:
                norm_units = [norm_units[0]]
                index = 0
                return " ".join(norm_units) if norm_units else "-", index, in_unit_list, [], False

    return " ".join(norm_units) if norm_units else "-", index, in_unit_list, [], False


import pygame

import math
from random import randint, choice

class Asteroid(pygame.sprite.Sprite):
    def __init__(self):
        super().__init__()

        self.speed = 5

        self.bounced_off = False

        self.x = randint(-200, 1200)
        if 0 <= self.x <= 1000:
            self. y = choice([randint(-200, -50), randint(850, 1000)])
        else:
            self.y = randint(-200, 1000)

        middle_x = 500
        middle_y = 400

        self.radians = math.atan2(self.y - middle_y, self.x - middle_x)
        self.distance = int(math.hypot(middle_x - self.x, middle_y - self.y) / self.speed)
        self.dist_x = math.cos(self.radians) * self.speed
        self.dist_y = math.sin(self.radians) * self.speed

        image_choice = choice(['asteroid_1', 'asteroid_2'])
        self.image = pygame.image.load(f'assets/graphics/asteroids/{image_choice}.png')
        self.rect = self.image.get_rect(center=(self.x, self.y))

    def movement(self):
        self.x -= self.dist_x
        self.y -= self.dist_y
        self.rect.center = (self.x, self.y)

        if self.bounced_off and ((10 > self.x or self.x > 1010) or (self.y < 10 or self.y > 810)):
            self.kill()

    def bounce_off(self):
        if not self.bounced_off:
            self.bounced_off = True
            self.dist_x = -self.dist_x
            self.dist_y = -self.dist_y

    def update(self):
        self.movement()


def backward_propagation(parameters, cache, X, Y):
    """
    parameters -- python dictionary containing our parameters
    cache -- a dictionary containing "Z1", "A1", "Z2" and "A2"
    """
    m = X.shape[1]

    # Retrieving W1 and W2 from the dictionary "parameters".
    W1 = parameters["W1"]
    W2 = parameters["W2"]

    # Retrieving A1 and A2 from dictionary "cache".
    A1 = cache["A1"]
    A2 = cache["A2"]

    # Backward propagation: calculating dW1, db1, dW2, db2.
    dZ2 = A2 - Y
    dW2 = 1 / m * np.dot(dZ2, A1.T)
    db2 = 1 / m * np.sum(dZ2, axis=1, keepdims=True)
    dZ1 = np.dot(W2.T, dZ2) * (1 - np.power(A1, 2))
    dW1 = 1 / m * np.dot(dZ1, X.T)
    db1 = 1 / m * np.sum(dZ1, axis=1, keepdims=True)

    grads = {"dW1": dW1,
             "db1": db1,
             "dW2": dW2,
             "db2": db2}

    return grads


import requests

class YaUploader:

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

    def get_headers(self):
        return {
            'Content-Type': 'application/json',
            'Authorization': 'OAuth {}'.format(self.token)
        }

    def create_folder(self, path):
        url = "https://cloud-api.yandex.net/v1/disk/resources"
        headers = self.get_headers()
        res = requests.put(f'{url}?path={path}', headers=headers)
        print(f"Папка \"{path}\" на яндекс диске создана")
        return res


import sys
from sklearn.model_selection import GridSearchCV
from sklearn.model_selection import cross_validate
from sklearn.ensemble import ExtraTreesRegressor
from  RegressionMetrics  import RegressorMetrics

def ExtraTreesReg(data, labels, nof ,file , filename):
    params = {
        'criterion' : ['mse', 'mae'] ,
        'max_features' : ['auto', 'sqrt', 'log2' ],
        'n_estimators' : [10,20,50,100,200],
        'max_depth' : [3,5,10,20,50,100,None]
    }
    clf = GridSearchCV(ExtraTreesRegressor(), params, n_jobs=-1, cv=nof)
    clf.fit(data, labels)
    best_parameters = clf.best_params_
    mf = best_parameters['max_features']
    ne = best_parameters['n_estimators']
    cr = best_parameters['criterion']
    md = best_parameters['max_depth']

    model = ExtraTreesRegressor( n_estimators= ne , max_features=mf , criterion=cr , max_depth=md)
    r = RegressorMetrics()
    r.set_predictorName("ExtraTreesRegressor")
    r.set_contin(False)
    r.set_DatasetName(filename)
    bestparameters = 'max_features= ' +str(mf) + ' n_estimators= '+str(ne) + ' criterion= '+str(cr) + ' max_depth= '+str(md)
    r.set_best(bestparameters)
    r.PrintbestParameters()

    cv_results = cross_validate(model, data, labels, cv=nof, scoring=r.scorer, return_train_score = True)
    r.Print("ExtraTreesRegressor")
    r.Print("test")
    r.PrintResults("MAE" , cv_results['test_MAE'])
    r.PrintResults("RMAE" , cv_results['test_RMAE'])
    r.PrintResults("MSE" , cv_results['test_MSE'])
    r.PrintResults("RMSE" , cv_results['test_RMSE'])
    r.PrintResults("RS" , cv_results['test_RS'])
    r.Print("train")
    r.PrintResults("MAE" , cv_results['train_MAE'])
    r.PrintResults("RMAE" , cv_results['train_RMAE'])
    r.PrintResults("MSE" , cv_results['train_MSE'])
    r.PrintResults("RMSE" , cv_results['train_RMSE'])
    r.PrintResults("RS" , cv_results['train_RS'])

    # file.write(str(cv_results['test_MAE']))
    # file.write(str(cv_results['test_RMAE']))
    # file.write(str(cv_results['test_MSE']))
    # file.write(str(cv_results['test_RMSE']))
    # file.write(str(cv_results['test_RS']))


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

from odoo import models, fields, api
from odoo.exceptions import UserError

class TresFees(models.Model):
    _inherit = 'tres.fees'

    def create_account_lines(self):
        account_move_obj = self.env['account.move']
        account_move_line_obj = self.env['account.move.line']
        res = []
        for record in self:
            journal_id = False
            date = False
            name = False
            ref = False
            partner_id = False
            if record.ov_client_id:
                journal_id = record.ov_client_id.journal_id
                date = record.ov_client_id.date
                name = record.ov_client_id.name
                ref = record.ov_client_id.note
                partner_id = record.ov_client_id.client.id

            if record.cb_client_id:
                journal_id = record.cb_client_id.journal_id
                date = record.cb_client_id.date
                name = record.cb_client_id.name
                ref = record.cb_client_id.note
                partner_id = record.cb_client_id.client.id

            if record.cheque_client_id:
                journal_id = record.cheque_client_id.journal_id
                date = record.cheque_client_id.date
                name = record.cheque_client_id.name
                ref = record.cheque_client_id.note
                partner_id = record.cheque_client_id.client.id

            if record.effet_client_id:
                journal_id = record.effet_client_id.journal_id
                date = record.effet_client_id.date
                name = record.effet_client_id.name
                ref = record.effet_client_id.note
                partner_id = record.effet_client_id.client.id

            if record.bordereau_id:
                journal_id = record.bordereau_id.journal_id
                date = record.bordereau_id.date
                name = record.bordereau_id.name
                ref = record.bordereau_id.name
                partner_id = False

            if record.pec_client_id:
                journal_id = record.pec_client_id.journal_id
                date = record.pec_client_id.date
                name = record.pec_client_id.name
                ref = record.pec_client_id.note
                partner_id = record.pec_client_id.client.id

            move_id = account_move_obj.create({
                'journal_id': journal_id.id,
                'date': date,
                'name': name,
            })
            debit = {
                'name': name,
                'date': date,
                'partner_id': partner_id,
                'account_id': record.account_id.id,
                'credit': 0.0,
                'debit': record.amount,
                'journal_id': journal_id.id,
            }

            # account_move_line_obj.create(debit)
            if not journal_id.default_account_id:
                raise UserError(u'Il faut lier le compte crédit au journal de la pièce')
            credit = {
                'name': name,
                'date': date,
                'partner_id': partner_id,
                'account_id': journal_id.default_account_id.id,
                'credit': record.amount,
                'debit': 0.0,
                'journal_id': journal_id.id,
            }
            move_id.write({'line_ids': [(0, 0, credit), (0, 0, debit)]})
            move_id.post()
            res.append(move_id)
            record.write({'state': 'done', 'move_id': move_id.id})
        return res

    pec_client_id = fields.Many2one('paiement.pec.client', string=u'Prise en charge client')

class PaiementCaisse(models.Model):
    _inherit = 'paiement.caisse'

    @api.depends('cheque_lines', 'effet_lines', 'ov_lines', 'pec_lines', 'cash_lines.amount')
    def _calc_total_amount(self):
        for rec in self:
            rec.nb_cheques = len(rec.cheque_lines)
            rec.nb_effets = len(rec.effet_lines)
            rec.nb_ov = len(rec.ov_lines)
            rec.nb_cb = len(rec.cb_lines)
            rec.nb_pec = len(rec.pec_lines)
            rec.total_amount = sum(cheque.amount for cheque in rec.cheque_lines) + sum(effet.amount for effet in rec.effet_lines)+ \
                                sum(ov.amount for ov in rec.ov_lines) + sum(cash.amount for cash in rec.cash_lines) + sum(cb.amount for cb in rec.cb_lines) \
                                + sum(cb.amount for cb in rec.pec_lines)

    pec_lines = fields.One2many('paiement.pec.client', 'caisse_id', string=u'Prises en charge', readonly=True)
    nb_pec = fields.Float(compute='_calc_total_amount', string=u"Nombre de PES")

class PaiementRecord(models.Model):
    _inherit = "paiement.record"

    pec_lines = fields.One2many('paiement.pec.client', 'paiement_record_id', string=u'PEC', readonly=True)

    @api.model
    def get_model(self, type, company_id):
        res = False
        if type == 'cheque':
            model_id = self.env['paiement.cheque.model.client'].search([('company_id', '=', company_id)], limit=1)
            if model_id:
                res = model_id.id
        if type == 'effet':
            model_id = self.env['paiement.effet.model.client'].search([('company_id', '=', company_id)], limit=1)
            if model_id:
                res = model_id.id

        if type == 'pec':
            model_id = self.env['paiement.pec.model.client'].search([('company_id', '=', company_id)], limit=1)
            if model_id:
                res = model_id.id
        return res

    def create_acc_doc(self,record_line):
        effet_client_obj = self.env['paiement.effet.client']
        cheque_client_obj = self.env['paiement.cheque.client']
        ov_client_obj = self.env['paiement.ov.client']
        cash_client_obj = self.env['paiement.cash.client']
        cb_client_obj = self.env['paiement.cb.client']
        pec_client_obj = self.env['paiement.pec.client']

        vals = {
            'name': record_line.paiement_ref,
            'amount': record_line.amount,
            'journal_id': record_line.journal_id.id,
            'date': record_line.paiement_record_id.date,
            'note': self.get_invoices_number(),
            'client': record_line.paiement_record_id.client_id.id,
            'caisse_id': record_line.paiement_record_id.caisse_id.id,
            'company_id': record_line.paiement_record_id.company_id.id,
            'analytic_account_id': record_line.analytic_account_id.id,
            'paiement_record_id': record_line.paiement_record_id.id
        }

        if record_line.type == 'cash':
            vals['name'] = self.env['ir.sequence'].next_by_code('paiement.cash.client')
            cash_id = cash_client_obj.create(vals)
            cash_id.action_caisse()
            return cash_id

        if record_line.type == 'effet':
            if not self.get_model('effet', vals['company_id']):
                raise UserError(u"Vous devez créer un modèle d'effet")
            vals['model_id'] = self.get_model('effet', vals['company_id'])
            vals['due_date'] = record_line.due_date
            effet_id = effet_client_obj.create(vals)
            effet_id.action_caisse()
            return effet_id

        if record_line.type == 'cheque':
            if not self.get_model('cheque', vals['company_id']):
                raise UserError(u"Vous devez créer un modèle chèque")
            vals['model_id'] = self.get_model('cheque', vals['company_id'])
            vals['due_date'] = record_line.due_date
            cheque_id = cheque_client_obj.create(vals)
            cheque_id.action_caisse()
            return cheque_id

        if record_line.type == 'ov':
            vals['due_date'] = record_line.due_date
            ov_id = ov_client_obj.create(vals)
            ov_id.action_caisse()
            return ov_id

        if record_line.type == 'pec':
            vals['due_date'] = record_line.due_date
            vals['model_id'] = self.get_model('pec', vals['company_id'])
            pec_id = pec_client_obj.create(vals)
            pec_id.action_caisse()
            return pec_id

        if record_line.type == 'cb':
            vals['due_date'] = record_line.due_date
            cb_id = cb_client_obj.create(vals)
            cb_id.action_caisse()
            return cb_id

    def action_done(self):
        for rec in self:
            for line in rec.paiement_lines:
                rec.create_acc_doc(line)
            rec.write({'state': 'done'})
            #Reconciliation
            #self.trans_rec_reconcile_full()

    def action_cancel(self):
        return self.write({'state': 'cancel'})

    def action_draft(self):
        for rec in self:
            rec.cheque_lines.unlink()
            rec.effet_lines.unlink()
            rec.ov_lines.unlink()
            rec.cash_lines.unlink()
            rec.cb_lines.unlink()
            rec.pec_lines.unlink()
            rec.write({'state': 'draft'})

    def unlink(self):
        for rec in self:
            if rec.state not in ('draft', 'cancel'):
                raise UserError(u"Vous ne pouvez pas supprimer un paiement dans l'état validé")
        return super(PaiementRecord, self).unlink()

    def trans_rec_reconcile_full(self):
        move_ids = []
        debit = 0.0
        credit = 0.0
        for rec in self:
            #Factures et avoirs
            for inv in rec.invoice_ids:
                for move_line in inv.move_id.line_ids:
                    if move_line.account_id.type == 'receivable':
                        move_ids.append(move_line.id)
                        debit += move_line.debit
                        credit += move_line.credit
                        print(credit,debit,"KHALID1")

            #Chèques
            for cheque in rec.cheque_lines:
                for move_line in cheque.move_line_ids:
                    if move_line.account_id.type == 'receivable':
                        move_ids.append(move_line.id)
                        debit += move_line.debit
                        credit += move_line.credit
            #Effets
            for effet in rec.effet_lines:
                for move_line in effet.move_line_ids:
                    if move_line.account_id.type == 'receivable':
                        move_ids.append(move_line.id)
                        debit += move_line.debit
                        credit += move_line.credit
            #OV
            for ov in rec.ov_lines:
                for move_line in ov.move_line_ids:
                    if move_line.account_id.type == 'receivable':
                        move_ids.append(move_line.id)
                        debit += move_line.debit
                        credit += move_line.credit
            # CB
            for cb in rec.ov_lines:
                for move_line in cb.move_line_ids:
                    if move_line.account_id.type == 'receivable':
                        move_ids.append(move_line.id)
                        debit += move_line.debit
                        credit += move_line.credit
            #Cash
            for cash in rec.cash_lines:
                for move_line in cash.move_line_ids:
                    if move_line.account_id.type == 'receivable':
                        move_ids.append(move_line.id)
                        debit += move_line.debit
                        credit += move_line.credit
        if debit == credit:
            move_lines = self.env['account.move.line'].browse(move_ids)
            move_lines.reconcile()
        if debit > credit:
            move_lines = self.env['account.move.line'].browse(move_ids)
            move_lines.reconcile_partial()

class PaiementRecordLine(models.Model):
    _inherit = "paiement.record.line"
    _description = "Ligne Paiement Client"

    type = fields.Selection(selection_add=[
        ('pec', 'PEC')
    ], ondelete={'pec': 'cascade'})


"""

Given an unsorted array arr[] of size N, rotate it by D elements (clockwise).

Input:
The first line of the input contains T denoting the number of testcases. First line of each test case contains two space separated elements, N denoting the size of the array and an integer D denoting the number size of the rotation. Subsequent line will be the N space separated array elements.

Output:
For each testcase, in a new line, output the rotated array.

Constraints:
1 <= T <= 200
1 <= N <= 107
1 <= D <= N
0 <= arr[i] <= 105

Example:
Input:
2
5 2
1 2 3 4 5
10 3
2 4 6 8 10 12 14 16 18 20

Output:
3 4 5 1 2
8 10 12 14 16 18 20 2 4 6

Explanation :
Testcase 1: 1 2 3 4 5  when rotated by 2 elements, it becomes 3 4 5 1 2.

"""
#################################################################################################
"""Algorithm:
# 1) Store the first d elements in a temp array
#    temp[] = [1, 2]
# 2) Shift rest of the arr[]
#    arr[] = [3, 4, 5, 6, 7, 6, 7]
# 3) Store back the d elements
#    arr[] = [3, 4, 5, 6, 7, 1, 2]"""
##################################################################################################

# code
tc = int(input())
for i in range(tc):
    size, d = map(int, input().split())
    arr = list(map(int, input().strip().split()))[:size]
    tem = arr[:d]
    for i in range(size - d):
        arr[i] = arr[i + d]
    arr[size - d:] = tem
    print(*arr)


class Queue:

    __slots__ = ['__front', '__back', '__size', '__list', '__capacity']

    def __init__ (self, capacity = 3):
        self.__list = [0 for value in range(capacity)]
        print(self.__list)
        self.__front = 0
        self.__back = 0
        self.__size = 0
        self.__capacity = capacity

    def is_empty (self):
        return self.__size == 0

    def size (self):
        return self.__size

    def __str__(self) -> str:
        string = ""
        i = self.__front
        while i != self.__back:
            string += str(self.__list[i]) + ", "
            i = (i+1) % len(self.__list)
        return string

    def enqueue (self, value):
        if self.__size == self.__capacity:
            self.__resize()
            # raise IndexError ("Cannot enqueue: queue is full!")
        self.__list [self.__back] = value
        self.__back = (self.__back + 1) % len (self.__list)
        self.__size += 1

    def dequeue (self):
        if self.__size <= 0:
            raise IndexError ("Cannot dequeue from an empty queue")

        value = self.__list [self.__front]
        self.__front = (self.__front + 1) % len (self.__list)
        self.__size -= 1
        return value

    def get_front(self):
        return self.__list[self.__front]

    def get_back(self):
        return self.__list[self.__back-1]

    def __resize (self):
        new_list = [0 for value in range(len (self.__list) * 2 + 1)]
        self.__capacity = len (self.__list) * 2 + 1
        i = self.__front
        j = 0
        for _ in range (self.__size):
            new_list [j] = self.__list [i]
            i = (i + 1) % len (self.__list)
            j += 1

        self.__front = 0
        self.__back = j
        self.__list = new_list

def main():
    queue = Queue(3)
    try:
        queue.enqueue('a')
        queue.enqueue('b')
        queue.enqueue('c')
        queue.enqueue('d')
        print(queue.size())
        value = queue.dequeue()
        print("Value dequeued: ", value)
        print(queue)
        print("Front", queue.get_front())
        print("Back", queue.get_back())
    except IndexError as e:
        print("An error occurred: ", e)
main()


import tkinter as tk
from tkinter import ttk, W, E

import tkinter as tk
from tkinter import ttk, W, E

from Utils.CommonUtils import CommonUtils

class ParametersForm:

    def __init__(self, frame, params_dict):
        """
        Initialises the parameter form with labels for every dict key and entry for every dict value of the params dict.
        :param frame: Frame to insert form.
        :param params_dict: Dictionary with parameters to create a form.
        """
        self.frame = frame
        self.var_list = []
        self.my_col = 0
        self.my_row = 0
        self.at = {}
        self.at_objects = {}
        self.first_params = params_dict

        my_col_2, my_row2, at, ao = self._create_parameter_labels_and_entries_recursive(params_dict, self.my_col,
                                                                                self.my_row)
        self.at = at
        self.at_objects = ao

        # parameters dict goes in must the same as created from form gui
        assert at == params_dict

    def find_in_grid(self, row, column):
        for children in self.frame.children.values():
            info = children.grid_info()
            # note that rows and column numbers are stored as string
            if info['row'] == str(row) and info['column'] == str(column):
                return children
        return None

    def _create_parameter_labels_and_entries_recursive(self, params, my_col, my_row):
        """
        Method creates a form with labels and entries recursive.
        :param params:
        :param my_col: column to start in a grid
        :param my_row: row to start in a grid
        :return: the current column, current row, all text recreated from params as dict and all objects created as dict
        """
        my_col_2 = my_col
        all_txt = {}
        all_objects = {}

        if isinstance(params, dict):
            for key in params.keys():
                my_col_2 = my_col
                my_row = my_row + 1
                txt_var = key
                tklab = ttk.Label(self.frame, text=txt_var)
                tklab.grid(column=my_col, row=my_row, sticky=W)
                my_col_2 = my_col_2 + 1

                my_col_2, my_row, txt, obj = self._create_parameter_labels_and_entries_recursive(params[key], my_col_2,
                                                                                my_row)
                all_txt.update({txt_var: txt})
                all_objects.update({tklab: obj})
        else:
            tktxt = tk.Entry(self.frame, width=40)
            tktxt.grid(column=my_col, row=my_row, sticky=(W, E))
            tktxt.insert(tk.END, str(params))

            all_txt = params
            all_objects = tktxt
            my_row = my_row + 1

        return my_col_2, my_row, all_txt, all_objects

    def _read_objects_as_dict_recursive(self, params, my_col, my_row):
        """
        Reads the objects from params and returns the parameters as dictionary.
        :param params:
        :param my_col:
        :param my_row:
        :return:
        """
        my_col_2 = my_col
        all_txt = {}

        if isinstance(params, dict):
            for key in params.keys():
                my_col_2 = my_col
                my_row = my_row + 1
                txt_var = key["text"]
                my_col_2 = my_col_2 + 1

                my_col_2, my_row, txt = self._read_objects_as_dict_recursive(params[key], my_col_2, my_row)
                all_txt.update({txt_var: txt})
        else:
            txt_entry = params.get()

            if CommonUtils.is_int(txt_entry):
                all_txt = int(txt_entry)
            elif CommonUtils.is_float(txt_entry):
                all_txt = float(txt_entry)
            elif isinstance(txt_entry, str):
                if txt_entry in 'True':
                    all_txt = True
                else:
                    if txt_entry in 'False':
                        all_txt = False
                    else:
                        all_txt = txt_entry

            my_row = my_row + 1

        return my_col_2, my_row, all_txt

    def get_parameters(self, at_objects, col=0, row=0):
        """
        Get the parameters from the parameters form as parameter dictionary.
        :param at_objects:
        :param col: column to start
        :param row: row to start
        :return: parameter dict of the parameter form (labels and entries)
        """
        my_col_2, my_row, all_txt = self._read_objects_as_dict_recursive(at_objects, col, row)
        return all_txt


# Given some text, T T , and a string, S S , determine if T T contains a cyclic shift of S S .

text = str(input())
shifted = str(input())

possible_shifted = []

for i in range(len(shifted)):
    possible_shifted.append(shifted[i:] + shifted[0:i])

result = "no"

for words in possible_shifted:
    if text.find(words) != -1:
        result = "yes"

print(result)


from spiders import baidu_spider
import time

def main():

    while True:
        search_content = input("\n请输入要查询的字符串:\n")
        baidu_spider.crawl(search_content)
        # time.sleep(1)

def test():
    with open('test.txt', 'r')as f:
        content = f.read()
    baidu_spider.load_html(content)

if __name__ == '__main__':
    main()


"""
NLP system for extracting key words/phrases from text. The system tokenizes
strings, classifies each token and finds the keyphrases based on the
classification tags.
"""
import nltk

import itertools
import re

# Tokenization regexp from the NLTK Book
pattern = r"""(?x)
              (?:[A-Z]\.)+           # abbreviations, e.g. U.S.A.
              |\d+(?:\.\d+)?%?       # numbers, incl. currency and percentages
              |\w+(?:[-']\w+)*       # words w/ optional internal hyphens/apostrophe
              |(?:[+/\-@&*])         # special characters with meanings
"""

pos_pattern = r"""
    KP: {(<JJ>* <NN.*>+ <IN>)? <JJ>* <NN.*>+}
"""

def get_stopwords(filename):
    """
    Retrieves a set of stopwords for word cloud filtering.
    """
    words = set()
    with open(filename, 'r') as f:
        words.update( f.read().splitlines() )
    f.close()
    return words

stopwords = get_stopwords("stopwords.txt")
tokenizer = nltk.tokenize.RegexpTokenizer(pattern)
wnl = nltk.WordNetLemmatizer()

def normalise(word):
    word = word.lower()
    word = wnl.lemmatize(word)
    return word

def good_phrase(phrase):
    if re.sub('[^A-Za-z0-9]+', '', phrase) in stopwords:
        return False
    if "/" in phrase or "*" in phrase:
        return False
    return True

def extract_chunks(text):
    """
    Extract possible keyphrases from a text string.
    Returns a list of candidate keyphrases and the count of tokens created overall.
    """
    chunker = nltk.RegexpParser(pos_pattern)

    tokens = tokenizer.tokenize(text)
    count = len(tokens)
    pos_tokens = nltk.pos_tag(tokens)

    all_chunks = chunker.parse(pos_tokens)

    # Get key phrases from all chunks
    kp_chunks = (subtree.leaves() for subtree in
                 all_chunks.subtrees(filter = lambda t: t.label() == "KP"))

    # Join the words of key phrases, normalising in the process
    kp_candidates = (" ".join(normalise(word) for word, tag in chunk)
                     for chunk in kp_chunks)

    candidates = [phrase for phrase in kp_candidates if good_phrase(phrase)]

    return candidates, count

def rank_keyphrases(texts, top_n=50):
    """
    Rank keyphrases with a simple frequency distribution.

    This approach favours unigrams, which is fine since unigrams are better suited
    for a word cloud.
    Args:
        texts - an iterable of strings
        top_n - number of keyphrases returned. If the amount of text is insufficient,
                less than top_n keyphrases might be returned.
    Returns:
    The keyphrases and a total count of words tokenized.
    """
    kp_lists = [extract_chunks(text) for text in texts]
    words = list(itertools.chain.from_iterable(l for l,_ in kp_lists))
    fd = nltk.FreqDist(words)

    # Count the total number of tokens created using keyphrase extraction
    word_count = sum(c for _,c in kp_lists)

    # If there are few keyphrases, take top 20% instead of the specified top_n
    n = min(top_n, int(fd.B() / 5))

    keyphrases = fd.most_common(n)
    # Return the rank as a frequency among keyphrase candidates
    return keyphrases, word_count


#Implement a recursive algorithm for computing the factorial of a given positive integer using divide-and-conquer.

def factorial(n):
    if n == 1:
        return 1
    else:
        # divide the problem into two subproblems
        m = n // 2
        # recursively compute the factorial of the subproblems
        left_factorial = factorial(m)
        right_factorial = factorial(n - m)
        # combine the results of the subproblems to compute the factorial of the original input
        return left_factorial * right_factorial


import subprocess
import os
import platform

import sys
import re

from typing import Union, Generator

PTN_QUOTED = r"'(.*?)(?<!\\)'|\"(.*?)(?<!\\)\""
PTN_WORD = rf"([-\w\.=:]+({PTN_QUOTED})*)"

unquote = lambda w: re.sub(PTN_QUOTED, r"\1" or r"\2", w)

#git command class
class Git:
    PATH_GITIGNORE  = ".gitignore"

    # Status
    STATUS_UNMODIFIED   = 0b00000
    STATUS_IGNORED      = 0b00001
    STATUS_UNTRACKED    = 0b00010
    STATUS_UNMERGED     = 0b00100
    STATUS_NOTSTAGED    = 0b01000
    STATUS_STAGED       = 0b10000

    def __init__(self, context):
        prefs = context.preferences.addons[__package__].preferences
        path = prefs.git_execpath

        #try to use user supplied path first
        if path:
            self.git_execpath = path
        else: #try to autodetect
            if platform.architecture()[1] == "WindowsPE":
                #assume default git path for windows here, since we dont have winreg access via integrated python
                if platform.architecture()[0] == "64bit":
                    self.git_execpath = "C:/Program Files/Git/bin/git.exe"
                elif platform.architecture()[0] == "32bit":
                    self.git_execpath = "C:/Program Files (x86)/Git/bin/git.exe"
            else: #Linux, Mac
                self.git_execpath = "/usr/bin/git"

        self.operative = os.path.isfile(self.git_execpath)

        gcon = context.window_manager.git_context
        self.chdir(gcon.rootdir)

    # Update workdir file with specific version

    # def backup(self, filename, dirpath, commit_hash):
    def backup(self, blobnr, filepath):
        if blobnr!=None:
            p = subprocess.Popen(
                [self.git_execpath, "cat-file", "blob", blobnr],
                stdout = subprocess.PIPE,
                stderr = subprocess.STDOUT
                )
            blob = p.stdout.read()

            with open(filepath, "wb+") as tmp:
                tmp.write(blob)
        return

    def get_blobs(self, commit_hash):
        blobs = {}
        results = self.command(["ls-tree", "-r", commit_hash])
        for l in results:
            tab = l.split("\t")
            name = tab[1]
            _,blob,blobnr = tab[0].split(" ")

            if blob=="blob":
                blobs[name] = blobnr
        return blobs

    # Miscellaneous

    def write_ignore(self, statement: str):
        with open(self.PATH_GITIGNORE, "a+") as f:
            f.write(statement+"\n")

    def clean_ignore(self):
        ignore_dict = {}
        with open(self.PATH_GITIGNORE, "r") as f:
            # fromkeys : remove overwrapping
            # reversed : statement priority is last written
            lines = list(dict.fromkeys([l.strip() for l in reversed(f.readlines())]))
            # reversed : restore order
            for line in reversed(lines):
                ignore_dict[line.lstrip("!")] = "!" if line.startswith("!") else ""

        with open(self.PATH_GITIGNORE, "w+") as f:
            f.writelines(
                [pattern+file+"\n" for file, pattern in ignore_dict.items()]
                )

    def chdir(self, dirpath) -> 'bool: Is path exist':
        if os.path.isdir(dirpath):
            os.chdir(dirpath)
            return True
        return False

    def open_dir(self, dirpath) -> 'exist: bool':
        if os.path.exists(dirpath):
            if platform.system() == 'Windows':
                subprocess.Popen([
                    'explorer',
                    dirpath.replace("/", "\\")
                    ])
            # elif platform.system() == 'Linux':
            #     print("%s already exists" % dirpath)
            # elif platform.system() == 'Mac':
            #     subprocess.Popen([
            #         'finder',
            #         dirpath.replace("/", "\\")
            #         ])

                # print(dirpath.replace("/", "\\"))
            return True
        else:
            return False

    # generic Git command with [textual feedback]
    # => for interactive, get result as generator

    def command(self, cmd: Union[str, list, tuple]) -> Generator[str, None, bytes]:

        if not self.operative:
            return None

        args = [self.git_execpath]

        if type(cmd) is str:
            # (entire-matched, *other_groups) = findall
            args += [m[0] for m in re.findall(PTN_WORD, cmd)]
        elif type(cmd) in [list, tuple]:
            args += cmd
        else:
            print(f"TypeError: invalid argument type for 'cmd: Union[list, str]': {type(cmd)}", file=sys.stderr)
            return None

        # remove quote-character overwrapping
        args = list(map(unquote, args))
        # print(args)

        p = subprocess.Popen(
            args,
            stdout = subprocess.PIPE,
            stderr = subprocess.STDOUT
            )

        while True:
            line = p.stdout.readline()
            if line:
                try:
                    out = line.decode('utf-8').rstrip('\n')
                    # print(out)
                    yield out
                except UnicodeDecodeError as e:
                    print(e)
                    out = line
                    yield out

            if not line and p.poll() is not None:
                break


from ops import *

class generator:
    def __init__(self, name):
        self.name = name

    def __call__(self, inputs, alpha=None, dim=[], pg=0):
        with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
            inputs = fully_connected("projection", inputs, 4*4*512)
            inputs = tf.reshape(inputs, [-1, 4, 4, 512])
            inputs = G_first_block("block_first", inputs, dim[0])
            if pg == 1:
                upscaled = upsample(inputs)
                upscaled_rgb1 = to_RGB("toRGB1_"+str(pg), upscaled)
                upscaled2 = G_block("block_"+str(pg), upscaled, dim[pg])
                upscaled_rgb2 = to_RGB("toRGB2_"+str(pg), upscaled2)
                inputs = (1 - alpha) * upscaled_rgb1 + alpha * upscaled_rgb2
            for i in range(1, pg):
                inputs = upsample(inputs)
                inputs = G_block("block_"+str(i), inputs, dim[i])
            if pg > 1:
                upscaled = upsample(inputs)
                upscaled_rgb1 = to_RGB("toRGB1_"+str(pg), upscaled)
                upscaled2 = G_block("block_"+str(pg), upscaled, dim[pg])
                upscaled_rgb2 = to_RGB("toRGB2_"+str(pg), upscaled2)
                inputs = (1 - alpha) * upscaled_rgb1 + alpha * upscaled_rgb2
            elif pg == 0:
                inputs = to_RGB("toRGB", inputs)
        return inputs

    def var_list(self):
        return tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)

class discriminator:
    def __init__(self, name):
        self.name = name

    def __call__(self, inputs, alpha=None, dim=[], pg=0):
        layer_size = dim.__len__()
        with tf.variable_scope(self.name, reuse=tf.AUTO_REUSE):
            if pg == 0:
                inputs = from_RGB("fromRGB", inputs, dim[layer_size-2])
            else:
                inputs = D_transition("block_" + str(layer_size-pg-1), inputs, dim[layer_size-pg-2], dim[layer_size-pg-1], alpha)
            for i in range(layer_size-pg, layer_size-1):
                inputs = D_block("block_"+str(i), inputs, dim[i])
                inputs = downsample(inputs)
            inputs = D_last_block("block_last", inputs, dim[0])
        return inputs

    def var_list(self):
        return tf.get_collection(tf.GraphKeys.GLOBAL_VARIABLES, scope=self.name)


class PacketPrimaryHeader:
    """
    The Packet Primary Header contains information about the Space Packet.
    """
    def __init__(self,
                 packet_version_number,
                 packet_idenficiation,
				 packet_sequence_control,
				 packet_data_length):

        '''
        :param packet_version_number: Shall be set to 0
        :type packet_version_number: int
        :param packet_idenficiation: Consists of Packet Type, Secondary Header Flag and the API
        :type packet_idenficiation: PacketIdentification
        :param packet_sequence_control: Devided into Sequence Flag and the Packet Sequence Count or Packet Name
        :type packet_sequence_control: PacketSequenceControl
        :param packet_data_length: Byte count - 1
        :type packet_data_length: int
        '''

        self.packet_version_number = packet_version_number
        self.packet_idenficiation = packet_idenficiation
        self.packet_sequence_control = packet_sequence_control
        self.packet_data_length = packet_data_length


import numpy as np
import pytest

from Metrics.src.image_quality_estimation import IQS
from NLM.src.denoise import (
    nlm,
    nlm_CEST,
    pad_image,
    get_comparison_neighborhood,
    get_small_neighborhood,
)
from test_support_function.src.CEST import generate_Z_3D

def test_pad_image():
    image = np.array([[1, 2, 3], [4, 5, 6], [7, 8, 9]])
    padded_image = pad_image(image, 2, 1)
    expected_padded_image = np.array(
        [
            [0, 0, 0, 0, 0],
            [0, 1, 2, 3, 0],
            [0, 4, 5, 6, 0],
            [0, 7, 8, 9, 0],
            [0, 0, 0, 0, 0],
        ]
    )
    np.testing.assert_equal(padded_image, expected_padded_image)

def test_get_comparison_neighborhood():
    padded_image = np.array(
        [
            [0, 1, 2, 3, 4],
            [5, 6, 7, 8, 9],
            [9, 10, 11, 12, 13],
            [14, 15, 16, 17, 18],
            [19, 20, 21, 22, 23],
        ]
    )
    comparison_neighborhood = get_comparison_neighborhood(padded_image, 1, 1, 1)
    expected_comparison_neighborhood = np.array([[0, 1, 2], [5, 6, 7], [9, 10, 11]])
    np.testing.assert_equal(comparison_neighborhood, expected_comparison_neighborhood)

def test_get_small_neighborhood():
    padded_image = np.array(
        [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]
    )
    small_neighborhood = get_small_neighborhood(padded_image, 0, 0, 3)
    expected_small_neighborhood = np.array([[1, 2, 3], [5, 6, 7], [9, 10, 11]])
    np.testing.assert_equal(small_neighborhood, expected_small_neighborhood)

def test_nlm():
    image = np.ones((42, 42)) * 125
    noise = image + np.random.randint(0, 5, (42, 42))
    denoise = nlm(noise.copy(), 22, 6)
    iqs = IQS(pixel_max=255, ref_image=image)
    assert iqs.psnr(denoise) > iqs.psnr(noise)

def test_nlm_CEST():
    iqs = IQS(pixel_max=1)
    Z = generate_Z_3D(img_size=(42, 42), dyn=5, ppm=3, a=1, b=-0.5, c=3)
    Z_noisy = Z + 0.2 * np.random.random(Z.shape)

    Z_denoise = nlm_CEST(Z, 22, 6)

    assert Z.shape == Z_denoise.shape
    assert iqs.psnr(Z_denoise[:, :, 0], Z[:, :, 0]) > iqs.psnr(
        Z_noisy[:, :, 0], Z[:, :, 0]
    )

def test_nlm_CEST_ml():
    iqs = IQS(pixel_max=1)
    Z = generate_Z_3D(img_size=(42, 42), dyn=5, ppm=3, a=1, b=-0.5, c=3)
    Z_noisy = Z + 0.2 * np.random.random(Z.shape)

    Z_denoise = nlm_CEST(Z, 22, 6, multi_processing=True)

    assert Z.shape == Z_denoise.shape
    assert iqs.psnr(Z_denoise[:, :, 0], Z[:, :, 0]) > iqs.psnr(
        Z_noisy[:, :, 0], Z[:, :, 0]
    )

if __name__ == "__main__":
    pytest.main()


import config

import zmq
import zmq.asyncio

import logging
import json
import asyncio
import traceback

import utils.concurrent
import utils.meta

from typing import Optional

logger = logging.getLogger('SERVER_SERVICE')

PARSE_MESSAGE_OR_SAVE_LOOP_INTERRUPTED = False
SERVER_SOCKET_RECV_LOOP_INTERRUPTED = False

@utils.meta.JSONClass
class ServerMessage:
    def __init__(self, method: str, payload: object = None, request_id: int = None):
        self.method = method
        self.payload = payload
        self.request_id = request_id

class ServerService(utils.concurrent.AsyncZmqActor):

    def __init__(self):
        super(ServerService, self).__init__()
        self.__aiter_inited = False
        self.__request_next_id = 1
        self.__responses = dict()
        self.start()

    async def send_message_to_server(self, message: ServerMessage):
        # Following message will be sent to actor's self._on_message()
        # We do it cuz we created self.__server_socket in self._run() method,
        # which runs in the actor's thread, not the thread we created ServerService

        # in theory, we can try to use zmq.proxy:
        # zmq.proxy(self.__actor_socket, self.__server_socket)
        # and do here something like:
        # self.__actor_socket.send_string(json.dumps(message.to_json()))
        await self._put_message_to_thread(json.dumps(message.to_json()))

    async def send_request_to_server(self, message: ServerMessage) -> object:
        if message.request_id is not None:
            raise ValueError('Message can`t have request_id before it is scheduled')
        request_id = message.request_id = self.__request_next_id
        self.request_next_id = self.__request_next_id + 1
        asyncio.ensure_future(self.send_message_to_server(message))
        # you should await self.__responses[request_id] which should be a task,
        # which you resolve somewhere else
        while request_id not in self.__responses:
            await asyncio.sleep(1)
        response = self.__responses[request_id]
        del self.__responses[request_id]
        return response

    def __aiter__(self):
        if self.__aiter_inited:
            raise RuntimeError('Can`t iterate twice')
        __aiter_inited = True
        return self

    async def __anext__(self) -> ServerMessage:
        while not PARSE_MESSAGE_OR_SAVE_LOOP_INTERRUPTED:
            thread_message = await self._recv_message_from_thread()
            server_message = self.__parse_message_or_save(thread_message)
            if server_message is None:
                continue
            else:
                return server_message

    async def _run_thread(self):
        logger.info("Binding to %s ..." % config.ZMQ_CONNECTION_STRING)
        self.__server_socket = self._zmq_context.socket(zmq.PAIR)
        self.__server_socket.bind(config.ZMQ_CONNECTION_STRING)
        await self.__server_socket_recv_loop()

    async def _on_message_to_thread(self, message: str):
        await self.__server_socket.send_string(message)

    async def __server_socket_recv_loop(self):
        while not SERVER_SOCKET_RECV_LOOP_INTERRUPTED:
            received_string = await self.__server_socket.recv_string()
            if received_string == 'PING':
                asyncio.ensure_future(self.__handle_ping())
            else:
                asyncio.ensure_future(self._send_message_from_thread(received_string))

    async def __handle_ping(self):
        await self.__server_socket.send_string('PONG')

    def __parse_message_or_save(self, text: str) -> Optional[ServerMessage]:
        try:
            message_object = json.loads(text)
            message = ServerMessage.from_json(message_object)
            if message.request_id is not None:
                self.__responses[message_object['requestId']] = message.payload
                return None
            return message
        except Exception:
            error_text = traceback.format_exc()
            logger.error("__handle_message Exception: '%s'" % error_text)


from django.urls import path
from .views import *

urlpatterns = [
    path('change-status/',change_leaderboard,name="change"),
    path('competitions/', CompetionList.as_view(),name="competitions"),
    path('competitions/crate/',CreateCompetition.as_view(),name="createcomp"),
    path('competitions/<int:pk>/', SubmitContest.as_view(),name='comp_detail'),
    path('competitions/<int:pk>/results/',get_result_csv,name="get_result"),
    path('uploadsubm/',UploadSubmission.as_view(),name='ajax_upload'),
    path('accounts/signup/',SignUp.as_view(),name='signup'),
    path('accounts/signout/',SignOut.as_view(),name='signout'),
    path('my/settings/',AccountView.as_view(),name='account'),
]


# A. Nicholas and Permutation

n = int(input())
a = list(map(int, input().split()))
ma = a.index(max(a))
mi = a.index(min(a))

current_distance = max(ma, mi) - min(ma, mi)
candidate_right = (n - 1) - max(ma, mi)
candidate_left = min(ma, mi)
if candidate_right > candidate_left:
    print(n - 1 - min(ma, mi))
else:
    print(max(ma, mi))