from collections import deque
import math
import multiprocessing
import resource
import sys

def reverseInt(n):
    return int(bin(n)[:1:-1], 2)

# brute force
def algo1(n, output=None):
    k = 1
    while True:
        kn = bin(k * n)[2:]
        if kn == kn[::-1]:
            kn = int(kn, base=2)
            if output:
                output.put((n, kn//n, kn, "algo1"))
            return (n, kn//n, kn, "algo1")
        k += 2

# Enumerate all palindromes k and check for the first one that is divisible by n.
def algo2(n, output=None):
    # Find kn = yay^R.
    lenkn = n.bit_length()
    odd = lenkn % 2
    a = 0
    y = 2 ** ((lenkn // 2) - 1)
    while True:
        kn = (y * (2 ** ((lenkn // 2) + odd))) + a + reverseInt(y)
        if kn % n == 0:
            if output:
                output.put((n, kn//n, kn, "algo2"))
            return (n, kn//n, kn, "algo2")
        if odd == 1 and a == 0:
            a = 2**(lenkn // 2)
        else:
            a = 0
            y += 1
            if y == 2 ** (lenkn // 2):
                if not odd:
                    y //= 2
                lenkn += 1
                odd = 1 - odd

# DP Subset Sum
# Potentially unstable for even n (might return something when nothing should be valid)
def algo3(n, output=None):
    lenkn = n.bit_length()
    odd = lenkn % 2
    while True:
        # Setup the problem set
        subsetSumSet = dict()
        for i in range(lenkn // 2):
            subsetSumSet[(lenkn - 1) - i] = ((2 ** i) + (2 ** ((lenkn - 1) - i))) % n
        if odd:
            subsetSumSet[lenkn // 2] = (2 ** (lenkn // 2)) % n
        # Setup the memoization and initial state
        precomputed = []
        subsetSumSolutions = dict()
        precomputed.append(2 ** (lenkn - 1))
        subsetSumSolutions[2 ** (lenkn - 1)] = subsetSumSet[lenkn - 1]
        if subsetSumSolutions[2 ** (lenkn - 1)] == 0:
            kn = (2 ** (lenkn - 1)) + reverseInt(2 ** (lenkn - 1))
            if output:
                output.put((n, kn//n, kn, "algo3"))
            return (n, kn//n, kn, "algo3")
        if odd:
            precomputed.append((2 ** (lenkn - 1)) + (2 ** (lenkn // 2)))
            subsetSumSolutions[(2 ** (lenkn - 1)) + (2 ** (lenkn // 2))] = (subsetSumSet[lenkn - 1] + subsetSumSet[lenkn // 2]) % n
            if subsetSumSolutions[(2 ** (lenkn - 1)) + (2 ** (lenkn // 2))] == 0:
                kn = ((2 ** (lenkn - 1)) + (2 ** (lenkn // 2))) + reverseInt((2 ** (lenkn - 1)) + (2 ** (lenkn // 2)))
                kn -= 2 ** (lenkn // 2)
                if output:
                    output.put((n, kn//n, kn, "algo3"))
                return (n, kn//n, kn, "algo3")
        # Run the problem
        for i in range(lenkn // 2 + odd, lenkn - 1):
            nextPrecomputed = []
            for j in precomputed:
                result = (subsetSumSolutions[j] + subsetSumSet[i]) % n
                resultIndex = j + (2 ** i)
                if result == 0:
                    kn = resultIndex + reverseInt(resultIndex)
                    if odd and ((2 ** (lenkn // 2)) & resultIndex):
                        kn -= 2 ** (lenkn // 2)
                    if output:
                        output.put((n, kn//n, kn, "algo3"))
                    return (n, kn//n, kn, "algo3")
                subsetSumSolutions[resultIndex] = result
                nextPrecomputed.append(resultIndex)
            precomputed = precomputed + nextPrecomputed
        # No solution found
        lenkn += 1
        odd = 1 - odd

# Do BFS over implicitly built DFA.
def algo4(n, output=None):
    # Setup tracking
    global pathTo
    pathTo = dict()
    bfsQueue = deque()
    # Setup initial states
    pathTo[(0, 2 % n, 0)] = ("", None, 0)
    bfsQueue.append((0, 2 % n, 0))
    pathTo[(0, 4 % n, 0)] = ("0", None, 1)
    bfsQueue.append((0, 4 % n, 0))
    pathTo[(1, 4 % n, 1)] = ("1", None, 1)
    bfsQueue.append((1, 4 % n, 1))
    # Loop
    while True:
        currentState = bfsQueue.popleft()
        nextStateZero = ((2 * currentState[0]) % n, (4 * currentState[1]) % n, currentState[2])
        if nextStateZero not in pathTo:
            pathTo[nextStateZero] = ("0", currentState, pathTo[currentState][2] + 2)
            bfsQueue.append(nextStateZero)
        elif (pathTo[nextStateZero][2] == pathTo[currentState][2] + 2) and (pathTo[nextStateZero][0] == "1"):
            pathTo[nextStateZero] = ("0", currentState, pathTo[currentState][2] + 2)
        nextStateOne = (((2 * currentState[0]) + 1 + currentState[1]) % n, (4 * currentState[1]) % n, 1)
        if nextStateOne not in pathTo:
            pathTo[nextStateOne] = ("1", currentState, pathTo[currentState][2] + 2)
            bfsQueue.append(nextStateOne)
        # Check for answer
        if nextStateOne[2] == 1 and nextStateOne[0] == 0:
            x = ""
            currentState = nextStateOne
            while True:
                path = pathTo[currentState]
                if path[1] == None:
                    x = x + path[0] + x[::-1]
                    kn = int(x, base=2)
                    if output:
                        output.put((n, kn//n, kn, "algo4"))
                    return (n, kn//n, kn, "algo4")
                else:
                    x += path[0]
                    currentState = path[1]

def multiAlgoSearch(n):
    output = multiprocessing.Queue()
    algo1process = multiprocessing.Process(target=algo1, args=(n, output))
    algo1process.start()
    algo2process = multiprocessing.Process(target=algo2, args=(n, output))
    algo2process.start()
    algo3process = multiprocessing.Process(target=algo3, args=(n, output))
    algo3process.start()
    algo4process = multiprocessing.Process(target=algo4, args=(n, output))
    algo4process.start()
    result = output.get()
    algo1process.terminate()
    algo2process.terminate()
    algo3process.terminate()
    algo4process.terminate()
    return result

def memory_limit(systemMemFrac):
    soft, hard = resource.getrlimit(resource.RLIMIT_AS)
    resource.setrlimit(resource.RLIMIT_AS, (get_memory() * 1024 * (systemMemFrac), hard))

def get_memory():
    with open('/proc/meminfo', 'r') as mem:
        free_memory = 0
        for i in mem:
            sline = i.split()
            if str(sline[0]) in ('MemFree:', 'Buffers:', 'Cached:'):
                free_memory += int(sline[1])
    return free_memory

if __name__ == "__main__":
    if True:
        memory_limit(0.4)
        n = 1
        with open("PalindromicMultiplesOutput.txt", "a") as outputFile:
            while n < 10000000:
                outputFile.write(" ".join([str(s) for s in multiAlgoSearch(n)]) + "\n")
                outputFile.flush()
                n += 2
    if False:
        n = 3054503
        #print(algo1(n))
        #print(algo2(n))
        #print(algo3(n))
        #print(algo4(n))
        print(multiAlgoSearch(n))


#!/usr/bin/python

"""

Author: Jun Zhu

"""
import pyOpt

from .linacopt_objective import Objective
from .linacopt_constraint import Constraint
from .linacopt_variable import Variable
from .linacopt_covariable import CoVariable
from .linacopt_staticvariable import StaticVariable

class Optimization(pyOpt.Optimization):
    """Inherited from pyOpt.Optimization class"""
    def __init__(self, name, obj_fun, *args, **kwargs):
        """Initialize Optimization object"""
        self._covariables = {}
        self._staticvariables = {}

        super().__init__(name, obj_fun, *args, **kwargs)

    def set_obj(self, *args, **kwargs):
        """Add a objective into the objective set"""
        i = self.firstavailableindex(self._objectives)

        if (len(args) > 0) and isinstance(args[0], Objective):
            self._objectives[i] = args[0]
        else:
            try:
                self._objectives[i] = Objective(*args, **kwargs)
            except:
                raise ValueError("Input is not a valid for a Objective "
                                 "Object instance\n")

        self._remove_duplicity(self._objectives, i)

    def del_obj(self, ix):
        """Delete the objective *ix* from the objective set

        :param ix: int/string
            Index (if int) or name (if string)
        """
        index = -1
        if isinstance(ix, int):
            if ix < 0:
                raise ValueError("Index must be an integer >= 0.\n")
            elif ix not in self._objectives:
                raise ValueError("Index is not found.\n")
            else:
                index = ix
        elif isinstance(ix, str):
            index = self._find_index_by_name(ix, self._objectives)
            if index < 0:
                raise ValueError("Objective '{}' is not found.\n".format(ix))

        del self._objectives[index]

        self.shift_index(self._objectives, index)

    def get_objset(self):
        """Get the objective set"""
        return self._objectives

    def set_con(self, *args, **kwargs):
        """Add a constraint into the constraint set"""
        i = self.firstavailableindex(self._constraints)

        if (len(args) > 0) and isinstance(args[0], Constraint):
            self._constraints[i] = args[0]
        else:
            try:
                self._constraints[i] = Constraint(*args, **kwargs)
            except:
                raise ValueError("Input is not a valid for a Constraint "
                                 "Object instance\n")

        self._remove_duplicity(self._constraints, i)

    def del_con(self, ix):
        """Delete the constraint *ix* from the constraint set

        :param ix: int/string
            Index (if int) or name (if string)
        """
        index = -1
        if isinstance(ix, int):
            if ix < 0:
                raise ValueError("Index must be an integer >= 0.\n")
            elif ix not in self._constraints:
                raise ValueError("Index is not found.\n")
            else:
                index = ix
        elif isinstance(ix, str):
            index = self._find_index_by_name(ix, self._constraints)
            if index < 0:
                raise ValueError("Constraint '{}' is not found.\n".format(ix))

        del self._constraints[index]

        self.shift_index(self._constraints, index)

    def get_conset(self):
        """Get the constraint set"""
        return self._constraints

    def set_var(self, *args, **kwargs):
        """Add a variable into the variable set"""
        i = self.firstavailableindex(self._variables)

        if (len(args) > 0) and isinstance(args[0], Variable):
            self._variables[i] = args[0]
        else:
            try:
                self._variables[i] = Variable(*args, **kwargs)
            except:
                raise ValueError(
                    "Input is not valid for a Variable Object instance.\n")

        # Try to inherit the value from the co-variable set.
        ii = self._find_index_by_name(self._variables[i].name,
                                      self._covariables)
        if ii >= 0:
            self._variables[i].value = self._covariables[ii].value
            self.del_covar(ii)
            print("\n{} was changed from co-variable to variable!".
                  format(self._variables[i].name))

        # Try to inherit the value from the static variable set.
        ii = self._find_index_by_name(self._variables[i].name,
                                      self._staticvariables)
        if ii >= 0:
            self._variables[i].value = self._staticvariables[ii].value
            self.del_staticvar(ii)
            print("\n{} was changed from static variable to variable!".
                  format(self._variables[i].name))

        self._remove_duplicity(self._variables, i)

    def del_var(self, ix):
        """Delete the variable *ix* from the variable set

        :param ix: int/string
            Index (if int) or name (if string)
        """
        index = -1
        if isinstance(ix, int):
            if ix < 0:
                raise ValueError("Index must be an integer >= 0.\n")
            elif ix not in self._variables:
                raise ValueError("Index is not found.\n")
            else:
                index = ix
        elif isinstance(ix, str):
            index = self._find_index_by_name(ix, self._variables)
            if index < 0:
                raise ValueError("Variable '{}' is not found.\n".format(ix))

        del self._variables[index]

        self.shift_index(self._variables, index)

    def get_varset(self):
        """Get the variable set"""
        return self._variables

    def set_covar(self, *args, **kwargs):
        """Add a co-variable into the co-variable set"""
        i = self.firstavailableindex(self._covariables)

        if (len(args) > 0) and isinstance(args[0], CoVariable):
            self._covariables[i] = args[0]
        else:
            try:
                self._covariables[i] = CoVariable(*args, **kwargs)
            except:
                raise ValueError(
                    "Input is not valid for a CoVariable object instance.\n")

        # Variable and static-variable cannot be changed to co-variable since
        # they will lose their values in the last optimization.
        try:
            self.del_var(self._covariables[i].name)
            print("\n{} was changed from variable to co-variable!".
                  format(self._covariables[i].name))
            print("Warning: changing variable to co-variable may lose "\
                  "the optimized result!\n")
        except ValueError:
            pass

        try:
            self.del_staticvar(self._covariables[i].name)
            print("\n{} was changed from static variable to co-variable!".
                  format(self._covariables[i].name))
            print("Warning: changing static variable to co-variable may lose "\
                  "the optimized result!\n")
        except ValueError:
            pass

        self._remove_duplicity(self._covariables, i)

    def del_covar(self, ix):
        """Delete the co-variable *ix* from the co-variable set

        :param ix: int/string
            Index (if int) or name (if string)
        """
        index = -1
        if isinstance(ix, int):
            if ix < 0:
                raise ValueError("Index must be an integer >= 0.\n")
            elif ix not in self._covariables:
                raise ValueError("Index is not found.\n")
            else:
                index = ix
        elif isinstance(ix, str):
            index = self._find_index_by_name(ix, self._covariables)
            if index < 0:
                raise ValueError("Co-variable '{}' is not found.\n".format(ix))

        del self._covariables[index]

        self.shift_index(self._covariables, index)

    def get_covarset(self):
        """Get co-variable set"""
        return self._covariables

    def set_staticvar(self, *args, **kwargs):
        """Add a static variable into the static variable set"""
        i = self.firstavailableindex(self._staticvariables)

        if (len(args) > 0) and isinstance(args[0], StaticVariable):
            self._staticvariables[i] = args[0]
        else:
            try:
                self._staticvariables[i] = StaticVariable(*args, **kwargs)
            except:
                raise ValueError(
                    "Input is not valid for a StaticVariable object instance\n")

        # Try to inherit the value from the variable set.
        ii = self._find_index_by_name(self._staticvariables[i].name,
                                      self._variables)
        if ii >= 0:
            self._staticvariables[i].value = self._variables[ii].value
            self.del_var(ii)
            print("\n{} was changed from variable to static variable!\n".
                  format(self._staticvariables[i].name))

        # Try to inherit the value from the co-variable set.
        ii = self._find_index_by_name(self._staticvariables[i].name,
                                      self._covariables)
        if ii >= 0:
            self._staticvariables[i].value = self._covariables[ii].value
            self.del_covar(ii)
            print("\n{} was changed from co-variable to static variable!\n".
                  format(self._staticvariables[i].name))

        if self._staticvariables[i].value is None:
            raise ValueError("Unknown value of static variable: {}".
                             format(self._staticvariables[i].name))

        self._remove_duplicity(self._staticvariables, i)

    def del_staticvar(self, ix):
        """Delete static variable *ix* from the static variable set

        :param ix: int/string
            Index (if int) or name (if string)
        """
        index = -1
        if isinstance(ix, int):
            if ix < 0:
                raise ValueError("Index must be an integer >= 0.\n")
            elif ix not in self._staticvariables:
                raise ValueError("Index is not found.\n")
            else:
                index = ix
        elif isinstance(ix, str):
            index = self._find_index_by_name(ix, self._staticvariables)
            if index < 0:
                raise ValueError("Static-variable '{}' is not found.\n".
                                 format(ix))

        del self._staticvariables[index]

        self.shift_index(self._staticvariables, index)

    def get_staticvarset(self):
        """Get the static variable set"""
        return self._staticvariables

    @staticmethod
    def shift_index(dict_, index):
        """"""
        while len(dict_) > index:
            dict_[index] = dict_[index+1]
            del dict_[index+1]
            index += 1

    @staticmethod
    def _remove_duplicity(dict_, ii):
        """Remove duplicity

        If the name of the ii-th item is equal to the name of another
        item with index i, the i-th item is set equal to the ii-th item
        and the latter is then removed.
        """
        for i, item in dict_.items():
            if i != ii and item.name == dict_[ii].name:
                dict_[i] = dict_[ii]
                del dict_[ii]
                return

    @staticmethod
    def _find_index_by_name(name, dict_):
        """Return the index of object with object.name == name"""
        index = -1
        for key, ele in dict_.items():
            if name == ele.name:
                index = key

        return index

    def __str__(self):
        """Print Structured Optimization Problem

        Overwrite the original method to include the summary of
        co-variables, static variables as well as functions in
        constraints and objectives.
        """
        text = "\nOptimization Problem -- %s\n%s\n" % (self.name, '='*80)

        text += "\nObjectives:\n"\
                "  {:18}  {:11}  {:11}  {:16}\n"\
                .format('Name', 'Value', 'Optimum', 'Function')

        for index in list(self._objectives.keys()):
            lines = str(self._objectives[index]).split('\n')
            text += lines[1] + '\n'

        if len(list(self._constraints.keys())) > 0:
            text += "\nConstraints:\n"\
                    "  {:18}  {:11}  {:48}\n"\
                    .format('Name', 'Value', 'Bound')
            for index in list(self._constraints.keys()):
                lines = str(self._constraints[index]).split('\n')
                text += lines[1] + '\n'

        text += "\nVariables (c - continuous, i - integer, d - discrete):\n"\
                "  {:18}  {:6}  {:11}  {:11}  {:11}\n"\
                .format('Name', 'Type', 'Value', 'Lower Bound', 'Upper Bound')
        for index in list(self._variables.keys()):
            lines = str(self._variables[index]).split('\n')
            text += lines[1] + '\n'

        if len(list(self._covariables.keys())) > 0:
            text += "\nCo-variables:\n"\
                    "  {:18}  {:16}  {:11}  {:11}\n"\
                    .format('Name', 'Dependent(s)', 'Slope(s)', 'Intercept')
            for index in list(self._covariables.keys()):
                lines = str(self._covariables[index]).split('\n')
                text += lines[1] + '\n'

        if len(list(self._staticvariables.keys())) > 0:
            text += "\nStatic variables:\n"\
                    "  {:18}  {:11}\n".format('Name', 'Value')
            for index in list(self._staticvariables.keys()):
                lines = str(self._staticvariables[index]).split('\n')
                text += lines[1] + '\n'

        return text


import json, requests
from django.http import HttpResponse
# from fb_app.utils import FacebookPageManager
from django.shortcuts import render, redirect
from django.contrib.auth.decorators import login_required

class FacebookPageManager(object):
    """ Manages the Facebook Page"""
    API_ENDPOINT = 'https://graph.facebook.com/v2.10'

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

    def get_page_info(self):
        # returns the page information (first page) fb page owned by the user
        payload = {'access_token': self.access_token}
        url = '{}/me/accounts'.format(self.API_ENDPOINT)
        resp = requests.get(url, params = payload)
        data = json.loads(resp.text)
        if data.get('data'):
            page_info = data.get('data')[0]
            payload = {'access_token': self.access_token, 'fields' : 'single_line_address,phone,is_published,overall_star_rating,emails,about,location'}
            url = '{}/{}/'.format(self.API_ENDPOINT, page_info.get('id'))
            resp = requests.get(url, params = payload)
            data = json.loads(resp.text)
            data.update({'emails' :','.join(data.get('emails', []))})
            page_info.update(data)
            located_in_us = True if data.get('location',{}).get('country','').strip().lower() in ['united states', 'usa', 'us', 'united states of america'] else False
            page_info.update({'listed' : True, 'located_in_us' : located_in_us})
        else:
            page_info = {'listed' : False}
        return page_info

    def update_page_info(self, data):
        # updates facebook page information
        # location =  '{'+'"city": "{city}", "street": "{street}", "state": "{state}", "country": "{country}", "zip": "{zip}"'.format(city=data.get('city'), street=data.get('street'),  state=data.get('state'), country=data.get('country'), zip=data.get('zip')) + '}'
        # payload = {'access_token': data.get('access_token'), 'about' : data.get('about',''),'phone' : data.get('phone',''), 'emails' : '["{}",]'.format(data.get('emails')), 'location' : location}
        payload = {'access_token': data.get('access_token'), 'about' : data.get('about',''),'phone' : data.get('phone',''), 'emails' : '["{}",]'.format(data.get('emails'))}
        url = '{}/{}/'.format(self.API_ENDPOINT, data.get('id'))
        resp = requests.post(url, params = payload)
        return json.loads(resp.text)

@login_required
def home(request):
    # Home page of the application
    social_user = request.user.social_auth.filter(provider='facebook', ).first()
    access_token = social_user.extra_data['access_token']
    fb_page_manager = FacebookPageManager(access_token)
    page_info = fb_page_manager.get_page_info()
    return render(request, 'fb/home.html', context=page_info)

@login_required
def update_page_info(request):
    # Updates the page information
    if request.method == 'POST':
        social_user = request.user.social_auth.filter(provider='facebook', ).first()
        access_token = social_user.extra_data['access_token']
        fb_page_manager = FacebookPageManager(access_token)
        data = fb_page_manager.update_page_info(request.POST)
        error_user_msg = data.get('error').get('error_user_msg') if data.get('error') else ''
        return HttpResponse(json.dumps({'success' : 1 if data.get('success') else 2, 'eum' : error_user_msg}), content_type="application/json")


# -*- coding: utf-8 -*-
"""
Created on Mon Oct 16 10:30:25 2017

@author: MannyXu
"""

def Mod37(i):
    if i % 37:
        return False
    else:
        return True

def nums(i):
    sevs = i % 10
    tens = (i // 10) % 10
    huds = i // 100
    num1 = tens * 100 + sevs * 10 + huds
    num2 = sevs * 100 + huds * 10 +tens
    return num1, num2

for i in range(100,1001):
    if Mod37(i):
        num1, num2 = nums(i)
        if Mod37(num1) and Mod37(num2):
            continue
        else:
            print("Bad Job")
            break
    elif i <1000:
        continue
    print("Good Job")


#coding:utf-8

import numpy as np

from kmeans import kmeans, avg_iou, load_dataset,visualize_data

ANNOTATIONS_PATH = "D:/DATASETS/VOC2012/VOCtrainval/VOC2007/Annotations"
CLUSTERS = 5

data = load_dataset(ANNOTATIONS_PATH)

visualize_data(data)

out = kmeans(data, k=CLUSTERS)
print("Accuracy: {:.2f}%".format(avg_iou(data, out) * 100))
print("Boxes:\n {}".format(out))

ratios = np.around(out[:, 0] / out[:, 1], decimals=2).tolist()
print("Ratios:\n {}".format(sorted(ratios)))


from cars.api.v1.brands.views import *
from django.urls import path

app_name = 'brands'
urlpatterns = [
    path('create/', CarBrandCreateView.as_view()),
    path('all/', CarBrandListView.as_view()),
    path('detail/<uuid:pk>', CarBrandDetailView.as_view()),
    path('sum/', CarBrandSumView.as_view()),
]


import random

def measured_variables(SC): #find total # measured variables
	number = -1
	for key, vallist in SC.items():
		for value in vallist:
			if value > number:
				number = value
	return number

def sbk(SC, total): #calc s, b, k probabilities
	sd = dict()
	bd = dict()
	kd = dict()
	for key,val in SC.items():
		sd[key] = len(val)/total
		bd[key] = 0.5*sd[key]
		kd[key] = len(val)
	return sd, bd, kd

def calc_Z(a, b, k, totalmv, sources): #estimation maximization alg
	d = random.uniform(0,1)
	Z = dict() #Z is set of latent variables
	count = 0
	while count < 20:
		count += 1
		j = 1
		while j <= totalmv:
			i = 1
			a_j = 1
			b_j = 1
			while i < sources:
				if j in SC[i]: mval = 1
				else: mval = 0
				a_j *= pow(a[i], mval)*pow((1-a[i]), (1-mval))
				b_j *= pow(b[i], mval)*pow((1-b[i]), (1-mval))
				i += 1
			Z[j] = (a_j*d) / float(a_j*d + b_j*(1-d))
			j+=1
		totalZ = sum(Z.values())
		i = 0
		while i < sources:
			i += 1
			z = 0
			for claim_id in SC[i]:
				z += Z[claim_id]
			try:
				a[i] = z / float(totalZ)
			except ZeroDivisionError:
				a[i] = 0
			try:
				b[i] = (k[i] - z) / float(totalmv - totalZ)
			except ZeroDivisionError:
				b[i] = 0
			d = totalZ/totalmv
	return Z

SC = dict()
infile = 'SCMatrix_Submit'
with open(infile, 'r') as f:
	for line in f:
		sourceid, claimid = map(int, line.strip().split(','))
		if sourceid not in SC:
			SC[sourceid] = []
		SC[sourceid].append(claimid)

totalmv = measured_variables(SC)
si, bi, ki = sbk(SC, totalmv)
#bi -> half si
#ki -> key = sourceid, value = num claimids they've posted
ai = si #si -> key = sourceid, value = si value

Z = calc_Z(ai, bi, ki, totalmv, len(SC)) #determine Z
outfile = 'task1.txt'
with open(outfile, 'w') as f: #calc results and send them to output file
	for key,val in Z.items():
		if val>=0.5: output = 1
		else: output = 0
		f.write(str(key)+","+str(output)+"\n")


def check_palindrome(str):
    j = len(str) - 1
    i = 0
    while(i<j):
        if(str[i] != str[j]):
            return False
        i +=1
        j -=1
    return True
str = check_palindrome(input("Enter string: "))

if str:
    print("String is palindrome")
else:
    print("String is Not palindrome")


# 抓取指定页面内容并解析出需要的内容（抓取异步数据）
# http://docs.python-requests.org/zh_CN/latest/user/quickstart.html  requests模块官方文档
import requests
import re
import os
from requests.exceptions import RequestException
from urllib import request
from lxml import etree  # lxml模块不是自带的，需要安装
import json
import time

def get_ajax_data(url, handler=''):
    """
    抓取异步加载的数据（图片等）
    :param url: 页面地址
    :param handler: 处理函数
    :return:
    """
    data = requests.get(url)
    # print(data.text)  # 返回的是Unicode型的数据，可能乱码
    # print(data.content)  # 返回的是bytes型的数据
    htmlPage = data.text
    prog = re.compile(r'app\.page\["pins"\].*')
    appPins = prog.findall(htmlPage)
    true = "true"
    null = None
    result = eval(appPins[0][19:-1])
    images = []
    if not os.path.exists('imgs'):
        os.mkdir("imgs")
    for i in result:
        info = {}
        info['id'] = str(i['pin_id'])
        info['url'] = "http://img.hb.aicdn.com/" + i["file"]["key"] + "_fw658"
        info['type'] = i["file"]["type"][6:]
        images.append(info)
    for image in images:
        req = requests.get(image["url"])
        imageName = image["id"] + "." + image["type"]
        with open("imgs/" + imageName, 'wb') as fp:
            fp.write(req.content)

def make_ajax_url(No):
    """ 返回ajax请求的url """
    return "http://huaban.com/favorite/beauty/?i5p998kw&max=" + No + "&limit=20&wfl=1"

get_ajax_data(r'http://huaban.com/favorite/beauty/')


#!/usr/bin/env python3

from ctypes import Structure, c_bool, c_uint8
from scr_core.state import DeviceStateEnum
from autonav_msgs.msg import SafetyLights
from scr_msgs.msg import SystemState
from scr_core.node import Node
import threading
import serial
import rclpy
import json
import os
import time

class SafetyLightsPacket(Structure):
    _fields_ = [
        ("autonomous", c_bool, 1),
        ("eco", c_uint8, 1),
        ("mode", c_uint8, 6),
        ("brightness", c_uint8, 8),
        ("red", c_uint8, 8),
        ("green", c_uint8, 8),
        ("blue", c_uint8, 8)
    ]

class SafetyLightsSerial(Node):
    def __init__(self):
        super().__init__("autonav_serial_safetylights")

    def configure(self):
        self.safetyLightsSubscriber = self.create_subscription(SafetyLights, "/autonav/SafetyLights", self.onSafetyLightsReceived, 20)
        self.pico = None
        self.writeQueue = []
        self.writeQueueLock = threading.Lock()
        self.writeThread = threading.Thread(target=self.picoWriteWorker)
        self.writeThread.daemon = True
        self.writeThread.start()

    def transition(self, old: SystemState, updated: SystemState):
        pass

    def picoWriteWorker(self):
        while rclpy.ok():
            does_exist = os.path.exists("/dev/autonav-mc-safetylights")
            if not does_exist:
                time.sleep(1)
                continue

            self.pico = serial.Serial("/dev/autonav-mc-safetylights", baudrate = 115200)
            self.setDeviceState(DeviceStateEnum.OPERATING)
            while self.pico is not None and self.pico.is_open:
                if not self.pico.is_open or self.pico.in_waiting > 0 or len(self.writeQueue) == 0:
                    continue

                self.writeQueueLock.acquire()
                if len(self.writeQueue) > 0:
                    jsonStr = json.dumps(self.writeQueue.pop(0))
                    try:
                        self.pico.write(bytes(jsonStr, "utf-8"))
                    except:
                        self.log(f"Failed to write to serial port: {jsonStr}")
                self.writeQueueLock.release()

    def onSafetyLightsReceived(self, lights: SafetyLights):
        data = {}
        data["autonomous"] = lights.autonomous
        data["eco"] = lights.eco
        data["mode"] = lights.mode
        data["brightness"] = lights.brightness
        data["red"] = lights.red
        data["green"] = lights.green
        data["blue"] = lights.blue
        self.writeQueueLock.acquire()
        self.writeQueue.append(data)
        self.writeQueueLock.release()

def main():
    rclpy.init()
    rclpy.spin(SafetyLightsSerial())
    rclpy.shutdown()

if __name__ == "__main__":
    main()


def sol(arr):
    n = len(arr)
    dict1={}
    data=[]
    for x in arr:
        if x not in dict1:
            dict1[x]=1
            print("No")
            data.append(x)
        else:
            print("Yes")
            data.append(x)
    return data

arr=[x for x in input().split()]
print(sol(arr))


"""biblioteca URL Configuration

The `urlpatterns` list routes URLs to views. For more information please see:
    https://docs.djangoproject.com/en/3.1/topics/http/urls/
Examples:
Function views
    1. Add an import:  from my_app import views
    2. Add a URL to urlpatterns:  path('', views.home, name='home')
Class-based views
    1. Add an import:  from other_app.views import Home
    2. Add a URL to urlpatterns:  path('', Home.as_view(), name='home')
Including another URLconf
    1. Import the include() function: from django.urls import include, path
    2. Add a URL to urlpatterns:  path('blog/', include('blog.urls'))
"""
from django.contrib import admin
from django.urls import path, include
from peliculas import views
from peliculas.api import ActoresView, ActorView, DirectorView, DirectoresView, PeliculaView, PeliculasView, valoracion
from django.conf.urls import handler404, handler500, handler403, handler400

handler404 = 'peliculas.views.error_404'
handler500 = 'peliculas.views.error_500'
handler403 = 'peliculas.views.error_403'
handler400 = 'peliculas.views.error_400'

urlpatterns = [
    path('peliculas/<int:pk>/', views.PeliculaDetalles.as_view(), name="detallesPelicula"),
    path('peliculas/', views.PeliculaListado.as_view(), name="listadoPeliculas"),
    path('director/', views.DirectorListado.as_view(), name ="listadoDirectores"),
    path('director/<int:pk>/', views.DirectorDetalles.as_view(), name="detallesDirector"),
    path('actores/', views.ActorListado.as_view(), name ="listadoActores"),
    path('actores/<int:pk>/', views.ActorDetalles.as_view(), name="detallesActor"),
    path('admin/', admin.site.urls),
    path('peliculas/nueva',views.PeliculaNueva.as_view() , name='pelicula_nueva'),
    path('peliculas/<int:pk>/editar/', views.PeliculaEditar.as_view(), name='pelicula_editar'),
    path('peliculas/<int:pk>/eliminar/',views.PeliculaEliminar.as_view(), name='pelicula_eliminar'),
    path('peliculas/<int:pk>/valorar/', views.valoracion , name='votacionPelicula'),
    path('director/nuevo',views.DirectorNuevo.as_view() , name='director_nuevo'),
    path('director/<int:pk>/editar/', views.DirectorEditar.as_view(), name='director_editar'),
    path('director/<int:pk>/eliminar/', views.DirectorEliminar.as_view(), name='director_eliminar'),
    path('actores/nuevo',views.ActorNuevo.as_view() , name='actor_nuevo'),
    path('actores/<int:pk>/editar/', views.ActorEditar.as_view(), name='actor_editar'),
    path('actores/<int:pk>/eliminar/', views.ActorEliminar.as_view(), name='actor_eliminar'),
    path('registro/',views.RegistroUsuario.as_view(), name='registro_usuario'),
    path('accounts/', include('django.contrib.auth.urls')),                                      #Add Django site authentication urls (for login, logout, password management)
    #Api Django Rest Framework
    path('api/', include("rest_framework.urls", namespace="rest_framework")),
    path('api/actores/', ActoresView.as_view(), name="apiView_listadoActores"),
    path('api/actores/<int:pk>/', ActorView.as_view(), name="apiView_detallesActor"),
    path('api/directores/', DirectoresView.as_view(), name ="apiView_listadoDirectores"),
    path('api/directores/<int:pk>/', DirectorView.as_view(), name="apiView_detallesDirector"),
    path('api/peliculas/', PeliculasView.as_view(), name="apiView_listadoPeliculas"),
    path('api/peliculas/<int:pk>/', PeliculaView.as_view(), name="apiView_detallesPelicula"),
    path('api/peliculas/<int:pk>/valorar/', valoracion , name='apiView_votacionPelicula'),

]


from django.shortcuts import render
from rest_framework import status

from rest_framework.decorators import api_view
from rest_framework.parsers import JSONParser
from rest_framework.response import Response

from soccer.models import Country, Soccerplayer, Club
from soccer.serializers import CountryOptionSerializer, CLubFormSerializer, SoccerplayerFormSerializer, \
    SoccerplayerListSerializer, ClubListSerializer

# Create your views here.

##Country
#@swagger_auto_schema(method='GET', responses={200: CountryOptionSerializer(many=True)})
@api_view(['GET'])
def country_option_list(request):
    countries = Country.objects.all()
    serializer = CountryOptionSerializer(countries, many=True)
    return Response(serializer.data)

##Club--------------------------------------------------------------------------------------------------------
#@swagger_auto_schema(method='GET', responses={200: ActorsOptionSerializer(many=True)})
@api_view(['GET'])
def club_option_list(request):
    club = Club.objects.all()
    serializer = ClubListSerializer(club, many=True)
    return Response(serializer.data)

@api_view(['POST'])
def club_form_create(request):
    serializer = CLubFormSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=201)
    return Response(serializer.errors, status=400)

@api_view(['PUT'])
def club_form_update(request,pk):
    try:
        club = Club.objects.get(pk=pk)
    except Club.DoesNotExist:
        return Response({'error': 'Club does not exist.'}, status=404)

    data = JSONParser().parse(request)
    serializer = CLubFormSerializer(club, data=data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors, status=400)

@api_view(['GET'])
def club_form_get(request,pk):
    try:
        club = Club.objects.get(pk=pk)
    except Club.DoesNotExist:
        return Response({'error': 'Club does not exist.'}, status=404)
    serializer = CLubFormSerializer(club)
    return Response(serializer.data)

@api_view(['GET','DELETE'])
def club_delete(request,pk):
    try:
        club = Club.objects.get(pk=pk)
    except Club.DoesNotExist:
        return Response({'error': 'club does not exist.'}, status=404)

    if request.method == 'GET':
        serializer = CLubFormSerializer(club)
        return Response(serializer.data)

    elif request.method == 'DELETE':
        club.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)

##soccerplayer----------------------------------------------------------------------------------
@api_view(['GET'])
def soccerplayer_option_list(request):
    soccerplayer = Soccerplayer.objects.all()
    serializer = SoccerplayerListSerializer(soccerplayer, many=True)
    return Response(serializer.data)

#@swagger_auto_schema(method='GET', responses={200: MovieFormSerializer()})
@api_view(['GET'])
def soccerplayer_form_get(request, pk):
    try:
        soccerplayer = Soccerplayer.objects.get(pk=pk)
    except Soccerplayer.DoesNotExist:
        return Response({'error': 'Soccerplayer does not exist.'}, status=404)
    serializer = SoccerplayerFormSerializer(soccerplayer)
    return Response(serializer.data)

#@swagger_auto_schema(method='POST', request_body=MovieFormSerializer, responses={200: MovieFormSerializer()})
@api_view(['POST'])
def soccerplayer_form_create(request):
    serializer = SoccerplayerFormSerializer(data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data, status=201)
    return Response(serializer.errors, status=400)

#@swagger_auto_schema(method='PUT', request_body=MovieFormSerializer, responses={200: MovieFormSerializer()})
@api_view(['PUT'])
def soccerplayer_form_update(request, pk):
    try:
        soccerplayer = Soccerplayer.objects.get(pk=pk)
    except Soccerplayer.DoesNotExist:
        return Response({'error': 'Soccerplayer does not exist.'}, status=404)

    serializer = SoccerplayerFormSerializer(soccerplayer, data=request.data)
    if serializer.is_valid():
        serializer.save()
        return Response(serializer.data)
    return Response(serializer.errors, status=400)

@api_view(['GET','DELETE'])
def soccerplayer_delete(request, pk):
    try:
        soccerplayer = Soccerplayer.objects.get(pk=pk)
    except Soccerplayer.DoesNotExist:
        return Response({'error': 'Soccerplayer does not exist.'}, status=404)

    if request.method == 'GET':
        serializer = SoccerplayerFormSerializer(soccerplayer)
        return Response(serializer.data)

    elif request.method == 'DELETE':
        soccerplayer.delete()
        return Response(status=status.HTTP_204_NO_CONTENT)


from datetime import timedelta
import time
import sys
import subprocess

WORK_TIME = timedelta(seconds=1500)
SHORT_BREAK = timedelta(seconds=300)
LONG_BREAK = timedelta(seconds=900)

def single_round(round_num, total_rounds):
    subprocess.run('clear')
    working = WORK_TIME
    print (f'Round {round_num} of {total_rounds}')
    while working:
        sys.stdout.write(f"\rTime to work: {working}")
        sys.stdout.flush()
        working -= timedelta(seconds=1)
        time.sleep(1)

def short_break():
    subprocess.run('clear')
    resting = SHORT_BREAK
    while resting:
        sys.stdout.write(f"\rOn short break for {resting}")
        sys.stdout.flush()
        resting -= timedelta(seconds=1)
        time.sleep(1)

def long_break():
    subprocess.run('clear')
    resting = LONG_BREAK
    while resting:
        sys.stdout.write(f"\rOn long break for {resting}")
        sys.stdout.flush()
        resting -= timedelta(seconds=1)
        time.sleep(1)

def pomodoro(total_rounds, long_break_after=2):
    rounds = [i for i in range(1, total_rounds+1)]
    for round_number in rounds:
        single_round(round_number, total_rounds)
        if round_number == total_rounds:
            break
        elif round_number % long_break_after == 0:
            long_break()
        else:
            short_break()

    subprocess.run('clear')
    print(f"Good job, you've done {total_rounds} rounds")

if __name__ == "__main__":
    pomodoro(4)


path='/home/robert/darknet/data/kishore_dataset/0_person/person_annot/'
path_1 = '/home/robert/darknet/data/kishore_dataset/0_person/person_obj/'

import os

for file_name in os.listdir(path):
              #print("file namee",file_name)
              nam = file_name.rstrip('.txt')
              ren = "person_"+file_name.rstrip('.txt')[-10:]
              print("renameee",ren)
              if nam+".jpg" in os.listdir(path_1):
                           print("entered loop")
                           os.rename(path_1+nam+".jpg",path_1+ren+".jpg")#(test2 empty test2 to test1)
                           os.rename(path+nam+".txt",path+ren+".txt")


def profile(seqs):
    '''
    >>> seqs = ['GCAAAACG', 'GCGAAACT', 'TACCTTCA', 'TATGTTCA', 'GCCTTAGG', 'GACTTATA', 'TCGGATCC']
    >>> profile(seqs)
    {'A': [0, 3, 1, 2, 3, 4, 0, 3], 'C': [0, 4, 3, 1, 0, 0, 5, 1], 'T': [3, 0, 1, 2, 4, 3, 1, 1], 'G': [4, 0, 2, 2, 0, 0, 1, 2]}
    '''
    length = len(seqs[0])
    for i in seqs:
        if len(i) != length:
            raise AssertionError('sequences should have equal length')


# coding: utf-8

from __future__ import absolute_import
from datetime import date, datetime  # noqa: F401

from typing import List, Dict  # noqa: F401

from swagger_server.models.base_model_ import Model
from swagger_server.models.entryproperties_key_type import EntrypropertiesKeyType  # noqa: F401,E501
from swagger_server.models.list_cid_set_events_response_cid_set_events import ListCidSetEventsResponseCidSetEvents  # noqa: F401,E501
from swagger_server.models.object import Object  # noqa: F401,E501
from swagger_server import util

class ListCidSetEventsResponse(Model):
    """NOTE: This class is auto generated by the swagger code generator program.

    Do not edit the class manually.
    """
    def __init__(self, signature: object=None, has_more_elements: bool=None, participant: AllOfListCidSetEventsResponseParticipant=None, key_type: EntrypropertiesKeyType=None, start_time: datetime=None, end_time: datetime=None, sync_verifier_start: AllOfListCidSetEventsResponseSyncVerifierStart=None, sync_verifier_end: AllOfListCidSetEventsResponseSyncVerifierEnd=None, cid_set_events: List[ListCidSetEventsResponseCidSetEvents]=None):  # noqa: E501
        """ListCidSetEventsResponse - a model defined in Swagger

        :param signature: The signature of this ListCidSetEventsResponse.  # noqa: E501
        :type signature: object
        :param has_more_elements: The has_more_elements of this ListCidSetEventsResponse.  # noqa: E501
        :type has_more_elements: bool
        :param participant: The participant of this ListCidSetEventsResponse.  # noqa: E501
        :type participant: AllOfListCidSetEventsResponseParticipant
        :param key_type: The key_type of this ListCidSetEventsResponse.  # noqa: E501
        :type key_type: EntrypropertiesKeyType
        :param start_time: The start_time of this ListCidSetEventsResponse.  # noqa: E501
        :type start_time: datetime
        :param end_time: The end_time of this ListCidSetEventsResponse.  # noqa: E501
        :type end_time: datetime
        :param sync_verifier_start: The sync_verifier_start of this ListCidSetEventsResponse.  # noqa: E501
        :type sync_verifier_start: AllOfListCidSetEventsResponseSyncVerifierStart
        :param sync_verifier_end: The sync_verifier_end of this ListCidSetEventsResponse.  # noqa: E501
        :type sync_verifier_end: AllOfListCidSetEventsResponseSyncVerifierEnd
        :param cid_set_events: The cid_set_events of this ListCidSetEventsResponse.  # noqa: E501
        :type cid_set_events: List[ListCidSetEventsResponseCidSetEvents]
        """
        self.swagger_types = {
            'signature': object,
            'has_more_elements': bool,
            'participant': AllOfListCidSetEventsResponseParticipant,
            'key_type': EntrypropertiesKeyType,
            'start_time': datetime,
            'end_time': datetime,
            'sync_verifier_start': AllOfListCidSetEventsResponseSyncVerifierStart,
            'sync_verifier_end': AllOfListCidSetEventsResponseSyncVerifierEnd,
            'cid_set_events': List[ListCidSetEventsResponseCidSetEvents]
        }

        self.attribute_map = {
            'signature': 'Signature',
            'has_more_elements': 'HasMoreElements',
            'participant': 'Participant',
            'key_type': 'KeyType',
            'start_time': 'StartTime',
            'end_time': 'EndTime',
            'sync_verifier_start': 'SyncVerifierStart',
            'sync_verifier_end': 'SyncVerifierEnd',
            'cid_set_events': 'CidSetEvents'
        }
        self._signature = signature
        self._has_more_elements = has_more_elements
        self._participant = participant
        self._key_type = key_type
        self._start_time = start_time
        self._end_time = end_time
        self._sync_verifier_start = sync_verifier_start
        self._sync_verifier_end = sync_verifier_end
        self._cid_set_events = cid_set_events

    @classmethod
    def from_dict(cls, dikt) -> 'ListCidSetEventsResponse':
        """Returns the dict as a model

        :param dikt: A dict.
        :type: dict
        :return: The ListCidSetEventsResponse of this ListCidSetEventsResponse.  # noqa: E501
        :rtype: ListCidSetEventsResponse
        """
        return util.deserialize_model(dikt, cls)

    @property
    def signature(self) -> object:
        """Gets the signature of this ListCidSetEventsResponse.

        :return: The signature of this ListCidSetEventsResponse.
        :rtype: object
        """
        return self._signature

    @signature.setter
    def signature(self, signature: object):
        """Sets the signature of this ListCidSetEventsResponse.

        :param signature: The signature of this ListCidSetEventsResponse.
        :type signature: object
        """

        self._signature = signature

    @property
    def has_more_elements(self) -> bool:
        """Gets the has_more_elements of this ListCidSetEventsResponse.

        Existem mais elementos para iterar  # noqa: E501

        :return: The has_more_elements of this ListCidSetEventsResponse.
        :rtype: bool
        """
        return self._has_more_elements

    @has_more_elements.setter
    def has_more_elements(self, has_more_elements: bool):
        """Sets the has_more_elements of this ListCidSetEventsResponse.

        Existem mais elementos para iterar  # noqa: E501

        :param has_more_elements: The has_more_elements of this ListCidSetEventsResponse.
        :type has_more_elements: bool
        """

        self._has_more_elements = has_more_elements

    @property
    def participant(self) -> AllOfListCidSetEventsResponseParticipant:
        """Gets the participant of this ListCidSetEventsResponse.

        :return: The participant of this ListCidSetEventsResponse.
        :rtype: AllOfListCidSetEventsResponseParticipant
        """
        return self._participant

    @participant.setter
    def participant(self, participant: AllOfListCidSetEventsResponseParticipant):
        """Sets the participant of this ListCidSetEventsResponse.

        :param participant: The participant of this ListCidSetEventsResponse.
        :type participant: AllOfListCidSetEventsResponseParticipant
        """
        if participant is None:
            raise ValueError("Invalid value for `participant`, must not be `None`")  # noqa: E501

        self._participant = participant

    @property
    def key_type(self) -> EntrypropertiesKeyType:
        """Gets the key_type of this ListCidSetEventsResponse.

        :return: The key_type of this ListCidSetEventsResponse.
        :rtype: EntrypropertiesKeyType
        """
        return self._key_type

    @key_type.setter
    def key_type(self, key_type: EntrypropertiesKeyType):
        """Sets the key_type of this ListCidSetEventsResponse.

        :param key_type: The key_type of this ListCidSetEventsResponse.
        :type key_type: EntrypropertiesKeyType
        """
        if key_type is None:
            raise ValueError("Invalid value for `key_type`, must not be `None`")  # noqa: E501

        self._key_type = key_type

    @property
    def start_time(self) -> datetime:
        """Gets the start_time of this ListCidSetEventsResponse.

        Data-hora do primeiro evento da lista  # noqa: E501

        :return: The start_time of this ListCidSetEventsResponse.
        :rtype: datetime
        """
        return self._start_time

    @start_time.setter
    def start_time(self, start_time: datetime):
        """Sets the start_time of this ListCidSetEventsResponse.

        Data-hora do primeiro evento da lista  # noqa: E501

        :param start_time: The start_time of this ListCidSetEventsResponse.
        :type start_time: datetime
        """
        if start_time is None:
            raise ValueError("Invalid value for `start_time`, must not be `None`")  # noqa: E501

        self._start_time = start_time

    @property
    def end_time(self) -> datetime:
        """Gets the end_time of this ListCidSetEventsResponse.

        Data-hora do último evento da lista  # noqa: E501

        :return: The end_time of this ListCidSetEventsResponse.
        :rtype: datetime
        """
        return self._end_time

    @end_time.setter
    def end_time(self, end_time: datetime):
        """Sets the end_time of this ListCidSetEventsResponse.

        Data-hora do último evento da lista  # noqa: E501

        :param end_time: The end_time of this ListCidSetEventsResponse.
        :type end_time: datetime
        """
        if end_time is None:
            raise ValueError("Invalid value for `end_time`, must not be `None`")  # noqa: E501

        self._end_time = end_time

    @property
    def sync_verifier_start(self) -> AllOfListCidSetEventsResponseSyncVerifierStart:
        """Gets the sync_verifier_start of this ListCidSetEventsResponse.

        :return: The sync_verifier_start of this ListCidSetEventsResponse.
        :rtype: AllOfListCidSetEventsResponseSyncVerifierStart
        """
        return self._sync_verifier_start

    @sync_verifier_start.setter
    def sync_verifier_start(self, sync_verifier_start: AllOfListCidSetEventsResponseSyncVerifierStart):
        """Sets the sync_verifier_start of this ListCidSetEventsResponse.

        :param sync_verifier_start: The sync_verifier_start of this ListCidSetEventsResponse.
        :type sync_verifier_start: AllOfListCidSetEventsResponseSyncVerifierStart
        """
        if sync_verifier_start is None:
            raise ValueError("Invalid value for `sync_verifier_start`, must not be `None`")  # noqa: E501

        self._sync_verifier_start = sync_verifier_start

    @property
    def sync_verifier_end(self) -> AllOfListCidSetEventsResponseSyncVerifierEnd:
        """Gets the sync_verifier_end of this ListCidSetEventsResponse.

        :return: The sync_verifier_end of this ListCidSetEventsResponse.
        :rtype: AllOfListCidSetEventsResponseSyncVerifierEnd
        """
        return self._sync_verifier_end

    @sync_verifier_end.setter
    def sync_verifier_end(self, sync_verifier_end: AllOfListCidSetEventsResponseSyncVerifierEnd):
        """Sets the sync_verifier_end of this ListCidSetEventsResponse.

        :param sync_verifier_end: The sync_verifier_end of this ListCidSetEventsResponse.
        :type sync_verifier_end: AllOfListCidSetEventsResponseSyncVerifierEnd
        """
        if sync_verifier_end is None:
            raise ValueError("Invalid value for `sync_verifier_end`, must not be `None`")  # noqa: E501

        self._sync_verifier_end = sync_verifier_end

    @property
    def cid_set_events(self) -> List[ListCidSetEventsResponseCidSetEvents]:
        """Gets the cid_set_events of this ListCidSetEventsResponse.

        :return: The cid_set_events of this ListCidSetEventsResponse.
        :rtype: List[ListCidSetEventsResponseCidSetEvents]
        """
        return self._cid_set_events

    @cid_set_events.setter
    def cid_set_events(self, cid_set_events: List[ListCidSetEventsResponseCidSetEvents]):
        """Sets the cid_set_events of this ListCidSetEventsResponse.

        :param cid_set_events: The cid_set_events of this ListCidSetEventsResponse.
        :type cid_set_events: List[ListCidSetEventsResponseCidSetEvents]
        """

        self._cid_set_events = cid_set_events


# 直接显示图片的漫画、插画类分享网站

'''
    跟网站结构相关的分析全抽离在这里，当网站改版时可以单独更新，而不影响主线策略
'''
import time
import scrapy
from scrapy.contrib.loader import ItemLoader

from .url_setting import *
from .mySetting import *

class TrimAll(object):
    def __call__(self, values):
        vlist =[]
        for s in values:
            vlist.append(s.replace(' ','').replace('\r\n',''))
        return vlist

#主入口的主体 用于循环
def GetMainCrycle(response):
    return response.css('.k_list-txt')

#主入口页面的翻页、找下一页
def Get_NextPage(response):
    next_url = response.css('.k_pape').xpath('a[text()="下一页"]/@href').extract()
    if len(next_url)>0:
        return scheme+allowed_domains[0]+next_url[0]
    else:
        return ''

#点击进去的地址
def GetContent_URL(item_selector):
    content_url = item_selector.xpath('./ul/li/a/@href').extract()
    if len(content_url) > 0 :
        return scheme+allowed_domains[0]+content_url[0]
    else:
        return ''

# 爬文章列表 取标题、预览、文章实际地址、发起日期等信息
def Loader_index(self,item_selector):
    l = ItemLoader(item={}, selector = item_selector)
    l.add_xpath('title', './ul/li/a/text()')
    l.add_xpath('url', './ul/li/a/@href')
    return l.load_item()

# 爬文章页 取标题、封面、预览、下载地址、发起日期信息
def Loader_content(response):
    l = ItemLoader(item={}, response = response)
    sub_title=''
    if len(l.get_xpath('//*[@class="pagenow"]/text()'))>=1:
        sub_title += '-'+l.get_xpath('//*[@class="pagenow"]/text()')[0]
    l.add_value('title',l.get_xpath('//*[@class="b_list-1a-1c"]/text()')[0]+sub_title)
    l.add_value('src_url',response.url)
    content_img = l.get_xpath('//*[@class="content-img"]/p/img/@src')
    l.add_value('content',content_img)
    l.add_value('image_urls',content_img)
    print('正下载图片：',content_img)
    time.sleep(len(content_img))
    return l.load_item()


import json

from ..web_gateway_testcase import WebGatewayTestCase

class TestDataChangeNoitfy(WebGatewayTestCase):

    def setUp(self):
        WebGatewayTestCase.setUp(self)

        #
        # send create subscription request to the opc ua server
        #
        req = {
            "Header": {
                "MessageType": "GW_CreateSubscriptionRequest",
                "ClientHandle": "client-handle",
                "SessionId": self.sessionId
            },
            "Body": {
            }

        }
        print("SEND: ", json.dumps(req, indent=4))
        self.ws.send(json.dumps(req))

        #
        # receive create subscription response from the opc ua server
        #
        str = self.ws.recv()
        print("RECV: ", str)
        res = json.loads(str)
        self.subscriptionId = res['Body']['SubscriptionId']

        #
        # send create monitored items request
        #
        req = {
            "Header": {
                "MessageType": "GW_CreateMonitoredItemsRequest",
                "ClientHandle": "client-handle",
                "SessionId": self.sessionId
            },
            "Body": {
                "SubscriptionId": self.subscriptionId,
                "ItemsToCreate": [
                    {
                        "ItemToMonitor": {
                            "NodeId": {
                                "Namespace": "3",
                                "Id": "218"
                            }
                        },
                        "RequestedParameters": {
                            "ClientHandle": "4712",
                            "SamplingInterval": "1000"
                        }
                    }
                ]
            }

        }
        print("SEND: ", json.dumps(req, indent=4))
        self.ws.send(json.dumps(req))

        #
        # receive monitored create items response
        #
        str = self.ws.recv()
        print("RECV: ", str)
        res = json.loads(str)
        self.monitoredItemId = res['Body']['Results'][0]['MonitoredItemId']

    def tearDown(self):
        #
        # send delete monitored items request
        #
        req = {
            "Header": {
                "MessageType": "GW_DeleteMonitoredItemsRequest",
                "ClientHandle": "client-handle",
                "SessionId": self.sessionId
            },
            "Body": {
                "SubscriptionId": self.subscriptionId,
                "MonitoredItemIds": [self.monitoredItemId]
            }

        }
        print("SEND: ", json.dumps(req, indent=4))
        self.ws.send(json.dumps(req))

        #
        # receive delete monitored items response
        #
        str = self.ws.recv()
        print("RECV: ", str)

        #
        # send delete subscriptions request to the opc ua server
        #
        req = {
            "Header": {
                "MessageType": "GW_DeleteSubscriptionsRequest",
                "ClientHandle": "client-handle",
                "SessionId": self.sessionId
            },
            "Body": {
                "SubscriptionIds": [self.subscriptionId]
            }

        }
        print("SEND: ", json.dumps(req, indent=4))
        self.ws.send(json.dumps(req))

        #
        # receive delete subscriptions response from the opc ua server
        #
        str = self.ws.recv()
        print("RECV: ", str)
        res = json.loads(str)

        WebGatewayTestCase.tearDown(self)

    def test_create_delete(self):

        #
        # receive data change request
        #
        for i in range(1, 5):
            str = self.ws.recv()
            print("RECV: ", str)
            res = json.loads(str)
            self.assertEqual(res['Header']['MessageType'], "GW_DataChangeNotify")
            self.assertEqual(res['Header']['ClientHandle'], "test_create_delete")
            self.assertEqual(res['Header']['SessionId'], self.sessionId)
            self.assertEqual(res['Body']['ClientHandleData'], "4712")
            self.assertIsNotNone(res['Body']['Value'])


#tính tổng các số nguyên tố
def songuyento(n):
    count = 0
    s =0
    for i in range(1, n + 1):
        if n % i == 0:
            s=s+i
            count += 1
    if count == 2:
        return s
    return "không phải là số nguyên tố"
n=int(input("nhap n= "))
print("tong cac so nguyen to la: ",songuyento(n))


import pygame
import time

class TimedTransition(object):
    TRANSITIONS_TIMER = pygame.USEREVENT+1
    transition_list = []
    min_interval = 0

    def __init__(self, end_function,
                 target_object=None, target_property=None, update_interval=None, target_value=None, max_time=None):
        self.target_object = target_object()
        TimedTransition.transition_list.append(lambda: self.update)
        self.last_check = 0
        self.start_time = time.time()
        self.next_check = self.start_time + update_interval
        self.update_interval = update_interval

        current_value = getattr(self.target_object, 'target_property')
        self.sign = target_value - current_value
        if TimedTransition.min_interval > self.update_interval:
            TimedTransition.min_interval = self.update_interval
            pygame.time.set_timer(self.TRANSITIONS_TIMER, self.update_interval)

    def update(self):
        current_time = time.time()
        if self.next_check >= current_time:
            current_value = getattr(self.target_object, 'target_property')
            diff = current_value - self.target_value
            current_value += max_time/update_interval

    @staticmethod
    def on_transation_timer( event):

        if event.type != TimedTransition.TRANSITIONS_TIMER:
            return
        to_remove = []
        for event in TimedTransition.transition_list:
            if not event.update():
                to_remove.append(event)

        for event in to_remove:
            TimedTransition.transition_list.remove(event)


#Simple truck driving program
#control the truck with the arrow keys.
import sys, pygame, math, time, TruckSimulator as tsim

pygame.init()
pi = math.pi
size = width, height = 1000, 500
white = 255,255,255
red = 255,0,0
screen = pygame.display.set_mode(size)
goal = (0, int(height/2))
steer = 0
speed = 0
myTruck = tsim.truck(400, 255, (pi), 0, red)

while(1):
       for event in pygame.event.get():
              if event.type == pygame.QUIT: sys. exit()
              if event .type == pygame.KEYDOWN:
                     if event.key == pygame.K_LEFT and steer > -18:
                            steer -= 2
                     if event.key == pygame.K_RIGHT and steer < 18:
                            steer += 2
                     if event.key == pygame.K_UP and speed < 22:
                            speed += 2
                     if event.key == pygame.K_DOWN and speed > -22:
                            speed -= 2
       nsteer = math.radians(steer)
       tsim.drive(myTruck,speed,nsteer)
       tsim.drawTruck(myTruck,goal,screen,1)
       time.sleep(0.1)


import pandas as pd
from sklearn.preprocessing import OrdinalEncoder
from sklearn.model_selection import train_test_split
from numpy import savetxt
import numpy as np
import pickle
import matplotlib.pyplot as plt
import collections
from scipy.io import arff
import scipy.stats

desired_width = 320
pd.set_option('display.width', desired_width)
pd.set_option('display.max_columns', 10)

def get_pie_chart_values(target_variable):
    counter = collections.Counter(target_variable)
    labels = ['Low Salary', 'High Salary']
    sizes = [counter['Low'], counter['High']]
    return labels, sizes

def pie_chart(target_variable):
    labels, sizes = get_pie_chart_values(target_variable)
    colors = ['#66b3ff', '#ffcc99']
    fig1, ax1 = plt.subplots()
    patches, texts, autotexts = ax1.pie(sizes, colors=colors, labels=labels, autopct='%1.1f%%', startangle=90)
    for text in texts:
        text.set_color('grey')
    for autotext in autotexts:
        autotext.set_color('grey')
    # Equal aspect ratio ensures that pie is drawn as a circle
    ax1.axis('equal')
    plt.tight_layout()
    plt.show()

def ordinal_encode(df, categories, column):
    encoder = OrdinalEncoder(categories=[categories],
                             handle_unknown='use_encoded_value', unknown_value=np.nan)
    df[df.columns[column]] = encoder.fit_transform(df.loc[:, [df.columns[column]]])
    return df[df.columns[column]]

def store_sets(X_train, X_test, y_train, y_test, columns):
    file_path_X_train = '../../data/processed/X_train.csv'
    file_path_y_train = '../../data/processed/y_train.csv'
    file_path_X_test = '../../data/processed/X_test.csv'
    file_path_y_test = '../../data/processed/y_test.csv'
    file_path_columns_names = '../../data/processed/columns_names.pickle'

    savetxt(file_path_X_train, X_train, delimiter=',')
    savetxt(file_path_y_train, y_train, delimiter=',')
    savetxt(file_path_X_test, X_test, delimiter=',')
    savetxt(file_path_y_test, y_test, delimiter=',')

    columns = columns[:-1]
    # print("Features Names", columns)
    open_file = open(file_path_columns_names, "wb")
    pickle.dump(columns, open_file)
    open_file.close()

def store_vehicle_sets(X, Y, X_train, X_test, y_train, y_test):
    file_path_X = '../../data/interim/vehicle/X.csv'
    file_path_y = '../../data/interim/vehicle/Y.csv'
    file_path_X_train = '../../data/interim/vehicle/X_train.csv'
    file_path_y_train = '../../data/interim/vehicle/y_train.csv'
    file_path_X_test = '../../data/interim/vehicle/X_test.csv'
    file_path_y_test = '../../data/interim/vehicle/y_test.csv'

    savetxt(file_path_X, X, delimiter=',')
    savetxt(file_path_y, Y, delimiter=',')
    savetxt(file_path_X_train, X_train, delimiter=',')
    savetxt(file_path_y_train, y_train, delimiter=',')
    savetxt(file_path_X_test, X_test, delimiter=',')
    savetxt(file_path_y_test, y_test, delimiter=',')

def x_y_split(df):
    # print("Columns", df.columns)
    # Separating the target variable
    X = df.values[:, 0:(len(df.columns) - 1)]
    Y = df.values[:, -1]
    # print("X", X)
    # print("Y", Y)
    return X, Y

def x_y_train_test_split_and_store(df, test_size):
    X, Y = x_y_split(df)
    file_path_X = '../../data/processed/X.csv'
    file_path_y = '../../data/processed/y.csv'
    savetxt(file_path_X, X, delimiter=',')
    savetxt(file_path_y, Y, delimiter=',')

    # Splitting the dataset into train and test
    X_train, X_test, y_train, y_test = train_test_split(X, Y,
                                                        test_size=test_size,
                                                        random_state=100)
    store_sets(X_train, X_test, y_train, y_test, df.columns)
    return X, Y, X_train, X_test, y_train, y_test

def vehicle_x_y_train_test_split_and_store(df, test_size):
    X, Y = x_y_split(df)
    # Splitting the dataset into train and test
    X_train, X_test, y_train, y_test = train_test_split(X, Y,
                                                        test_size=test_size,
                                                        random_state=100)
    store_vehicle_sets(X, Y, X_train, X_test, y_train, y_test)
    return X, Y, X_train, X_test, y_train, y_test

def store_validation_sets(X_validation, y_validation):
    file_path_X_validation = '../../data/processed/X_validation.csv'
    file_path_y_validation = '../../data/processed/y_validation.csv'
    savetxt(file_path_X_validation, X_validation, delimiter=',')
    savetxt(file_path_y_validation, y_validation, delimiter=',')

def x_y_validation_split_and_store(df):
    X, Y = x_y_split(df)
    store_validation_sets(X, Y)

def load_data():
    dict = {}
    list_files = ['training_set', 'validation_set',
                  'validation_set_decision_tree_pruning',
                  'test_set_naive_bayes']
    for file in list_files:
        file_path = f'../../data/raw/{file}.csv'
        df = pd.read_csv(file_path)
        del df['Instance']
        # check_dataset(df)
        dict[file] = df
    df_training_set = dict['training_set']
    df_validation_set = dict['validation_set']
    df_validation_set_decision_tree_pruning = dict['validation_set_decision_tree_pruning']
    df_test_set_naive_bayes = dict['test_set_naive_bayes']

    return df_training_set, df_validation_set, df_validation_set_decision_tree_pruning, df_test_set_naive_bayes

def store_x_test_set(df, file_name):
    # delete target column with NaNs
    del df[df.columns[-1]]
    file_path_test_set = f'../../data/processed/{file_name}.csv'
    savetxt(file_path_test_set, df, delimiter=',')

def encode_sets(df_training_set, df_validation_set, df_validation_set_decision_tree_pruning, df_test_set_naive_bayes):
    df = df_training_set.append(df_validation_set)
    df = df.append(df_validation_set_decision_tree_pruning)
    df = df.append(df_test_set_naive_bayes)
    df_features = pd.get_dummies(df[[df.columns[0],
                                     df.columns[1]]],
                                 prefix={'Education Level': 'education_level',
                                         'Career': 'career'},
                                 drop_first=True)
    df_features[df.columns[2]] = ordinal_encode(df, ['Less than 3', '3 to 10', 'More than 10'], 2)
    df_features[df.columns[-1]] = ordinal_encode(df, ['Low', 'High'], 3)
    df_features.reset_index(drop=True)
    df_training_set = df_features.iloc[0:len(df_training_set)]
    df_validation_set = df_features.iloc[len(df_training_set):(len(df_training_set) +
                                                               len(df_validation_set))]

    df_validation_set_decision_tree_pruning = df_features.iloc[(len(df_training_set) +
                                                                len(df_validation_set)):(len(df_training_set) +
                                                                                len(df_validation_set) +
                                                                                len(df_validation_set_decision_tree_pruning))]
    df_test_set_naive_bayes = df_features.iloc[(len(df_training_set) +
                                                len(df_validation_set) +
                                                len(df_validation_set_decision_tree_pruning)):]

    return df_training_set, df_validation_set, df_validation_set_decision_tree_pruning, df_test_set_naive_bayes

def split_and_store_sets(df_training_set, df_validation_set,
                         df_test_set_decision_tree_pruning,  # same as validation set.
                         df_test_set_naive_bayes, test_size):
    x_y_train_test_split_and_store(df_training_set, test_size)  # first tree
    x_y_validation_split_and_store(df_validation_set)  # pruning tree
    store_x_test_set(df_test_set_naive_bayes, 'x_test_set_naive_bayes')

def load_salary_sets():
    df_training_set, df_validation_set, df_validation_set_decision_tree_pruning, df_test_set_naive_bayes = load_data()
    pie_chart(df_training_set[df_training_set.columns[-1]])

    print(df_training_set)
    print(df_validation_set)
    print(df_validation_set_decision_tree_pruning)
    print(df_test_set_naive_bayes)

    df_training_set, df_validation_set, df_validation_set_decision_tree_pruning, \
    df_test_set_naive_bayes = encode_sets(
        df_training_set,
        df_validation_set,
        df_validation_set_decision_tree_pruning,
        df_test_set_naive_bayes)

    print(df_training_set)
    print(df_validation_set)
    print(df_validation_set_decision_tree_pruning)
    print(df_test_set_naive_bayes)

    TEST_SIZE = 0.3
    split_and_store_sets(df_training_set,
                         df_validation_set,
                         df_validation_set_decision_tree_pruning,
                         df_test_set_naive_bayes, TEST_SIZE)

def load_vehicle_set():
    file_path = f'../../data/raw/veh-prime.arff'
    data = arff.loadarff(file_path)
    df = pd.DataFrame(data[0])
    return df

def encode_class_and_store(df):
    replace_values = {b'noncar': 0, b'car': 1}
    df[df.columns[-1]] = df[df.columns[-1]].replace(replace_values)
    file_path_vehicle_df = '../../data/interim/vehicle/vehicle_df.pickle'
    open_file = open(file_path_vehicle_df, "wb")
    pickle.dump(df, open_file)
    open_file.close()
    return df

def calculate_correlation(df):
    feature_list = df.columns[:-1]
    target_variable = df.columns[-1]
    dict = {}
    for feature in feature_list:
        # [0] because it returns first r and p-value.
        dict[feature] = scipy.stats.pearsonr(df[feature], df[target_variable])[0]
    return dict

def list_features_from_highest_correlation(df):
    dict_correlation = calculate_correlation(df)
    dict_correlation = {k: abs(v) for k, v in
                        sorted(dict_correlation.items(), key=lambda item: abs(item[1]), reverse=True)}
    print("The correlation of the features with the target variable (car or not car), from highest absolute "
          "correlation to "
          "lowest is: \n", dict_correlation)

    features_correlation = pd.DataFrame(dict_correlation.items(), columns=['Features', 'Pearson Correlation'])
    file_path_dict_correlation = '../../data/interim/vehicle/features_correlation.pickle'
    open_file = open(file_path_dict_correlation, "wb")
    pickle.dump(features_correlation, open_file)
    open_file.close()

def load_encode_and_calculate_correlation_vehicle_set():
    df = load_vehicle_set()
    df = encode_class_and_store(df)
    list_features_from_highest_correlation(df)
    return df

def process_vehicle_data():
    df_training_set = load_encode_and_calculate_correlation_vehicle_set()
    test_size = 0.3
    vehicle_x_y_train_test_split_and_store(df_training_set, test_size)

def main():
    load_salary_sets()
    process_vehicle_data()

if __name__ == '__main__':
    main()

data mining/decision tree_KNN_Naive Bayes/src/data/make_dataset.py


from ElastiSim_Statistics import plot_jobs_gantt, plot_node_utilization
import csv

def get_csv_dict(path: str):
    try:
        with open(path) as f:
            return [row for row in csv.DictReader(f)]
    except Exception as error:
        print("Unable to calculate metrics, file %s missing", path)
    return []

def avg(l, div=None, default=None, tuple=None):
    if div is None:
        div = len(l)
    if len(l) == 0 or div == 0:
        return default
    if tuple is not None:
        l = [i[tuple] for i in l]
    return float(sum(l)) / float(div)

def generate_statistics_figures(path, scaling_factor=1):
    def execute(func, path, *in_csv, scaling_factor=1):
        out_path = path + func.__name__ + ".pdf"
        in_path = [path + i + ".csv" for i in in_csv]
        if len(in_path) == 1:
            func(in_path[0]).write_image(out_path, format="pdf", scale=scaling_factor)
        else:
            func(in_path).write_image(out_path, format="pdf", scale=scaling_factor)

    # execute(plot_system_wide_cpu, path, "cpu_utilization")
    # execute(plot_network_activity, path, "network_activity")
    # execute(plot_pfs_utilization, path, "pfs_utilization")
    # execute(plot_utilization, path, "cpu_utilization", "network_activity", "pfs_utilization")
    # execute(plot_cpu_per_node, path, "cpu_utilization")
    # execute(plot_jobs_box, path, "job_statistics")
    # execute(plot_jobs_violin, path, "job_statistics")
    execute(plot_jobs_gantt, path, "job_statistics", scaling_factor=scaling_factor)
    execute(
        plot_node_utilization, path, "node_utilization", scaling_factor=scaling_factor
    )

# calculate job statistics
def generate_job_statistics(path: str, metrics: dict):
    csv_dict = get_csv_dict(path)
    if len(csv_dict) == 0:
        return

    total_runtime = max(float(r["End Time"]) for r in csv_dict)
    metrics["total_runtime"] = total_runtime

    total_wait_time = sum(float(r["Wait Time"]) for r in csv_dict)
    metrics["average_wait_time"] = total_wait_time / len(csv_dict)

    total_turnaround_time = sum(float(r["Turnaround Time"]) for r in csv_dict)
    metrics["average_turnaround_time"] = total_turnaround_time / len(csv_dict)

    total_makespan = sum(float(r["Makespan"]) for r in csv_dict)
    metrics["average_makespan"] = total_makespan / len(csv_dict)

    job_amount = max(int(r["ID"]) for r in csv_dict)
    malleable_job_amount = sum(1 if r["Type"] == "malleable" else 0 for r in csv_dict)
    return job_amount, malleable_job_amount

# calculate node statistics
def generate_node_statistics(path: str, metrics: dict, end_node_utilization_threshold=0.9):
    csv_dict = get_csv_dict(path)
    if len(csv_dict) == 0:
        return

    node_usage = dict()
    node_state = dict()
    for row in csv_dict:
        node = row["Node"]
        time = float(row["Time"])
        current_state = node_state.setdefault(node, (0, "free"))
        if current_state[1] == "allocated":
            allocation_time = time - current_state[0]
            node_usage[node] = node_usage.setdefault(node, 0) + allocation_time
        node_state[node] = (time, row["State"])

    node_amount = len(node_usage)
    avg_node_utilization = (sum(v for v in node_usage.values()) / node_amount if node_amount != 0 else 0)
    if "total_runtime" in metrics and metrics["total_runtime"] is not None:
        total_runtime = metrics["total_runtime"]
        metrics["average_node_utilization"] = avg_node_utilization / total_runtime

    last_max = None
    current_max = node_amount
    for row in csv_dict:
        current_max += 1 if row["State"] != "free" else -1
        if current_max > node_amount * end_node_utilization_threshold:
            last_max = float(row["Time"])
    metrics["end_of_max"] = last_max

# calculate scheduler event amounts, average node amount per event, average event amount per job
def generate_event_statistics(path: str, metrics: dict, job_amount, malleable_amount=None):
    csv_dict = get_csv_dict(path)
    if len(csv_dict) == 0:
        return

    # event: (event_amount : int, avg nodes per event : list, avg events per job : dict)
    event_dict = {"SHRINK": [0, [], dict()], "EXPAND": [0, [], dict()]}
    for row in csv_dict:
        event, nodes, job = (row["Event"], row["Nodes"], row["Jobs"])
        if event not in event_dict:
            continue

        node_amount = nodes.count("N")
        event_dict[event][0] += 1
        event_dict[event][1].append(node_amount)
        if job not in event_dict[event][2]:
            event_dict[event][2][job] = 0
        event_dict[event][2][job] += 1

    for event_type, vals in event_dict.items():
        event_dict[event_type][1] = avg(vals[1], default=0)
        event_dict[event_type][2] = avg(
            vals[2].values(), div=malleable_amount or job_amount, default=0
        )

    metrics["shrink_event"] = event_dict["SHRINK"]
    metrics["expand_event"] = event_dict["EXPAND"]

def generate_statistics(path):
    metrics = dict()
    job_amount, malleable_job_amount = generate_job_statistics(path + "job_statistics.csv", metrics)
    generate_node_statistics(path + "node_utilization.csv", metrics)
    generate_event_statistics(path + "event.csv", metrics, malleable_job_amount)
    return metrics


import random
import os
import errno
import functools
import socket
import fcntl
import struct
import array

from machination.exceptions import InvalidArgumentNumberError
from machination.exceptions import ArgumentValidationError

def ordinal(num):
    '''
    Returns the ordinal number of a given integer, as a string.
    eg. 1 -> 1st, 2 -> 2nd, 3 -> 3rd, etc.
    '''
    if 10 <= num % 100 < 20:
        return '{0}th'.format(num)
    else:
        val = {1 : 'st', 2 : 'nd', 3 : 'rd'}.get(num % 10, 'th')
        return '{0}{1}'.format(num, val)

def accepts(*accepted_arg_types):
    '''
    A decorator to validate the parameter types of a given function.
    It is passed a tuple of types. eg. (<type 'tuple'>, <type 'int'>)

    Note: It doesn't do a deep check, for example checking through a
          tuple of types. The argument passed must only be types.
    '''
    def accept_decorator(validate_function):
        # Check if the number of arguments to the validator
        # function is the same as the arguments provided
        # to the actual function to validate. We don't need
        # to check if the function to validate has the right
        # amount of arguments, as Python will do this
        # automatically (also with a TypeError).
        @functools.wraps(validate_function)
        def decorator_wrapper(*function_args, **function_args_dict):
            if len(accepted_arg_types) is not len(accepted_arg_types):
                raise InvalidArgumentNumberError(validate_function.__name__)

            # We're using enumerate to get the index, so we can pass the
            # argument number with the incorrect type to ArgumentValidationError.
            for arg_num, (actual_arg, accepted_arg_type) in enumerate(zip(function_args, accepted_arg_types)):

                if accepted_arg_type != None and not isinstance(actual_arg,accepted_arg_type):
                    ord_num = ordinal(arg_num + 1)
                    raise ArgumentValidationError(ord_num,
                                                  validate_function.__name__,
                                                  accepted_arg_type)

            return validate_function(*function_args)
        return decorator_wrapper
    return accept_decorator

def listPath(d):
    if(os.path.exists(d)):
        return [os.path.join(d, f) for f in os.listdir(d)]
    else:
        return []

def randomMAC():
    mac = [ 0x00, 0x16, 0x3e,
        random.randint(0x00, 0x7f),
        random.randint(0x00, 0xff),
        random.randint(0x00, 0xff) ]
    return ':'.join(map(lambda x: "%02x" % x, mac))

def mkdir_p(path):
    try:
        os.makedirs(path)
    except OSError as exc:
        if exc.errno == errno.EEXIST and os.path.isdir(path):
            pass
        else: raise

def demote(user_uid, user_gid):
    def result():
        os.setgid(user_gid)
        os.setuid(user_uid)
    return result

def getAllNetInterfaces():
    max_possible = 128  # arbitrary. raise if needed.
    vals = max_possible * 32
    s = socket.socket(socket.AF_INET, socket.SOCK_DGRAM)
    names = array.array('B', '\0' * vals)
    outbytes = struct.unpack('iL', fcntl.ioctl(
        s.fileno(),
        0x8912,  # SIOCGIFCONF
        struct.pack('iL', vals, names.buffer_info()[0])
    ))[0]
    namestr = names.tostring()
    lst = []
    for i in range(0, outbytes, 40):
        name = namestr[i:i+16].split('\0', 1)[0]
        if name != "lo":
          lst.append(name)
    return lst


# 백준 9613
# 실버 3 / GCD
from itertools import combinations
import sys
T = int(sys.stdin.readline().strip())
for _ in range(T):
    data = list(map(int,sys.stdin.readline().split()))
    case = list(combinations(data[1:], 2))
    result = 0
    for item in case :
        sub_result = 1
        small_num =min(item[0],item[1])
        for i in range(small_num, 1, -1):
            if item[0] % i ==0 and item[1] % i == 0:
                sub_result *= i
                break
        result += sub_result
    print(result)


import numpy as np
import pandas as pd
from scipy import stats
from sklearn import datasets
from sklearn.model_selection import KFold
from sklearn.model_selection import GridSearchCV
from sklearn.gaussian_process import kernels as sk_kern
from sklearn.gaussian_process import GaussianProcessRegressor
from sklearn import preprocessing
from sklearn.ensemble import RandomForestRegressor
from statistics import mean
import sys

# python corr_run.py INPUT_PATH SEED
# ex) python corr_run.py ../Input/ddG.csv 1
args = sys.argv
INPUT_PATH = args[1]
SEED = int(args[2])
NUM_SPLIT = 4
CONS4 = ['SIE-Scwrlmut', 'Rosmut', 'FoldXB', 'FoldXS', 'EXPT']
CONS3 = ['SIE-Scwrlmut', 'Rosmut', 'FoldXS', 'EXPT']
FI4 = ['SIE-Scwrlmut', 'Rosmut', 'DS-B', 'mCSM-AB', 'EXPT']

def standardization(df):
    y = df['EXPT']
    X = df.drop('EXPT', axis=1)
    X = (X - X.values.mean()) / X.values.std(ddof=0)
    df = pd.concat([X, y], axis=1)
    return df

def calc_gpr(df, kf):
    GP_corr_list = []
    df_gpr = df.copy()
    df_gpr = standardization(df_gpr)  # standardization
    for train, test in kf.split(df_gpr):
        kernel = (sk_kern.RBF() +
                  sk_kern.ConstantKernel() +
                  sk_kern.WhiteKernel())

        # separeting data
        train_df = df_gpr.iloc[train]
        test_df = df_gpr.iloc[test]
        train_X = train_df.drop('EXPT', axis=1)
        train_y = train_df['EXPT']
        test_X = test_df.drop('EXPT', axis=1)
        test_y = test_df['EXPT']

        # training
        gp_rbf = GaussianProcessRegressor(kernel=kernel)
        gp_rbf.fit(train_X, train_y)

        # Varidation
        test_gp = gp_rbf.predict(test_X)
        gp_corr = np.corrcoef(test_y, test_gp)[0, 1]
        GP_corr_list.append(gp_corr)
    return str(mean(GP_corr_list))

def calc_rf(df, kf, seed):
    Forest_corr_list = []
    df_rf = df.copy()
    df_rf = standardization(df_rf)  # standardization
    df_fi = pd.DataFrame()
    for i, data in enumerate(kf.split(df_rf)):
        train, test = data
        train_df = df_rf.iloc[train]
        test_df = df_rf.iloc[test]
        train_X = train_df.drop('EXPT', axis=1)
        train_y = train_df['EXPT']
        test_X = test_df.drop('EXPT', axis=1)
        test_y = test_df['EXPT']

        # training
        forest = RandomForestRegressor(random_state=seed)
        params = {'n_estimators': [3, 10, 100, 1000, 10000]}
        forest = GridSearchCV(forest, params, cv=4, scoring='r2', n_jobs=1,iid=False)
        y_forest = forest.fit(train_X, train_y).predict(train_X)

        # varidation
        test_forest = forest.predict(test_X)
        forest_corr = np.corrcoef(test_y, test_forest)[0, 1]
        Forest_corr_list.append(forest_corr)

        # feature importance
        df_fi[str(seed) + str(i)] = forest.best_estimator_.feature_importances_

    # feature importance
    df_fi.index = df.columns[0:-1]
    df_fi.to_csv('../Output/1/' + str(seed) + '.csv')

    return str(mean(Forest_corr_list))

def calc_mono(df, kf):
    mono_list = []
    df_mono = df.copy()
    for data in kf.split(df_mono):
        train, test = data
        test_df = df_mono.iloc[test]

        mono_corr = test_df.corr(method='pearson')
        mono_list.append(mono_corr['EXPT'])

    df_corr = pd.concat(mono_list, axis=1).mean(axis='columns')
    return df_corr

def calc_cons(df, kf):
    cons_list = []
    df_cons = df.copy()
    for data in kf.split(df_cons):
        train, test = data
        test_df = df_cons.iloc[test]
        test_X = test_df.drop('EXPT', axis=1)
        test_y = test_df['EXPT']

        test_cons = test_X.apply(calc_zscore, axis=0).mean(axis=1)
        cons_corr = np.corrcoef(test_y, test_cons)[0, 1]
        cons_list.append(cons_corr)
    return str(mean(cons_list))

def calc_zscore(score_list):
    MAD = score_list.mad()
    med = score_list.median()
    zscore = (score_list - med)/(1.4826*MAD)
    return zscore

def main():
    df = pd.read_csv(INPUT_PATH)
    kf = KFold(n_splits=NUM_SPLIT, random_state=SEED, shuffle=True)
    print('cons4\t' + calc_cons(df[CONS4], kf))
    print('cons3\t' + calc_cons(df[CONS3], kf))
    df_st = standardization(df)
    print('RFR_4\t' + calc_rf(df[FI4], kf, SEED))
    print('RFR\t' + calc_rf(df, kf, SEED))
    print('GPR\t' + calc_gpr(df_st, kf))
    print('GPR_4\t' + calc_gpr(df_st[FI4], kf))

    print(calc_mono(df, kf))

if __name__ == '__main__':
    main()


import torch
import numpy as np

from torch.autograd import Variable
from torch.nn import functional as F

# Let's start from inputting data into a simple RNN first

class TutorialLSTM(torch.nn.Module):

    def __init__(self, nb_layers, nb_lstm_units=100, nb_lstm_layers=1, embedding_dim=3, n_factors=3, batch_size=3):

        super(TutorialLSTM, self).__init__()

        self.vocab = {'<PAD>': 0, 'is': 1, 'it': 2, 'too': 3, 'late': 4, 'now': 5, 'say': 6, 'sorry': 7, 'ooh': 8, 'yeah':9}
        self.tags = {'<PAD>': 0, 'VB': 1, 'PRP': 2, 'RB': 3, 'JJ': 4, 'NNP': 5}

        self.nb_layers = nb_layers
        self.nb_lstm_units = nb_lstm_units
        self.nb_lstm_layers = nb_lstm_layers
        self.embedding_dim = embedding_dim
        self.n_factors = n_factors
        self.batch_size = batch_size
        self.nb_vocab_words = len(self.vocab)

        self.nb_tags = len(self.tags) - 1

        # self.lstm

        self.__build_model()

    def __build_model(self):

        nb_vocab_words = len(self.vocab)

        padding_idx = self.vocab['<PAD>']
        self.word_embedding = torch.nn.Embedding(
            num_embeddings=nb_vocab_words,
            embedding_dim=self.embedding_dim,
            padding_idx=padding_idx
        )

        self.lstm = torch.nn.LSTM(
            input_size=self.embedding_dim,
            hidden_size=self.nb_lstm_units,
            num_layers=self.nb_lstm_layers,
            batch_first=True,
        )

        self.hidden_to_tag = torch.nn.Linear(self.nb_lstm_units, self.nb_tags)

        # the feedforward NN for the attention layer
        # self.attetion_a = torch.nn.Linear()

        # the feedforward NN for the contextual
        # TODO: change to Xavier initialization later
        self.Wc = torch.randn(self.n_factors, self.nb_lstm_units, self.nb_lstm_units)
        self.Wm = torch.randn(self.nb_lstm_units, self.nb_vocab_words)
        self.Wy = torch.randn(self.nb_lstm_units, 1)

    def init_hidden(self):
        hidden_a = torch.randn(self.nb_lstm_layers, self.batch_size, self.nb_lstm_units)
        hidden_b = torch.randn(self.nb_lstm_layers, self.batch_size, self.nb_lstm_units)

        hidden_a = Variable(hidden_a)
        hidden_b = Variable(hidden_b)

        return  (hidden_a, hidden_b)

    def forward(self, X, X_lengths, Xc):
        self.hidden = self.init_hidden()
        # self.hidden = torch.randn(self.nb_lstm_layers, self.batch_size, self.nb_lstm_units)

        batch_size, seq_len = X.size()

        X = self.word_embedding(X)

        X = torch.nn.utils.rnn.pack_padded_sequence(X, X_lengths, batch_first=True)

        X, self.hidden = self.lstm(X, self.hidden)

        hs, _ = self.hidden

        # print ("hs:", hs) #sequence embeedding

        # TODO: attention layer here

        bc = torch.matmul(hs.transpose(0,1), self.Wc[torch.argmax(Xc, axis=0)])
        bc = torch.squeeze(bc)

        # print ("hs + bc:", (hs+bc).shape)
        # print ("(hs + bc) * Wm)", torch.matmul(hs + bc, self.Wm).shape)

        output_m = torch.matmul(hs + bc, self.Wm)
        output_y = torch.matmul(hs + bc, self.Wy)
        #
        #
        # X, _ = torch.nn.utils.rnn.pad_packed_sequence(X, batch_first=True)
        #
        # X = X.contiguous()
        # X = X.view(-1, X.shape[2])
        #
        # X = self.hidden_to_tag(X)
        #
        # X = F.log_softmax(X, dim=1)
        #
        # X = X.view(batch_size, seq_len, self.nb_tags)
        #
        # y_hat = X
        #
        # return y_hat
        return output_m, output_y

    def _sequence_mask(self, sequence_length, max_len=None):
        if max_len is None:
            max_len = sequence_length.data.max()

        batch_size = sequence_length.size(0)
        seq_range = torch.range(0, max_len - 1).long()
        seq_range_expand = seq_range.unsqueeze(0).expand(batch_size, max_len)
        seq_range_expand = Variable(seq_range_expand)

        seq_length_expand = (sequence_length.unsqueeze(1).expand_as(seq_range_expand))

        return seq_range_expand < seq_length_expand

    def compute_loss(self, Y_hat, Y, X_lenghts):
        """
        Reference: https://gist.github.com/jihunchoi/f1434a77df9db1bb337417854b398df1
        """

        logits_flat = Y_hat.view(-1, Y_hat.size(-1))
        log_probs_flat = F.log_softmax(logits_flat)

        target_flat = Y.view(-1, 1)
        target_flat -= 1
        target_flat[target_flat < 0] = 0

        losses_flat = -torch.gather(log_probs_flat, dim=1, index=target_flat)

        losses = losses_flat.view(*Y.size())

        mask = self._sequence_mask(sequence_length=X_lengths, max_len=Y.size(1))
        losses = losses * mask.float()
        loss = losses.sum() / X_lengths.float().sum()

        return loss

sent_1_x = ['is', 'it', 'too', 'late', 'now', 'say', 'sorry']
sent_1_y = ['VB', 'PRP', 'RB', 'RB', 'RB', 'VB', 'JJ']

sent_2_x = ['ooh', 'ooh']
sent_2_y = ['NNP', 'NNP']

sent_3_x = ['sorry', 'yeah']
sent_3_y = ['JJ', 'NNP']

X = [sent_1_x, sent_2_x, sent_3_x]
Y = [sent_1_y, sent_2_y, sent_3_y]

vocab = {'<PAD>': 0, 'is': 1, 'it': 2, 'too': 3, 'late': 4, 'now': 5, 'say': 6, 'sorry': 7, 'ooh': 8, 'yeah':9}
tags = {'<PAD>': 0, 'VB': 1, 'PRP': 2, 'RB': 3, 'JJ': 4, 'NNP': 5}

X = [[vocab[word] for word in sentence] for sentence in X]
Y = [[tags[tag] for tag in sentence] for sentence in Y]

# pad X
X_lengths = [len(sentence) for sentence in X]

pad_token = vocab['<PAD>']
longest_sent = max(X_lengths)
batch_size  = len(X)
padded_X = np.ones((batch_size, longest_sent)) * pad_token

for i, x_len in enumerate(X_lengths):
    sequence = X[i]
    padded_X[i, 0:x_len] = sequence[:x_len]

# pad Y
Y_lengths = [len(sentence) for sentence in Y]

pad_token = tags['<PAD>']
longest_sent = max(Y_lengths)
batch_size = len(Y)
padded_Y = np.ones((batch_size, longest_sent)) * pad_token

for i, y_len in enumerate(Y_lengths):
    sequence = Y[i]
    padded_Y[i, 0:y_len] = sequence[:y_len]

padded_X = np.asarray(padded_X)
X_lengths = np.asarray(X_lengths)

padded_Y = np.asarray(padded_Y)
Y_lengths = np.asarray(Y_lengths)

Xc  = np.zeros((3, 3))
Xc[0,0] = 1
Xc[1,1] = 1
Xc[2,2] = 1

# https://github.com/huggingface/transformers/issues/2952
padded_X = torch.from_numpy(padded_X).type(torch.LongTensor)
X_lengths = torch.from_numpy(X_lengths)

padded_Y = torch.from_numpy(padded_Y).type(torch.LongTensor)
Y_lengths = torch.from_numpy(Y_lengths)

Xc = torch.from_numpy(Xc).type(torch.float)

model = TutorialLSTM(1)
output_m, output_y = model(padded_X, X_lengths, Xc)
# loss = model.compute_loss(y_pred, padded_Y, X_lengths)