from itertools import *
from collections import *
from heapq import *
from bisect import *
from copy import *
import math
import sys
sys.setrecursionlimit(1<<20)
INF = float('inf')

n = int(input())
T = list(map(int,input().split()))
V = list(map(int,input().split()))
dp = [[0]*101 for _ in range(n+1)]
dp[0][0] = 1
for i in range(0,n):
    t = T[i]
    VV = V[i]
    for v in range(101):
        if dp[i][v] and v<=VV:
            for w in range(101):
                if w<=VV:
                    if t<abs(v-w):
                        continue
                    a = VV-v
                    b = VV-w
                    if a+b<=t:
                        S = (VV+v)*a/2 + VV*(t-a-b) + (w + VV)*b/2
                    else:
                        x = (w-v+t)/2
                        y = (t-w+v)/2
                        S = (2*v+x)*x/2 + (w+v+x)*y/2
                    dp[i+1][w] = max(dp[i+1][w], dp[i][v]+S)
print(dp[n][0]-1)


from collections import Counter, defaultdict, deque
import heapq

# LC1584. Min Cost to Connect All Points
def minCostConnectPoints(self, points: List[List[int]]) -> int:
    # Prim's algo: O(V^2), V = len(points), space O(V)
    d = {(x, y): float('inf') if i else 0 for i, (x, y) in enumerate(points)}
    res = 0
    while d:
        x, y = min(d, key=d.get)  # obtain the current minimum edge
        res += d.pop((x, y))      # and remove the corresponding point
        for x1, y1 in d:          # for the rest of the points, update the minimum manhattan distance
            d[(x1, y1)] = min(d[(x1, y1)], abs(x-x1)+abs(y-y1))
    return res
def minCostConnectPoints(self, a: List[List[int]]) -> int:  # O(n^2 logn) and O(n^2)
    # Prim's algorithm: standard algo, not using condition: edges are not restricted
    n = len(a)
    ans, visited, pq, i = 0, set([0]), [], 0
    rem = set(range(1, n))  # nodes not processed
    dist = lambda x, y: abs(x[0] - y[0]) + abs(x[1] - y[1])
    while len(visited) < n:
        for j in rem:
            if j not in visited: heappush(pq, (dist(a[i], a[j]), j))
        while pq[0][1] in visited:
            heappop(pq)
        val, i = heappop(pq)
        visited.add(i)
        rem.discard(i)
        ans += val
    return ans

# LC743. Network Delay Time
# https://www.toutiao.com/article/7076288121079431692/
def networkDelayTime(self, times: List[List[int]], n: int, k: int) -> int:  # O(E + VlogV)
    graph = collections.defaultdict(list) # dijkstra's with heap
    for u, v, w in times: graph[u].append((v, w))
    pq, dist = [(0, k)], {} # cost 0 at node k; distance or cost
    while pq:
        d, node = heapq.heappop(pq)
        if node in dist: continue
        dist[node] = d
        for nei, d2 in graph[node]:
            if nei not in dist: heapq.heappush(pq, (d+d2, nei))
    return max(dist.values()) if len(dist) == n else -1

# LC332. Reconstruct Itinerary O(E^d), d max flights from any node.
def findItinerary(self, tickets: List[List[str]]) -> List[str]:  # O(ElogE)
    if not tickets: return []
    dgraph = defaultdict(list)
    for origin, dest in tickets: dgraph[origin].append(dest)
    for _, dests in dgraph.items(): dests.sort(reverse=True) # lexical order
    ret = []
    def dfs(origin):
        dests = dgraph[origin]
        while dests: dfs(dests.pop()) # remove edge by Hierholzer's Algorithm
        ret.append(origin)  # dfs adds end nodes first, lastly add start.  - postorder traverse
    dfs('JFK')
    return ret[::-1]

# LC787. Cheapest Flights Within K Stops - weighted graph
def findCheapestPrice(self, n, flights, src, dst, K):  # O(E) runtime, O(E + V) space
    if src == dst: return 0
    d, seen = collections.defaultdict(list), collections.defaultdict(lambda: float('inf'))  # d is graph
    for u, v, p in flights: d[u] += [(v, p)]  # O(E) space
    dque = deque([(src, -1, 0)])
    while dque:  # O(E)
        pos, k, cost = dque.popleft()
        if pos == dst or k == K: continue
        for nei, p in d[pos]:
            if cost + p >= seen[nei]: continue
            else:
                seen[nei] = cost+p  # O(V) space
                dque.append((nei, k+1, cost+p))
    return seen[dst] if seen[dst] < float('inf') else -1

# LC2065. Maximum Path Quality of a Graph
def maximalPathQuality(self, values: List[int], edges: List[List[int]], maxTime: int) -> int:
    G = collections.defaultdict(dict)  ## O(EV)
    for i, j, t in edges: G[i][j] = G[j][i] = t
    @cache
    def dfs(i, seen, time):
        res = sum(values[j] for j in seen) if i == 0 else 0  # come back to 0
        for j in G[i]:
            if time >= G[i][j]: res = max(res, dfs(j, seen | {j}, time - G[i][j]))
        return res
    return dfs(0, frozenset({0}), maxTime)
def maximalPathQuality(self, values: List[int], edges: List[List[int]], maxTime: int) -> int:
    neighbours = defaultdict(list)  ## O(ElogV) for Dijkstra, worst is EV.
    for v,w,t in edges:
        neighbours[v].append((w,t))
        neighbours[w].append((v,t))
    times = defaultdict(lambda: float("inf"))
    hq = [(0, 0)]
    while hq:
        time, node = heappop(hq)
        if times[node] == float("inf"):
            times[node] = time
            for nb, t in neighbours[node]:
                if time + t <= maxTime//2 and times[nb] == float("inf"):
                    heappush(hq, (time + t, nb))
    @cache
    def dfs(time, value, visited, node):
        res = value if node == 0 else -float("inf")
        for nb, t in neighbours[node]:
            if time + t + times[nb] <= maxTime:
                val = dfs(time + t, value, visited | (1<<nb), nb)
                res = max(res, val if (visited >> nb) & 1 else val + values[nb])
        return res
    return dfs(0, values[0], 1, 0)

# LC1135. Connecting Cities With Minimum Cost
def minimumCost(self, N: int, connections: List[List[int]]) -> int:
    G = collections.defaultdict(list)
    for city1, city2, cost in connections:  # O(E)
        G[city1].append((cost, city2))
        G[city2].append((cost, city1))
    queue, visited = [(0, N)], set()  # [1] Arbitrary starting point N costs 0.
    total = 0
    while queue: # [3] Exit if all cities are visited. O(VlogV)
        cost, city = heapq.heappop(queue)
        if city not in visited:
            visited.add(city)
            total += cost # [2] Grow tree by one edge.
            for edge in G[city]:
                heapq.heappush(queue, edge)
    return total if len(visited) == N else -1


"""Defines a wrapper around user selectable optimisation engines.
"""
import sys
from deap.base import Toolbox

# skpar
from skpar.core.utils import get_logger
from skpar.core.evaluate import Evaluator
from skpar.core.pso import PSO
from skpar.core.pscan import PSCAN
from skpar.core.parameters import get_parameters

OPTENGINES = {"pso": PSO, "pscan": PSCAN}

LOGGER = get_logger(__name__)

def get_optargs(userinp):
    """Parse user input for optimisation related arguments."""
    try:
        algo = userinp.get("algo", "pso").lower()
        options = userinp.get("options", {})
        try:
            parameters = get_parameters(userinp["parameters"])
        except KeyError as exc:
            LOGGER.critical(
                "Parameters must be defined under" 'optimisation" in the input file.'
            )
            LOGGER.critical(exc)
            sys.exit(2)
        return algo, options, parameters
    except AttributeError:
        return None

class Optimiser(object):
    """Wrapper for different optimization engines."""

    def __init__(self, algo, parameters, evaluate, options=None, verbose=True):
        try:
            self.optengine = OPTENGINES[algo]
        except KeyError:
            LOGGER.critical("Unsupported optimisation algorithm %s", algo)
            sys.exit(2)
        self.evaluate = evaluate
        self.parameters = parameters
        if options is None:
            options = {}
        self.optimise = OPTENGINES[algo](self.parameters, self.evaluate, **options)
        self.logger = LOGGER
        # report all tasks and objectives
        if verbose:
            log = self.logger.info
        else:
            log = self.logger.debug
        log("Algorithm: {}".format(algo))
        for item in parameters:
            log(item)

    def __call__(self, **kwargs):
        output = self.optimise(**kwargs)
        return output

    def report(self, *args, **kwargs):
        """Report optimiser state."""
        try:
            self.optimise.report(*args, **kwargs)
        except AttributeError:
            # assume optimiser does not have a report method
            pass


import pandas as pd
import numpy as np
#implementing logistic regression

thresh_hold = 0.0000001#Adjust this for faster processing !
def Gradient_Descent(X_train,y,theta):
	lin_est = np.dot(np.transpose(theta),X_train) ## computing here the h(x)
	t_1 = np.dot(np.transpose(y),np.log(sigmoid(lin_est))) ##computing y*log(h(x))
	t_2 = np.dot(np.transpose(1-y),np.log(1-sigmoid(lin_est)))##computing (1-y)*h(x)
	err = -(t_1+t_2)
	cost= err/np.shape(X_train)[1] ##the error term
	print('Starting Cost theta initiated as one:',cost)
	old = cost
	diff = cost
	while(diff >= thresh_hold):
		grad = np.dot(X_train,(sigmoid(lin_est)-y))
		grad = grad/np.shape(X_train)[1]
		alpha = 0.09
		theta = theta - alpha*grad
		lin_est = np.dot(np.transpose(theta),X_train)
		t_1 = np.dot(np.transpose(y),np.log(sigmoid(lin_est)))
		t_2 = np.dot(np.transpose(1-y),np.log(1-sigmoid(lin_est)))
		err = -(t_1+t_2)
		cost= err/np.shape(X_train)[1]
#		print('Iter_Cost: ',cost)
		diff = abs(cost-old)
		old = cost
	return(theta,cost,lin_est)

def sigmoid(x):
    sigmoid = 1/(1+np.exp(-x))
    return(sigmoid)

def binary_func(pred_og):
    for i in range(len(pred_og)):
        if pred_og[i]>0.5:
            pred_og[i]=1
        else:
            pred_og[i]=0
    return(pred_og)
data = pd.read_csv('Logistic_regression.csv')
data.drop('Unnamed: 0',axis=1,inplace=True)
print('Implementing Logistic Regression')
l = (data.drop('quality',axis=1)).columns
feature_list = l
feature = (data[feature_list].values)
theta = np.ones(len(feature_list)+1) #INITIATING THETA
a=np.ones(len(feature)) #ADDING X(0)
X_train = np.insert(feature,0,a,axis=1) #ADDING X(0)
X_train =np.transpose(X_train)
y = np.array(data['quality'])
theta,cost,lin_est = Gradient_Descent(X_train,y,theta)
pred_og = sigmoid(lin_est)
pred_og = binary_func(pred_og)
print('The metrics for logistic regression using my implementation of logistic regression::')
from sklearn.metrics import classification_report
print(classification_report(y,pred_og))
from sklearn.linear_model import LogisticRegression
lr = LogisticRegression(solver='saga',penalty='none')
lr.fit(data.drop('quality',axis=1),data['quality'])
from sklearn.metrics import classification_report

pred = lr.predict(data.drop('quality',axis=1))
print('The metrics for logistic regression using SAGA solver::')
print(classification_report(y,pred))

preds = pd.DataFrame(pred_og,columns=['predicted_by_me'])
preds['predicted_by_not_me'] = pred
error_rate = (preds['predicted_by_me'] != preds['predicted_by_not_me']).mean()

print('The prediction mismatch between my model and the SAGA solver model is ::',error_rate)
print('\n')
print('Implementing Cross-Validation')
print('\n')

##3 fold cross validation for my model begins::

slot_1 = data.iloc[0:533]
slot_2 = data.iloc[533:1066]
slot_3 = data.iloc[1066:1599]

precision=[]
recall=[]
accuracy=[]
for i in range(1,4):
    if i == 1:
        test_set = slot_1
        train_set = slot_2.append(slot_3)
        feature_list = l #USED LIST OF FEATURES GETS INCREASED EVERY ITERATION
        feature = (train_set[feature_list].values) #GETTING VALUES FROM DATA FRAME AS AN ARRAY
        theta = np.ones(len(feature_list)+1) #INITIATING THETA
        a=np.ones(len(feature)) #ADDING X(0)
        X_train = np.insert(feature,0,a,axis=1) #ADDING X(0)
        X_train =np.transpose(X_train)
        y = np.array(train_set['quality'])
        params,cost,lin_est = Gradient_Descent(X_train,y,theta)
        feature_test = (test_set[feature_list].values)
        theta = params #PARAMS ARE THE FINAL ITERATION PARAMETERS
        a=np.ones(len(feature_test)) #ADDING X(0)
        X_test = np.insert(feature_test,0,a,axis=1)
        X_test =np.transpose(X_test)
        y_test = np.array(test_set['quality'])
        test_err = np.dot(np.transpose(theta),X_test)
        temp = binary_func(sigmoid(test_err))
        from sklearn.metrics import classification_report
        X =classification_report(y_test,temp,output_dict=True)
        precision.append(X['1']['precision'])
        recall.append(X['1']['recall'])
        accuracy.append(X['accuracy'])

    if i == 2:
        test_set = slot_2
        train_set = slot_1.append(slot_3)
        feature_list = l #USED LIST OF FEATURES GETS INCREASED EVERY ITERATION
        feature = (train_set[feature_list].values) #GETTING VALUES FROM DATA FRAME AS AN ARRAY
        theta = np.ones(len(feature_list)+1) #INITIATING THETA
        a=np.ones(len(feature)) #ADDING X(0)
        X_train = np.insert(feature,0,a,axis=1) #ADDING X(0)
        X_train =np.transpose(X_train)
        y = np.array(train_set['quality'])
        params,cost,lin_est = Gradient_Descent(X_train,y,theta)
        feature_test = (test_set[feature_list].values)
        theta = params #PARAMS ARE THE FINAL ITERATION PARAMETERS
        a=np.ones(len(feature_test)) #ADDING X(0)
        X_test = np.insert(feature_test,0,a,axis=1)
        X_test =np.transpose(X_test)
        y_test = np.array(test_set['quality'])
        test_err = np.dot(np.transpose(theta),X_test)
        temp = binary_func(sigmoid(test_err))
        from sklearn.metrics import classification_report
        X =classification_report(y_test,temp,output_dict=True)
        precision.append(X['1']['precision'])
        recall.append(X['1']['recall'])
        accuracy.append(X['accuracy'])
    if i == 3:
        test_set = slot_3
        train_set = slot_1.append(slot_2)
        feature_list = l #USED LIST OF FEATURES GETS INCREASED EVERY ITERATION
        feature = (train_set[feature_list].values) #GETTING VALUES FROM DATA FRAME AS AN ARRAY
        theta = np.ones(len(feature_list)+1) #INITIATING THETA
        a=np.ones(len(feature)) #ADDING X(0)
        X_train = np.insert(feature,0,a,axis=1) #ADDING X(0)
        X_train =np.transpose(X_train)
        y = np.array(train_set['quality'])
        params,cost,lin_est = Gradient_Descent(X_train,y,theta)
        feature_test = (test_set[feature_list].values)
        theta = params #PARAMS ARE THE FINAL ITERATION PARAMETERS
        a=np.ones(len(feature_test)) #ADDING X(0)
        X_test = np.insert(feature_test,0,a,axis=1)
        X_test =np.transpose(X_test)
        y_test = np.array(test_set['quality'])
        test_err = np.dot(np.transpose(theta),X_test)
        temp = binary_func(sigmoid(test_err))
        from sklearn.metrics import classification_report
        X =classification_report(y_test,temp,output_dict=True)
        precision.append(X['1']['precision'])
        recall.append(X['1']['recall'])
        accuracy.append(X['accuracy'])
print('\n')
print('The metrics for a 3 fold cross validation on my logistic regression model')
print('precision:',sum(precision)/3)
print('recall:',sum(recall)/3)
print('accuracy:',sum(accuracy)/3)
print('\n')
from sklearn.model_selection import cross_val_score
l = (data.drop('quality',axis=1)).columns
precision_saga=cross_val_score(lr,data[l],data['quality'],cv=3,scoring = 'precision')
recall_saga=cross_val_score(lr,data[l],data['quality'],cv=3,scoring = 'recall')
accuracy_saga=cross_val_score(lr,data[l],data['quality'],cv=3,scoring = 'accuracy')
print('The metrics for a 3 fold cross validation on SAGA solver model')
print('precision:',sum(precision_saga)/3)
print('recall:',sum(recall_saga)/3)
print('accuracy:',sum(accuracy_saga)/3)


from pipelines.hyperparameter_tuning import HyperParameterTuning
from steps.classifier.random_forest_classifier import (
    RandomForestClassifierParameters,
    train_and_predict_best_rf_classifier,
    train_and_predict_rf_classifier,
)
from steps.data.load_breast_cancer_data import load_breast_cancer_data
from steps.data.load_iris_data import load_iris_data
from steps.evaluation.accuracy import calc_accuracy

from zenml.config.docker_settings import DockerSettings
from zenml.integrations.constants import SKLEARN

docker_settings = DockerSettings(required_integrations=[SKLEARN])

if __name__ == "__main__":
    HyperParameterTuning.as_template_of("iris_random_forest")(
        load_data_step=load_iris_data,
        train_and_predict_step=train_and_predict_rf_classifier,
        train_and_predict_best_model_step=train_and_predict_best_rf_classifier,
        evaluate_step=calc_accuracy,
        hyperparameters_conf_list=[
            RandomForestClassifierParameters(n_estimators=100),
            RandomForestClassifierParameters(n_estimators=200),
            RandomForestClassifierParameters(n_estimators=300),
            RandomForestClassifierParameters(n_estimators=400),
        ],
    ).run(enable_cache=False, settings={"docker": docker_settings})

    HyperParameterTuning.as_template_of("breast_cancer_random_forest")(
        load_data_step=load_breast_cancer_data,
        train_and_predict_step=train_and_predict_rf_classifier,
        train_and_predict_best_model_step=train_and_predict_best_rf_classifier,
        evaluate_step=calc_accuracy,
        hyperparameters_conf_list=[
            RandomForestClassifierParameters(n_estimators=100),
            RandomForestClassifierParameters(n_estimators=100, max_depth=5),
            RandomForestClassifierParameters(
                n_estimators=100, criterion="entropy"
            ),
        ],
    ).run(enable_cache=False, settings={"docker": docker_settings})


"""
[프린터]
각 문서 순서대로 중요도가 담긴 배열이 주어질 때, n번째 있는 문서가 몇 번째 순서로 출력되는지 알아보는 함수
"""
from typing import List
from collections import deque
from itertools import islice

def main(priorities: List, location: List):

    # 몇 번쨰 순서로 출력된다 = 몇 번째 순서로 중요한 중요도를 가지냐
    # 제일 중요한 것부터 출력한다.

    index_priority_pair = deque(enumerate(priorities))
    nth_print = 0

    # 출력되기 위해서는 제일 중요한 것을 앞으로 보내야 한다. -> 중요도를 비교
    while True:
        target_to_print = index_priority_pair.popleft()
        initial_index, priority_to_print = target_to_print
        if any(priority_to_print < priority[1] for priority in index_priority_pair):
                index_priority_pair.append(target_to_print)
        else:
            nth_print += 1
            if initial_index == location:
                return nth_print

if __name__ == "__main__":
    priorities_list = [[2, 1, 3, 2], [1, 1, 9, 1, 1, 1], [1, 5, 3, 4, 9]]
    location_list = [2, 0, 2]
    for priorities, location in zip(priorities_list, location_list):
        print(main(priorities, location))


import numpy, pandas, sys
import matplotlib
matplotlib.use('Agg')
import matplotlib.pyplot as plt, mpld3

def readData(platform, fileType):
    outputMode = 'processed-%s-pspcpu.csv' % platform
    outputCpusDetail = 'processed-%s-CpusDetail.csv' % platform
    if(fileType == 'cpus'):
        return pandas.read_csv(outputCpusDetail)
    else:
        return  pandas.read_csv(outputMode, header=None)

def graph():

    data1 = readData('lambda', 'cpus')
    data2 = readData('docker', 'cpus')

    x1 = data1.iloc[3:,0].replace(',','.', regex=True).astype(float)
    y1cpu = data1.iloc[3:,3].replace(',','.', regex=True).astype(float)
    y1cpu0 = data1.iloc[3:,8].replace(',','.', regex=True).astype(float)
    y1cpu1 = data1.iloc[3:,13].replace(',','.', regex=True).astype(float)

    x2= data2.iloc[3:,0].replace(',','.', regex=True).astype(float)
    y2cpu = data2.iloc[3:,3].replace(',','.', regex=True).astype(float)
    y2cpu0 = data2.iloc[3:,8].replace(',','.', regex=True).astype(float)
    y2cpu1 = data2.iloc[3:,13].replace(',','.', regex=True).astype(float)

    fig, axes = plt.subplots(2, 2, figsize=(20,12))

    axes[0,0].set_xlabel('memory')
    axes[0,0].set_ylabel('percent')
    axes[0,0].set_title('Lambda, cpu0 -> red, cpu1 -> green, cpu -> blue')
    # cpu0 -> red, cpu1 -> green, cpu -> blue
    axes[0,0].plot(list(x1), list(y1cpu0), '-ro', list(x1), list(y1cpu1), '-go', list(x1), list(y1cpu), '-bo')
    axes[0,0].axis([0, 3050, 0, 100])

    axes[0,1].set_xlabel('memory')
    axes[0,1].set_ylabel('percent')
    axes[0,1].set_title('Docker, cpu0 -> red, cpu1 -> green, cpu -> blue')
    # cpu0 -> red, cpu1 -> green, cpu -> blue
    axes[0,1].plot(list(x2), list(y2cpu0), '-ro', list(x2), list(y2cpu1), '-go', list(x2), list(y2cpu), '-bo')
    axes[0,1].axis([0, 2.5, 0, 100])

    data3 = readData('lambda', 'pspcpu')
    data4 = readData('docker', 'pspcpu')

    x3 = data3[data3.columns[0]].replace(',','.', regex=True).astype(float)
    y3 = data3[data3.columns[1]].replace(',','.', regex=True).astype(float)

    x4 = data4[data4.columns[0]].replace(',','.', regex=True).astype(float)
    y4 = data4[data4.columns[1]].replace(',','.', regex=True).astype(float)

    axes[1,0].set_xlabel('memory')
    axes[1,0].set_ylabel('pcpu')
    axes[1,0].set_title('Lambda')
    axes[1,0].plot(list(x3), list(y3), '-bo')
    axes[1,0].axis([0, 3050, 0, 210])

    axes[1,1].set_xlabel('cpu')
    axes[1,1].set_ylabel('pcpu')
    axes[1,1].set_title('Docker')
    axes[1,1].plot(list(x4), list(y4), '-ro')
    axes[1,1].axis([0, 2.5, 0, 210])

    mpld3.save_html(fig,'figure1.html')
    print('figure1.html created!')

def processed(platform, attrName):
    inputName = 'out-%s-proc.csv' % platform
    ouputName = 'processed-%s-proc.csv' % platform
    outputMode = 'processed-%s-pspcpu.csv' % platform
    outputCpusDetail = 'processed-%s-CpusDetail.csv' % platform
    try:
        data = pandas.read_csv(inputName)

        # print(data.pivot_table(index=[ attrName], columns='cpuName', values='cpu%')) # output the mean?
        new = pandas.DataFrame(columns = [attrName,'indexBatch','cpu', 'cpu0', 'cpu1','totalPsPcpu'])
        cpu, cpu0, cpu1 = 0, 0, 0
        for index, row in data.iterrows():
            if(index % 3 == 0 and index != 0):
                new.loc[len(new)] = [row[attrName], row['indexBatch'], cpu, cpu0, cpu1, row['totalPsPcpu']]
            if(index % 5000 == 0):
                print('@Row ',index)
            if(row['cpuName'] == 'cpu0'):
                cpu0 = row['cpu%']
            elif(row['cpuName'] == 'cpu1'):
                cpu1 = row['cpu%']
            else:
                cpu = row['cpu%']

        new.to_csv(ouputName)

        # new = pandas.read_csv(ouputName) # test

        ps_pcpu_df = new[[attrName, 'totalPsPcpu']]
        ps_pcpu_df.groupby(attrName)['totalPsPcpu'].apply(lambda x: x.value_counts().head(1)).to_csv(outputMode)
        new.drop(['totalPsPcpu'], axis=1, inplace=True)
        new.groupby([attrName])[['cpu','cpu0','cpu1']].agg(['min', 'max', 'mean', 'std', 'count']).round(3).to_csv(outputCpusDetail)

        print(ouputName + ' created!')
        print(outputMode + ' created!')
        print(outputCpusDetail + ' created!')

    except Exception as error:
        print('Error:', error)

processed('lambda', '#memory')
processed('docker', '#cpu')

graph()


import os
from dotenv import load_dotenv
import logging as saves_logger

class SavesResponder:

    def _reload_response(self):
        with open(self.RESPONSE_FILENAME, 'r') as handle:
            self.RESPONSE_MESSAGE = ''
            contents = handle.readlines()

            self.RESPONSE_MESSAGE = ''.join(map(str, contents))
            saves_logger.debug(f'Loaded response: {self.RESPONSE_MESSAGE}')

        return self.RESPONSE_MESSAGE

    def _reload_keywords(self):
        self.KEYWORDS.clear()
        with open(self.KEYWORDS_FILENAME, 'r') as handle:
            contents = handle.readlines()

            for line in contents:
                line = line.strip('\r')
                line = line.strip('\n')
                self.KEYWORDS.append(line.lower())
                saves_logger.debug(f'Added keyword: {line}')

        return self.KEYWORDS

    def _add_keyword(self, keyword):
        self.KEYWORDS.append(keyword.lower())

        with open(self.KEYWORDS_FILENAME, 'w') as handle:
                handle.writelines(f'{keyword}\n' for keyword in self.KEYWORDS)

        return self.KEYWORDS

    def _remove_keyword(self, keyword):
        self.KEYWORDS.remove(keyword.lower())

        with open(self.KEYWORDS_FILENAME, 'w') as handle:
                handle.writelines(f'{keyword}\n' for keyword in self.KEYWORDS)

        return self.KEYWORDS

    def __init__(self):
        self.KEYWORDS_FILENAME = 'keywords.txt'
        self.RESPONSE_FILENAME = 'response.txt'
        self.VOLUNTEER_MESSAGE = 'I picked up on a keyword in conversation: '

        load_dotenv()
        self.TOKEN = os.getenv('BOT_TOKEN')
        self.VOLUNTEER_CHANNEL_ID = int(os.getenv('VOLUNTEER_CHANNEL_ID'))
        self.KEYWORDS = []
        self.RESPONSE_MESSAGE = ''
        self._reload_keywords()
        self._reload_response()


import sys
from collections import deque
input = sys.stdin.readline

N, M = map(int, input().split())
arr = [list(map(int, input().split())) for _ in range(N)]
move = deque(list(map(int, input().split())) for _ in range(M)) # d, s
cloud = deque([[N-1, 0], [N-2, 0], [N-1, 1], [N-2, 1]])
waterBug = deque()
visited = [[0] * N for _ in range(N)]
ans = 0
while move:
    d, s = move.popleft()
    dy = [0, 0, -1, -1, -1, 0, 1, 1, 1]
    dx = [0, -1, -1, 0, 1, 1, 1, 0, -1]
    no = []
    visited = [[0] * N for _ in range(N)]
    for i in range(len(cloud)):
        y, x = cloud.popleft()
        ny = y + dy[d] * s
        nx = x + dx[d] * s
        if ny < 0:
            ny = (N + ny) % N
        elif ny >= N:
            ny = ny % N
        if nx < 0:
            nx = (N + nx) % N
        elif nx >= N:
            nx = nx % N
        arr[ny][nx] += 1
        visited[ny][nx] = 1
        waterBug.append([ny, nx])
    by = [1, 1, -1, -1]
    bx = [1, -1, 1, -1]
    while waterBug:
        y, x = waterBug.popleft()
        num = 0
        for i in range(4):
            ny = y + by[i]
            nx = x + bx[i]
            if 0 <= ny < N and 0 <= nx < N:
               if arr[ny][nx] >= 1:
                   num += 1
        arr[y][x] += num
    for i in range(N):
        for j in range(N):
            if visited[i][j] == 1:
                continue
            else:
                if arr[i][j] >= 2:
                    arr[i][j] -= 2
                    cloud.append([i, j])
for i in range(N):
    for j in range(N):
        ans += arr[i][j]
print(ans)

백준/골드/21610.마법사 상어와 비바라기.py

21610.마법사 상어와 비바라기.py


from __future__ import division
from __future__ import print_function
import numpy as np
from scipy.cluster.vq import vq, kmeans2

class PQ(object):
    def __init__(self, M, Ks, verbose=True):
        assert 0 < Ks <= 2 ** 32
        self.M, self.Ks, self.verbose = M, Ks, verbose
        self.code_dtype = np.uint8 if Ks <= 2 ** 8 else (np.uint16 if Ks <= 2 ** 16 else np.uint32)
        self.codewords = None
        self.Ds = None
        self.Dim = -1

    def class_message(self):
        return "Subspace PQ, M: {}, Ks : {}, code_dtype: {}".format(self.M, self.Ks, self.code_dtype)

    def fit(self, vecs, iter):
        assert vecs.dtype == np.float32
        assert vecs.ndim == 2
        N, D = vecs.shape
        assert self.Ks < N, "the number of training vector should be more than Ks"
        self.Dim = D

        reminder = D % self.M
        quotient = int(D / self.M)
        dims_width = [quotient + 1 if i < reminder else quotient for i in range(self.M)]
        self.Ds = np.cumsum(dims_width)     # prefix sum
        self.Ds = np.insert(self.Ds, 0, 0)  # insert zero at beginning

        # [m][ks][ds]: m-th subspace, ks-the codeword, ds-th dim
        self.codewords = np.zeros((self.M, self.Ks, np.max(dims_width)), dtype=np.float32)
        for m in range(self.M):
            if self.verbose:
                print("#    Training the subspace: {} / {}, {} -> {}".format(m, self.M, self.Ds[m], self.Ds[m+1]))
            vecs_sub = vecs[:, self.Ds[m]:self.Ds[m+1]]
            self.codewords[m, :, :self.Ds[m+1] - self.Ds[m]], _ = kmeans2(
                vecs_sub, self.Ks, iter=iter, minit='points')

        return self

    def encode(self, vecs):
        assert vecs.dtype == np.float32
        assert vecs.ndim == 2
        N, D = vecs.shape

        # codes[n][m] : code of n-th vec, m-th subspace
        codes = np.empty((N, self.M), dtype=self.code_dtype)
        for m in range(self.M):
            vecs_sub = vecs[:, self.Ds[m]: self.Ds[m+1]]
            codes[:, m], _ = vq(vecs_sub,
                                self.codewords[m, :, :self.Ds[m+1] - self.Ds[m]])

        return codes

    def decode(self, codes):
        assert codes.ndim == 2
        N, M = codes.shape
        assert M == self.M
        assert codes.dtype == self.code_dtype

        vecs = np.empty((N, self.Dim), dtype=np.float32)
        for m in range(self.M):
            vecs[:, self.Ds[m]: self.Ds[m+1]] = self.codewords[m, codes[:, m], :self.Ds[m+1] - self.Ds[m]]

        return vecs

    def compress(self, vecs):
        return self.decode(self.encode(vecs))


# -*- coding: utf-8; -*-
#
# (c) 2013 Mandriva, http://www.mandriva.com
#
# This file is part of Mandriva Pulse Pull Client.
#
# Pulse Pull client is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# Pulse Pull Client is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with MMC.  If not, see <http://www.gnu.org/licenses/>.
import cx_Threads
import logging
import logging.config
import threading

from poller import Poller
from config import PullClientConfig

class Handler(object):

    # no parameters are permitted; all configuration should be placed in the
    # configuration file and handled in the Initialize() method
    def __init__(self):
        self.stopEvent = cx_Threads.Event()
        self.pollerStop = threading.Event()
        self.config = PullClientConfig.instance()
        logging.config.fileConfig(self.config.config_file)

    # called when the service is starting
    def Initialize(self, configFileName):
        pass

    # called when the service is starting immediately after Initialize()
    # use this to perform the work of the service; don't forget to set or check
    # for the stop event or the service GUI will not respond to requests to
    # stop the service
    def Run(self):
        logger = logging.getLogger()
        logger.info("PullClient starting...")
        self.poller = Poller(self.pollerStop)
        self.poller.start()
        logger.info("PullClient started.")
        self.stopEvent.Wait()

    # called when the service is being stopped by the service manager GUI
    def Stop(self):
        logger = logging.getLogger()
        logger.info("PullClient stopping...")
        self.pollerStop.set()
        self.poller.join()
        logger.info("PullClient stopped.")
        self.stopEvent.Set()


import List_loading
from List_loading import solvent_names, solvent_temperature, solvent_enthalpy
import Choose
from Choose import chosen_solvent
from math import e

Temperature_range = range((int(input("What's the starting temperature (C°)?: ")))+273,int(input("What's the final temperature (C°)?: "))+273)

def Clausius():

    T1 = (float(solvent_temperature[chosen_solvent])+273)
    P1 = 100000
    Hvap = int(solvent_enthalpy[chosen_solvent])
    T2 = x
    exponent = float(((Hvap)/(8.314))*(1/(T1)-1/(T2)))
    soucinitel = float(e**exponent)
    P2 = int((P1*soucinitel))
    print("T= ", x,"K", " P= ",P2, " Pa")

for _ in Temperature_range:
    x = _
    Clausius()


import speech_recognition as sr

def select_microphone(device_index):
    recognizer = sr.Recognizer()

    with sr.Microphone(device_index=device_index) as source:
        print("Sprechen Sie etwas...")
        audio = recognizer.listen(source)

    try:
        recognized_text = recognizer.recognize_google(audio, language="de-DE")
        print("Erkannter Text: ", recognized_text)
    except sr.UnknownValueError:
        print("Ich habe dich nicht verstanden.")
    except sr.RequestError as e:
        print("Fehler bei der Spracherkennung: {0}".format(e))

if __name__ == "__main__":
    # Liste aller verfügbaren Mikrofone abrufen
    mic_list = sr.Microphone.list_microphone_names()

    # Drucken Sie die Namen der verfügbaren Mikrofone und ihre zugehörigen Indizes
    for i, mic_name in enumerate(mic_list):
        print(f"Index {i}: {mic_name}")

    # Wählen Sie das gewünschte Mikrofon aus, indem Sie den entsprechenden Index verwenden
    selected_microphone_index = 0  # Ersetzen Sie 0 durch den gewünschten Index des Mikrofons
    select_microphone(selected_microphone_index)


import airflow
import os
import sys
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
import setup_bucket

from airflow.models import DAG
from airflow.operators.dummy_operator import DummyOperator
from airflow.operators.bash_operator import BashOperator
from airflow.operators.python_operator import ShortCircuitOperator
from airflow.utils.trigger_rule import TriggerRule
from datetime import timedelta

# To run entire dag:
# shell> airflow resetdb
# shell> airflow webserver -p 9990
# shell> airflow scheduler
# wait for the dag to be triggered by scheduler! and hopefully it wont fail! :)

# To test one task like initial_load for example run this with today's date in YYYY-MM-DD format:
# shell> airflow webserver -p 9990
# shell> airflow test retail_dag initial_load <today's_date>

# To get these airflow libraries for pycharm run this in windows shell
# pip install apache-airflow --no-deps

conn = setup_bucket.get_boto3_connection()
resource = conn[1]
bucketName = conn[2]
bucket = resource.Bucket(bucketName)

default_args = {
    'owner': 'airflow',
    'depends_on_past': False,
    'start_date': airflow.utils.dates.days_ago(1)
}

dag = DAG(
    'retail_dag',
    default_args=default_args,
    description='A simple retail DAG',
    schedule_interval=timedelta(minutes=1),
    max_active_runs=1,
    catchup=False
)

start = DummyOperator(
    task_id='start',
    dag=dag
)

inc = BashOperator(
    task_id='incremental_load',
    bash_command="spark-submit --packages mysql:mysql-connector-java:5.1.38,org.apache.spark:spark-avro_2.11:2.4.0 ~/RetailCaseStudy/IncrementalLoads.py ",
    dag=dag
)

av_par = BashOperator(
    task_id='avro_parquet',
    bash_command="spark-submit --packages org.apache.spark:spark-avro_2.11:2.4.0  ~/RetailCaseStudy/AVRO_Parquet.py ",
    trigger_rule=TriggerRule.ONE_SUCCESS,
    dag=dag
)

par_agg = BashOperator(
    task_id='parquet_agg',
    bash_command="spark-submit ~/RetailCaseStudy/Parquet_Agg.py ",
    dag=dag
)

init = BashOperator(
    task_id='initial_load',
    bash_command="spark-submit --packages mysql:mysql-connector-java:5.1.38,org.apache.spark:spark-avro_2.11:2.4.0 ~/RetailCaseStudy/InitialLoads.py ",
    trigger_rule=TriggerRule.ONE_FAILED,
    dag=dag
)

def any_new_rows():
    for obj in bucket.objects.all():
        key = obj.key
        if key == "trg/new_data":
            return True
    return False

any_new_rows_task = ShortCircuitOperator(
    task_id='any_new_rows',
    python_callable=any_new_rows,
    trigger_rule=TriggerRule.ONE_SUCCESS,
    dag=dag
)

csv_snowflake = BashOperator(
    task_id="csv_snowflake",
    bash_command="python3 ~/RetailCaseStudy/save_csv_to_snowflake.py ",
    dag=dag
)

finish = DummyOperator(
    task_id="finish",
    dag=dag
)

start >> inc
inc >> init
init >> av_par
inc >> any_new_rows_task
any_new_rows_task >> av_par
av_par >> par_agg
par_agg >> csv_snowflake
csv_snowflake >> finish


#!/usr/bin/python3
'''This modules contains a function to add integers

Example:
add_integer(1, 2) -> 3
'''

def add_integer(a, b=98):
    ''' adds 2 integers: a and b.

    if any of the argument is a float,
    it is first casted to an integer.

    Args:
        a (int): an integer
        b (int): an integer
    '''
    if type(a) not in [int, float]:
        raise TypeError('a must be an integer')
    if type(b) not in [int, float]:
        raise TypeError('b must be an integer')
    a = int(a)
    b = int(b)
    return a + b


import numpy as np
import statsmodels.api as statm
import statsmodels.formula.api as statfa
import pandas as pd
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D

data = [[2, 0, 81], [4, 4, 93], [6, 2, 91], [8, 3, 97]]
X = [i[0:2] for i in data]
Y = [i[2] for i in data]

X_1=statm.add_constant(X)
results=statm.OLS(Y,X_1).fit()

hour_class=pd.DataFrame(X,columns=['study_hours','private_class'])
hour_class['Score']=pd.Series(Y)

model = statfa.ols(formula='Score ~ study_hours + private_class', data=hour_class)

results_formula = model.fit()

a, b = np.meshgrid(np.linspace(hour_class.study_hours.min(),hour_class.study_hours.max(),100),
                   np.linspace(hour_class.private_class.min(),hour_class.private_class.max(),100))

X_ax = pd.DataFrame({'study_hours': a.ravel(), 'private_class': b.ravel()})
fittedY=results_formula.predict(exog=X_ax)
fig = plt.figure()

graph = fig.add_subplot(111, projection='3d')

graph.scatter(hour_class['study_hours'],hour_class['private_class'],hour_class['Score'],
              c='blue',marker='o', alpha=1)
graph.plot_surface(a,b,fittedY.values.reshape(a.shape),
                   rstride=1, cstride=1, color='none', alpha=0.4)
graph.set_xlabel('study_hours')
graph.set_ylabel('private_class')
graph.set_zlabel('Score')

plt.show()


# -*- coding: utf-8 -*-
from django.conf import settings as conf_settings
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required
from django.contrib import messages

# import models
from apps.qa.models.vendor_user import VendorUser as CQVendorUser
from apps.qa.models.senbay_user import SenbayUser

# import views
from apps.core.views.vendor_common.login_user_info import *

# import api request util
from apps.qa.views.senbay_api.base import request_get, request_post, request_delete

@login_required(login_url='/qa/')
def index(request):
    """ Reservations """
    user_obj = get_login_user_objects(request)

    context = {
        "title": "Reservations",
        "namespace": "qa"
    }

    return render(request, "vendor/reservation/reservation.html", context)

@login_required(login_url='/qa/')
def settings(request):
    """ Reservations Settings"""
    # Get Senby User
    vendor_user = CQVendorUser.objects.filter(auth_user=request.user).first()
    senbay_user = SenbayUser.objects.filter(vendor_user_id=vendor_user.id, is_active=True).first()

    if "token" in request.GET and "sbid" in request.GET and "vuid" in request.GET:
        # Senbay Integration
        print("Senbay Integration....")
        print(request.GET["token"])
        token = request.GET["token"]
        sbid = request.GET["sbid"]
        vuid = request.GET["vuid"]

        if request.user.id == int(vuid):

            senbay_user = SenbayUser.objects.filter(id=sbid).first()

            if token != "error":
                senbay_user.jwt_token = token
                senbay_user.is_active = True

                try:
                    senbay_user.save()
                except Exception as e:
                    print("ERROR: saving...")
                    print(request.user.id)
                    print(vendor_user.id)
                    print(token)
                    print('%r' % e)

    context = request_get(request, "team_detail", None, None)

    if context is None:
        context = dict()
        if not senbay_user:
            senbay_user = SenbayUser()
            senbay_user.vendor_user_id = vendor_user.id
        senbay_user.is_active = False
        senbay_user.save()

    senbay_login_url = conf_settings.SENBAY_LOGIN_URL + "?qa=1&sbid=" + str(senbay_user.id) + "&vuid=" + str(request.user.id)

    context["senbay_user"] = senbay_user
    context["senbay_login_url"] = senbay_login_url
    context["title"] = "Reservations"
    context["namespace"] = "qa"

    return render(request, "vendor/reservation/settings.html", context)

@login_required(login_url='/qa/')
def update_team_setting(request):

    context = request_post(request, "team_detail", request.POST)

    return redirect("/qa/reservation/settings")

@login_required(login_url='/qa/')
def template_list(request):

    context = request_get(request, "template", None, None)
    if context is None:
        context = dict()

    context["title"] = "Schedule List"
    context["namespace"] = "qa"

    return render(request, "vendor/reservation/template_list.html", context)

@login_required(login_url='/qa/')
def template_once(request):

    context = request_get(request, "get_template_once", None, None)
    context["title"] = "One Time URL"
    context["namespace"] = "qa"

    return render(request, "vendor/reservation/template_detail.html", context)

@login_required(login_url='/qa/')
def template_create_once(request):

    context = request_post(request, "create_template_once", request.POST)
    context["title"] = "One Time URL"
    context["namespace"] = "qa"

    return render(request, "vendor/reservation/created_once_url.html", context)

@login_required(login_url='/qa/')
def template_detail(request, template_id=None):
    if template_id is None:
        context = request_post(request, "template", None)

    else:
        context = request_get(request, "template", None, template_id)

    context["template_id"] = template_id
    context["title"] = "Schedule Edit"
    context["namespace"] = "qa"
    return render(request, "vendor/reservation/template_detail.html", context)

@login_required(login_url='/qa/')
def template_add(request):

    context = request_post(request, "add_template", request.POST)
    context["title"] = "Schedule Edit"
    context["namespace"] = "qa"

    return redirect("/qa/reservation/template/list/")

@login_required(login_url='/qa/')
def template_edit(request, template_id):
    request_post(request, "add_template", request.POST)
    request_delete(request, "template", template_id)

    return redirect("/qa/reservation/template/list/")

@login_required(login_url='/qa/')
def template_delete(request, template_id=None):

    context = request_delete(request, "template", template_id)
    context["title"] = "Schedule List"
    context["namespace"] = "qa"

    return redirect("/qa/reservation/template/list/")

@login_required(login_url='/qa/')
def account_detail(request):
    context = request_get(request, "account", None, None)
    if context is None:
        context = dict()
    context["title"] = "Account Edit"
    context["namespace"] = "qa"
    return render(request, "vendor/reservation/account.html", context)

@login_required(login_url='/qa/')
def update_account_setting(request):
    context = request_post(request, "account", request.POST)
    context["title"] = "Account UPDATE"
    context["namespace"] = "qa"
    messages.success(request, 'アカウント設定を保存しました。')

    return render(request, "vendor/reservation/account.html", context)

@login_required(login_url='/qa/')
def schedule_now_list(request):

    context = request_get(request, "get_schedule_now_list", None, None)
    if context is None:
        context = dict()

    context["title"] = "Schedule List"
    context["namespace"] = "qa"

    return render(request, "vendor/reservation/schedule_list.html", context)

@login_required(login_url='/qa/')
def schedule_past_list(request):

    context = request_get(request, "get_schedule_past_list", None, None)
    context["title"] = "Schedule Past List"
    context["namespace"] = "qa"

    return render(request, "vendor/reservation/schedule_list.html", context)

@login_required(login_url='/qa/')
def schedule_delete(request, schedule_history_id=None, mode_menu=None):
    data = {
        "schedule_history_id": schedule_history_id,
        "mode_menu": mode_menu,
    }

    context = request_post(request, "delete_schedule", data)

    if mode_menu == "now":
        context = request_get(request, "get_schedule_now_list", None, None)
    else:
        context = request_get(request, "get_schedule_past_list", None, None)

    context["title"] = "Schedule List"
    context["namespace"] = "qa"

    messages.success(request, 'スケジュール履歴を削除しました。')

    return render(request, "vendor/reservation/schedule_list.html", context)


import numpy as np

print("enter m (nodes) = ")
d = 0.8
m2 = int(input())
ma2 = np.zeros((m2, m2), dtype=int)
ma2[0][0] = 1
for i in range(m2):
    if i + 1 < m2:
        ma2[i][i + 1] = 1

print("matrix: ")
print(ma2)

pr = [1] * m2

def cal_pagerank(s2):
    return (1 - d) + d * s2

4
for a in range(10):
    for i in range(len(ma2)):
        s = 0
        col = [row[i] for row in ma2]
        for j in range(len(col)):
            if col[j] == 1:
                s += pr[j] / sum(ma2[j])

        pr[i] = cal_pagerank(s)

print("page rank: ")
print([round(num, 3) for num in pr])


# cook your dish here
l = []
for i in range(3):
    num = int(input())
    l.append(num)

def seclargest(l):
    if len(l)<= 1:
        return None
    lar = l[0]
    slar = None

    for x in l[1:]:
        if x >lar:
            slar = lar
            lar = x
        elif x!= lar:
            if slar == None or slar < x:
                slar = x

    return slar

print(seclargest(l))


import ijson
import lzip
from ptp2.utils import flatten_nested_keys, listify_nested_keys

def unzip_lizp(file_path, extract_to=None, return_stream=False):
    """
    Unzip a .lizp file and optionally return a file stream.

    Args:
        file_path (str): The path to the .lizp file.
        extract_to (str, optional): The directory where to extract the file. Defaults to None.
        return_stream (bool, optional): Whether to return a file stream. Defaults to False.

    Returns:
        file stream if open is True, None otherwise.

    Raises:
        ValueError: If extract_to is not set and open is False.
    """
    if extract_to is None and not open:
        raise ValueError("Either 'extract_to' must be set or 'open' must be True.")

    # unzip file
    if file_path.startswith('http'):
        in_memory_extract = lzip.decompress_url(file_path)
    else:
        in_memory_extract = lzip.decompress_file(file_path)

    # extract to file
    if extract_to is not None:
        # write to file in extract_to
        with open(extract_to, 'wb') as f:
            f.write(in_memory_extract)

    if return_stream:
        # return file stream
        return in_memory_extract

def stream_json(json_input, i = None, fun=lambda x: x, filter_fun = lambda x: True, limit=None, prefix='item'):
    """
    Stream a JSON file or a file stream.

    Args:
        json_input (str or file stream): The path to the JSON file or a file stream.
        i (int, optional): The index of the record to start streaming from. Defaults to None.
        fun (function, optional): A function to apply to each record. Defaults to identity function.
        filter_fun (function, optional): A function to filter records. Defaults to function that always returns True.
        limit (int, optional): The maximum number of records to stream. Defaults to None.
        prefix (str, optional): The prefix for the ijson.items function. Defaults to 'item'.

    Yields:
        The next record from the JSON file or file stream that passes the filter function, after applying the function `fun`.
    """

    print('streaming json')

    if isinstance(json_input, str):
        json_file = open(json_input, 'rb')
    else:
        json_file = json_input

    for j, record in enumerate(ijson.items(json_file, prefix)):

        if i is not None:
            if j < i:
                continue
            elif j > i:
                break

        if filter_fun(record):
            yield fun(record)

            if limit is not None:
                limit -= 1
                if limit == 0:
                    break
        else:
            continue

    if isinstance(json_input, str):
        json_file.close()

def load_json(json_input, i = None, fun=lambda x: x, filter_fun = lambda x: True, limit=None, prefix='item'):
    """
    Load a JSON file.

    Args:
        json_input (str or file stream): The path to the JSON file or a file stream.
        i (int, optional): The index of the record to start loading from. Defaults to None.
        fun (function, optional): A function to apply to each record. Defaults to identity function.
        filter_fun (function, optional): A function to filter records. Defaults to function that always returns True.
        limit (int, optional): The maximum number of records to load. Defaults to None.
        prefix (str, optional): The prefix for the ijson.items function. Defaults to 'item'.

    Returns:
        list: A list of records from the JSON file that pass the filter function, after applying the function `fun`.
    """

    return list(stream_json(json_input, i, fun, filter_fun, limit, prefix))

class ParltrackRecord(object):
    """Parltrack record."""

    # names of elements to exclude from DataFrame
    df_varnames_exclude = ['changes', 'meta']

    # keys of elements to flatten from single element lists to the element itself
    keynames_flatten = []

    # keys of elements to listify from single element to a list containing the element
    keynames_listify = []

    def __init__(self, record):
        """Initialize."""

        self.changes = []
        self.meta = {}

        # set all attributes from record dict
        for k, v in record.items():
            setattr(self, str(k).lower().replace(' ', '_'), v)

    def to_dict(self):
        """Convert to dict."""

        return {k: v for k, v in self.__dict__.items() if k not in ParltrackRecord.df_varnames_exclude}

    def to_df(self):
        """Convert to pandas DataFrame."""

        import pandas as pd

        return pd.DataFrame([self.to_dict()])

    def __repr__(self):
        """Repr."""

        return str(self.to_dict())

    def __str__(self):
        """Str."""

        return str(self.to_dict())

    def flatten_keys(self):
        """Flatten specified keys."""

        for key_path in self.keynames_flatten:
            flatten_nested_keys(self.__dict__, key_path)

    def listify_keys(self):
        """Listify specified keys."""

        for key_path in self.keynames_listify:
            listify_nested_keys(self.__dict__, key_path)

class ParltrackSubRecord(ParltrackRecord):
    """Parltrack sub-record."""

    def __init__(self, record):
        """Initialize."""

        super().__init__(record)

        del self.changes
        del self.meta


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

def getNum(head):
    p1 = head
    n1 = ""
    while p1:
        num = str(p1.value)
        n = len(num)
        if n < 4:
            x = ["0" for x in range(n,4)]
            st = "".join(x)
            num = st+num

        n1 += str(num)
        p1 = p1.next

    return int(n1)

def addTwoHugeNumbers(a, b):
    n1 = getNum(a)
    n2 = getNum(b)

    result = n1+n2
    res = str(result)

    arr = []
    n = len(res)

    for x in xrange(n,-1,-4):
        end = x
        start = x-4

        if start < 0:
            start = 0
        if end > 0:
            num = res[start:end]
            arr.append(int(num))

    r = None

    for x in arr[::-1]:
        if r:
            node = ListNode(int(x))
            n = r
            while True:
                if not n.next:
                    n.next = node
                    break

                n = n.next
        else:
            r = ListNode(int(x))

    return r


# 쌓여있는 막대기의 수 cnt
# '(' cnt+=1 -> 레이저 시작(?)
#  뒤를 체크(범위!)-> 앞을 체크 -> index 사용
# else ')'
# if st[i-1] == '('  # 레이저
#           cnt -= 1, ans+=cnt
# else # ')' 막대기 끝
#           cnt-=1, ans+=1

for tc in range(1,int(input())+1):

    st = input()
    ans = cnt = 0
    for i in range(len(st)):
        if st[i] == '(':
            cnt += 1
        else:           # ')'
            if st[i-1]=='(':
                cnt-=1
                ans+=cnt
            else:
                cnt-=1
                ans+=1

    print(f'#{tc} {ans}')


import torch
from torch import nn, optim
from torch.nn import functional as F
from torch.nn.modules import Sequential
from torchvision import datasets, transforms
from torch.autograd import Variable

from collections import OrderedDict

class Generator(nn.Module):
	def __init__(self):
		super(Generator, self).__init__()
		self.g = Sequential(
			nn.ConvTranspose2d(64, 512, kernel_size=(1, 1), stride=(1, 1), bias=False),
			nn.BatchNorm2d(512, eps=1e-5, momentum=0.1, affine=True),
			nn.ReLU(inplace=True),
			nn.ConvTranspose2d(512, 256, kernel_size=(7, 7), stride=(1, 1), bias=False),
			nn.BatchNorm2d(256, eps=1e-5, momentum=0.1, affine=True),
			nn.ReLU(inplace=True),
			nn.ConvTranspose2d(256, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False),
			nn.BatchNorm2d(128, eps=1e-5, momentum=0.1, affine=True),
			nn.ReLU(inplace=True),
			nn.ConvTranspose2d(128, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False),
			nn.BatchNorm2d(64, eps=1e-5, momentum=0.1, affine=True),
			nn.ReLU(inplace=True),
			nn.ConvTranspose2d(64, 1, kernel_size=(3, 3), stride=(1, 1), padding=(1, 1), bias=False),
			nn.Tanh(),
		)

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

# two head
class DiscriminatorAndQ(nn.Module):
	def __init__(self):
		super(DiscriminatorAndQ, self).__init__()

		# shared by discriminator and Q
		self.front = Sequential(
			nn.Conv2d(1, 64, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False),
			nn.LeakyReLU(0.2, inplace=True),
			nn.Conv2d(64, 128, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False),
			nn.BatchNorm2d(128, eps=1e-5, momentum=0.1, affine=True),
			nn.LeakyReLU(0.2, inplace=True),
			nn.Conv2d(128, 256, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False),
			nn.BatchNorm2d(256, eps=1e-5, momentum=0.1, affine=True),
			nn.LeakyReLU(0.2, inplace=True),
			nn.Conv2d(256, 512, kernel_size=(4, 4), stride=(2, 2), padding=(1, 1), bias=False),
			nn.BatchNorm2d(512, eps=1e-5, momentum=0.1, affine=True),
			nn.LeakyReLU(0.2, inplace=True),
		)

		self.d = Sequential(
			nn.Conv2d(512, 1, kernel_size=(1, 1), stride=(1, 1), padding=(0, 0), bias=False),
			nn.Sigmoid(),
		)

		self.q = Sequential(
			nn.Linear(512, 100, bias=True),
			nn.ReLU(inplace=True),
			nn.Linear(100, 10, bias=True),
		)

	def D(self, img):
		out = self.front(img)
		out = self.d(out)
		out = out.view(-1, 1)
		return out

	def Q(self, img):
		out = self.front(img)
		out = out.view(-1, 512)
		out = self.q(out)
		return out

	def forward(self, img):
		pass


from django.db import models

#Implemented! Not verified.

class Chore(models.Model):
    #only items required are the
    name = models.TextField(blank=True)              #
    description = models.TextField(blank=True)
    isAllDay = models.BooleanField(blank=False)
    points = models.IntegerField(blank=False)

    #The following is the "Chore Status" enum from the Chore.Status category.
    #Due to time constraints, I'm going to make this an int that will evaluate client side
    #Here, 0= "COMPLETED", 1= "ACTIVE" and 2 = "POSTPONED.
    status = models.IntegerField(blank=False, null=False)

    #The same happens for Recurrence Pattern, another enum
    #Here, 0 = "NONE", 1 = "DAILY", 2 = "WEEKLY", and 3 = "MONTHLY"
    recurrence_pattern = models.IntegerField(null=False, blank=False)

    #RecurrenceEndDate is of type Date, but we're ok with it being null, it's what we expect since the feature is not
    # yet implemented.
    recurrence_enddate = models.DateTimeField(null=True, blank=False)

    #deadline is a datetime object that IS required
    deadline = models.DateTimeField(blank=False)

    #The following are the associations for chore to other objects

    #creator association( to a single User). when pulling value, look for the first (and only) value
    created_by = models.ForeignKey(
        'users.User',
        related_name='chores_created',             #the users -> choresCreated query allows me to show all the chores a user has made
        on_delete=models.SET_NULL,
        null=True,
    )

    #assignee association (to a single User)
    #MUST TEST IF YOU CAN QUERY users -> chores
    assigned_to = models.ForeignKey(
        'users.User',
        related_name='chores_assigned',            #the users -> choresAssigned query allows me to show all the chores a user has been assigned
        on_delete=models.SET_NULL,
        null=True,
    )

    #resources[]  association (a list of Resources

        #We actually aren't going to make a resource association. By creating an association

    belongs_to = models.ForeignKey(
        'households.Household',
        related_name="chores",
        on_delete=models.SET_NULL,
        null=True,
    )


import os

BAD_COLS = {
    "Metadata_ChannelNumber_Organoid",
    "Metadata_FileLocation_Organoid",
    "Metadata_Frame_Organoid",
    "Metadata_Plate_Organoid",
    "Metadata_Series_Organoid",
    "Metadata_Site_Organoid",
    "Metadata_Well_Organoid",
    "Children_NucleiObjects_Count",
    "Number_Object_Number_Organoid",
    "Parent_NucleiObjectsPrimary",
    "Metadata_ChannelNumber_Nuclei",
    "Metadata_FileLocation_Nuclei",
    "Metadata_Frame_Nuclei",
    "Metadata_Plate_Nuclei",
    "Metadata_Series_Nuclei",
    "Metadata_Site_Nuclei",
    "Metadata_Well_Nuclei",
    "Metadata_ChannelNumber",
    "Metadata_FileLocation",
    "Metadata_Frame",
    "Metadata_Plate",
    "Metadata_Series",
    "Metadata_Site",
    "Metadata_Well",
    "Channel_Channels",
    "Count_MergedCellObjects",
    "Count_NucleiObjects",
    "Count_NucleiObjectsPrimary",
    "ExecutionTime_01Images",
    "ExecutionTime_02Metadata",
    "ExecutionTime_03NamesAndTypes",
    "ExecutionTime_04Groups",
    "ExecutionTime_05ColorToGray",
    "ExecutionTime_06RescaleIntensity",
    "ExecutionTime_07IdentifyPrimaryObjects",
    "ExecutionTime_08SplitOrMergeObjects",
    "ExecutionTime_09IdentifyPrimaryObjects",
    "ExecutionTime_10RelateObjects",
    "ExecutionTime_11MeasureObjectIntensity",
    "ExecutionTime_12MeasureObjectSizeShape",
    "ExecutionTime_13MeasureGranularity",
    "ExecutionTime_14MeasureTexture",
    "ExecutionTime_15MeasureImageAreaOccupied",
    "ExecutionTime_19OverlayOutlines",
    "ExecutionTime_20SaveImages",
    "ExecutionTime_17MeasureGranularity",
    "FileName_Channels",
    "FileName_OrigOverlay",
    "Frame_Channels",
    "Group_Index",
    "Group_Number",
    "Height_Channels",
    "ImageSet_ImageSet",
    "MD5Digest_Channels",
    "Metadata_ChannelNumber",
    "Metadata_FileLocation",
    "Metadata_Frame",
    "Metadata_Plate",
    "Metadata_Series",
    "Metadata_Site",
    "Metadata_Well",
    "ModuleError_01Images",
    "ModuleError_02Metadata",
    "ModuleError_03NamesAndTypes",
    "ModuleError_04Groups",
    "ModuleError_05ColorToGray",
    "ModuleError_06RescaleIntensity",
    "ModuleError_07IdentifyPrimaryObjects",
    "ModuleError_08SplitOrMergeObjects",
    "ModuleError_09IdentifyPrimaryObjects",
    "ModuleError_10RelateObjects",
    "ModuleError_11MeasureObjectIntensity",
    "ModuleError_12MeasureObjectSizeShape",
    "ModuleError_13MeasureGranularity",
    "ModuleError_14MeasureTexture",
    "ModuleError_15MeasureImageAreaOccupied",
    "ModuleError_19OverlayOutlines",
    "ModuleError_20SaveImages",
    "PathName_Channels",
    "PathName_OrigOverlay",
    "ProcessingStatus",
    "Scaling_Channels",
    "Series_Channels",
    "URL_Channels",
    "URL_OrigOverlay",
    "Width_Channels",
    "Metadata_C",
    "Metadata_SizeC",
    "Metadata_SizeT",
    "Metadata_SizeX",
    "Metadata_SizeY",
    "Metadata_SizeZ",
    "Metadata_Z",
    "Channel_Raw",
    "Channel_background",
    "Channel_nuclei_border",
    "Channel_nuclei_mask",
    "Count_nuclei_mask_borders_scaled_object",
    "Count_nuclei_mask_scaled_object",
    "ExecutionTime_05RescaleIntensity",
    "ExecutionTime_07RescaleIntensity",
    "ExecutionTime_08RescaleIntensity",
    "ExecutionTime_09ImageMath",
    "ExecutionTime_10RescaleIntensity",
    "ExecutionTime_11IdentifyPrimaryObjects",
    "ExecutionTime_12IdentifySecondaryObjects",
    "ExecutionTime_14MeasureObjectIntensity",
    "ExecutionTime_15MeasureObjectSizeShape",
    "ExecutionTime_16MeasureGranularity",
    "ExecutionTime_17MeasureTexture",
    "ExecutionTime_18MeasureImageAreaOccupied",
    "ExecutionTime_19MeasureImageQuality",
    "ExecutionTime_23OverlayOutlines",
    "ExecutionTime_24Tile",
    "ExecutionTime_25SaveImages",
    "ExecutionTime_26SaveImages",
    "Frame_Raw",
    "Frame_background",
    "Frame_nuclei_border",
    "Frame_nuclei_mask",
    "Height_Raw",
    "Height_background",
    "Height_nuclei_border",
    "Height_nuclei_mask",
    "Metadata_C_Image",
    "Metadata_Series_Image",
    "Metadata_SizeC_Image",
    "Metadata_SizeT_Image",
    "Metadata_SizeX_Image",
    "Metadata_SizeY_Image",
    "Metadata_SizeZ_Image",
    "Metadata_Z_Image",
    "ModuleError_05RescaleIntensity",
    "ModuleError_07RescaleIntensity",
    "ModuleError_08RescaleIntensity",
    "ModuleError_09ImageMath",
    "ModuleError_10RescaleIntensity",
    "ModuleError_11IdentifyPrimaryObjects",
    "ModuleError_12IdentifySecondaryObjects",
    "ModuleError_14MeasureObjectIntensity",
    "ModuleError_15MeasureObjectSizeShape",
    "ModuleError_16MeasureGranularity",
    "ModuleError_17MeasureTexture",
    "ModuleError_18MeasureImageAreaOccupied",
    "ModuleError_19MeasureImageQuality",
    "ModuleError_23OverlayOutlines",
    "ModuleError_24Tile",
    "ModuleError_25SaveImages",
    "ModuleError_26SaveImages",
    "Scaling_Raw",
    "Scaling_background",
    "Scaling_nuclei_border",
    "Scaling_nuclei_mask",
    "Series_Raw",
    "Series_background",
    "Series_nuclei_border",
    "Series_nuclei_mask",
    "Channel_Raw",
    "Channel_background",
    "Channel_nuclei_border",
    "Channel_nuclei_mask",
    "Count_nuclei_mask_borders_scaled_object",
    "Count_nuclei_mask_scaled_object",
    "ExecutionTime_05RescaleIntensity",
    "ExecutionTime_07RescaleIntensity",
    "ExecutionTime_08RescaleIntensity",
    "ExecutionTime_09ImageMath",
    "ExecutionTime_10RescaleIntensity",
    "ExecutionTime_11IdentifyPrimaryObjects",
    "ExecutionTime_12IdentifySecondaryObjects",
    "ExecutionTime_14MeasureObjectIntensity",
    "ExecutionTime_15MeasureObjectSizeShape",
    "ExecutionTime_16MeasureGranularity",
    "ExecutionTime_17MeasureTexture",
    "ExecutionTime_18MeasureImageAreaOccupied",
    "ExecutionTime_19MeasureImageQuality",
    "ExecutionTime_23OverlayOutlines",
    "ExecutionTime_24Tile",
    "ExecutionTime_25SaveImages",
    "ExecutionTime_26SaveImages",
    "Frame_Raw",
    "Frame_background",
    "Frame_nuclei_border",
    "Frame_nuclei_mask",
    "Height_Raw",
    "Height_background",
    "Height_nuclei_border",
    "Height_nuclei_mask",
    "Metadata_C_Image",
    "Metadata_Series_Image",
    "Metadata_SizeC_Image",
    "Metadata_SizeT_Image",
    "Metadata_SizeX_Image",
    "Metadata_SizeY_Image",
    "Metadata_SizeZ_Image",
    "Metadata_Z_Image",
    "ModuleError_05RescaleIntensity",
    "ModuleError_07RescaleIntensity",
    "ModuleError_08RescaleIntensity",
    "ModuleError_09ImageMath",
    "ModuleError_10RescaleIntensity",
    "ModuleError_11IdentifyPrimaryObjects",
    "ModuleError_12IdentifySecondaryObjects",
    "ModuleError_14MeasureObjectIntensity",
    "ModuleError_15MeasureObjectSizeShape",
    "ModuleError_16MeasureGranularity",
    "ModuleError_17MeasureTexture",
    "ModuleError_18MeasureImageAreaOccupied",
    "ModuleError_19MeasureImageQuality",
    "ModuleError_23OverlayOutlines",
    "ModuleError_24Tile",
    "ModuleError_25SaveImages",
    "ModuleError_26SaveImages",
    "Scaling_Raw",
    "Scaling_background",
    "Scaling_nuclei_border",
    "Scaling_nuclei_mask",
    "Series_Raw",
    "Series_background",
    "Series_nuclei_border",
    "Series_nuclei_mask",
    "Width_Raw",
    "Width_background",
    "Width_nuclei_border",
    "Width_nuclei_mask",
    "Intensity_UpperQuartileIntensity_Raw_scaled",
    "Location_CenterMassIntensity_X_Raw_scaled",
    "Location_CenterMassIntensity_Y_Raw_scaled",
    "Location_CenterMassIntensity_Z_Raw_scaled",
    "Location_Center_X",
    "Location_Center_Y",
    "Location_Center_Z",
    "Location_MaxIntensity_X_Raw_scaled",
    "Location_MaxIntensity_Y_Raw_scaled",
    "Location_MaxIntensity_Z_Raw_scaled",
    "Number_Object_Number",
    "Channel_Raw",
    "Channel_background",
    "Channel_nuclei_border",
    "Channel_nuclei_mask",
    "Count_nuclei_mask_borders_scaled_object",
    "Count_nuclei_mask_scaled_object",
    "ExecutionTime_05RescaleIntensity",
    "ExecutionTime_07RescaleIntensity",
    "ExecutionTime_08RescaleIntensity",
    "ExecutionTime_09ImageMath",
    "ExecutionTime_10RescaleIntensity",
    "ExecutionTime_11IdentifyPrimaryObjects",
    "ExecutionTime_12IdentifySecondaryObjects",
    "ExecutionTime_14MeasureObjectIntensity",
    "ExecutionTime_15MeasureObjectSizeShape",
    "ExecutionTime_16MeasureGranularity",
    "ExecutionTime_17MeasureTexture",
    "ExecutionTime_18MeasureImageAreaOccupied",
    "ExecutionTime_19MeasureImageQuality",
    "ExecutionTime_23OverlayOutlines",
    "ExecutionTime_24Tile",
    "ExecutionTime_25SaveImages",
    "ExecutionTime_26SaveImages",
    "Frame_Raw",
    "Frame_background",
    "Frame_nuclei_border",
    "Frame_nuclei_mask",
    "ExecutionTime_01Images",
    "ExecutionTime_02Metadata",
    "ExecutionTime_03NamesAndTypes",
    "ExecutionTime_04Groups",
    "ExecutionTime_06RescaleIntensity",
    "ExecutionTime_13MeasureGranularity",
    "ExecutionTime_14MeasureImageAreaOccupied",
    "ExecutionTime_15MeasureImageIntensity",
    "ExecutionTime_17MeasureImageQuality",
    "ExecutionTime_19MeasureObjectIntensity",
    "ExecutionTime_20MeasureObjectIntensityDistribution",
    "ExecutionTime_23MeasureObjectSizeShape",
    "ExecutionTime_26MeasureTexture",
}

import pandas as pd
import numpy as np
import json

def zero_is_control(df):
    index_headers = list(df.index.names)
    df = df.reset_index()
    # index = df.index.to_frame()
    df["Drug"] = df["Drug"].where(df["Conc /uM"] != 0, other="Control")
    return df.set_index(index_headers)

def force_numeric(df):
    return (
        df.select_dtypes("number")
        .apply(pd.to_numeric)
        .select_dtypes("number")
        .replace([np.inf, -np.inf], np.nan)
        .dropna(axis=1)
        # .replace([np.inf, -np.inf], np.nan)
        # .dropna(axis=1)
        .sample(frac=1)
    )

def nuclei_primary_filter(df):
    return df.filter(regex=r"^((?!NucleiObjectsPrimary).)*$")

def drop_bad_cols_by_file(df, bad_cols_file):
    if bad_cols_file == "":
        return df
    with open(bad_cols_file, "r") as f:
        # print(*raw_df_indexed.columns,sep='\n')
        return df.pipe(drop_bad_cols, json.load(f))

def drop_bad_cols(df, bad_cols):
    return df.drop(columns=bad_cols, errors="ignore")

def drop_bad_index(df):
    return df.reindex(df.index.dropna())

class Cellprofiler(pd.DataFrame):
    def __init__(
        self,
        *args,
        data_folder=None,
        nuclei_path="Secondary.csv",
        image_path=None,
        organoid_path=None,
        object_headers=["ImageNumber", "ObjectNumber"],
        filename_headers=["Date", "Drug", "Cell", "Replicate", "Conc /uM"],
        # regex_pattern=r"[\/\\](?P<Date>[\d]+)_.+_(?P<Cell>ISO[\d]+)_(?P<Drug>[A-Za-z0-9]+)_(?P<Concentration>[\d\w_-]+uM)(?:.+Position_(?P<Position>[\d]))?",
        regex_pattern=r"(?P<Date>([\d]{4}\-[\d]{2}\-[\d]{2})).+_(?P<Cell>ISO[\d]+)_(?P<Drug>[A-Za-z0-9]+)_(?P<Concentration>[\d\w_-]+uM)(?:.+Position_(?P<Position>[\d]))?",
        SAVE_FIG=False,
        SAVE_CSV=False,
        bad_cols_file="",
        bad_cols=BAD_COLS,
        CHANNELS=1,
        ZEROISCONTROL=True,
        **kwargs,
    ):
        # sns.set()

        np.random.seed(42)
        # super(Cellprofiler,  self).__init__()
        # self.df = pd.read_csv(nuclei_file_path)
        # print(data_folder)
        attrs = {
            "data_folder": data_folder,
            "nuclei_path": nuclei_path,
            "image_path": image_path,
            "organoid_path": organoid_path,
            "SAVE_FIG": SAVE_FIG,
            "SAVE_CSV": SAVE_CSV,
            "bad_cols_file": bad_cols_file,
            "bad_cols": bad_cols,
            "CHANNELS": CHANNELS,
            "ZEROISCONTROL": ZEROISCONTROL,
            "object_headers": object_headers,
            "filename_headers": filename_headers,
            "regex_pattern": regex_pattern,
            "index_headers": object_headers + filename_headers,
        }
        super(Cellprofiler, self).__init__(*args)
        self.attrs = attrs

        # print(self.attrs)
        # df = self.get_df()
        # super(Cellprofiler, self).__init__(*args,**kwargs)
        # return CellprofilerActive
        # super(Cellprofiler, self).__init__(,*args, **kwargs)
        # self = pd.DataFrame(df)
        # self.attrs.update(vars)
        # self.update(df)

        # print(df)
        # self.index_headers = self.object_headers + self.filename_headers
        # df = self.get_df()
        #     # .pipe(self.extract_headers,regex_pattern,filename_headers,index_headers)
        #     # .pipe(self.drop_bad_cols,bad_cols_file)
        #     # .pipe(self.zero_is_control)
        #     )

        # self.update(df.to_dict())
        # print(df)

        # print(self)

    def get_data(self):
        # df = self.get_df(self.attrs["data_folder"],self.attrs["nuclei_path"])

        df = (
            self.get_df(self.attrs["data_folder"], self.attrs["nuclei_path"])
            .pipe(
                self.extract_headers,
                self.attrs["regex_pattern"],
                self.attrs["filename_headers"],
                self.attrs["index_headers"],
            )
            .pipe(drop_bad_cols, self.attrs["bad_cols"])
            .pipe(drop_bad_cols_by_file, self.attrs["bad_cols_file"])
            .pipe(zero_is_control)
            .pipe(drop_bad_index)
            .pipe(force_numeric)
        )
        # df = CellprofilerDataFrame(df,attrs=self.attrs)

        # df = CellprofilerDataFrame(df)
        df.attrs.update(self.attrs)
        # return CellprofilerDataFrame(df,attrs=self.attrs)
        return df

    @property
    def _constructor(self):
        # print(self.attrs)
        # print("hello")
        return Cellprofiler

    def get_df(self, data_folder, nuclei_path):
        # return pd.DataFrame()
        # raw_df = self.read_data()
        # extracted_data = self.extract_filenames(
        #     self.get_filenames(raw_df),
        #     regex_pattern,
        #     filename_headers)
        return self.read_data(data_folder, nuclei_path)

    def read_data(self, data_folder, nuclei_path):
        nuclei_df = pd.read_csv(os.path.join(data_folder, nuclei_path))
        # if image_path is not None:
        #     image_df = pd.read_csv(f"{data_folder}/{image_path}")
        #     image_nuclei_df = pd.merge(
        #         nuclei_df,
        #         image_df,
        #         on="ImageNumber",
        #         how="left",
        #         # left_index = True,
        #         suffixes=("", "_Image"),
        #     )
        #     return image_nuclei_df
        return nuclei_df

    # %% Utils

    def extract_headers(self, df, regex_pattern, filename_headers, index_headers):
        filenames = self.get_filenames(df)
        extracted_data = self.extract_filenames(
            filenames, regex_pattern, filename_headers
        )
        df_indexed = df.join(extracted_data).set_index(index_headers)
        return df_indexed.drop(
            ["Metadata_FileLocation", "PathName_image"], axis=1, errors="ignore"
        )

    def get_filenames(self, df):
        return df[
            df.columns.intersection(["Metadata_FileLocation", "PathName_image"])
        ].iloc[:, 0]

    def extract_filenames(self, filenames, regex_pattern, filename_headers, decimals=3):
        extracted_data = filenames.str.extract(regex_pattern)
        extracted_data["filenames"] = filenames
        extracted_data["Replicate"] = extracted_data["Position"].fillna(1)
        extracted_data["Conc /uM"] = pd.to_numeric(
            extracted_data["Concentration"]
            .str.replace("_", ".")
            .str.replace("-", ".")
            .str.replace("uM", "")
        ).round(decimals=decimals)
        return extracted_data[filename_headers]


import re
import numpy as np
import dictionaries
import csv

# Global sets storing determiners and nouns for comparison
determiners = []
stems = []
endings = []
determiner_name_to_number = {}
big_stems = []
small_stems = []
ending_mapping = {}
det_to_size_and_plurality = {}
levenshtein_threshold = 1
big_stems_number_to_training_or_testing = {}
small_stems_number_to_training_or_testing = {}

def split_by_regex(response):
    split_string = re.split(" ",response)

    # Case of single string
    if(len(split_string) == 1):
        return (single_string_parse(split_string))
    if(len(split_string) == 2):
        return (two_string_parse(split_string))
    if(len(split_string) == 3):
        return (three_string_parse(split_string))

    # greater than 3 or empty string, return sentinel value
    return (None,None,None)

# Considers the first string to be the determiner and tries to split the second string into syllables
def two_string_parse(response):
    contains_something = re.search("something",response[1])

    # Checks for the word "something" in the noun. If found, checks for a suffix, if found, it splits, otherwise, returns sentinel value (None,None,None)
    if(contains_something):
        split_something = re.split("something", response[1])

        if(split_something[1] == ''):
            return (None,None,None)
        return (response[0], "something",split_something[1])
    noun = response[1]

    # This regex searches for [consonant cluster][vowel cluster][consonant cluster]. If found, it splits this into the first syllable and returns (determiner, first syllable, second syllable)

    # Case where we start with consonant i.e. heefesh
    if(re.findall("[b-df-hj-np-tv-z]+[aeiou]+[b-df-hj-np-tv-z]+", noun)):
        first_syllable = re.findall("[b-df-hj-np-tv-z]+[aeiou]+[b-df-hj-np-tv-z]+", noun)[0]
        second_syllable = re.split("[b-df-hj-np-tv-z]+[aeiou]+[b-df-hj-np-tv-z]+", noun,1)[1]
        return (response[0],first_syllable, second_syllable)
    # Case where we start with vowel i.e. eefesh, uses wildcard in place of + for 0 or more instead of
    # 1 or more
    elif(re.findall("[b-df-hj-np-tv-z]*[aeiou]+[b-df-hj-np-tv-z]+", noun)):
        first_syllable = re.findall("[b-df-hj-np-tv-z]*[aeiou]+[b-df-hj-np-tv-z]+", noun)[0]
        second_syllable = re.split("[b-df-hj-np-tv-z]*[aeiou]+[b-df-hj-np-tv-z]+", noun,1)[1]
        return (response[0],first_syllable, second_syllable)
    # Case where we couldn't parse syllables
    else:
        return (None,None,None)

# If a single string was entered, we attempt to parse the string as a determiner or a noun by passing in (string, string) to the 2 string function
def single_string_parse(response):
    return two_string_parse([response[0], response[0]])

# Concatenates second and third string entry and considers first entry to be determiner
def three_string_parse(response):
    return two_string_parse([response[0], response[1]+response[2]])

def levenshtein(seq1, seq2):
    size_x = len(seq1) + 1
    size_y = len(seq2) + 1
    matrix = np.zeros ((size_x, size_y))
    for x in xrange(size_x):
        matrix [x, 0] = x
    for y in xrange(size_y):
        matrix [0, y] = y

    for x in xrange(1, size_x):
        for y in xrange(1, size_y):
            if seq1[x-1] == seq2[y-1]:
                matrix [x,y] = min(
                    matrix[x-1, y] + 1,
                    matrix[x-1, y-1],
                    matrix[x, y-1] + 1
                )
            else:
                matrix [x,y] = min(
                    matrix[x-1,y] + 1,
                    matrix[x-1,y-1] + 1,
                    matrix[x,y-1] + 1
                )
    return (matrix[size_x - 1, size_y - 1])

# type: 0 -> determiner 1 -> stem 2 -> ending
def morpheme_levenshtein_comparison(morpheme, threshold, type):
    # Stores the current lowest LD between the morpheme and the possible options
    curr_closest = 1000000

    if(type == 0):
        for i in determiners:
            if (levenshtein(morpheme, i) == curr_closest):
                # Unmappable, 0 indicates that morpheme is not gibberish, needs human inspect
                return (0, morpheme)
            if(levenshtein(morpheme, i) < threshold and levenshtein(morpheme, i) < curr_closest):
                curr_closest = levenshtein(morpheme, i)
                curr_best = i

        # Mapped to nothing because every LD higher than threshold, -1 indicates gibberish
        if (curr_closest == 1000000):
            return (-1, morpheme)

        # Mapped successfully, return 1 indicating success and the mapped morpheme
        return (1, curr_best)
    elif (type == 1):
        for i in stems:
            if (levenshtein(morpheme, i) == curr_closest):
                # Unmappable, 0 indicates that morpheme is not gibberish, needs human inspect
                return (0, morpheme)
            if(levenshtein(morpheme, i) < threshold and levenshtein(morpheme, i) < curr_closest):
                curr_closest = levenshtein(morpheme, i)
                curr_best = i

        # Mapped to nothing because every LD higher than threshold, -1 indicates gibberish
        if (curr_closest == 1000000):
            return (-1, morpheme)

        # Mapped successfully, return 1 indicating success and the mapped morpheme
        return (1, curr_best)
    elif (type == 2):
        for i in endings:
            if (levenshtein(morpheme, i) == curr_closest):
                # Unmappable, 0 indicates that morpheme is not gibberish, needs human inspect
                return (0, morpheme)
            if(levenshtein(morpheme, i) < threshold and levenshtein(morpheme, i) < curr_closest):
                curr_closest = levenshtein(morpheme, i)
                curr_best = i

        # Mapped to nothing because every LD higher than threshold, -1 indicates gibberish
        if (curr_closest == 1000000):
            return (-1, morpheme)

        # Mapped successfully, return 1 indicating success and the mapped morpheme
        return (1, curr_best)

def determiner_assignment(det_names):

    # Maps determiners to their data
    det_map = {}
    for det in det_names:
        reg_data = re.findall("det[0-9]+_(?:(?:big)|(?:small))[0-9]+[ps]",det)
        if(reg_data):
            # Big or small
            big_or_small = re.findall("(?:(?:big)|(?:small))",reg_data[0])[0]

            # Determines plurality given input string
            number_and_plurality = re.findall("[0-9]+[ps]+",reg_data[0])[0]
            p_or_s = re.findall("[ps]+",number_and_plurality)[0]

            # Determines number for determiner
            det_number = int(re.findall("[0-9]+",reg_data[0])[0])

            # Maps determiner to its assignment in tuple form i.e. 1: ("big", "p")
            det_map[det_number] = (big_or_small, p_or_s)
        else:
            # Invalid input, bad news
            return -1
    global det_to_size_and_plurality
    det_to_size_and_plurality = det_map

def obtain_det_names(filename):
    det_names = []
    with open(filename) as csvfile:
        row_reader = csv.reader(csvfile, delimiter=',')
        for row in row_reader:
            # Obtains training/testing designation for big and small neighborhoods
            reg_data = re.findall("det[0-9]+_(?:(?:big)|(?:small))[0-9]+[ps]",row[2])
            big_or_small = re.findall("(?:(?:big)|(?:small))",reg_data[0])[0]
            monster_num = int(re.findall("[0-9]+",reg_data[0])[1])
            if (big_or_small == 'big'):
                big_stems_number_to_training_or_testing[monster_num] = row[5]
            else:
                small_stems_number_to_training_or_testing[monster_num] = row[5]
            det_names.append(row[2])
            det_names.append(row[3])
    return det_names

# Determines the mapping from names of determiners, stems and endings to their numbers, plurality, and
# neighborhood and updates global maps
# IMPORTANT NOTE: filename must be a csv file, if not, change to csv in excel first then call this
def det_stem_ending_mapping(filename):
    with open(filename) as csvfile:
        row_reader = csv.reader(csvfile, delimiter=',')
        for row in row_reader:
            # map determiners
            if("det" in row[0]):
                test_number = re.compile('det[1-4]')
                if(test_number.match(row[0]) != None):
                    find_number = re.compile("\d")
                    determiner_name_to_number[row[1]] = int(find_number.findall(row[0])[0])
                    determiners.append(row[1])
            # map big neighborhood
            if("big" in row[2]):
                test_number = re.compile('big\d')
                if(test_number.match(row[2]) != None):
                    big_stems.append(row[3])
                    stems.append(row[3])
            # map small neighborhood
            if("small" in row[4]):
                test_number = re.compile('small\d')
                if(test_number.match(row[4]) != None):
                    small_stems.append(row[5])
                    stems.append(row[5])
            # map ending plurality
            if("big" in row[7]):
                ending_mapping[row[8]] = ('big', 's')
                ending_mapping[row[9]] = ('big', 'p')
                endings.append(row[8])
                endings.append(row[9])
            if("small" in row[7]):
                ending_mapping[row[8]] = ('small', 's')
                ending_mapping[row[9]] = ('small', 'p')
                endings.append(row[8])
                endings.append(row[9])

# This function will return a 2x2 list of strings that is structured as the table appears in page 4 of the
# "instructions and codebooks" document. The table will only have the first 2 rows filled out, as
# This is enough to extrapolate sufficient information to obtain the next 3 rows, which will be performed
# in a different function
def get_initial_table(filename):
    table = []
    with open(filename) as csvfile:
        row_reader = csv.reader(csvfile, delimiter=',', quotechar='|')
        for row in row_reader:
            if('typed_response' in row[4]):
                pass
            else:
                typed_response = row[4]
                target_response = row[5]
                # add row to table, add contents as first 2 entries to list
                table.append([])
                table[len(table)-1].append(typed_response)
                table[len(table)-1].append(target_response)
    return(table)

def fill_out_table(initial_table):
    for row in initial_table:
        typed_response = split_by_regex(row[0])
        # NOTE: this part of the code relies on the expected response to be properly split by regex,
        # in testing, this was always the case
        expected_response = split_by_regex(row[1])

        if(typed_response[0] == None):
            row.append("couldn't")
            row.append("parse")
            row.append("syllables")
        else:

        # Determiner analysis
            correct_det = morpheme_levenshtein_comparison(expected_response[0], 1, 0)
            det_analysis = morpheme_levenshtein_comparison(typed_response[0], 1, 0)
            if(correct_det[0] == -1):
                print("PROBLEM: correct value did not map, check levenshtein threshold")
                exit(0)
                pass
            if(det_analysis[0] == -1):
                row.append("unmappable no match: "+det_analysis[1])
            elif(det_analysis[0] == 0):
                row.append("unmappable multiple matches: "+det_analysis[1])
            elif(det_analysis[1] == correct_det[1]):
                row.append("correct")
            else:
                row.append(det_to_size_and_plurality[determiner_name_to_number[det_analysis[1]]])
        # Stem analysis
            correct_stem = morpheme_levenshtein_comparison(expected_response[1], 1, 1)
            stem_analysis = morpheme_levenshtein_comparison(typed_response[1], 1, 1)

            # IMPORTANT NOTE: I had to change "burr" in language_pilot5 to "bur", it does not like that
            # the unconnected ending has 2 rs, and also that isn't really necessary
            if(correct_stem[0] == -1):
                print("PROBLEM: correct value did not map, check levenshtein threshold")
                exit(0)
                pass
            if(stem_analysis[0] == -1):
                row.append("unmappable no match: "+stem_analysis[1])
            elif(stem_analysis[0] == 0):
                row.append("unmappable multiple matches: "+stem_analysis[1])
            elif(stem_analysis[1] == correct_stem[1]):
                row.append("correct")
            else:
                to_comp = stem_analysis[1]
                if(to_comp in big_stems):
                    monster_number = big_stems.index(to_comp)
                    row.append("big "+big_stems_number_to_training_or_testing[monster_number])
                else:
                    monster_number = small_stems.index(to_comp)
                    row.append("small "+small_stems_number_to_training_or_testing[monster_number])
        # Ending analysis
            correct_ending = morpheme_levenshtein_comparison(expected_response[2], 1, 2)
            ending_analysis = morpheme_levenshtein_comparison(typed_response[2], 1, 2)
            if(correct_ending[0] == -1):
                print("PROBLEM: correct value did not map, check levenshtein threshold")
                exit(0)
                pass
            if(ending_analysis[0] == -1):
                row.append("unmappable no match: "+ending_analysis[1])
            elif(ending_analysis[0] == 0):
                row.append("unmappable multiple matches: "+ending_analysis[1])
            elif(ending_analysis[1] == correct_ending[1]):
                row.append("correct")
            else:
                row.append(ending_mapping[ending_analysis[1]])
    return initial_table

def retrieve_results(morpheme_map_file, setup_file, data_file, output_file):
    det_stem_ending_mapping(morpheme_map_file)
    det_names = obtain_det_names(setup_file)
    determiner_assignment(det_names)
    data_table = get_initial_table(data_file)
    table = fill_out_table(data_table)
    with open(output_file, 'w') as f:
        details = ["typed_response", "target_response", "typed_det", "typed_stem", "typed_ending"]
        write = csv.writer(f)
        write.writerow(details)
        write.writerows(table)

#det_stem_ending_mapping("language_pilot5.csv")
#det_names = obtain_det_names("s_71993_setup.csv")
#determiner_assignment(det_names)
#data_table = get_initial_table("production_test_data_pilot5.csv")
#fill_out_table(data_table)
morpheme_map_file = "language_pilot5.csv"
setup_file = "s_71993_setup.csv"
data_file = "production_test_data_pilot5.csv"
output_file = "output.csv"
retrieve_results(morpheme_map_file, setup_file, data_file, output_file)


#!/usr/bin/python3
import sys
import os
import sqlite3
import pyqtgraph
import pg_time_axis
from PyQt5 import QtWidgets, QtCore, QtGui

# Import UI.py
# Convert .ui to .py in Terminal
# python -m PyQt5.uic.pyuic -x [FILENAME].ui -o [FILENAME].py
from QtPyQtGraph import Ui_MainWindow

class Ui(QtWidgets.QMainWindow):
    def __init__(self, parent=None, **kwargs):
        super(Ui, self).__init__(parent, **kwargs)
        self.ui = Ui_MainWindow()
        self.ui.setupUi(self)

        # Connect button actions
        self.ui.pbExit.clicked.connect(self.exit)
        self.ui.pbReboot.clicked.connect(self.reboot)
        self.ui.pbShutdown.clicked.connect(self.shutdown)

        # Configure Plot
        # Initialise data arrays
        self.data_tst = []
        self.data_temp_1 = []
        self.data_av_temp_1 = []

        # Add Legend
        self.ui.plotWindow.addLegend()

        # Set Update Timer
        self.timer = QtCore.QTimer(self)
        self.timer.timeout.connect(self.updater)
        self.timer.start(5000)

        self.showFullScreen()
        self.show()

    def updater(self):
        # Initialise sqlite
        con = sqlite3.connect('data.db')
        cur = con.cursor()

        # Create table
        cur.execute('''CREATE TABLE IF NOT EXISTS temperature
                    (timestamp real, temperature1 real, temperature2 real,
                    temperature3 real, temperature4 real, temperature5 real)''')

        # Select all data ordered and append lists
        cur.execute("SELECT * FROM temperature ORDER BY timestamp DESC LIMIT 1440")
        self.data = cur.fetchall()
        for row in self.data:
            self.data_tst.append(row[0])
            self.data_temp_1.append(row[1])

        # Average value
        self.average_1 = sum(self.data_temp_1) / float(len(self.data_temp_1))
        for i in self.data_temp_1:
            self.data_av_temp_1.append(self.average_1)

        # Add the Date-time axis
        axis = pg_time_axis.DateAxisItem(orientation='bottom')
        #axis.attachToPlotItem(self.ui.plotWindow.getPlotItem())

        # Plot data
        self.ui.plotWindow.plot(x=self.data_tst, y=self.data_temp_1, pen="r", name="Top °C")
        self.ui.plotWindow.plot(x=self.data_tst, y=self.data_av_temp_1, pen="r")

        # Save (commit) the changes
        con.commit()

        # Close connection
        con.close()

    def exit(self):
        sys.exit()

    def reboot(self):
        os.system("sudo shutdown -r now")

    def shutdown(self):
        os.system("sudo shutdown -P now")

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = Ui()
    sys.exit(app.exec_())


print('Crie um programa que leia o ano de nascimento de sete pessoas. No final, mostre quantas pessoas ainda não atingiram a maioridade e quantas já são maiores.')

from datetime import date
atual = date.today().year
maiores = 0
menores = 0
for c in range(1, 8):
    ano = int(input(f'Ano de nascimento da {c}ª pesssoa: '))
    idade = atual - ano
    if idade >= 18:
        maiores += 1
    elif idade < 18:
        menores += 1
print(f'A quantidade de pessoas que são maiores de idade é {maiores}!')
print(f'A quantidade de pessoas que são menores de idade é {menores}!')


"""
# Definition for a Node.
class Node:
    def __init__(self, val: int = 0, left: 'Node' = None, right: 'Node' = None, next: 'Node' = None):
        self.val = val
        self.left = left
        self.right = right
        self.next = next
"""
from collections import deque

class Solution:
    def connect(self, root: 'Node') -> 'Node':
        '''
        method 1 DFS
        :param root:
        :return:
        '''
        res = []
        def dfs(root, level):
            if len(res) < level:
                res.append([])
            if root:
                res[level-1].append(root)
                dfs(root.left, level+1)
                dfs(root.right, level+1)
        cur = root
        dfs(root, 1)
        for level in res:
            for i in range(1, len(level)):
                level[i-1].next = level[i]
        return cur

        '''
        method 2 BFS
        :param root:
        :return:
        '''
        q = deque([root])
        cur = root
        while q:
            size = len(q)
            level = []
            for _ in range(size):
                root = q.popleft()
                if root:
                    if root.left:
                        q.append(root.left)
                    if root.right:
                        q.append(root.right)
                    if level:
                        last = level.pop()
                        last.next = root
                    level.append(root)
        return cur