#Paths
sd_path = 'spk_detectors_folder'
df_folder = 'dataframes_folder'
plots_path = 'plots_folder'
results_path = 'results_folder'
positions_path = 'positions_folder'
input_images_path = 'input_images_folder'
gabor_folder = 'gabor_outputs'

#Simulation
image_selected = '/sinusoid.png'
images_to_simulate = [input_images_path + image_selected ]

num_images_to_simulate = len(images_to_simulate)
ms_per_stimuli = 700.0
simulation_time = ms_per_stimuli * num_images_to_simulate
ms_steady_state_simulation = 0.0

lateral_connections = True

#Size
num_hipercolumns = 7
columns_in_hipercolumns = 10
x_cortex_size = num_hipercolumns * columns_in_hipercolumns
y_cortex_size = num_hipercolumns * columns_in_hipercolumns
cortex_size = x_cortex_size * y_cortex_size

#Gabor
K_size = 700; Lambda = 100 / 4 ;Psi = 0 ;Sigma = Lambda * 0.36; Gamma = 0.7
cut_pixels = 0
get_output_gabors = 0
max_rescaling_factor_gabor = 80
correct_gamma = 0

#Nest
local_num_threads = 2
resolution = 0.1

#Number of orientations
num_orientations = 4

#Layers
extent = [float(num_hipercolumns), float(num_hipercolumns)]
ratio_exc_inh = 1
neurons_per_column_inh = 1
neurons_per_column_exc = ratio_exc_inh * neurons_per_column_inh
poisson_bias = 0.0

#Poisson
factor = 430.9
factor_bias = 0.0

############ Dictionaries

#Layers
edge_wrap = True
allow_autapses = True
allow_multapses = False

#Kernel
stddev_c_rf = 0.08

p_center_inh = 1.0
mean_lat_conn_inh = 3 * stddev_c_rf
stddev_lat_conn_inh = 1 * stddev_c_rf
input_stddev_inh = 0.0

p_center_exc = p_center_inh * 1
mean_lat_conn_exc = 0.0
stddev_lat_conn_exc = stddev_lat_conn_inh * 2
input_stddev_exc = 0.0

n_sigmas_inside_mask = 3.5
n_microcolumn_height = 10
n_microcolumn_width  = 10
n_rows_latconn_inh   = int(stddev_lat_conn_inh * n_sigmas_inside_mask * 2 * n_microcolumn_height) + 1
n_cols_latconn_inh   = int(stddev_lat_conn_inh * n_sigmas_inside_mask * 2 * n_microcolumn_width)  + 1
n_rows_latconn_exc   = int(stddev_lat_conn_exc * n_sigmas_inside_mask * 2 * n_microcolumn_height) + 1
n_cols_latconn_exc   = int(stddev_lat_conn_exc * n_sigmas_inside_mask * 2 * n_microcolumn_width)  + 1

#Synapse model
syn_model_inh = 'static_synapse_hpc'
syn_model_exc = 'static_synapse_hpc'

#Delays
delay_exc = 1.0 ; delay_exc_large = 1.0 ; delay_inh = 0.5
slowness_exc = 1.0; slowness_exc_large = 1.0; slowness_inh = 0.05

# Large lateral connections
kappa_j = 0.126 * 2.0
kappa_w = 0.14 * 2.0
weight_large_range_exc_exc = 0.02
weight_large_range_exc_inh = 0.02
rescale = 2.0 ; radius_lat = 2.5

# Short lateral connections
ratio_inh_exc_w = 4.0
p_center_inh_exc = 0.6 ;        weight_inh_exc = - ratio_inh_exc_w * 0.75
p_center_inh_inh = 0.1 ;        weight_inh_inh = - ratio_inh_exc_w * 0.1
p_center_exc_exc = 0.4 ;        weight_exc_exc =  0.4
p_center_exc_inh = 0.5 ;        weight_exc_inh =  0.6

input_weight_poiss = 1.0

dict_poiss_to_v1  = {'connection_type': 'divergent','weights': input_weight_poiss ,
                      'mask': {'grid': {'rows': 1, 'columns': 1}},
                      'allow_autapses': allow_autapses, 'allow_multapses': allow_multapses}

dict_divergent    = {'connection_type': 'divergent','mask': {'grid': {'rows': 1, 'columns': 1}},
                      'allow_autapses': allow_autapses, 'allow_multapses': allow_multapses}

short_range_inh_exc =  {'connection_type': 'convergent',
                        'mask': {'grid':{'rows':n_rows_latconn_inh ,'columns':n_cols_latconn_inh },
                                 'anchor':{'row':(n_rows_latconn_inh - 1)//2,'column':(n_cols_latconn_inh - 1)//2}},
                        'delays': {'linear':{'c':delay_inh,'a':slowness_inh}},
                        'kernel': {'gaussian':{'p_center': p_center_inh_exc , 'sigma':stddev_lat_conn_inh , 'mean': mean_lat_conn_inh}},
                        'weights': weight_inh_exc ,
                        'synapse_model':syn_model_inh,
                        'allow_autapses': allow_autapses, 'allow_multapses': allow_multapses}

short_range_inh_inh =  {'connection_type': 'convergent',
                        'mask': {'grid':{'rows':n_rows_latconn_inh,'columns':n_cols_latconn_inh},
                                 'anchor':{'row':(n_rows_latconn_inh - 1)//2,'column':(n_cols_latconn_inh - 1)//2}},
                        'delays': {'linear':{'c':delay_inh,'a':slowness_inh}},
                        'kernel': {'gaussian':{'p_center': p_center_inh_inh  , 'sigma':stddev_lat_conn_inh , 'mean': mean_lat_conn_inh}},
                        'weights': weight_inh_inh ,
                        'synapse_model':syn_model_inh,
                        'allow_autapses': allow_autapses, 'allow_multapses': allow_multapses}

short_range_exc_exc =  {'connection_type': 'convergent',
                        'mask': {'grid':{'rows':n_rows_latconn_exc,'columns':n_cols_latconn_exc},
                                 'anchor':{'row':(n_rows_latconn_exc - 1)//2,'column':(n_cols_latconn_exc - 1)//2}},
                        'delays': {'linear':{'c':delay_exc,'a':slowness_exc}},
                        'kernel': {'gaussian':{'p_center': p_center_exc_exc, 'sigma':stddev_lat_conn_exc , 'mean':mean_lat_conn_exc}},
                        'weights':  weight_exc_exc  ,
                        'synapse_model':syn_model_exc,
                        'allow_autapses': allow_autapses, 'allow_multapses': allow_multapses}

short_range_exc_inh =  {'connection_type': 'convergent',
                        'mask': {'grid':{'rows':n_rows_latconn_exc,'columns':n_cols_latconn_exc},
                                 'anchor':{'row':(n_rows_latconn_exc - 1)//2,'column':(n_cols_latconn_exc - 1)//2}},
                        'delays': {'linear':{'c':delay_exc,'a':slowness_exc}},
                        'kernel': {'gaussian':{'p_center': p_center_exc_inh  , 'sigma':stddev_lat_conn_exc, 'mean':mean_lat_conn_exc}},
                        'weights':  weight_exc_inh ,
                        'synapse_model':syn_model_exc,
                        'allow_autapses': allow_autapses, 'allow_multapses': allow_multapses}

#Modelos
RS_dict =  {'a':0.02, 'b':0.2, 'c':-65.,'d':8.0, 'V_th':30.}
FS_dict =  {'a':0.1, 'b':0.2, 'c':-65., 'd':2.0, 'V_th':30.}

#Results (image, video, EEG, frequencies)
window_time = 0
re_size = (x_cortex_size,y_cortex_size)
frames_per_second = 20
num_max_frequencies = 2
broadband_initial = 25
broadband_end_1 = 60
broadband_end_2 = 100
image_from = int(ms_steady_state_simulation) + 100
eeg_freqs_from = int(ms_steady_state_simulation) + 200
eeg_freqs_until = eeg_freqs_from + 500
radius = columns_in_hipercolumns / 2 * 1000
collect_data_bool = 0


#!/usr/bin/python3

import base64
import json
from urllib.error import URLError
from urllib.parse import urlencode
from urllib.request import Request, urlopen

from mycommon import JsonFile

def get_auth_value(client_id, client_secret):

    a = "{}:{}".format(client_id, client_secret)
    b = base64.encodestring(a.encode("utf-8"))[:-1]
    return "Basic {}".format(b.decode("utf-8"))

def get_access_token_json(token_url, code, auth_value, redirect_uri):

    # Using OAuth 2.0 — Fitbit Web API Docs
    # Authorization Code Grant Flow: Access Token Request
    # https://dev.fitbit.com/docs/oauth2/#access-token-request

    params = {
        "code": code,
        "grant_type": "authorization_code",
        "redirect_uri": redirect_uri,
    }

    body = urlencode(params).encode("utf-8")

    req = Request(token_url)
    req.add_header("Authorization", auth_value)
    req.add_header("Content-Type", "application/x-www-form-urlencoded")

    try:
        # HTTP POST
        res = urlopen(req, body)
    except URLError as e:
        print(e)
        content = e.read().decode("utf-8")
        e.close()
        return content
    else:
        content = res.read().decode("utf-8")
        res.close()
        return content

token_url = "https://api.fitbit.com/oauth2/token"

jsonfile = JsonFile("fitbit-auth.json")
auth = jsonfile.read()

auth_value = get_auth_value(auth["client_id"], auth["client_secret"])
print(auth_value)

access_token_json = get_access_token_json(
    token_url, auth["code"], auth_value, auth["callback_url"])
print(access_token_json)

data = json.loads(access_token_json)
auth["access_token"] = data["access_token"]
auth["refresh_token"] = data["refresh_token"]
auth["user_id"] = data["user_id"]
jsonfile.write(auth)


from socket import *

s = socket()
s.bind(('127.0.0.1', 12345))
s.listen(4)

c, addr = s.accept()
print("connnect form", addr)

file = open('apex.txt', 'wb')

while True:
    data = c.recv(1024)
    if not data:
        break
    file.write()

file.close()
c.close()
s.close()


import collections
from typing import List

#Runtime: 100 ms, faster than 54.94% of Python3 online submissions for How Many Numbers Are Smaller Than the Current Number.
#Memory Usage: 13.8 MB, less than 80.42% of Python3 online submissions for How Many Numbers Are Smaller Than the Current Number.

#fun fact: if not spots[n2[i]] returns true if its value is 0 (fails test case)
class Solution:
    def smallerNumbersThanCurrent(self, nums: List[int]) -> List[int]:
        n2 = sorted(nums)
        spots = collections.defaultdict(dict)
        for i in range(len(n2)):
            if n2[i] not in spots:
                spots[n2[i]] = i
        for i in range(len(nums)):
            nums[i] =spots[nums[i]]
        return nums

s = Solution()
print(s.smallerNumbersThanCurrent([7,7,7,7]))


import time
from _pybytes import Pybytes
from _pybytes_config import PybytesConfig
import keys

conf = PybytesConfig().read_config()
pybytes = Pybytes(conf)

pybytes.start()

i = 0

while i<5:
    #The value can be a sensor reading being done here
    value = 5

    #Sending to pybytes in channel 1
    pybytes.send_signal(1, value)
    print("sending: {}".format(value))

    #Send every 5 seconds
    time.sleep(5)
    i += 1


# Definition for singly-linked list.
# class ListNode:
#     def __init__(self, x):
#         self.val = x
#         self.next = None

class Solution:
    def detectCycle(self, head: Optional[ListNode]) -> Optional[ListNode]:
        curr = head
        store = set()

        while curr:
            if curr in store:
                return curr
            store.add(curr)
            curr = curr.next

        return None


import re

keywords = [
    'card verification', 'card identification number', 'cvn', 'cid', 'cvc2',
    'cvv2', 'pin block', 'security code', 'security number', 'security no',
    'issue number', 'issue no', 'cryptogramme', 'numéro de sécurité',
    'numero de securite', 'kreditkartenprüfnummer', 'kreditkartenprufnummer',
    'prüfziffer', 'prufziffer', 'sicherheits Kode', 'sicherheitscode',
    'sicherheitsnummer', 'verfalldatum', 'codice di verifica',
    'cod. sicurezza', 'cod sicurezza', 'n autorizzazione', 'código', 'codigo',
    'cod. seg', 'cod seg', 'código de segurança', 'codigo de seguranca',
    'codigo de segurança', 'código de seguranca', 'cód. segurança',
    'cod. seguranca', 'cod. segurança', 'cód. seguranca', 'cód segurança',
    'cod seguranca', 'cod segurança', 'cód seguranca', 'número de verificação',
    'numero de verificacao', 'ablauf', 'gültig bis', 'gültigkeitsdatum',
    'gultig bis', 'gultigkeitsdatum', 'scadenza', 'data scad',
    'fecha de expiracion', 'fecha de venc', 'vencimiento', 'válido hasta',
    'valido hasta', 'vto', 'data de expiração', 'data de expiracao',
    'data em que expira', 'validade', 'valor', 'vencimento', 'transaction',
    'transaction number', 'reference number', 'セキュリティコード', 'セキュリティ コード',
    'セキュリティナンバー', 'セキュリティ ナンバー', 'セキュリティ番号'
    'amex', 'american express', 'americanexpress', 'americano espresso',
    'Visa', 'mastercard', 'master card', 'mc', 'mastercards', 'master cards',
    'diners club', 'dinersclub', 'discover', 'discover card', 'discovercard',
    'discover cards', 'JCB', 'BrandSmart', 'japanese card bureau',
    'carte blanche', 'carteblanche', 'credit card', 'cc#', 'cc#:',
    'expiration date', 'exp date', 'expiry date', 'date expiration',
    'bank card', 'bankcard', 'card number', 'card num', 'cardnumber',
    'cardnumbers', 'card numbers', 'creditcard', 'credit cards', 'creditcards',
    'ccn', 'card holder', 'cardholder', 'card holders', 'cardholders',
    'check card', 'checkcard', 'check cards', 'checkcards', 'debit card',
    'debitcard', 'debit cards', 'debitcards', 'atm card', 'atmcard',
    'atm cards', 'atmcards', 'enroute', 'en route', 'card type',
    'Cardmember Acct', 'cardmember account', 'Cardno', 'Corporate Card',
    'Corporate cards', 'Type of card', 'card account number',
    'card member account', 'Cardmember Acct.', 'card no.', 'card no',
    'card number', 'carte bancaire', 'carte de crédit', 'carte de credit',
    'numéro de carte', 'numero de carte', 'nº de la carte', 'nº de carte',
    'kreditkarte', 'karte', 'karteninhaber', 'karteninhabers',
    'kreditkarteninhaber', 'kreditkarteninstitut', 'kreditkartentyp',
    'eigentümername', 'kartennr', 'kartennummer', 'kreditkartennummer',
    'kreditkarten-nummer', 'carta di credito', 'carta credito', 'n. carta',
    'n carta', 'nr. carta', 'nr carta', 'numero carta', 'numero della carta',
    'numero di carta', 'tarjeta credito', 'tarjeta de credito',
    'tarjeta crédito', 'tarjeta de crédito', 'tarjeta de atm', 'tarjeta atm',
    'tarjeta debito', 'tarjeta de debito', 'tarjeta débito',
    'tarjeta de débito', 'nº de tarjeta', 'no. de tarjeta', 'no de tarjeta',
    'numero de tarjeta', 'número de tarjeta', 'tarjeta no', 'tarjetahabiente',
    'cartão de crédito', 'cartão de credito', 'cartao de crédito',
    'cartao de credito', 'cartão de débito', 'cartao de débito',
    'cartão de debito', 'cartao de debito', 'débito automático',
    'debito automatico', 'número do cartão', 'numero do cartão',
    'número do cartao', 'numero do cartao', 'número de cartão',
    'numero de cartão', 'número de cartao', 'numero de cartao', 'nº do cartão',
    'nº do cartao', 'nº. do cartão', 'no do cartão', 'no do cartao',
    'no. do cartão', 'no. do cartao', 'rupay', 'union pay', 'unionpay',
    'diners', 'クレジットカード番号', 'クレジットカードナンバー', 'クレジットカード＃', 'クレジットカード', 'クレジット',
    'クレカ', 'カード番号', 'カードナンバー', 'カード＃', 'アメックス', 'アメリカンエクスプレス', 'アメリカン エクスプレス',
    'Visaカード', 'Visa カード', 'マスターカード', 'マスター カード', 'マスター', 'ダイナースクラブ',
    'ダイナース クラブ', 'ダイナース', '有効期限', '期限', 'キャッシュカード', 'キャッシュ カード', 'カード名義人',
    'カードの名義人', 'カードの名義', 'デビット カード', 'デビットカード', '中国银联', '银联'
]

any_cards = [
    r'\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b', r'\b\d{16}\b',
    r'\b\d{15}\b', r'\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{3}\b'
]

class CreditCard:

    def __init__(self):
        # card regex for validating credit card numbers from
        # https://www.regular-expressions.info/creditcard.html
        # recognizes Visa, MasterCard, American Express, Diners Club,
        # Discover and JCB
        #
        # Visa, Master, Diners, JCB = 16 digits
        visa_card = r'\b(?:4[0-9]{12}(?:[0-9]{3})?)\b'
        self.re_visa = re.compile(visa_card)
        master_card = r'\b(?:5[1-5][0-9]{2}|222[1-9]|22[3-9][0-9]|2[3-6][0-9]{2}|27[01][0-9]|2720)[0-9]{12}\b'
        self.re_master = re.compile(master_card)
        diners_card = r'\b3(?:0[0-5]|[68][0-9])[0-9]{11}\b'
        self.re_diners = re.compile(diners_card)
        jcb = r'\b(?:2131|1800|35\d{3})\d{11}\b'
        self.re_jcb = re.compile(jcb)
        # Amex = 15 digits
        amex_card = r'\b3[47][0-9]{13}\b'
        self.re_amex = re.compile(amex_card)
        # anything like a card number
        self.any_cc_rex = r'(\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{4}\b)|(\b\d{16}\b)|(\b\d{15}\b)|(\b\d{4}[-\s]?\d{4}[-\s]?\d{4}[-\s]?\d{3}\b)'
        self.keywords = keywords

    def card_type(self, card_number):
        visa = self.re_visa.match(card_number)
        if visa is not None:
            return "Visa"
        master = self.re_master.match(card_number)
        if master is not None:
            return "MasterCard"
        amex = self.re_amex.match(card_number)
        if amex is not None:
            return "Amex"
        diners = self.re_diners.match(card_number)
        if diners is not None:
            return "Diners"
        jcb = self.re_jcb.match(card_number)
        if jcb is not None:
            return "JCB"
        return "Invalid/Unknown"

    # Luhn algorithm to check if card is valid
    # implementation based on
    # https://en.wikipedia.org/wiki/Luhn_algorithm
    def is_valid(self, card_number):

        sums = 0
        n = len(card_number)
        alt = False
        for i in reversed(range(n)):
            if (i == n - 1):
                alt = True
                continue
            if (alt is False):
                sums += int(card_number[i])
                alt = True
            else:
                v = int(card_number[i]) * 2
                sums += ((v // 10) + (v % 10))
                alt = False

        return ((10 - (sums % 10)) == (int(card_number[n - 1])))


"""import os

for filename in os.listdir(os.getcwd()):
    with open(os.path.join(os.getcwd(),'sample.txt'), 'r') as f:
        print(f.read())"""

# The above would be able to read the current working directory of the file.

fhand = open('sample.txt')
count = 0
for line in fhand:
    count = count + 1
print('Line Count: ', count)

fhand = open('sample.txt')
inp = fhand.read()
print(len(inp))
print(inp[:20])

fhand = open('sample.txt')
for line in fhand:
    line = line.rstrip()
    if line.startswith('From:') :
        print(line)

fname = input('Enter the file name:  ')
try:
    fhand = open(fname)
except:
    print("File cannot be opened:", fname)
    quit()

count = 0
for line in fhand:
    if line.startswith('Subject:') :
        count = count + 1
print('There were ', count, ' subject ines in ', fname)


import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import Read_Dataset
import Model
from sklearn.preprocessing import LabelEncoder
from sklearn.metrics import accuracy_score
import torch
from torch.nn.init import xavier_uniform_

t = []
with open('../IRIS/iris.txt') as txt:
    r = txt.readlines()
    for row in r:
        row = row.split('\n')[0]
        p = row.split(',')
        t.append(p)

df = pd.DataFrame(t, columns=['Septal-Length','Septal-Width','petal-Length','Petal-Width','Species'])

def prepare_data(data_file):
    dataset = Read_Dataset(data_file)
    train, test = dataset.get_splits()
    train_dl = torch.utils.data.DataLoader(train, batch_size = 32, shuffle = True)
    test_dl  = torch.utils.data.DataLoader(test, batch_size = 1024, shuffle = False)
    return train_dl, test_dl

def train_model(train_dl, model):
    loss_func = torch.nn.CrossEntropyLoss()
    optimizer = torch.optim.SGD(model.parameters(), lr = 0.01, momentum=0.9)
    for epoch in range(500):
        for i , (inputs, targets) in enumerate(train_dl):
            optimizer.zero_grad()
            y_h = model(inputs)
            loss = loss_func(y_h, targets)
            loss.backward()
            optimizer.step()

def eval_model(test_dl, model):
    predictions, actuals = list(), list()
    for i , (inputs, targets) in enumerate(test_dl):
        y_h = model(inputs)
        y_h = y_h.detach().numpy()
        actual = targets.numpy()
        y_h = np.argmax(y_h, axis=1)
        actual = actual.reshape((len(actual), 1))
        y_h = y_h.reshape((len(y_h), 1))
        predictions.append(y_h)
        actuals.append(actual)
    predictions, actuals = np.vstack(predictions), np.vstack(actuals)
    acc =  accuracy_score(actuals, predictions) *100
    return acc

def predict(row, model):
    row = torch.Tensor([row])
    y_h = model(row)
    y_h = y_h.detach().numpy()
    return y_h

path = '~/.torch/datasets/mnist'
train_dl, test_dl = prepare_data(path)
print(len(train_dl.dataset), len(test_dl.dataset))
# define the network
model = Model(1)
# # train the model
train_model(train_dl, model)
# evaluate the model
acc = eval_model(test_dl, model)
print('Accuracy: %.3f' % acc)


from harlem.data.joint_distribution import JointDistribution
import numpy as np

class IndependentMargins(JointDistribution):

    def __init__(self,
                 margins=None,
                 param_margins=None,
                 *args,
                 **kwargs
                 ):
        super(IndependentMargins, self).__init__(margins, param_margins, *args, **kwargs)

    def cdf(self, xz, margin=None):
        if not margin:
            log_cdf = sum([np.log(cdf(xz[i])) for i, cdf in enumerate(self.marginal_cdfs)])
            return np.exp(log_cdf)
        else:
            return self.marginal_cdfs[margin](xz)

    def pdf(self, xz, margin=None):

        if not margin:
            log_pdf = sum([np.log(pdf(xz[i])) for i, pdf in enumerate(self.marginal_pdfs)])
            return np.exp(log_pdf)
        else:
            return self.marginal_pdfs[margin](xz)

    def rvs(self, n):
        return [rvs(n) for rvs in self.marginal_rvss]


import spacy
import sys
import nltk
from nltk.tokenize import word_tokenize
import gzip
import json
import pandas as pd
import re
from nltk.stem import WordNetLemmatizer
import math
count = 0
# sys.stdout.write("Download progress: %d%%   \r" % (progress) )
# sys.stdout.flush()
stopwords = nltk.corpus.stopwords.words('english')
nlp = spacy.load('en_core_web_sm', disable=['parser', 'ner']);

def getNaNs(df):
    c = 0
    for d in df['reviewText']:
        c = c+1;
        if(type(d) == float):
            print(c)

# df =pd.read_csv('All_Beauty.csv')
# df = df[:1000]
# getNaNs(df)
# isNaN(df['reviewText'][547])
# df
# filteredDf= df[df['reviewText'].apply(isNotNaN)]
# len(filteredDf)
# math.isnan(df['reviewText'][547])

def filterOutNonString(df, column):
    words_only = df[column];
    # print(len(words_only))
    bin = [type(words)==str for words in words_only]
    not_words = [i for i, x in enumerate(bin) if x == False]
    for indx in not_words:
        df = df.drop([indx])
    return df

def isNotNaN(review_text):
    try:
        return not math.isnan(review_text);
    except(TypeError):
        return True

def getDfFromJSON(path):
    print("Getting data from JSON")
    data = []
    count = 0
    with gzip.open(path) as f:
        for l in f:
            doc = json.loads(l.strip())
            sys.stdout.write("Reviews processed: %d   \r" % (count) )
            sys.stdout.flush()
            count = count + 1
            data.append(doc)
            # data.append(json.loads(l.strip()))
    sys.stdout.write("Reviews processed: %d   " % (count) )
    sys.stdout.flush()
    print("Generating df...")
    df = pd.DataFrame.from_dict(data);
    print("adding Column Names to DF")
    df = df[['overall', 'reviewText','summary']];
    print("Unfiltered df", len(df))
    df = df[df['reviewText'].apply(isNotNaN)]
    print("Returning Filtered df", len(df))
    return df

def tokenize(review):
    # tokenization
    words =word_tokenize(review);
    #remove digits and other symbols except "@"--used to remove email
    words = [re.sub(r"[^A-Za-z@]", "", word) for word in words]
    #e websites and email address
    words = [re.sub(r'\S+com', '', word) for word in words]
    words = [re.sub(r'\S+@\S+', '', word) for word in words]
    #remove empty spaces
    words = [word for word in words if word!='']
    return words;

def lowerCaseList(words_list):
    return [word.lower() for word in words_list];

def removeStopWords(text_list, stop_list):
    return [word for word in text_list if word not in stop_list]

def lemmatizeList(words_list,lemmatizer):
    return [lemmatizer(word) for word in words_list]

def lemmatizer(word):
    return nlp(word)[0].lemma_

def preprocessForSentimentAnalsis(review,stop_words,lemmatizer):
    # print("    Input String       : ",review)
    words = tokenize(review);
    stop_words  = lowerCaseList(stop_words);
    input_words = lowerCaseList(words)
    # print("    Tokenized string   : ",input_words)
    stoplessWords = removeStopWords(input_words, stop_words);
    # print("    Stopless Words     : ",stoplessWords)
    lemmatizedWords = lemmatizeList(stoplessWords, lemmatizer)
    # print("    Lemmatized Words   : ",lemmatizedWords)
    return lemmatizedWords

def preprocessDocumentList(document_array,stop_words=stopwords,lemmatizer=lemmatizer):
    return [preprocessForSentimentAnalsys(document, stopwords, lemmatizer) for document in document_list]

def preprocess(review):
    try:
        return preprocessForSentimentAnalsis(review, stopwords, lemmatizer);
    except(TypeError):
        print("This is an error")
        print(type(review),review)
        return "  "

# print("Cleaning Negative String...");
# preprocess("This product had a very rough start!");
# print("Cleaning Positive String...");
# preprocess("I immediately knew I was going to enjoy this!");

#                                 Feature            Labels
# allWords        = ['w1', 'w2' , 'w3',  ... ,'wn'] [ 5  ]
# review1 :         [0,     0   ,  0  ,  ... ,  1 ] [ 2  ]
# review2 :         [1,     0   ,  1  ,  ... ,  0 ] [ 4  ]
#                                 :
#                                 :
# reviewn:          [0,     0   ,  0  , ... ,  1 ] [ 4  ]
# review2 :         [1,     0   ,   1,  ... ,  0 ] [  1 ]


import rospy
from geometry_msgs.msg import Point, Twist

kp = 1
kd = 0
ki = 0
t_prev = 0   #last time stamp
e_prev = 0  #previous error
I = 0   #last integrating error
sample_pts=[]
#gps callback (topic -----/fix)
def callback(msg):
    lon = msg.longitude
    lat = msg.latitude
    t = rospy.Time.now()
    goal_lat, goal_lon = goal()
    pid(lon, lat, goal_lon, goal_lat,t)

# from 0,0 reach 10,0 ---gazebo frame
def goal():
    goal_lon, goal_lat = 0, 0

    return goal_lat, goal_lon

def pid(current_lon, current_lat, goal_lon, goal_lat,t):

    error_lon = goal_lon - current_lon
    error_lat = goal_lat - current_lat
    corr_lon = kp*(error_lon)
    corr_lat=kp*(error_lat)
    correction=[corr_lon, corr_lat]

    # P_x = kp*error_lon
    # I = I + ki*e*(t - t_prev)
    # D = Kd*(e - e_prev)/(t - t_prev)

    return (correction)

rospy.init_node('pid') # defining the ros node - publish_node
goal_pos=rospy.Publisher("/pid/pos", Float64, queue_size=1)
rate =rospy.Rate(100) # frequency at which publishing    0.01 sec

while not rospy.is_shutdown():
    pub = rospy.Subscriber('/fix', Twist, callback)

    rate.sleep()


print("Reajuste salarial Letra B")

def CalcularSalarioReajustado(Salario, Percentual):
    return Salario + (Salario * Percentual / 100)
N = int(input("Digite o valor de N: "))
funcionarios = []

for i in range(N):
    print(f"\nFuncionário {i+1}:")
    Nome = input("Informe o nome do funcionário: ")
    Salario = float(input("Informe o Salário: "))

    funcionario = {
        "Nome": Nome,
        "Salario": Salario
    }

    funcionarios.append(funcionario)

print("\nInformações dos funcionários:")
print(f"Nome: {funcionario['nome']}")
print(f"Salário: {funcionario['salario']}")

if Salario != N:
    NovoSalario_25 = CalcularSalarioReajustado(Salario, 25)
    NovoSalario_20 = CalcularSalarioReajustado(Salario, 20)
    NovoSalario_15 = CalcularSalarioReajustado(Salario, 15)
    NovoSalario_10 = CalcularSalarioReajustado(Salario, 10)

print(funcionario,"seu salario de" , Salario, f"foi reajustado para 25%, sendo: R${NovoSalario_25:.2f}", "seu novo salario.")
print(funcionario,"seu salario de" , Salario, f"foi reajustado para 20%, sendo: R${NovoSalario_20:.2f}", "seu novo salario.")
print(funcionario,"seu salario de" , Salario, f"foi reajustado para 15%, sendo: R${NovoSalario_15:.2f}", "seu novo salario.")
print(funcionario,"seu salario de" , Salario, f"foi reajustado para 10%, sendo: R${NovoSalario_10:.2f}", "seu novo salario.")

print(".")


import urllib
import time

def download(url:str,path:str):
  '''ファイルのダウンロード

  url: ファイルのurl
  path: 保存先
  '''
  print("音声登録")
  print("url:" + url)
  print("path:" + path)
  header = {"User-Agent": "Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:47.0) Gecko/20100101 Firefox/47.0"}
  request = urllib.request.Request(url=url,headers=header)
  with urllib.request.urlopen(request) as web_file, open(path,'wb') as local_file:
      local_file.write(web_file.read())
  #time.sleep(0.1)


# https://leetcode.com/contest/biweekly-contest-80/problems/successful-pairs-of-spells-and-potions/
# 6096. Successful Pairs of Spells and Potions
from typing import List
from bisect import bisect

class Solution:
    def successfulPairs(self, spells: List[int], potions: List[int], success: int) -> List[int]:
        smallest_spells = [0] * len(potions)
        for i, p in enumerate(potions):
            smallest_spells[i] = success / p
        smallest_spells.sort()  # O(nlogn)

        ans = []
        for s in spells:
            ans.append(bisect(smallest_spells, s))
        return ans

print(Solution().successfulPairs([5, 1, 3], [1, 2, 3, 4, 5], 7))
print(Solution().successfulPairs([3, 1, 2], [8, 5, 8], 16))


import torch
from allennlp.nn import util
from allennlp.nn.util import min_value_of_dtype, replace_masked_values

def batched_span_select(target: torch.Tensor, spans: torch.LongTensor):
    """
    The given `spans` of size `(batch_size, num_spans, 2)` indexes into the sequence
    dimension (dimension 2) of the target, which has size `(batch_size, sequence_length,
    embedding_size)`.
    This function returns segmented spans in the target with respect to the provided span indices.
    It does not guarantee element order within each span.
    # Parameters
    target : `torch.Tensor`, required.
        A 3 dimensional tensor of shape (batch_size, sequence_length, embedding_size).
        This is the tensor to be indexed.
    indices : `torch.LongTensor`
        A 3 dimensional tensor of shape (batch_size, num_spans, 2) representing start and end
        indices (both inclusive) into the `sequence_length` dimension of the `target` tensor.
    # Returns
    span_embeddings : `torch.Tensor`
        A tensor with shape (batch_size, num_spans, max_batch_span_width, embedding_size]
        representing the embedded spans extracted from the batch flattened target tensor.
    span_mask: `torch.BoolTensor`
        A tensor with shape (batch_size, num_spans, max_batch_span_width) representing the mask on
        the returned span embeddings.
    """
    # both of shape (batch_size, num_spans, 1)
    span_starts, span_ends = spans.split(1, dim=-1)

    # shape (batch_size, num_spans, 1)
    # These span widths are off by 1, because the span ends are `inclusive`.
    span_widths = span_ends - span_starts

    # We need to know the maximum span width so we can
    # generate indices to extract the spans from the sequence tensor.
    # These indices will then get masked below, such that if the length
    # of a given span is smaller than the max, the rest of the values
    # are masked.
    max_batch_span_width = span_widths.max().item() + 1

    # Shape: (1, 1, max_batch_span_width)
    max_span_range_indices = util.get_range_vector(
        max_batch_span_width, util.get_device_of(target)
    ).view(1, 1, -1)
    #     print(max_batch_span_width)
    #     print(max_span_range_indices)
    # Shape: (batch_size, num_spans, max_batch_span_width)
    # This is a broadcasted comparison - for each span we are considering,
    # we are creating a range vector of size max_span_width, but masking values
    # which are greater than the actual length of the span.
    #
    # We're using <= here (and for the mask below) because the span ends are
    # inclusive, so we want to include indices which are equal to span_widths rather
    # than using it as a non-inclusive upper bound.
    span_mask = max_span_range_indices <= span_widths
    #     raw_span_indices = span_ends - max_span_range_indices
    raw_span_indices = span_starts + max_span_range_indices
    #     print(raw_span_indices)
    #     print(target.size())
    # We also don't want to include span indices which are less than zero,
    # which happens because some spans near the beginning of the sequence
    # have an end index < max_batch_span_width, so we add this to the mask here.
    span_mask = span_mask & (raw_span_indices < target.size(1))
    #     print(span_mask)
    #     span_indices = torch.nn.functional.relu(raw_span_indices.float()).long()
    span_indices = raw_span_indices * span_mask
    #     print(span_indices)

    # Shape: (batch_size, num_spans, max_batch_span_width, embedding_dim)
    span_embeddings = util.batched_index_select(target, span_indices)

    return span_embeddings, span_mask


from typing import List

class Solution:
    def search(self, nums: List[int], target: int) -> int:
        left = 0
        right = len(nums)-1
        # = covers the case where there is one number in the list
        while (left <= right):
            medium = (left+right)//2
            if nums[medium] > target:
                right = medium-1
            elif nums[medium] < target:
                left = medium+1
            else:
                return medium
        return -1


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sun Apr  5 03:28:06 2020

@author: eneskuralay
"""

class Emp(object):
    age = 25
    salary = 1000       # $

    def ageSalaryRatio(self):
        result = self.age / self.salary
        print("Method: ",result)
        return result

def ageSalaryRatio(age,salary):
    result = age/salary
    print("Func: ",result)
    return result

e1 = Emp()
e1.ageSalaryRatio()
print(e1.ageSalaryRatio())

age1= 30
salary1 = 3000

ageSalaryRatio(age1,salary1)
print(ageSalaryRatio(age1,salary1))

## Area

def findArea(a,b):      # a = pi , b = r

    area = a*b**2
    return area

pi = 3.14
r = 5

result1 = findArea(pi,r)
result2 = findArea(pi,10)

result3 = result1 + result2

print("result 1 : ",result1," result 2 : ",result2," result 3 : ",result3)


""" Create a function that sakes an integer argument of seconds
and converts the value into a string describing how many hours
and minutes comprise that many seconds.

Any remaining seconds left over are ignored.

Note:
The string output needs to be in the specific form - "X hour(s) and X minute(s)"

3600 --> "1 hour(s) and 0 minute(s)"
3601 --> "1 hour(s) and 0 minute(s)"
3500 --> "0 hour(s) and 58 minute(s)"
323500 --> "89 hour(s) and 51 minute(s)"

Tests:
    >>> to_time(3600)
    '1 hour(s) and 0 minute(s)'

    >>> to_time(3601)
    '1 hour(s) and 0 minute(s)'

    >>> to_time(3500)
    '0 hour(s) and 58 minute(s)'

    >>> to_time(323500)
    '89 hour(s) and 51 minute(s)'
"""

def to_time(seconds):

    hours = seconds // 3600
    # print(hours)
    rem_minutes = seconds % 3600
    minutes = rem_minutes // 60
    # print(minutes)

    return f"{hours} hour(s) and {minutes} minute(s)"

if __name__ == '__main__':
    import doctest
    if doctest.testmod().failed == 0:
        print("\n ✨ WE GOT IT - THIS TIME! ⌚✨ ")


def lcm(x, y):
   if x > y:
       z = x
   else:
       z = y
   while(True):
       if((z % x == 0) and (z % y == 0)):
           lcm = z
           break
       z += 1
   return lcm
def norm(A,B,C):
    if C==1:
        A=A*B
        B=1
        print(str(A) + " * (" + str(B) + ")^1/" + str(C))
        return;
    coeff = 0
    c_isneg=False
    if C<0:
        c_isneg=True
        C=abs(C)

    tempB=B
    while(tempB%C ==0):
        coeff+=1
        tempB=tempB//C
    coeff = (coeff//C) * C
    if coeff ==0:
        coeff =1
    B=B//coeff
    if B==1:
        C=1
    if c_isneg:
        x=A
        y=coeff
        while y != 0:
            #get gcd(A,coeff)
            #gcd will be x
            (x, y) = (y, x % y)
        A=A//x
        B=((coeff//x)**C)*B
        #coeff = coeff//x

        C=-C
    else:
        A=A*coeff
    print(str(A) + " * (" + str(B) + ")^1/" + str(C))

def multiply(A1,B1,C1,A2,B2,C2):
    if C1==C2:
        A=A1*A2
        B=B1*B2
        C=C1
        norm(A,B,C)
    if (C1<0 and C2<0) or (C1>0 and C2>0):
        is_C_neg = False
        if C1<0:
            is_C_neg = True
            C1=abs(C1)
            C2=abs(C2)
        l = lcm(C1,C2)
        B1 = B1 ** (l//C1)
        B2 = B2 ** (l//C2)
        C=l
        B=B1*B2
        A=A1*A2
        if is_C_neg:
            C=-C
        norm(A,B,C)
    else:
        print("cannot multiply cleanly (there are radicals in both numerator and denominator)")

multiply(1,5,3,1,2,2)


import pickle
import matplotlib.pyplot as plt
import numpy as np

data = {}
with open('data_generator.pickle','rb') as f:
	data = pickle.load(f)
	f.closed

toi_2009 = 0
toi_2010 = 0
hindu_2009 = 0
hindu_2010 = 0
x = np.arange(2)
#plt.title('Total articles by year')

for i, j in enumerate(data['article_publisher']):
	if j=='toi':
		if (data['article_year'][i]=='2009'):
			toi_2009 += 1
		else:
			toi_2010 += 1
	else:
		if (data['article_year'][i]=='2009'):
			hindu_2009 += 1
		else:
			hindu_2010 += 1

plt.figure(1)
plt.subplot(121)
plt.bar(x,height=[toi_2009, toi_2010], color='green')
plt.xticks(x,['TOI 2009', 'TOI 2010'])
plt.title('Times of India')

plt.subplot(122)
plt.bar(x, height=[hindu_2009, hindu_2010])
plt.xticks(x,['HINDU 2009', 'HINDU 2010'])
plt.title('The Hindu')

#plt.figure(2)

state_dict = {}
state = []
for var in data['article_state']:
	if var not in state:
		state.append(var)
		state_dict[var] = [0 for i in xrange(7)]

state.sort()

for i, var in enumerate(data['target']):
	for j ,k in enumerate(var):
		state_dict[ data['article_state'][i] ][j] += k

plt.rcdefaults()
fig, ax = plt.subplots(1,7, sharey=True)
labl = ['awareness','drugs','finance','future_prospects','legal','statistics','stigma']
for i in xrange(7):
	#plt.subplot(1,7,i+1)
	y_pos = np.arange(len(state))
	cl =[]
	for st in state:
		cl.append(state_dict[st][i])
	ax[i].barh(y_pos, cl, align='center', color='green', ecolor='black')
	if i==0:
		ax[i].set_yticks(y_pos)

	ax[i].invert_yaxis()
	if i>0:
		ax[i].yaxis.set_visible(False)
	else:
		ax[i].set_yticklabels(state, minor=False)
	ax[i].set_title(labl[i])
	for j, v in enumerate(cl):
		ax[i].text(v,j,str(v))

# plt.rcdefaults()
# # fig, ax = plt.subplots()
# y_pos = np.arange(7)
# print len(y_pos)
# print len(state_dict[state[0]])
# plt.barh(y_pos, state_dict[state[0]])

plt.show()


from django.urls import path
# 引入views.py
from . import views

app_name = 'AutoResult'

urlpatterns = [

    path('AutoItem_edit/', views.AutoItem_edit, name='AutoItem_edit'),
    path('AutoResult_edit/', views.AutoResult_edit, name='AutoResult_edit'),
    path('AutoResult_search/', views.AutoResult_search, name='AutoResult_search'),
    path('AutoResult_summary/', views.AutoResult_summary, name='AutoResult_summary'),
    # path('CDM-export/', views.CDM_export, name='CDM_search'),

]


import sqlite3
import json
import sys

sys.path.append('/Users/kevinlbelk/Documents/Programing/CodePlatoon/Week11/Saturday/yet-another-blog/')
from database.initialize_db import insert_users, insert_posts

def build_json_response(json_body):
    return f"HTTP/1.1 200 OK\r\nContent-Type:application/json\r\nContent-Length:{len(json_body)}\r\n\r\n{json_body}"

def handle_users(request):
    if request["method"] == "GET":
        return json.dumps({"req": "users GET"})
    elif request["method"] == "POST":
        return json.dumps({"req": "users POST"})

def get_user_id(username):
        with sqlite3.connect("../database/blog_db.sqlite") as conn:
            user_query = """
            SELECT username, id from User;
            """
            cur = conn.cursor()
            users = cur.execute(user_query).fetchall()
        conn.close()
        user_list = [ name for name in users if name[0] == username]
        if len(user_list) > 0:
            return user_list[0][1]
        else :
            with sqlite3.connect("../database/blog_db.sqlite") as conn:
                cur = conn.cursor()
                cur.executescript(insert_users(username, f'{username}.gmail.com'))
            conn.close()
            return ((users[-1][1]) + 1)

def all_post():
    with sqlite3.connect("../database/blog_db.sqlite") as conn:
        all_posts_query = """
        select title,contents,username from Post join User on Post.user_id = User.id;
        """
        cur = conn.cursor()
        all_posts = cur.execute(all_posts_query).fetchall()
        all_posts_formatted = [{"author": p[2], "title": p[0], "content": p[1]} for p in all_posts]
    conn.close()
    return all_posts_formatted

def handle_posts(request):
    if request["method"] == "GET":
        all_posts_formatted =  all_post()
        return json.dumps({"data": all_posts_formatted})
    elif request["method"] == "POST":
        user_id = get_user_id(request['author'])
        with sqlite3.connect("../database/blog_db.sqlite") as conn:
            cur = conn.cursor()
            cur.executescript(insert_posts(request['title'], request['content'], user_id ))
        conn.close()
        all_posts_formatted =  all_post()
        return json.dumps({"data": all_posts_formatted})


from chirpy.response_generators.one_turn_hack.responses import one_turn_responses
from chirpy.core.regex.regex_template import RegexTemplate

class ChattyTemplate(RegexTemplate):
    slots = {
        'chatty_phrase': [str(key) for key in one_turn_responses.keys()],
    }
    templates = [
        "{chatty_phrase}",
        "alexa {chatty_phrase}",
    ]
    positive_examples = [("talk about you", {'chatty_phrase': "talk about you"}),
                         ("can i have a conversation", {'chatty_phrase': "can i have a conversation"})]
    negative_examples = ["let's talk about movies",
                         "news",
                         "politics"]


#This file is created to load the training and test data sets and feature selection

import sys
import os
import pandas as pd

base_folder    = os.path.join(os.getcwd(), '..', '..')

#Path to sampled training and test data
training_data = (r'D:\rubenwe\Documents\MainProgramming\Data\data_vrachtwagen_training.csv')
test_data = (r'D:\rubenwe\Documents\MainProgramming\Data\data_vrachtwagen_test.csv')

#Independent factors + dependent factor (speed) to select from data set
feature_sel_SVR = ['SpdLimit', 'LengthMeter', 'IsPaved', 'HasSpdBumps', 'NrSharpTurns'
                     ,'IsPriority', 'IsUrban', 'IsRamp']
feature_sel_RF = ['SpdLimit', 'LengthMeter', 'IsPaved', 'HasSpdBumps', 'NrSharpTurns'
                     ,'IsPriority', 'IsUrban', 'IsRamp']
feature_sel_LR = ['SpdLimit', 'LengthMeter', 'IsPaved', 'HasSpdBumps', 'NrSharpTurns'
                     ,'IsPriority', 'IsUrban', 'IsRamp']
feature_sel_NN = ['SpdLimit', 'LengthMeter', 'IsPaved', 'HasSpdBumps', 'NrSharpTurns'
                     ,'IsPriority', 'IsUrban', 'IsRamp']

                     #, 'SpdPatMax', 'SpdPatAvg', 'SpdPatMin', 'HasTrafficSignal', ]

def load_and_prepare(data_file_name):
    #Load data file with speeds and factors for road segments
    data_set_learning = pd.read_csv(data_file_name , sep = ';')
    data_set_learning = data_cleaning(data_set_learning)
    return data_set_learning

def data_cleaning(data):
    #Ignore speed if GPS point is more than 20m away
    data = data[data.MeterAwayFromGps <= 20]
    #Ignore is speed is on ferry edge
    data = data[data.IsFerry == 0]
    #Ignore speed limits of >130
    data = data[data.SpdLimit <= 130]
    return data


from heapq import heappush, heappop

from day_base import Day

dimensions = (0, 0)

class PriorityQueue:

    def __init__(self, iterable=[]):
        self.heap = []
        for value in iterable:
            heappush(self.heap, (0, value))

    def add(self, value, priority=0):
        heappush(self.heap, (priority, value))

    def pop(self):
        _, value = heappop(self.heap)
        return value

    def __len__(self):
        return len(self.heap)

def get_neighbours(i, j, i_max, j_max):
    neighbours = {(i, j + 1), (i, j - 1), (i - 1, j), (i + 1, j)}
    return list(filter(lambda x: 0 <= x[0] < i_max and 0 <= x[1] < j_max, neighbours))

def get_successors(i, j, grid):
    neighbours = get_neighbours(i, j, *dimensions)
    current_height = grid[i][j]
    successors = []
    for (k, l) in neighbours:
        if grid[k][l] - 1 <= current_height:
            successors.append((k, l))
    return successors

def get_distance(i, j, k, l):
    return abs(i - k) + abs(j - l)

def find_path(start: tuple[int, int], goal: tuple[int, int], grid) -> list[tuple[int, int]]:
    visited = set()
    origin = dict()
    distance = {start: 0}
    frontier = PriorityQueue([start])

    while frontier:
        node = frontier.pop()
        if node in visited:
            continue
        if node == goal:
            return reconstruct_path(origin, start, node)
        visited.add(node)

        for neighbour in get_successors(*node, grid):
            frontier.add(neighbour, distance[node] + 1 + get_distance(*neighbour, *goal))
            if neighbour not in distance or distance[node] + 1 < distance[neighbour]:
                distance[neighbour] = distance[node] + 1
                origin[neighbour] = node
    return None

def reconstruct_path(came_from, start, goal):
    reverse_path = [goal]
    while goal != start:
        goal = came_from[goal]
        reverse_path.append(goal)
    return list(reversed(reverse_path))

def print_grid_attempted(grid, locations):
    for i, row in enumerate(grid):
        str_row = ""
        for j, val in enumerate(row):
            if (i, j) in locations:
                str_row += "\033[92m" + chr(ord('a') + grid[i][j]) + "\033[0m"
            else:
                str_row += chr(ord('a') + val)
        print(str_row)
    print()

class Day12(Day):
    """
    With the help of A* search we find the shortest path from S to E.
    The implementation of the A* search algorithm is based on
        https://leetcode.com/problems/shortest-path-in-binary-matrix/solutions/313347/A*-search-in-Python/
    """

    def __init__(self):
        super().__init__(2022, 12, 'Hill Climbing Algorithm', expected_a=31, expected_b=29, debug=False)

    def parse(self):
        grid = [[ord(char) - ord('a') for char in row] for row in self.input]
        start_pos = None
        for i, row in enumerate(grid):
            for j, height in enumerate(row):
                if height == ord('S') - ord('a'):
                    grid[i][j] = -1
                    start_pos = (i, j)
                if height == ord('E') - ord('a'):
                    grid[i][j] = 26
                    end_pos = (i, j)
        return grid, start_pos, end_pos

    def part_a(self) -> int:
        grid, start_position, end_position = self.parse()
        global dimensions
        dimensions = (len(grid), len(grid[0]))
        path = find_path(start_position, end_position, grid)
        return len(path) - 1

    def part_b(self) -> int:
        grid, _, end_position = self.parse()
        global dimensions

        # Replace the original starting position.
        grid = [[(x if x > 0 else 0) for x in row] for row in grid]

        # Find all possible starting coordinates.
        starting_coordinates = []
        for i in range(len(grid)):
            for j in range(len(grid[0])):
                if grid[i][j] == 0:
                    starting_coordinates.append((i, j))

        # Find the shortest path
        shortest_path = None

        for i, starting_coordinate in enumerate(starting_coordinates):
            path = find_path(starting_coordinate, end_position, grid)
            if path is not None and (shortest_path is None or len(shortest_path) > len(path)):
                shortest_path = path

        return len(shortest_path) - 1

if __name__ == '__main__':
    (Day12()).run()


from tfmodellib import TFModel, TFModelConfig, graph_def, docsig

import tensorflow as tf

@graph_def
@docsig
def build_linreg_graph(input_tensor, out_size):
    """
    Defines a graph for linear regression.

    Parameters
    ----------
    input_tensor : Tensor
        The input tensor of size NUM_SAMPLES x INPUT_SIZE.

    out_size : int
        Dimensionality of the output space.

    Returns
    -------
    y_output : Tensor
        The network's output.
    """

    y_output = tf.layers.dense(input_tensor, units=out_size, activation=None)

    return y_output

class LinRegConfig(TFModelConfig):

    def init(self):
        self.update(
                in_size=1,
                out_size=1)

class LinReg(TFModel):

    def build_graph(self):

        # define input and target placeholder
        self.x_input = tf.placeholder(dtype=tf.float32, shape=[None, self.config['in_size']])
        self.y_target = tf.placeholder(dtype=tf.float32, shape=[None, self.config['out_size']])

        # define the base graph
        self.y_output = build_linreg_graph(input_tensor=self.x_input, **self.config)

        # define learning rate
        self.learning_rate = tf.placeholder(dtype=tf.float32, shape=[])

        # define loss
        self.loss = tf.losses.mean_squared_error(self.y_target, self.y_output)

        # define optimizer
        self.optimizer = tf.train.AdamOptimizer(learning_rate=self.learning_rate)
        self.minimize_op = self.optimizer.minimize(self.loss)

    def run_update_and_loss(self, batch_inputs, batch_targets, learning_rate):
        loss, _ = self.sess.run([self.loss, self.minimize_op],
                feed_dict={
                        self.x_input: batch_inputs,
                        self.y_target: batch_targets,
                        self.learning_rate: learning_rate})
        return loss

    def run_loss(self, batch_inputs, batch_targets, learning_rate):
        loss = self.sess.run(self.loss,
                feed_dict={
                        self.x_input: batch_inputs,
                        self.y_target: batch_targets})
        return loss

    def run_output(self, inputs):
        return self.sess.run(self.y_output, feed_dict={self.x_input: inputs})

if __name__ == '__main__':

    import numpy as np
    import matplotlib.pyplot as plt
    import logging

    # create the model
    conf = LinRegConfig(log_level=logging.DEBUG, in_size=1, out_size=1)
    model = LinReg(conf)

    # generate some data
    x = np.random.rand(1000).reshape((-1,1))
    y = 10 * np.random.randn() * x + 10 * np.random.randn() + np.random.randn(x.size).reshape(x.shape)

    x_train = x[:int(x.shape[0]*0.8)]
    y_train = y[:x_train.shape[0]]
    x_valid = x[x_train.shape[0]:]
    y_valid = y[x_train.shape[0]:]

    # run the training
    for _ in range(50):
        model.train(
                train_inputs=x_train,
                train_targets=y_train,
                validation_inputs=x_valid,
                validation_targets=y_valid,
                batch_size=None,
                learning_rate=1.0)

    # get estimates
    y_est = model.run_output(inputs=x_valid)

    # plot data and estimates
    plt.plot(x_valid, y_valid, ls='none', marker='.', color='g')
    plt.plot(x_valid, y_est, ls='none', marker='.', color='b')
    plt.show()


#!/bin/python3
# author: Jan Hybs
import datetime
import os
import pathlib
import shutil

from exceptions import FatalException, CompileException, ConfigurationException
from utils.crypto import b64encode
from collections import OrderedDict
from uuid import uuid4

from loguru import logger

from database.objects import Course, Languages, Problem, User
from env import Env
from processing import ExecutorStatus, ProcessRequestType
from processing.result import ExecutorResult
from utils.events import MultiEvent

def hook_warn_empty_input(id, result: ExecutorResult):
    if not result.stdinn:
        raise FatalException('Input file is empty', ['please double check your input files for test', id])
    return result

def add_cmd_to_result(id, result: ExecutorResult):
    if not result.console:
        result.console = ' '.join(result.cmd)
    return result

class ProcessRequest(object):
    """
    :type _compile_result: ExecutorResult
    :type _run_results: OrderedDict[str, ExecutorResult]
    :type course: Course
    :type problem: Problem
    :type lang: Languages
    :type user: User
    :type action_executor: processing.actions.AbstractAction
    """

    def __init__(self, user, lang, type, solution, course, problem, cases=None, docker=True, **kwargs):
        self.course = Course.db()[course]
        self.problem = self.course.problem_db[problem]
        self.lang = Languages.db().get(lang)
        self.case_ids = cases or self.problem.test_ids
        self.cases = self.problem.tests_from_ids(self.case_ids)
        self.datetime = datetime.datetime.now()

        self.user = user
        self.solution = solution
        self.type = ProcessRequestType(type)

        self.rand = '%s-%s' % (self.user.id, uuid4())
        self.uuid = uuid4().hex
        # self.rand = 'jan.hybs-c45ea043-15ae-4c3b-9c5d-352bc8cd5937'

        self._compile_result = None
        self._evaluation = ExecutorResult.empty_result(id='evaluation')
        self._run_results = OrderedDict()
        self.rest = kwargs

        self.event_process = MultiEvent('on-process')

        self.event_compile = MultiEvent('on-compile')
        self.event_execute = MultiEvent('on-execute')

        self.event_compare_test = MultiEvent('on-compare-test')
        self.event_execute_test = MultiEvent('on-execute-test')
        self.running = False
        self.docker = docker
        self.action_executor = None

        self.problem_dir = pathlib.Path(Env.problems, self.course.id, self.problem.id)
        self.result_dir = pathlib.Path(Env.tmp, self.course.id, self.problem.id, self.rand)

        # create dirs, but it SHOULD ALREADY EXISTS
        self.problem_dir.mkdir(parents=True, exist_ok=True)
        self.problem_dir.joinpath('output').mkdir(parents=True, exist_ok=True)
        self.problem_dir.joinpath('input').mkdir(parents=True, exist_ok=True)
        self.problem_dir.joinpath('.error').mkdir(parents=True, exist_ok=True)

    def __repr__(self):
        return ('Request(\n'
                '  user={self.user.id}\n'
                '  course={self.course.name}\n'
                '  problem={self.problem.id}\n'
                '  language={self.lang}\n'
                '  type={self.type}\n'
                ')').format(self=self)

    def peek(self):
        return dict(
            uuid=self.uuid,
            user=self.user.id,
            course=self.course,
            lang=self.lang,
            prob=self.problem,
            action=self.type,
            evaluation=self._evaluation,
        )

    @property
    def result_list(self):
        return self._run_results.items()

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

    def process(self):
        self._run_results = OrderedDict()
        for id, case, subcase in self._walk_cases():
            self[id] = ExecutorResult.empty_result(id=id)

        # no tests provided
        if not self._run_results:
            self.event_process.open_event.trigger(self, self._run_results)
            self.evaluate_solution()
            self.event_process.close_event.trigger(self, self._run_results)
            raise ConfigurationException('No tests provided in yaml config file')

        # emit event
        if self.type is ProcessRequestType.GENERATE_INPUT:
            from processing.actions.generate_input import ProcessRequestGenerateInput
            self.action_executor = ProcessRequestGenerateInput(self, self.problem_dir)

        elif self.type is ProcessRequestType.GENERATE_OUTPUT:
            from processing.actions.generate_output import ProcessRequestGenerateOutput
            self.action_executor = ProcessRequestGenerateOutput(self, self.problem_dir)

        elif self.type is ProcessRequestType.SOLVE:
            from processing.actions.solve import ProcessRequestSolve
            self.action_executor = ProcessRequestSolve(self, self.result_dir)
        else:
            raise FatalException('Unsupported action {}'.format(self.type))

        try:
            self.event_process.open_event.trigger(self, self._run_results)
            self.action_executor.run()
            self.evaluate_solution()
            self.event_process.close_event.trigger(self, self._run_results)
        except CompileException as ex:
            logger.info('compilation failed')
            self.evaluate_solution()
            raise ex

        return self._run_results

    def _walk_cases(self):
        for case in self.cases:
            for subcase in case.cases():
                yield subcase.id, case, subcase

    def __getitem__(self, id):
        return self._run_results[id]

    def __setitem__(self, id, value):
        self._run_results[id] = value

    def __iter__(self):
        return iter(self._walk_cases())

    def destroy(self):
        try:
            if self.action_executor:
                self.action_executor.destroy()
            else:
                logger.warning('cannot destroy empty action_executor')
        except:
            logger.exception('could not destroy executor')

    def evaluate_solution(self):
        if not self._run_results:
            self._evaluation = ExecutorResult(status=ExecutorStatus.SKIPPED).register('FINAL RESULT')
            self._evaluation.message = 'No tests to run'
            return

        statuses = [x.status for x in self._run_results.values()]
        if self._compile_result:
            if self._compile_result.status in (ExecutorStatus.COMPILATION_FAILED, ExecutorStatus.GLOBAL_TIMEOUT):
                for k, v in self._run_results.items():
                    self._run_results[k].status = ExecutorStatus.SKIPPED

            statuses = [x.status for x in self._run_results.values()]
            statuses.append(self._compile_result.status)

        unique = set(statuses)
        max_status = max(unique)

        self._evaluation = ExecutorResult(None, max_status).register('FINAL RESULT')

        if self.action_executor:
            self._evaluation.duration = self.action_executor.duration

        status = self._evaluation.status
        action = self.type
        if action is ProcessRequestType.SOLVE:
            self._evaluation.message = status.message

        elif action is ProcessRequestType.GENERATE_INPUT:
            if status is ExecutorStatus.OK:
                self._evaluation.message = 'Input files generated'
            else:
                self._evaluation.message = status.message

        elif action is ProcessRequestType.GENERATE_OUTPUT:
            if status is ExecutorStatus.OK:
                self._evaluation.message = 'Output files generated'
            else:
                self._evaluation.message = status.message

    def save_result(self):
        if self.type is not ProcessRequestType.SOLVE:
            logger.warning('No need to save action type {} ', self.type)
            logger.warning('It is already saved in a problem directory: {}', self.problem_dir)
            return

        format_dict = dict(
            problem=self.problem,
            course=self.course,
            user=self.user,
            lang=self.lang,
            status=self._evaluation.status,
            datetime=self.datetime,
        )
        student_base_dir = Env.results.joinpath(Env.student_dir_format.format(**format_dict))

        student_base_dir.mkdir(parents=True, exist_ok=True)
        results = list(student_base_dir.glob('*'))
        total_attempts = len(results)
        format_dict['attempt'] = total_attempts + 1

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

        student_version = Env.student_version_format.format(**format_dict)

        student_full_dir = student_base_dir.joinpath(student_version)
        logger.info('Saving result to {}', student_full_dir)

        if self.result_dir.exists():
            shutil.copytree(self.result_dir, student_full_dir)
        else:
            logger.warning('dir {} does not exists', self.result_dir)
        student_full_dir.mkdir(parents=True, exist_ok=True)

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

        results = list()
        for id, test in self._run_results.items():
            results.append(Env.student_result_test_txt_format.format(test=test))

        format_dict['results'] = '\n'.join(results)
        result_txt = Env.student_result_txt_format.format(**format_dict)
        student_full_dir.joinpath('result.txt').write_text(result_txt)
        logger.info('saving result.txt: \n{}', result_txt)

    def get_log_dict(self):
        doc = dict(
            user=self.user.id,
            datetime=datetime.datetime.now(),
            action=self.type.value,
            course=self.course.id,
            problem=self.problem.id,
            docker=self.docker,
        )
        if self.lang:
            doc['language'] = self.lang.id
        if self.solution:
            doc['solution'] = self.solution

        return doc

    def get_result_dict(self):
        base = self.get_log_dict()
        if self._evaluation:
            base['result'] = self._evaluation.peek(full=False)

        if self._compile_result:
            base['compilation'] = self._compile_result.peek(full=False)

        if self._run_results:
            tests = list()
            for id, test in self._run_results.items():
                if test:
                    tests.append(test.peek(full=False))
            base['tests'] = tests

        return base

    def _register_attachment(self, id, name, path: pathlib.Path):
        in_ref = path.parent.parent == self.problem_dir

        if in_ref:
            url = path.parts[-5:]
        else:
            url = path.parts[-6:]

        parts64 = b64encode(dict(url=url))
        self[id].add_attachment(dict(name=name+'.txt', url=parts64))

def _configure_cmd(cmd, file):
    opts = dict(
        filename=os.path.basename(file),
        filepath=file,
        file=os.path.basename(file).split('.')[0],
    )
    cp = []
    for c in cmd:
        s = str(c).replace('<', '{').replace('>', '}')
        cp.append(s.format(**opts))
    return cp


import tensorflow.keras.backend as K
import tensorflow as tf
import numpy as np
import pandas as pd
import click

from ..util.distribute import distributed, ReduceOp
from ..util.dataset import unmasked
from ..image.filter.gaussian import gaussian
from ..image.filter.laplace import gaussian_laplace_multi
from ..eval.footprint import calc_sim_cos
from ..eval.footprint import calc_sim_area

from .segment import get_segment_index

def modify_footprint(footprint):
    i = np.arange(footprint.shape[0])
    j = np.argpartition(-footprint, 1)
    second = footprint[i, j[:, 1]]
    footprint[i, j[:, 0]] = second
    cond = second > 0.0
    return cond

def check_accept(footprint, peaks, radius, thr_abs, thr_rel, thr_sim):
    peaks['accept'] = 'yes'
    x = peaks['radius']
    cond1 = x == radius[0]
    cond2 = x == radius[-1]

    segment = (footprint > 0.5).astype(np.float32)
    area = np.sum(segment, axis=1)
    peaks['area'] = area
    cond3 = (area >= thr_abs + thr_rel * np.pi * x ** 2)

    sim = calc_sim_area(segment, ~(cond1 ^ cond2 ^ cond3))
    peaks['sim'] = sim
    cond4 = sim > thr_sim

    peaks.loc[cond4, 'accept'] = 'large_sim'
    peaks.loc[cond3, 'accept'] = 'large_area'
    peaks.loc[cond2, 'accept'] = 'large_r'
    peaks.loc[cond1, 'accept'] = 'small_r'

def clean_footprint(data, index, mask, radius, batch, verbose):

    @distributed(ReduceOp.CONCAT, ReduceOp.CONCAT, ReduceOp.CONCAT, ReduceOp.CONCAT, ReduceOp.CONCAT)
    def _clean(imgs, mask, radius):
        logs = gaussian_laplace_multi(imgs, radius)
        nk, h, w = tf.shape(logs)[0], tf.shape(logs)[2], tf.shape(logs)[3]
        hw = h * w
        lsr = tf.reshape(logs, (nk, -1))
        pos = tf.cast(tf.argmax(lsr, axis=1), tf.int32)
        rs = pos // hw
        ys = (pos % hw) // w
        xs = (pos % hw) % w
        logs = tf.gather_nd(logs, tf.stack([tf.range(nk), rs], axis=1))
        tf.print(tf.shape(logs))

        out = tf.TensorArray(tf.float32, size=0, dynamic_size=True)
        firmness = tf.TensorArray(tf.float32, size=0, dynamic_size=True)
        nx = tf.size(rs)
        for k in tf.range(nx):
            img, log, y, x = imgs[k], logs[k], ys[k], xs[k]
            pos = get_segment_index(log, y, x, mask)
            slog = tf.gather_nd(log, pos)
            slogmin = tf.math.reduce_min(slog)
            slogmax = tf.math.reduce_max(slog)
            simg = tf.gather_nd(img, pos)
            simgmin = tf.math.reduce_min(simg)
            simgmax = tf.math.reduce_max(simg)
            val = (simg - simgmin) / (simgmax - simgmin)
            img = tf.scatter_nd(pos, val, [h, w])
            out = out.write(k, tf.boolean_mask(img, mask))
            firmness = firmness.write(k, (slogmax - slogmin) / (simgmax - simgmin))
        return out.stack(), firmness.stack(), rs, ys, xs

    nk = data.shape[0]
    dataset = tf.data.Dataset.from_tensor_slices(data)
    dataset = unmasked(dataset, mask)
    dataset = dataset.batch(batch)

    mask = tf.convert_to_tensor(mask, tf.bool)
    radius = tf.convert_to_tensor(radius, tf.float32)

    with click.progressbar(length=nk, label='Clean') as prog:
        footprint, f, r, y, x = _clean(dataset, mask, radius, prog=prog)
    r = tf.gather(radius, r)
    peaks = pd.DataFrame(dict(firmness=f.numpy(), radius=r.numpy(), x=x.numpy(), y=y.numpy()), index=index)
    return footprint.numpy(), peaks


import numpy as np
from scipy.spatial.distance import *
from scipy.stats import wasserstein_distance

import scanpy as sc

def get_m_sc(a_use,pval_thre,cls,method='pval_topk'):
#     a_use = a_m_hepa
#     pval_thre = 0.01
    sc.tl.rank_genes_groups(a_use,n_genes=a_use.shape[1],groupby=cls)

    rec2mat_fun = lambda rec: np.vstack([np.array(list(m)) for m in rec])
    name_mat = a_use.uns['rank_genes_groups']['names']
    pval_mat = a_use.uns['rank_genes_groups']['pvals_adj']
    score_mat = a_use.uns['rank_genes_groups']['scores']

    name_mat = rec2mat_fun(name_mat)
    pval_mat = rec2mat_fun(pval_mat)
    score_mat = rec2mat_fun(score_mat)
    n_cls = name_mat.shape[1]

    rst_list = []
    for i in range(n_cls):
        if method=='pval_thre':
            cur_idx = (pval_mat[:,i]<=pval_thre)
        elif method=='pval_topk':
            kth_p = np.sort(pval_mat[:,i])[pval_thre]
            cur_idx = (pval_mat[:,i]<=kth_p)
        elif method=='score_thre':
            cur_idx = (score_mat[:,i]>=pval_thre)
        elif method=='score_topk':
            kth_score = np.flip(np.sort(score_mat[:,i]))[pval_thre]
            cur_idx = (score_mat[:,i]>=kth_score)

        cur_m = name_mat[cur_idx,i]
        rst_list.append(cur_m)
        print(cur_m.shape[0])
    a_use.uns[cls+'_mz'] = rst_list
    return a_use

def get_dist_mat_emd(a,method='emd'):

    train_x = a.uns['train_x']
    num_cells = a.shape[0]
    num_features = a.shape[1]
    cell_idx = a.uns['cell_idx']
    cell_pixel_dict = {}
    pixel_count = []
    for i in range(num_cells):
        cur_pixels = train_x[cell_idx==i+1,:]
        cell_pixel_dict[i] = cur_pixels
        pixel_count.append(cur_pixels.shape[0])

    dist_mat = np.zeros(shape=(num_features,num_cells,num_cells))
    for k in range(num_features):
        if k%10==0:
            print(k)
#         print(k)
        if method=='emd':
            for i in range(num_cells):
                for j in range(num_cells):
                    cur_dist = wasserstein_distance(cell_pixel_dict[i][:,k],cell_pixel_dict[j][:,k])
        #             cur_dist = euclidean(np.mean(cell_pixel_dict[i][:,k]),np.mean(cell_pixel_dict[j][:,k]))
        #             print(k,i,j,cur_dist)
#                     cur_dist = np.mean(cell_pixel_dict[i][:,k])-np.mean(cell_pixel_dict[j][:,k])
                    dist_mat[k,i,j] = cur_dist
        elif method=='euclidean':
            cur_dist_mat = squareform(pdist(a.X[:,k][:,None]))
            dist_mat[k,:,:] = cur_dist_mat
    a.uns['feature_wise_distmat'] = dist_mat
    return a

def get_wbr(dist_mat,pred_list):
    wbr_list = []
    for i in range(dist_mat.shape[0]):
        cur_dist_mat = dist_mat[i,:,:]
        within_sum_1 = np.sum(dist_mat[i,:,:][pred_list==1,:][:,pred_list==1])
        within_sum_0 = np.sum(dist_mat[i,:,:][pred_list==0,:][:,pred_list==0])
        between_sum_1 = np.sum(dist_mat[i,:,:][pred_list==1,:][:,pred_list==0])
        between_sum_0 = np.sum(dist_mat[i,:,:][pred_list==0,:][:,pred_list==1])
        wbr = (within_sum_1+within_sum_0)/(between_sum_1+between_sum_0)
        wbr_list.append(wbr)
    return np.array(wbr_list)

def get_wbr_mat(a_use,cls):
#     a_use = a
# #     wbr_thre = 10
#     cls = 'SIMLR'
#     method='topk'
    dist_mat = a_use.uns['feature_wise_distmat']
    unique_labels = np.unique(a_use.obs[cls])
    wbr_mat = np.zeros(shape=(a_use.shape[1],unique_labels.shape[0]))

    for i in range(wbr_mat.shape[1]):
        cur_label = unique_labels[i]
        cur_pred = a_use.obs[cls].copy().astype('str')
        cur_pred[cur_pred!=cur_label] = -1
        cur_pred[cur_pred==cur_label] = 0
        cur_pred = -cur_pred
        cur_wbr_list = get_wbr(dist_mat,cur_pred)
        wbr_mat[:,i] = cur_wbr_list

    a_use.uns['rank_genes_groups_emd'] = {
        'scores':wbr_mat

    }
    return a_use

def get_m_emd(a_use,thre,cls,method='topk',alg='emd'):
    a_use = get_dist_mat_emd(a_use,method=alg)
    a_use = get_wbr_mat(a_use,cls)

    score_mat = a_use.uns['rank_genes_groups_emd']['scores']
    n_cls = score_mat.shape[1]
    name_list = a_use.var_names
    rst_list = []
    for i in range(n_cls):
        if method=='thre':
            cur_idx = (score_mat[:,i]<=thre)
        elif method=='topk':
            kth_p = np.sort(score_mat[:,i])[thre]
            cur_idx = (score_mat[:,i]<=kth_p)

        cur_m = np.array(name_list[cur_idx])
        rst_list.append(cur_m)
        print(cur_m.shape[0])
    a_use.uns[cls+'_mz_emd'] = rst_list
    return a_use


#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Thu Jul 13 16:05:11 2023

@author: Jia Wei Teh

This script contains functions that are used to run artikel.py
"""

import numpy as np
import pandas as pd
from scoreboard import scoreboard
from language.dictionary import prompt

def load_file():

    # Reading an excel file
    excelFile = "./data/artikel.xlsx"
    excelFile = pd.read_excel(excelFile)
    # Update excel file into CSV file
    excelFile.to_csv("./data/artikel.csv", index = None, header=True)
    # Reading and Converting the output csv file into a dataframe object
    data_csv = pd.read_csv("./data/artikel.csv", skip_blank_lines = True, skiprows= [0], header = None)

    return data_csv

def qna_section(config, survival):

    # read csv
    data_full = np.genfromtxt('./data/artikel.csv', delimiter=',', encoding="utf8", dtype = None)
    data_array = data_full[1:]
    header_array = data_full[0]
    # convert dictionary to list
    # get keys and values
    # randomise for QnA
    data_array = randomiser(data_array)
    # set number of questions
    questions, answers = data_array[:,1], data_array[:,0]

    # enter survival mode. Default is 3 lifes.
    if survival:
        current_hearts = int(config.artikel_challenge.hearts)
        print('       ' + '\u2500'*8 + "# Begin quiz #" + '\u2500'*8 + '\n')
        # print('Was ist der richtige Artikel für dieses Nomen?')
        print(prompt['What are the correct articles for these nouns? [der/die/das]'])
        # survival score
        survival_score = 0
        analysis_message = ""
        # loop as long as we are still surviving
        while current_hearts > 0:
            wrong_questions, wrong_answers = [], []
            # ask questions
            wrong_questions, wrong_answers, current_hearts, index = ask_question_survival(questions, answers, wrong_questions, wrong_answers, current_hearts, int(config.artikel_challenge.hearts))
            # update score
            survival_score += index  - len(wrong_answers)
            # analysis
            current_analysis = get_analysis(wrong_questions, wrong_answers, data_array)
            analysis_message += current_analysis
            # if all right, continue loop
            if current_hearts > 0:
                # re-randomize
                questions, answers = randomiser(questions, answers)
        # score
        print(prompt['You have successfully answered %s questions correctly!']%str(survival_score))
        scoreboard.update_scoreboard(survival_score)
        scoreboard.show_scoreboard()
        print(prompt['Here are the correct answers for the part(s) where you\'ve made mistake(s):'])
        print(analysis_message)
        print('        ' + '\u2500'*8 + "# End quiz #" + '\u2500'*8)

    elif not survival:
        questions, answers = set_number(questions, answers, config)
        # initialise list for wrong answers
        wrong_questions, wrong_answers = [], []
        print('       ' + '\u2500'*8 + prompt["# Begin quiz #"] + '\u2500'*8)
        print(prompt['What are the correct articles for these nouns? [der/die/das]'])

        incorrect_counter = True
        # loop through lists until all questions are answered correctly.
        while True:
            wrong_questions, wrong_answers = ask_questions(questions, answers, wrong_questions, wrong_answers)
            # Right after the first loop, record the wrong answers for analysis.
            # If nothing is wrong, finish.
            if not wrong_questions:
                if incorrect_counter:
                    # calculate the score
                    questions_right = int(len(questions) - len(wrong_questions))
                    total_length = len(questions)
                    precentage = int(questions_right/len(questions)*100)
                break
            else:
                # If something is wrong, run this part only once
                if incorrect_counter:
                    # calculate the score
                    questions_right = int(len(questions) - len(wrong_questions))
                    total_length = len(questions)
                    precentage = int(questions_right/len(questions)*100)
                    # provide extra analysis
                    analysis_message = get_analysis(wrong_questions, wrong_answers, data_array)
                    # shutdown
                    incorrect_counter = False

                # update list into new ones
                questions, answers = wrong_questions, wrong_answers
                wrong_questions, wrong_answers = [], []
                # randomise again
                questions, answers = randomiser(np.array(questions), np.array(answers))

        # score
        print(prompt['You scored %s (%s%%).']%(str(questions_right), str(precentage)))
        if precentage != 100:
            print(prompt['Here are the correct answers for the part(s) where you\'ve made mistake(s):'])
            print(analysis_message)
        print('        ' + '\u2500'*8 + prompt["# End quiz #"] + '\u2500'*8)
    return

def get_analysis(wrong_questions, wrong_answers, data_array):

    message = ""
    # message for error answers
    for ii, (artikel, noun) in enumerate(list(zip(wrong_answers, wrong_questions))):
        # add translation
        translation = data_array[np.where(data_array[:,1] == wrong_questions[ii])[0][0]][2]
        message += "\033[33m{:<4}\033[35m{:<20}\033[39m{:<20}\033[39m\n".format(artikel, noun, "["+translation+"]")

    return message

def set_number(questions, answers, config):
    # TODO: add def number

    # what is the maximum questions allowed?
    max_questions = len(questions)
    # what does the user want?
    user_number = input(prompt['Please indicate the number of questions for your quiz: '])
    # error-proofing
    while True:
        # if nothing, set as max
        if user_number == "":
            print('No input detected.')
            user_number = max_questions
            break
        else:
            try:
                int(user_number)
                break
            except:
                user_number = input(prompt['The input is invalid. Please enter a valid number: '])

    user_number = int(user_number)
    # case if user provides too large of a value
    if user_number >= max_questions:
        print('We will set the number of questions to the maximum limit. (maximum = %s)'%str(max_questions))
        user_number = max_questions
    # from user's input, cut down the list.
    questions = questions[:user_number]
    answers = answers[:user_number]

    return questions, answers

def ask_questions(questions, answers, wrong_questions, wrong_answers):

    # basic form of query
    query = questions[0] + ': '

    for ii, q in enumerate(questions):

        user_answer = input(query).replace(" ","")

        # if the answer is correct, overwrite the NEXT question on the same line
        if user_answer == answers[ii].replace(" ",""):
            # special case if it is the last index, stop query.
            if (ii+1) == len(questions):
                print('\033[1A\033[2K\033[1A')
                return wrong_questions, wrong_answers
            # if there is no overflow, overwrite with new query.
            else:
                query = '\033[1A\033[2K' + questions[ii+1] + ': '
        # if the answer is wrong, delete the query and reprint with incorrect.
        else:
            # record wrong pairs
            wrong_questions.append(questions[ii])
            wrong_answers.append(answers[ii])
            print('\033[1A\033[2K' + q + ': ' + user_answer + ' {:<20}'.format(prompt['(Incorrect)']))
            # special case if it is the last index, stop query
            if (ii+1) == len(questions):
                return wrong_questions, wrong_answers
            else:
                query = questions[ii+1] + ': '

    return wrong_questions, wrong_answers

def ask_question_survival(questions, answers, wrong_questions, wrong_answers, hearts, max_hearts):

    # current hearts
    current_hearts = hearts

    # basic form of query
    query = print_hearts(max_hearts, current_hearts) + questions[0] + ': '

    for index, q in enumerate(questions):
        # don't ask questions once no more hearts left.
        if current_hearts <= 0:
            break
        else:
            hearts_status = print_hearts(max_hearts, current_hearts)
            user_answer = input(query).replace(" ","")
            # if the answer is correct, overwrite the NEXT question on the same line
            if user_answer == answers[index].replace(" ",""):
                # special case if it is the last index, stop query.
                if (index+1) == len(questions):
                    print('\033[1A\033[2K\033[1A')
                    return wrong_questions, wrong_answers, current_hearts, index
                # if there is no overflow, overwrite with new query.
                else:
                    query = '\033[1A\033[2K' + hearts_status  + questions[index+1] + ': '
            # if the answer is wrong, delete the query and reprint with incorrect.
            else:
                current_hearts -= 1
                # record wrong pairs
                wrong_questions.append(questions[index])
                wrong_answers.append(answers[index])
                print('\033[1A\033[2K' + hearts_status + q + ': ' + user_answer + ' ' + prompt['(Incorrect)'])
                # special case if it is the last index, stop query.
                if (index+1) == len(questions):
                    return wrong_questions, wrong_answers, current_hearts, index
                # otherwise, next query.
                else:
                    hearts_status = print_hearts(max_hearts, current_hearts)
                    query = hearts_status  + questions[index+1] + ': '

    return wrong_questions, wrong_answers, current_hearts, index

def print_hearts(max_hearts, current_hearts):

    filled_heart = "\u2665 "
    empty_heart = "\u2661 "
    # how many filled hearts?
    hearts = empty_heart * (max_hearts - current_hearts) + filled_heart * current_hearts + '\t'

    return hearts

def randomiser(*args):
    # Hardcoded - accepts only either the entire DataFrame, or a question-answer tuple.
    # randomise indices
    if len(args) == 1:
        array = args[0]
        indices = np.arange(array.shape[0])
        np.random.shuffle(indices)
        # re-order array with indices
        array = array[indices]
        return array
    else:
        questions, answers = args
        indices = np.arange(questions.shape[0])
        np.random.shuffle(indices)
        # re-order array with indices
        questions = questions[indices]
        answers = answers[indices]
        return questions, answers


def pawnPositions(initialPosition):

    finalPosition = set([])

    for i in range(len(initialPosition)):

        p = ord(initialPosition[i][0]) - 1
        l = ord(initialPosition[i][1])

        if p == 48:
            continue  # pawn in raw 1

        if l == 97:  # pawn in column a

            finalPosition.add(chr(p) + chr(l + 1))

        elif l == 104:  # pawn in column h

            finalPosition.add(chr(p) + chr(l - 1))

        else:

            finalPosition.add(chr(p) + chr(l + 1))
            finalPosition.add(chr(p) + chr(l - 1))

    return finalPosition

def horsePositions(initialPosition):

    finalPositions = set([])

    operands = [
        [1, -2],
        [1, 2],
        [2, -1],
        [2, 1],
        [-1, -2],
        [-1, 2],
        [-2, -1],
        [-2, 1],
    ]  # possible horse moves

    p = ord(initialPosition[0])
    l = ord(initialPosition[1])

    for i in range(8):

        possiblePosition = chr(p + operands[i][0]) + chr(l + operands[i][1])

        if ord(possiblePosition[0]) > 56 or ord(possiblePosition[0]) < 49:

            continue

        if ord(possiblePosition[1]) > 104 or ord(possiblePosition[1]) < 97:

            continue

        finalPositions.add(possiblePosition)

    return finalPositions

def main():

    horseHouse = input()

    number = 0

    while horseHouse != "0":

        number += 1

        h = horsePositions(horseHouse)

        pawnHouse = [None] * 8

        for i in range(8):

            pawnHouse[i] = input()

        p = pawnPositions(pawnHouse)

        moviments = len(h.difference(p))

        print("Caso de Teste #{}: {} movimento(s).".format(number, moviments))

        horseHouse = input()

if __name__ == "__main__":

    main()


from turtle import forward
import torch
from torch import nn as nn
from torch.nn import functional as F

def make_model(args, parent=False):
    return RRDBNet(args)

class ResidualDenseBlock(nn.Module):
    """Residual Dense Block.

    Used in RRDB block in ESRGAN.

    Args:
        num_feat (int): Channel number of intermediate features.
        num_grow_ch (int): Channels for each growth.
    """

    def __init__(self, num_feat=64, num_grow_ch=32):
        super(ResidualDenseBlock, self).__init__()
        self.conv1 = nn.Conv2d(num_feat, num_grow_ch, 3, 1, 1)
        self.conv2 = nn.Conv2d(num_feat + num_grow_ch, num_grow_ch, 3, 1, 1)
        self.conv3 = nn.Conv2d(num_feat + 2 * num_grow_ch, num_grow_ch, 3, 1, 1)
        self.conv4 = nn.Conv2d(num_feat + 3 * num_grow_ch, num_grow_ch, 3, 1, 1)
        self.conv5 = nn.Conv2d(num_feat + 4 * num_grow_ch, num_feat, 3, 1, 1)

        self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)

        # initialization
        # default_init_weights([self.conv1, self.conv2, self.conv3, self.conv4, self.conv5], 0.1)

    def forward(self, x):
        x1 = self.lrelu(self.conv1(x))
        x2 = self.lrelu(self.conv2(torch.cat((x, x1), 1)))
        x3 = self.lrelu(self.conv3(torch.cat((x, x1, x2), 1)))
        x4 = self.lrelu(self.conv4(torch.cat((x, x1, x2, x3), 1)))
        x5 = self.conv5(torch.cat((x, x1, x2, x3, x4), 1))
        # Emperically, we use 0.2 to scale the residual for better performance
        return x5 * 0.2 + x

class RRDB(nn.Module):
    """Residual in Residual Dense Block.

    Used in RRDB-Net in ESRGAN.

    Args:
        num_feat (int): Channel number of intermediate features.
        num_grow_ch (int): Channels for each growth.
    """

    def __init__(self, num_feat, num_grow_ch=32):
        super(RRDB, self).__init__()
        self.rdb1 = ResidualDenseBlock(num_feat, num_grow_ch)
        self.rdb2 = ResidualDenseBlock(num_feat, num_grow_ch)
        self.rdb3 = ResidualDenseBlock(num_feat, num_grow_ch)

    def forward(self, x):
        out = self.rdb1(x)
        out = self.rdb2(out)
        out = self.rdb3(out)
        # Emperically, we use 0.2 to scale the residual for better performance
        return out * 0.2 + x

def make_layer(block, n_layers):
    layers = []
    for _ in range(n_layers):
        layers.append(block())
    return nn.Sequential(*layers)

class RRDBNet(nn.Module):
    """Networks consisting of Residual in Residual Dense Block, which is used
    in ESRGAN.

    ESRGAN: Enhanced Super-Resolution Generative Adversarial Networks.

    We extend ESRGAN for scale x2 and scale x1.
    Note: This is one option for scale 1, scale 2 in RRDBNet.
    We first employ the pixel-unshuffle (an inverse operation of pixelshuffle to reduce the spatial size
    and enlarge the channel size before feeding inputs into the main ESRGAN architecture.

    Args:
        num_in_ch (int): Channel number of inputs.
        num_out_ch (int): Channel number of outputs.
        num_feat (int): Channel number of intermediate features.
            Default: 64
        num_block (int): Block number in the trunk network. Defaults: 23
        num_grow_ch (int): Channels for each growth. Default: 32.
    """

    def __init__(self, args, num_in_ch=3, num_out_ch=3, scale=4, num_feat=64, num_block=23, num_grow_ch=32):
        super(RRDBNet, self).__init__()

        self.scale = args.scale[0]
        assert self.scale==4, "only support scale 4"

        num_block = args.n_resblocks
        # num_feat = args.n_feats
        # num_out_ch = args.n_colors

        # self.conv_first = nn.Conv2d(num_in_ch, num_feat, 3, 1, 1)
        modules_head = [nn.Conv2d(num_in_ch, num_feat, 3, 1, 1)]

        # self.body = make_layer(RRDB, num_block, num_feat=num_feat, num_grow_ch=num_grow_ch)
        # self.conv_body = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        modules_body = [RRDB(num_feat) for _ in range(num_block)]
        modules_body.append(nn.Conv2d(num_feat, num_feat, 3, 1, 1))

        # upsample
        # self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        # self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        # self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        # self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
        # self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)

        self.head = nn.Sequential(*modules_head)
        self.body = nn.Sequential(*modules_body)
        self.tail = Tail(num_out_ch, num_feat)

    def forward(self, x):
        # feat = x
        # feat = self.conv_first(feat)
        # body_feat = self.conv_body(self.body(feat))
        # feat = feat + body_feat

        # # upsample
        # feat = self.lrelu(self.conv_up1(F.interpolate(feat, scale_factor=2, mode='nearest')))
        # feat = self.lrelu(self.conv_up2(F.interpolate(feat, scale_factor=2, mode='nearest')))
        # out = self.conv_last(self.lrelu(self.conv_hr(feat)))

        feat = self.head(x)
        body_feat = self.body(feat)
        feat = feat + body_feat

        out = self.tail(feat)

        return out

class Tail(nn.Module):
    def __init__(self, num_out_ch, num_feat=64):
        super(Tail, self).__init__()

        self.conv_up1 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        self.conv_up2 = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        self.conv_hr = nn.Conv2d(num_feat, num_feat, 3, 1, 1)
        self.conv_last = nn.Conv2d(num_feat, num_out_ch, 3, 1, 1)
        self.lrelu = nn.LeakyReLU(negative_slope=0.2, inplace=True)

    def forward(self, x):
        x = self.lrelu(self.conv_up1(F.interpolate(x, scale_factor=2, mode='nearest')))
        x = self.lrelu(self.conv_up2(F.interpolate(x, scale_factor=2, mode='nearest')))
        x = self.conv_last(self.lrelu(self.conv_hr(x)))

        return x


import math

from day_base import Day

class Day10(Day):

    def __init__(self):
        super().__init__(2021, 10, "Syntax Scoring")
        self.closing_open = {
            ')': '(',
            ']': '[',
            '}': '{',
            '>': '<'
        }

    def part_a(self):
        point_map = {
            ')': 3,
            ']': 57,
            '}': 1197,
            '>': 25137
        }

        points = 0
        for line in self.input:
            bracket_scopes = []
            for item in line:
                if item in self.closing_open.values():
                    bracket_scopes.append(item)
                elif self.closing_open[item] == bracket_scopes[len(bracket_scopes) - 1][0]:
                    bracket_scopes.pop()
                else:
                    # Corrupt line
                    points += point_map[item]
                    break
        return points

    def part_b(self):
        point_map = {
            '(': 1,
            '[': 2,
            '{': 3,
            '<': 4
        }

        # Filter out the corrupt lines
        corrupt_lines = []
        for l in self.input:
            scopes = []
            for i in l:
                if i in self.closing_open.values():
                    scopes.append(i)
                elif self.closing_open[i] == scopes[len(scopes) - 1]:
                    scopes.pop()
                else:
                    corrupt_lines.append(l)
                    break
        incomplete_lines = list(set(self.input) - set(corrupt_lines))

        # Calculate the points!
        all_points = []
        for l in incomplete_lines:
            points = 0
            scopes = []
            for i in l:
                if i in self.closing_open.values():
                    scopes.append(i)
                else:
                    scopes.pop()
            for v in reversed(scopes):
                points = points * 5 + point_map[v]
            all_points.append(points)

        all_points = sorted(all_points)
        return all_points[math.floor(len(all_points) / 2)]

if __name__ == '__main__':
    Day10().run()


"""
purpose:输入图片路径辨识图片文字,并输出文字的文件档案  ※tesseract要先装好,预设在Program Files(x86) ,在Popen路径要设绝对
                                                   ※tesseract记得第三步要设置path环境变数自动设定

input:图片路径,存结果的文件档案名称[没指定的话就存成当下路径result.txt档]

output:输出判别文字的文件

child = subprocess.Popen(tesseract):把文字给tesseract判别,再回传出结果

open: 档案输出记得utf8

"""

def image_identification(image,result="result"):

    import cv2,subprocess
    tesseract= "C:\\Program Files (x86)\\Tesseract-OCR\\tesseract.exe "+image+" "+result
    child = subprocess.Popen(tesseract)
    child.wait()
    load_result = result + ".txt"
    text = open(load_result,encoding="utf8").read().strip() #读取文字结果
    print("验证码为"+text) #打印


from . import util, plotting, test_images
import numpy as np
import pandas as pd
from skimage.measure import regionprops
from skimage.io import imread
from scipy import spatial

DATA_DIR = test_images.__file__.split('__init__.py')[0]

def data(name):
    merge_dir = DATA_DIR + 'merge_'+name+'.tif'
    labels_dir = DATA_DIR + 'segmentation_'+name+'.tif'
    return imread(merge_dir), imread(labels_dir)

def get_centroids(labels):
    cellprops = regionprops(labels)
    return np.round([cell.centroid for cell in cellprops]).astype(int)

def get_angle(points, simplices):
    """Compute the communicability angle of each edge"""
    A_idx, B_idx, C_idx = util.simp_permutations(simplices)
    A, B, C = points[A_idx], points[B_idx], points[C_idx]
    CA_unit = util.normalized_vector(A-C)
    CB_unit = util.normalized_vector(B-C)

    if len(CA_unit.shape)>1:
        return np.arccos((CA_unit*CB_unit).sum(axis=1))*180/np.pi

    return np.arccos((CA_unit*CB_unit).sum())*180/np.pi

def get_max(points):
    """Compute maximum communicability angle and cell-cell distance."""
    #Max distance
    #Calculate convex hull
    hull = spatial.ConvexHull(points)
    hull_points = points[hull.vertices,:]

    # Get the farthest apart points
    dist_max = spatial.distance.cdist(hull_points, hull_points, metric='euclidean').max()
    return np.array([180, dist_max])

def normalize(x, points):
    """Min-Max normalization"""
    x = np.array(x)
    xmax = get_max(points)
    xmin = x.min(axis=0)
    return (x-xmin)/(xmax-xmin)

def loss(x, points):
    x = normalize(x, points)
    if x.ndim==1:
        return x
    return np.mean(x, axis=1)

def ecdf(x):
    """Compute the N-dimensional Empirical Cumulative Distribution Function (ECDF) of x"""
    x = np.array(x)
    is1d = x.ndim==1
    n = len(x)
    if is1d:
        x = np.sort(x)
        F = np.arange(1, n+1) / n
    else:
        #Note: x is dominated by y if x_i <= y_i for all i.
        #Order by 1-norm to reduce search of dominated nodes
        idx = x.sum(axis=1).argsort()
        x = x[idx]
        #Count dominated nodes
        F = np.array([(x[:i+1]<=x[i]).all(axis=1).sum() for i in range(n)])/n
    return x, F

def efficiency(x, points):
    """Calculates the communicability efficiency of a set of points linked by edges which attributes are detailed in the array x"""
    x, F = ecdf(x)
    L = loss(x, points)
    return x, np.clip(F-L, 0, 1)

def get_thresholds(x, points):
    x, E = efficiency(x, points)
    return x[np.where(E==E.max())[0][0]]

def edgeprops(points):
    simplices = spatial.Delaunay(points).simplices
    if simplices.shape[1]==4:
        simplices = util.reshape_simp(simplices)
    #Edge properties
    edges_idx = util.simp2edge(simplices)
    df_edges = pd.DataFrame(edges_idx, columns=['source', 'target'])

    #Communicability angle
    df_edges['angle'] = get_angle(points, simplices)
    df_edges = df_edges.sort_values('angle', ascending=False)

    #Remove duplicated edges
    df_edges[['source', 'target']] = df_edges[['source', 'target']].T.apply(sorted).T
    df_edges = df_edges.drop_duplicates(subset=['source', 'target'], keep='first')

    #Distance between regions
    edges = points[df_edges[['source', 'target']]]
    df_edges['dist'] = np.linalg.norm(edges[:, 1]-edges[:, 0], axis=1)

    thresholds = get_thresholds(df_edges[['angle', 'dist']], points)
    df_edges['isNeighbor'] = np.all(df_edges[['angle', 'dist']] <= thresholds, axis=1)
    return df_edges, thresholds

def neighbors_graph(points, coords=True, with_thresholds=False):
    df_edges, thresholds = edgeprops(points)
    edges = np.array(df_edges[['source', 'target']][df_edges['isNeighbor']])
    if coords:
        edges = points[edges]

    if with_thresholds:
        return edges, thresholds

    return edges

def plot_layout(points, **kwargs):
    return plotting.plot_layout(points, **kwargs)

def plot_neighbors_graph(points, edges, **kwargs):
    return plotting.plot_neighbors_graph(points, edges, **kwargs)

def plot_overlap(img, points, edges, **kwargs):
    return plotting.plot_overlap(img, points, edges, **kwargs)


import json
import os
from openai import OpenAI
import time
from dotenv import load_dotenv

load_dotenv() #needed to load the API key from .env

def insert_string(original, inserted, index):
    """
    Combine two strings by inserting the second string into the first string at the specified index.

    Parameters:
    - original (str): The original string.
    - inserted (str): The string to be inserted.
    - index (int): The index where the insertion should occur.

    Returns:
    - str: The combined string.
    """
    combined = original[:index] + inserted + original[index:]
    return combined

def prompts_for_words_one_by_one(filename):
    """
    :param filename: name of txt file to read in the prompts, prompts should be in the form
    "text | index", where the text contains a gap at the 'index' into where the words will be inserted
    :return: a list of tuples, where the first element of the tuple is the text and second element is the index.
    """
    prompts = list()
    with open(f'{filename}.txt', encoding="UTF-8") as f:
        for line in f:
            res = line.strip().split(" | ")
            prompts.append((res[0], int(res[1])))
    return prompts

def run_prompt_all_words_one_by_one(words, prompt):
    """
    :param words: list of words to test
    :param prompt: prompt in the form of a tuple (text, index), where the text is the string of the prompt and
    index is where a word will be inserted.
    :return: returns the list of all the words returned
    """
    client = OpenAI()
    print(prompt)
    response_words = list()
    for word in words:
        time.sleep(3)
        prompt_to_send = insert_string(prompt[0], word.lower(), int(prompt[1]))
        try:
            response = client.chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": prompt_to_send}
                ]
            )

            generated_word = response.choices[0].message.content
            print(f"{generated_word}")
            response_words.append(generated_word)

        except Exception as e:
            print(f"Error during API call: {e}")
    return response_words

def run_prompt_100_words_together(words, prompt, filename):
    """
    :param words: list of words to test
    :param prompt: prompt, where there is the string <word>, where individual words will be inserted
    :param filename: name of the .txt file, where the results will be written
    """
    client = OpenAI()
    response_words = list()
    words_to_send = ""
    for word in words:
        words_to_send += word+", "
    print(words_to_send)
    for i in range(20):
        time.sleep(1)
        prompt_to_send = "You are given a prompt and a list of words. For each word, replace the string '<word>' with a word from the list and give back a one word response, return answers for all words."
        try:
            # Timeout to keep loop from freezing
            response = client.with_options(timeout=30).chat.completions.create(
                model="gpt-3.5-turbo",
                messages=[
                    {"role": "system", "content": prompt_to_send},
                    {"role": "user", "content": "prompt:" + prompt},
                    {"role": "user", "content": "words:" + words_to_send}
                ]
            )

            generated = response.choices[0].message.content
            #print(f"{generated}")
            response_words.append(generated)

        except Exception as e:
            print(f"Error during API call: {e}")

    outfile = open(f"{filename}.txt", "w")
    for line in response_words:
        outfile.write(line + "\n")
        outfile.write("\n")
    outfile.close()

words = list()
with open('words.txt', encoding="UTF-8") as f:
    for line in f:
        words.append(line.strip())

print(words)

#comment uncomment methods as you please

prompt = "Provide a word that evokes emotions or thoughts related to the word <word>."
run_prompt_100_words_together(words, prompt, "response_prompt_2_20_times_7")

#prompts = prompts_for_words_one_by_one("prompts")

#print(run_prompt_all_words_one_by_one(words, prompts[5]))