"""
Given a string s formed by digits ('0' - '9') and '#' .
We want to map s to English lowercase characters as follows:

Characters ('a' to 'i') are represented by ('1' to '9') respectively.
Characters ('j' to 'z') are represented by ('10#' to '26#') respectively.
Return the string formed after mapping.

It's guaranteed that a unique mapping will always exist.

Example 1:
Input: s = "10#11#12"
Output: "jkab"
Explanation: "j" -> "10#" , "k" -> "11#" , "a" -> "1" , "b" -> "2".
"""

def freqAlphabets(s):
    ans = []
    i = len(s)-1
    while i >= 0:
        # j = 106 = 10#  so 96 + s[i]
        if s[i] == '#':
            c = s[i-2:i]
            i -= 3
            ans.append(chr(96 + int(c)))
        else:
            ans.append(chr(96 + int(s[i])))
            i -= 1

    return ''.join(ans[::-1])

s = "10#11#12"
print(freqAlphabets(s))


""" util function.py """

import os
import argparse
import tensorflow as tf
import numpy as np
import json
from datetime import datetime

def get_config_from_json(json_file):
    """
        Get the config from a json file
        param: json_file;
        return: config(dictionary).
    """
    # parse the configurations from the config json file provided
    with open(json_file, 'r') as config_file:
        config_dict = json.load(config_file)

    return config_dict

def save_config(config):
    """
        Save the current config dict in a txt file for future reference.
        param: python dict;
        return: None.
    """
    dateTimeObj = datetime.now()
    timestampStr = dateTimeObj.strftime("%d-%b-%Y-%H-%M")
    filename = config['checkpoint_dir'] + 'training_config_{}.txt'.format(timestampStr)
    config_to_save = json.dumps(config)
    f = open(filename, "w")
    f.write(config_to_save)
    f.close()
    print('The current config is saved at {}'.format(filename))

def process_config(json_file):
    """
        Process the config json file and create various directories to save experiment results.
        param: json_file;
        return: config(dictionary).
    """
    config = get_config_from_json(json_file)
    print("The current config is:\n{}\n".format(config))

    save_name = 'prior-{}-{}-{}-{}-{}-{}-mixture-{}'.format(
        config['prior'],
        config['num_hidden_units'],
        config['code_size'],
        config['representation_size'],
        config['inner_activation'],
        config['n_layers_inner_VAE'],
        config['n_mixtures'])
    print("Experiment results will be saved at:\n{}\n".format(save_name))

    # if config['exp_name'] == "celebA":
    #     config['data_path'] = '/home/shuyu/Documents/dataset/celeba/'

    if config['load_dir'] == "default":
        save_dir = "./experiments/{}/batch-{}".format(
            config['exp_name'],
            config['batch_size'])
        config['summary_dir'] = os.path.join(save_dir, save_name, "summary/")
        config['result_dir'] = os.path.join(save_dir, save_name, "result/")
        config['checkpoint_dir'] = os.path.join(save_dir, save_name, "checkpoint/")
    else:
        save_dir = config['load_dir']
        config['summary_dir'] = "./figures/{}/summary/".format(config['exp_name'])
        config['result_dir'] = "./figures/{}/result/".format(config['exp_name'])
        config['checkpoint_dir'] = os.path.join(save_dir, config['exp_name'])
    print("Models will be saved / loaded at:\n{}".format(config['checkpoint_dir']))
    print("Results will be saved at:\n{}\n".format(config['result_dir']))

    return config

def create_dirs(dirs):
    """
        dirs - a list of directories to create if these directories are not found
        param: dirs;
        return: exit_code: 0 - success;  -1 - failed.
    """
    try:
        for dir_ in dirs:
            if not os.path.exists(dir_):
                os.makedirs(dir_)
        return 0
    except Exception as err:
        print("Creating directories error: {0}".format(err))
        exit(-1)

def count_trainable_variables(scope_name):
    """
        compute the total number of trainable parameters in a network defined in the scope_name.
        param: scope name of a network;
        return: an integer indicating the total number of trainable parameters.
    """
    total_parameters = 0
    for variable in tf.trainable_variables(scope_name):
        # shape is an array of tf.Dimension
        shape = variable.get_shape()
        variable_parameters = 1
        for dim in shape:
            variable_parameters *= dim.value
        total_parameters += variable_parameters
    print(
        'The total number of trainable parameters in the {} model is: {}k.'.format(
            scope_name, np.around(total_parameters/1000, 2)))
    return total_parameters

def get_args():
    argparser = argparse.ArgumentParser(description=__doc__)
    argparser.add_argument(
        '-c', '--config',
        metavar='C',
        default='None',
        help='The Configuration file')
    args = argparser.parse_args()
    return args

def preprocess_input_original(x):
    x /= 255.
    x -= 0.5
    x *= 2.
    return x

def preprocess_input_generated(x):
    x = np.clip(x, 0., 1.)
    x -= 0.5
    x *= 2.
    return x

def compute_FID_score(data_file1, data_file2, FID_network, pooling_option, second_set='generated'):
    dataset1 = np.load(data_file1)
    dataset1 = dataset1['sampled_images']
    dataset1 = dataset1.astype(np.float32)
    dataset1 = preprocess_input_original(dataset1)

    dataset2 = np.load(data_file2)
    dataset2 = dataset2['sampled_images']
    dataset2 = dataset2.astype(np.float32)
    if second_set == 'generated':
        dataset2 = preprocess_input_generated(dataset2)
    else:
        dataset2 = preprocess_input_original(dataset2)

    sess = tf.compat.v1.InteractiveSession()
    dataset1 = tf.image.resize_images(dataset1, (64, 64))
    dataset2 = tf.image.resize_images(dataset2, (64, 64))
    dataset1 = dataset1.eval()
    dataset2 = dataset2.eval()
    sess.close()

    if FID_network == "inception":
        inception_v3_model = tf.keras.applications.InceptionV3(
            include_top=False,
            weights='imagenet',
            input_shape=(140, 140, 3),
            pooling=pooling_option)
        inception_v3_model.trainable = False
        inception_v3_model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
                                   loss='mse',  # mean squared error
                                   metrics=['mae'])

        sess = tf.compat.v1.InteractiveSession()
        resized_input1 = tf.image.resize_images(dataset1, (140, 140))
        resized_input2 = tf.image.resize_images(dataset2, (140, 140))
        resized_input1 = resized_input1.eval()
        resized_input2 = resized_input2.eval()
        sess.close()

        act1 = inception_v3_model.predict(resized_input1, batch_size=100)
        act2 = inception_v3_model.predict(resized_input2, batch_size=100)

    elif FID_network == "VGG":
        vgg16_model = tf.keras.applications.vgg16.VGG16(
            include_top=False,
            weights='imagenet',
            input_shape=(128, 128, 3),
            pooling=pooling_option)
        vgg16_model.trainable = False
        vgg16_model.compile(optimizer=tf.keras.optimizers.Adam(0.01),
                            loss='mse',  # mean squared error
                            metrics=['mae'])
        act1 = vgg16_model.predict(dataset1, batch_size=100)
        act2 = vgg16_model.predict(dataset2, batch_size=100)

    sess = tf.Session()
    FID_score = tf.contrib.gan.eval.frechet_classifier_distance_from_activations(
        tf.constant(act1),
        tf.constant(act2))
    print("FID score between {} and {} is:\n{}".format(data_file1, data_file2, sess.run(FID_score)))


import main
import requests, os

def reddit(input):
    print(input)
    args = input.split(" ")
    print(args)
    if input == "":
        main.send_msg("```no subreddit requested!```")
    else:
        page = requests.get(f'https://www.reddit.com/r/{input}/new/.json').json()
        if 'error' in page.keys():
            print('retry?')
            main.time.sleep(1)
            reddit(input)
        else:
            imageurl = page['data']['children'][0]['data']['url_overridden_by_dest']
            print(imageurl)
            print(os.path.basename(imageurl))
            imagetitle = page['data']['children'][0]['data']['title']
            postlonk = 'www.reddit.com' + page['data']['children'][0]['data']['permalink']
            print(imageurl, imagetitle, postlonk)
            pic = open('download/'+os.path.basename(imageurl), 'xb')
            pic.write(requests.get(imageurl, allow_redirects=True).content)
            main.media_with_caption('download/'+os.path.basename(imageurl),f'{imagetitle}\n\n{postlonk}')
            os.remove('download/'+os.path.basename(imageurl))

def help():
    main.send_msg("```Sends Posts from reddit, yay! -_-```")


from sys import stdin

while True:
    n=stdin.readline().strip()
    if n=='0':
        break
    res=1
    for i in n:
        if i=='1':
            res+=3
        elif i=='0':
            res+=5
        else:
            res+=4
    print(res)


from django.contrib.auth.models import AbstractUser
from django.core.validators import RegexValidator
from django.db import models
from django.utils.translation import gettext_lazy as _

# Create your models here.
from image_optimizer.fields import OptimizedImageField

phone_regex = RegexValidator(regex=r"^\+?(88)01[3-9][0-9]{8}$", message=_('Enter Bangladeshi Number with country code'))

class CustomUser(AbstractUser):
    phone_number = models.CharField(validators=[phone_regex], max_length=20, verbose_name=_("Mobile Number"))
    Address = models.CharField(max_length=100)
    photo = OptimizedImageField(upload_to='user_photo', optimized_image_output_size=(400, 300),
                                optimized_image_resize_method='cover')

class Moderator(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='moderator')
    moderator_number = models.CharField(max_length=10)

    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name}"

categories = (
    ('Restaurant Owner', 'Restaurant Owner'),
    ('General User', 'General User'),
)

class Writer(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.CASCADE, related_name='writer')
    facebook = models.CharField(max_length=100, null=True, blank=True)
    linked_in = models.CharField(max_length=100, null=True, blank=True)
    Category = models.CharField(max_length=100, choices=categories)

    def __str__(self):
        return f"{self.user.first_name} {self.user.last_name}"

class FollowMe(models.Model):
    Followed_by = models.ForeignKey(Writer, on_delete=models.DO_NOTHING, related_name='follower')
    Following = models.ForeignKey(Writer, on_delete=models.DO_NOTHING, related_name='following')
    following_date = models.DateTimeField(auto_now=True)

class ContactModel(models.Model):
    user = models.ForeignKey(CustomUser, on_delete=models.DO_NOTHING, related_name='contact_user')
    full_name = models.CharField(max_length=200)
    email = models.EmailField()
    Message = models.TextField()


import logging

from typing import List
from .xgraph import XGraph
from .layer import XLayer

logger = logging.getLogger("pyxir")

class XGraphFactory(object):

    def build_from_xlayer(self,
                          net: List[XLayer],
                          xgraph: XGraph = None,
                          name: str = 'xgraph',
                          blobs: bool = False,
                          output_png: str = None) -> XGraph:
        """Build the XGraph from a list of XLayers"""
        if xgraph is None:
            xgraph = XGraph(name)

        for X in net:

            # Make sure tops are not set
            X.tops = []
            xgraph.add(X)

        return xgraph


#!/usr/bin/env python

from utilities import *

MAX_MESSAGE_TYPE = 128

def MSG_RANGE(start,end,size=MAX_MESSAGE_TYPE):
    b=Bitset()
    b.Resize(size)
    b.Reset()
    if start>-1 and end>start:
          i=0
          while i<=end-start and start+i<b.Size():
               b[start+i]=1
               i=i+1
    return b

def MSG(a,size=MAX_MESSAGE_TYPE):
    b=Bitset()
    b.Resize(size)
    b.Reset()
    if a>-1 and a<MAX_MESSAGE_TYPE:
        b[a]=1
    return b

#messages and message ranges
MSG_NONE                  = -1
MSG_ALL                   = MSG_RANGE(0,MAX_MESSAGE_TYPE)

#message hooks/callback function names
MSG_DEFAULT_CALLBACK = "OnMessage"
MSG_HOOK=[]
#set default callback function to "OnMessage"
for i in range(MAX_MESSAGE_TYPE):
    MSG_HOOK.append(MSG_DEFAULT_CALLBACK)


#!/usr/bin/python
from re import compile as Regex, sub as regex_replace
import re

get_pieces = lambda src,sp:[p.strip() for p in src.split(sp) if len(p.strip())>0]
convert_whitespace = lambda src:regex_replace("\s\s+"," ",src)

class GrammarParser:
	rule_searcher = Regex('([^\s][^=]+)\s*=([^;]+);$',re.MULTILINE)
	#alternate_searcher =
	def __init__(self,grammar_text):
		self.grammar_text = grammar_text.strip()#convert_whitespace(grammar_text).strip()
	def build_grammar(self):
		grammar = {}
		def split_pieces(haystack,needle):
			last,escaped,i,e=0,False,0,len(haystack)
			while i<e:
				if escaped:
					if haystack[i]=='\\':
						haystack[i:] = haystack[i+1:]
						e-=1
					elif haystack[i]==needle:
						haystack[i-1:]=haystack[i:]
				elif haystack[i]==needle:
					yield haystack[last:i]
					last = i = i+1
				else:
					i+=1
			yield haystack[last:]
		for match in self.rule_searcher.finditer(self.grammar_text):
			rule_name = convert_whitespace(match.group(1)).strip()
			rule_definition = convert_whitespace(match.group(2)).strip()
			grammar[rule_name]=list(split_pieces(rule_definition,'|'))
		return grammar

if __name__=="__main__":
	sample_ebnf = """
		digit excluding zero = "1" | "2" | "3" | "4" | "5" | "6" | "7" | "8" | "9" ;
		digit                = "0" | digit excluding zero, (* Realistically we wouldn't want this to repeat. *);
		addition operator    = "+";
		subtraction operator = "-";
		multiplication operator = "*";
		division operator    = "/";
		mathematic operator = multiplication operator | division operator | addition operator | subtraction;
		mathematic expression = number, mathematic operator, number;
		number = digit | mathematic expression;
	"""
	grammar = GrammarParser(sample_ebnf).build_grammar()
	from json import dump;dump(grammar,open("parsed.json",'w'),indent=2,sort_keys=True)


from flask import Flask, render_template, url_for, flash, redirect, request, abort, Blueprint, make_response
from flask_login import current_user, login_required
#
from webapp import db
from webapp.utils import send_email
from webapp.models import User, Role, Post, Permission
from webapp.main.forms import JoinForm
import datetime
from webapp.decorators import admin_required, permission_required, role_required, able_to_join

main = Blueprint('main', __name__)

@main.app_context_processor
def inject_permissions():
    return dict(Permission=Permission)

@main.app_context_processor
def inject_users():
    return dict(User=User)

@main.route('/')
@main.route('/home')
def home():
    page = request.args.get('page', 1, type=int)
    show_followed = False
    if current_user.is_authenticated:
        show_followed = bool(request.cookies.get('show_followed', ''))
    if show_followed:
        query = current_user.followed_posts
    else:
        query = Post.query
    posts=query.order_by(Post.date_posted.desc()).paginate(page, per_page=4, error_out=False)
    return render_template('main/index.html', active_home='active', datetime=datetime, current_time=datetime.datetime.utcnow(), posts=posts, show_followed=show_followed)

@main.route('/about')
def about():
    return render_template('main/about.html', title='About', active_about='active')
#!following section
@main.route('/follow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def follow(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.', 'danger')
        return redirect(url_for('main.home'))
    if current_user.is_following(user):
        flash('You already following this user.', 'info')
        return redirect(url_for("posts.user_posts", username=username))
    if user.has_role('Individual'):
        flash('One cannot follow this type of user.', 'danger')
        return redirect(url_for('main.home'))
    current_user.follow(user)
    db.session.commit()
    send_email(user.email, 'A new Follower', 'email/notify', user=user, follower=current_user, following=True)
    flash('You are now following %s.' %username, 'success')
    return redirect(url_for("posts.user_posts", username=username))

@main.route('/unfollow/<username>')
@login_required
@permission_required(Permission.FOLLOW)
def unfollow(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.', 'danger')
        return redirect(url_for('main.home'))
    if not current_user.is_following(user):
        flash('You cannot unfollow a user which you have not followed.', 'danger')
        return redirect(url_for('main.home'))
    current_user.unfollow(user)
    db.session.commit()
    flash('You have now unfollowed %s.' %username, 'success')
    return redirect(url_for("posts.user_posts", username=username))

@main.route('/followers/<username>')
@login_required
def followers(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.', 'danger')
        return redirect(url_for('main.home'))
    if user.has_role('Individual'):
        flash('An Individual(user) account does not have followers', 'info')
        return redirect(url_for('main.followed', username=user.username))
    page = request.args.get('page', 1, type=int)
    pagination = user.followers.paginate(
        page, per_page=20, error_out=False
    )
    follows = [{'user': item.follower, 'timestamp': item.timestamp}
                for item in pagination.items]
    return render_template('account/follows.html', user=user, title='Followers of',
                            endpoint='main.followers', pagination=pagination, follows=follows)

@main.route('/followed/<username>')
@login_required
def followed(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.', 'danger')
        return redirect(url_for('main.home'))
    u = user.followed.filter_by(followed_id=current_user.id).first()
    page = request.args.get('page', 1, type=int)
    pagination = user.followed.paginate(
        page, per_page=20, error_out=False
    )
    follows = [{'user': item.followed, 'timestamp': item.timestamp}
                for item in pagination.items]
    return render_template('account/follows.html', user=user, title='Following',
                            endpoint='main.followed', pagination=pagination, follows=follows)

@main.route('/all_posts')
@login_required
def show_all():
    resp = make_response(redirect(url_for('main.home')))
    resp.set_cookie('show_followed', '', max_age= - 130*24*60*60)#30days
    return resp
@main.route('/followed_posts')
@login_required
def show_followed():
    resp = make_response(redirect(url_for('main.home')))
    resp.set_cookie('show_followed', '1', max_age=30*24*60*60)#30days
    return resp
#!following section

#!joining section
@main.route('/join_request/<shorten>', methods=['POST','GET'])
@login_required
@able_to_join
def join_request(shorten):
    user = User.query.filter_by(shorten=shorten).first()
    if user is None:
        flash('Association does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if current_user.is_a_member(user):
        flash('You already apart of this Association.', 'info')
        return redirect(url_for("posts.user_posts", shorten=shorten))
    if not user.has_role('Association'):
        flash('This user is not an association.', 'danger')
        return redirect(url_for('main.home'))
    form = JoinForm()
    if form.validate_on_submit():
        current_user.temp_join(user, why_join=form.why_join.data,
            want_to_achieve=form.want_to_achieve.data, willing_to_offer=form.willing_to_offer.data)
        db.session.commit()
        send_email(user.email, 'Join Request', 'email/notify', user=user, member=current_user, join=True)
        flash('Your join request has been sent to %s.' %shorten, 'success')
        return redirect(url_for("posts.user_posts", username=user.username))
    return render_template('account/join_details.html', title=f'{user.shorten}', heading=f'Join {user.shorten}', form=form, name=f' {current_user.f_name} {current_user.l_name} ')

@main.route('/cancel/join_request/<shorten>')
@login_required
@able_to_join
def cancel_join_request(shorten):
    user = User.query.filter_by(shorten=shorten).first()
    if user is None:
        flash('Association does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if current_user.is_a_member(user):
        flash('You already apart of this Association. leave instead', 'info')
        return redirect(url_for("posts.user_posts", username=user.username))
    if not user.has_role('Association'):
        flash('This user is not an association.', 'danger')
        return redirect(url_for('main.home'))
    current_user.temp_leave(user)
    db.session.commit()
    flash('Your join request to %s has been cancelled' %shorten, 'success')
    return redirect(url_for("posts.user_posts", username=user.username))

@main.route('/temporal/members')
@login_required
@role_required('Association')
def temp_members():
    user = User.query.filter_by(shorten=current_user.shorten).first()
    page = request.args.get('page', 1, type=int)
    pagination = user.temp_members.paginate(
        page, per_page=20, error_out=False
    )
    temp_joins = [{'user': item.temp_member, 'timestamp': item.timestamp}
                for item in pagination.items]
    return render_template('account/temp_members.html', user=user, title='Temporal Members of',
                            endpoint='main.temp_members', pagination=pagination, temp_joins=temp_joins)

@main.route('/temporal/members/<username>')
@login_required
@role_required('Association')
def review(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('User does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if not user.temp_is_a_member(current_user):
        flash('unauthorized!! user have not made any join request', 'danger')
        return redirect(url_for('main.home'))
    if user.has_role('Association'):
        flash('This user is an association. Therefore cannot Join', 'danger')
        return redirect(url_for('main.home'))
    member = user.temp_association.filter_by(temp_association_id=current_user.id).first()
    return render_template('account/review.html',user=user, member=member)

@main.route('/accept/<username>')
@login_required
@role_required("Association")
def accept(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('User does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if current_user.is_association_for(user):
        flash('user is already apart of this Association.', 'info')
        return redirect(url_for("posts.user_posts", username=username))
    if user.has_role('Association'):
        flash('This user is an association. Therefore cannot Join', 'danger')
        return redirect(url_for('main.home'))
    if not current_user.temp_is_association_for(user):
        flash('This user has not made any join request to your association. Therefore you cannot accept a join request', 'danger')
        return redirect(url_for('main.home'))
    member = user.temp_association.filter_by(temp_association_id=current_user.id).first()
    user.join(current_user, why_join=member.why_join, want_to_achieve=member.want_to_achieve, willing_to_offer=member.willing_to_offer)
    user.temp_leave(current_user)
    db.session.commit()
    send_email(user.email, 'You are accepted', 'email/notify', user=user, org=current_user, accept=True)
    flash('%s is now a member.' %username, 'success')
    return redirect(url_for("posts.user_posts", username=user.username))
#todo > confirm leave >later

@main.route('/reject/<username>')
@login_required
@role_required("Association")
def reject(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('User does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if not current_user.temp_is_association_for(user):
        flash('You cannot reject a user who have not applied.', 'danger')
        return redirect(url_for('main.home'))
    user.temp_leave(current_user)
    db.session.commit()
    flash('%s has been rejected.' %username, 'success')
    return redirect(url_for("posts.user_posts", username=current_user.username))

@main.route('/make_leave/<username>')
@login_required
@role_required("Association")
def make_leave(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('User does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if not current_user.is_association_for(user):
        flash('user cannot leave this association since user have not joined.', 'danger')
        return redirect(url_for('main.home'))
    user.leave(current_user)
    db.session.commit()
    flash('Now %s is not a member.' %username, 'success')
    return redirect(url_for("posts.user_posts", username=current_user.username))

@main.route('/leave/<shorten>')
@login_required
@able_to_join
def leave(shorten):
    user = User.query.filter_by(shorten=shorten).first()
    if user is None:
        flash('Association does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if not current_user.is_a_member(user):
        flash('You cannot leave an association which you have not joined.', 'danger')
        return redirect(url_for('main.home'))
    current_user.leave(user)
    db.session.commit()
    flash('Now you are not a member of %s.' %shorten, 'success')
    return redirect(url_for("posts.user_posts", username=user.username))

@main.route('/members/<shorten>')
@login_required
def members(shorten):
    user = User.query.filter_by(shorten=shorten).first()
    if user is None:
        flash('Invalid user.', 'danger')
        return redirect(url_for('main.home'))
    if not user.has_role('Association'):
        flash('User is not an association account', 'danger')
        return redirect(url_for('main.home'))
    page = request.args.get('page', 1, type=int)
    pagination = user.members.paginate(
        page, per_page=20, error_out=False
    )
    joins = [{'user': item.member, 'timestamp': item.timestamp}
                for item in pagination.items]
    return render_template('account/joins.html', user=user, title='Members of',
                            endpoint='main.members', pagination=pagination, joins=joins)

@main.route('/active/members/<username>')
@login_required
@role_required('Association')
def view_member(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('User does not exist.', 'danger')
        return redirect(url_for('main.home'))
    if not user.is_a_member(current_user):
        flash('unauthorized!! user is not a member of this Association', 'danger')
        return redirect(url_for('main.home'))
    if user.has_role('Association'):
        flash('This user is an association. Therefore cannot be a member', 'danger')
        return redirect(url_for('main.home'))
    member = user.association.filter_by(association_id=current_user.id).first()
    return render_template('account/review.html',user=user, member=member)

@main.route('/associations/<username>')
@login_required
def associations(username):
    user = User.query.filter_by(username=username).first()
    if user is None:
        flash('Invalid user.', 'danger')
        return redirect(url_for('main.home'))
    if user.has_role('Association'):
        flash('User is an association account. It does not join other associations', 'danger')
        return redirect(url_for('main.home'))
    page = request.args.get('page', 1, type=int)
    pagination = user.association.paginate(
        page, per_page=20, error_out=False
    )
    joins = [{'user': item.association, 'timestamp': item.timestamp}
                for item in pagination.items]
    return render_template('account/joins.html', user=user, title='Associations of',
                            endpoint='main.associations', pagination=pagination, joins=joins)
#!joining section ends

@main.route('/unnotify/<username>')
@login_required
def unnotify(username):
    user = User.query.filter_by(username=username).first()
    #deletes notify message
    if current_user.is_authenticated:
        u = current_user.association.filter_by(association_id=user.id).first()
        if u and u.notify:
            u.notify=''
            db.session.commit()
        u = user.followed.filter_by(followed_id=current_user.id).first()
        if u and u.notify:
            u.notify=''
            db.session.commit()
    return redirect(url_for('posts.user_posts', username=username))


""" Magic Chef """
def main():
    """ Calculate score by condtion """

    while True:
        text = input()

        score = 0

        for char in text:
            if char == " ":
                score += 100
            elif char in "aeiouAEIOU":
                score -= 75
            else:
                score += 50

        if score <= 150:
            print("cheese")
        elif score >= 151 and score <= 350:
            print("chocolate")
        elif score >= 351 and score <= 600:
            print("banana-milk")
        elif score >= 601 and score <= 800:
            print("pee-po")
        elif score >= 801 and score <= 949:
            print("salmon")
        elif score >= 950 and score <= 1000:
            print("YES! CHEF")
            break
        elif score >= 1000:
            print("rainbow")

main()


#Uses python3

import sys
import queue

def bfs(adj, s):
    dist = [-1 for i in range(len(adj))]
    d = queue.Queue()
    for i in s:
        dist[i] = 0
        d.put(i)
    while not d.empty():
        u = d.get()
        for i in range(len(adj[u])):
            if dist[adj[u][i]] == -1:
                 d.put(adj[u][i])
                 dist[adj[u][i]] = dist[u] + 1
    a = set()
    for i in range(len(dist)):
        if dist[i] != -1:
            a.add(i)
    return a

def shortet_paths(adj, cost, s, distance, reachable, shortest):
    #write your code here
    distance[s] = 0

    for i in range(len(adj) - 1):
        flag = 0
        for j in range(len(adj)):
            for k in range(len(adj[j])):
                v = adj[j][k]
                if distance[v] > distance[j] + cost[j][k]:
                    distance[v] = distance[j] + cost[j][k]
                    flag = 1
        if not flag:
            break

    # The additional iteration
    a = set()
    if flag:
        i = len(adj) - 1
        for j in range(len(adj)):
            for k in range(len(adj[j])):
                v = adj[j][k]
                if distance[v] > distance[j] + cost[j][k]:
                    distance[v] = distance[j] + cost[j][k]
                    a.add(v)

    b = bfs(adj, a)

    sets = set()
    sets.add(s)
    c = bfs(adj, sets)

    for i in range(len(adj)):
        if i in c:
            reachable[i] = 1
        if i in b | a:
            shortest[i] = 0
    #pass

if __name__ == '__main__':
    input = sys.stdin.read()
    data = list(map(int, input.split()))
    n, m = data[0:2]
    data = data[2:]
    edges = list(zip(zip(data[0:(3 * m):3], data[1:(3 * m):3]), data[2:(3 * m):3]))
    data = data[3 * m:]
    adj = [[] for _ in range(n)]
    cost = [[] for _ in range(n)]
    for ((a, b), w) in edges:
        adj[a - 1].append(b - 1)
        cost[a - 1].append(w)
    s = data[0]
    s -= 1
    #distance = [10**19] * n
    distance = [float('inf')] * n # NOTE: Not sure if it is needed
    reachable = [0] * n
    shortest = [1] * n
    shortet_paths(adj, cost, s, distance, reachable, shortest)
    for x in range(n):
        if reachable[x] == 0:
            print('*')
        elif shortest[x] == 0:
            print('-')
        else:
            print(distance[x])


n = int(input())
lst = []
ans = []

for i in range(n-9*len(str(n))-1,n):
    lst = list(map(int,str(i)))
    mn = i
    for j in lst:
        mn+=j
    if mn == n:
        ans.append(i)

print(min(ans))


import socket, logging
from threading import Thread
from time import sleep
from evento import Event
logger = logging.getLogger(__name__)

#
# Client
#

def createClientSocket(host='127.0.0.1', port=4445, socketTimeout=0.1):
  logger.info('createClientSocket: {}:{}'.format(host,port))
  s = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

  # non-blocking
  if socketTimeout != None:
    s.settimeout(socketTimeout)

  # connect to server
  errno = s.connect_ex((host, port))

  if errno:
    logger.warn('failed to connect to server: {}'.format(errno))
    return None

  return s

class ClientThread:
  """
  ClientThread manages a thread that creates a socket to connect to
  a server. Once the connection is established, control is passed on to
  the connectionFunc specified in the constructor. When the connectionFunc
  returns, this is considered to be an end of the connection and the thread
  will try to re-establish connection with the server.
  """

  def __init__(self, host='127.0.0.1', port=4445, reconnectDelay=1.0, connectionFunc=None, maxBytes=1024, start=True):
    self.host = host
    self.port = port

    self.reconnectDelay = 0.8
    self.connectionFunc = connectionFunc
    self.maxBytes = maxBytes
    self.threadHandle = None

    self.connectEvent = Event()
    self.disconnectEvent = Event()
    self.connectFailureEvent = Event()
    self.dataEvent = Event()

    self.activeSocket = None
    self.activeConnectionFunc = None
    self.running = False

    if start:
      self.running = True
      self.start()

  def __del__(self):
    self.stop()

  def start(self):
    def threadFunc():
      while self.running:
        if not self.activeSocket:
          s = createClientSocket(self.host, self.port, socketTimeout=None)

          if not s:
            logger.warning('Failed to connect to {}:{}'.format(self.host, self.port))
            self.connectFailureEvent((self.host, self.port))
            sleep(0.5)
            continue

          self.activeSocket = s
          self.connectEvent(s)

        s = self.activeSocket

        # not that we're connected, pass control to the connectionFunc specified in our
        # constructor, or, if no connectionFunc was specifed, run our default connection
        # func which published incoming data usng self.dataEvent
        func = self.connectionFunc if self.connectionFunc else self._defaultConnectionFunc

        # connection func can be a list, if it's not, for simplicity
        # we'll just turn it into a single item list here
        if type(func) != type([]) and type(func) != type(()):
          func = [func]

        for f in func:
          self.activeConnectionFunc = f
          f(s)

        self.activeConnectionFunc = None
        self.activeSocket = None
        self.disconnectEvent(s)

        sleep(self.reconnectDelay)

    self.threadHandle = Thread(target=threadFunc)
    self.threadHandle.start()

  def stop(self, wait=True, tryToStopConnectionFunc=True):
    logger.debug('ClientThread.stop')
    self.running = False

    # BONUS; if we've passed on the control to an external
    # we'll try to see if it has a stop method, and execute
    # it if it does.
    if tryToStopConnectionFunc and self.activeConnectionFunc:
      if 'stop' in dir(self.activeConnectionFunc):
        logger.info('ClientThread found stop method on activeConnectionFunc, attempting to stop it...')
        self.activeConnectionFunc.stop()
        self.activeConnectionFunc = None

    if self.threadHandle and wait:
      self.threadHandle.join()

    self.threadHandle = None

  def _defaultConnectionFunc(self, socket):
    while self.running:
      data = socket.recv(self.maxBytes)

      if not data:
        return

      self.dataEvent(data)

  def getSocket(self):
    return self.activeSocket

class PacketReader:
  """
  Reads packets (header/body) from a socket.
  """

  def __init__(self, numBuffers=2):
    self.buffers = list(map(lambda i: None, range(max(1,numBuffers))))
    self.bufferCursor = 0

  @classmethod
  def receive_bytes(cls, s, size, buffer=None, continueFunc=None, allowGrowBuffer=False):
    # if no buffer specified, we'll just create or own
    if not buffer:
      buffer = bytearray(size)

    # if buffer is too small to hold specified number of bytes
    if len(buffer) < size:
      # either grow it...
      if allowGrowBuffer:
        # grow buffer
        buffer.extend(bytearray(size - len(buffer)))
      # ... or abort
      else:
        # logger.warning('PacketStreamReceiver receive_bytes aborted because buffer was too small ({}) for specified number of bytes to fetch ({})'.format(len(buffer), size))
        # return None, 0

        # simply create new buffer
        buffer = bytearray(size)

    view = memoryview(buffer)
    bytesleft = size

    while continueFunc == None or continueFunc():
      if bytesleft == 0:
        logger.debug('Read all {} bytes'.format(size))
        return buffer, size

      nbytes = s.recv_into(view, bytesleft)

      if not nbytes:
        logger.debug('Connection terminated while reading {} bytes (received: {})'.format(size, size-bytesleft))
        return None, 0

      # logger.debug('Received {} bytes'.format(nbytes))
      view = view[nbytes:]
      bytesleft -= nbytes

    logger.debug('receive_bytes discontinued ({}/{})'.format(size-bytesleft, size))
    return None, 0

  @classmethod
  def parse_int(cls, buf):
    b0 = 0xFF & buf[0]
    b1 = 0xFF & buf[1]
    b2 = 0xFF & buf[2]
    b3 = 0xFF & buf[3]
    return ((b0 << 24) | (b1 << 16) | (b2 << 8) | b3)

  def read(self, socket, onDisconnect=None, continueFunc=None):
    if socket == None:
      if onDisconnect: onDisconnect()
      return None
    # logger.debug('PacketReader.read')

    # select next buffer
    buffer = self.buffers[self.bufferCursor]

    # logger.info('Reading header...')
    # read packet header (body size)
    buffer, size = PacketReader.receive_bytes(socket, 4, buffer, continueFunc)

    if not buffer:
      logger.debug('Connection terminated while reading header')
      if onDisconnect: onDisconnect()
      return None

    body_size = PacketReader.parse_int(buffer)
    logger.debug('Got body size: {}'.format(body_size))

    buffer, size = PacketReader.receive_bytes(socket, body_size, buffer, continueFunc)

    if not buffer:
      logger.debug('Connection terminated while reading body')
      if onDisconnect: onDisconnect()
      return None

    self.buffers[self.bufferCursor] = buffer # receive_bytes method might have allocated a newer bigger buffer
    self.bufferCursor = self.bufferCursor+1 if self.bufferCursor+1 < len(self.buffers) else 0

    return buffer, size

class PacketStreamReceiver:
  """
  Receives packets (header/body pairs) in and endless loop, passing received
  packets to the handler specified in the constructor. Designed to be invoked
  using the receive method inside a ClientThread.
  """

  def __init__(self, handler, numBuffers=2):
    self.reader = PacketReader(numBuffers=numBuffers)
    self.packetHandler = handler
    self.stoppedByOwner = False

  def receive(self, socket):
    self.disconnected = False

    # loop until told to stop
    while not self.stoppedByOwner and not self.disconnected:
      def onDisconnect():
        self.disconnected = True

      packet = self.reader.update(onDisconnect=onDisconnect)

      if packet:
        buffer, size = packet
        self.packetHandler(buffer, size)

  def stop(self):
    logger.debug('PacketStreamReceiver.stop')
    self.stoppedByOwner = True

  __call__ = receive

def readPacketsIntoBuffer(buffer):
  """
  This function returns an instance of PacketStreamReceiver,
  which is callable with a socket param, and can thuse be
  used as 'connectionFunc' option for a SocketClientThread.
  """
  return PacketStreamReceiver(handler=buffer.write_buffer_and_size)

def appendPacketsIntoList(queue):
  def handler(data, size):
    queue.append((data,size))

  return PacketStreamReceiver(handler=handler)

from json import loads as json_loads
from json.decoder import JSONDecodeError

class PacketStreamInfo:
  """
  PacketStreamInfo wraps all logic for sending outgoing,
  and processing incoming packet stream information.
  """

  REQUEST_BODY = b'GET/info.json'

  def __init__(self, data):
    self.info = None

  @classmethod
  def sendRequest(cls, socket):
    # we're sorta faking restfull HTTP interface
    request = PacketStreamInfo.REQUEST_BODY
    return PacketService.sendDataAsPacket(socket, request)

  @classmethod
  def parse(cls, data, size):
    if size > 2048:
      return None

    if len(data) != size:
      data = data[0:size] #memoryview(data)[0:size].bytes

    json = None

    try:
      json = json_loads(data.decode('utf-8'))
    except JSONDecodeError as err:
      return None
    except UnicodeDecodeError as err:
      return None

    # check if there's a valid 'format' attribute in the json
    format = json['format'] if 'format' in json else ''

    if format.startswith('streaminfo-'):
      return json

    return None

#
# Service
#

class SocketConnectionAccepter:
  """
  Creates and manages a socket on which incoming connections are accepted.
  The update() method should be called in the application's main loop,
  to process new incoming connections.
  """

  def __init__(self, port=4445, maxConnections=5, maxPortAttempts=5, socketTimeout=0.2, start=True):
    self.port = port
    self.maxConnections = maxConnections
    self.maxPortAttempts = maxPortAttempts
    self.socketTimeout = socketTimeout

    self.activeSocket = None

    if start:
      self.start()

  def __del__(self):
    self.stop()

  def start(self):
    s = SocketConnectionAccepter.createSocket(self.port, maxConnections=self.maxConnections, maxPortAttempts=self.maxPortAttempts, socketTimeout=self.socketTimeout)

    # failed to create socket, wait and retry?
    if not s:
      logger.warning('Could not create service socket on port: {}'.format(self.port))
      return

    self.activeSocket = s

  def stop(self):
    if self.activeSocket:
      self.activeSocket.close()
      self.activeSocket = None

  def update(self, onConnection=None):
    s = self.activeSocket

    # make sure we have a valid socket
    if not s:
      self.start()

    clientsocket = None
    addr = None

    # accept new connection on socket (non-blocking)
    try:
      clientsocket, addr = s.accept()
    except socket.timeout:
      clientsocket = None
      return
    except BlockingIOError as err:
      clientsocket = None
      logging.warn('Accept error: {}'.format(err.errno))

    # notify caller about any new connections
    if clientsocket and addr:
      if onConnection:
        onConnection(clientsocket, addr)
      else:
        logger.warn('Unhandled new socket connection')
        clientsocket.close()

  @classmethod
  def createSocket(cls, portNum, maxConnections, maxPortAttempts=1, socketTimeout=None):
    sock = None

    try:
      sock = socket.socket(
        socket.AF_INET, #Internet
        # socket.SOCK_DGRAM #UDP
        socket.SOCK_STREAM)
    except OSError as msg:
      return None

    attempt = 0
    while attempt < maxPortAttempts:
      try:
        host = ''
        port = portNum + attempt
        logger.info('Trying to bind server socket to: {}:{}...'.format(host, port))

        #Bind to any available address on port *portNum*
        sock.bind((host, port))

        sock.listen(maxConnections)

        #Prevent the socket from blocking until it receives all the data it wants
        #Note: Instead of blocking, it will throw a socket.error exception if it
        #doesn't get any data
        # sock.setblocking(0)
        if socketTimeout:
          sock.settimeout(socketTimeout)

      except OSError as msg:
        # sock.close()
        logging.warn('binding socket failed')
        attempt += 1
        continue

      logging.warn('socket bound to {}'.format(portNum+attempt))
      return sock

    logger.warn("Failed to bind server thread socket after {} port attempt(s)".format(maxPortAttempts))
    return None

class ServerThread:
  """
  Manages a thread that runs a socket connection accepter
  """

  def __init__(self, port=4445, start=True, connectionHandler=None, maxConnections=1, maxPortAttempts=5, socketTimeout=0.5, idleFunc=None):
    self.connectionAccepter = SocketConnectionAccepter(port=port, maxConnections=maxConnections, maxPortAttempts=maxPortAttempts, socketTimeout=socketTimeout, start=start)
    self.idleFunc = idleFunc

    self.connectionHandler = connectionHandler
    self.threadHandle = None
    self.running = False

    if start:
      self.start()

  def start(self):
    def threadFunc():
      self.connectionAccepter.start()

      while self.running:
        self.connectionAccepter.update(onConnection=self.handleConnection)
        sleep(0.2)

      self.connectionAccepted.stop()

    self.running = True
    self.threadHandle = Thread(target=threadFunc)
    self.threadHandle.start()

  def stop(self, wait=True):
    self.running = False

    if self.threadHandle and wait:
      self.threadHandle.join()

    self.threadHandle = None

  def handleConnection(self, socket, addr):
    if self.connectionHandler:
      self.connectionHandler(socket, addr)
      return

    logger.warn("ServerThread has not connection handler")

class PacketConsumer:
  """
  PacketConsumer represents a single consumer
  connected via sockets. It provides an interface
  to receive data (non-blocking) from the specified consumer.
  """
  def __init__(self, socket, addr, timeout=0.2):
    self.socket = socket
    self.addr = addr

    self.socket.settimeout(timeout)

  def __del__(self):
    self.close()

  def close(self):
    self.socket.close()

  def recv(self):
    # logger.info('Reading header...')
    # read packet header (body size)
    try:
      data = self.socket.recv(4)
    except socket.timeout:
      return None
    except ConnectionResetError:
      return False

    if len(data) != 4:
      return None

    body_size = PacketReader.parse_int(data)
    logger.debug('consumer got package size: {}'.format(body_size))

    data = self.socket.recv(body_size)
    logger.debug('consumer received packet data ({} bytes): {}'.format(len(data), data))

    return data

from json import dumps as json_dumps

class PacketService:
  """
  PacketService Manages a list of connected consumers.
  and sends packets (header/body) of data to them
  """

  DEFAULT_INFO = {'format':'streaminfo-0.0.1'}

  def __init__(self, serviceId, port=4445, start=True, infoData=DEFAULT_INFO):
    self.serviceId = serviceId # will be used (WIP) for announcing present of service via broadcasts
    self.port = port
    self.infoData = infoData
    self.consumers = []

    if not 'format' in self.infoData:
      data = {}
      data.update(PacketService.DEFAULT_INFO)
      data.update(self.infoData)
      self.infoData = data

    if start:
      self.start()

  def __del__(self):
    self.stop()

  def start(self):
    pass

  def stop(self):
    self.consumers.clear()

  def update(self):
    self.processIncomingConsumerData()

  def submit(self, buffer, size):
    logger.debug('PacketService.submit, to {} consumers'.format(len(self.consumers)))

    body = memoryview(buffer)[0:size]

    failures = []

    consumers = self.consumers.copy()

    for c in consumers:
      if not PacketService.sendDataAsPacket(c.socket, body, size=size):
        failures.append(c)

    for f in failures:
      self.onDisconnect(c)

  @classmethod
  def sendDataAsPacket(cls, socket, data, size=None):
    if size == None:
      size = len(data)

    if size != len(data):
      data = memoryview(data)[0:size]

    # create 4-byte unsigned int header with body size
    header = bytearray(4)
    header[0] = (size >> 24) & 0xFF
    header[1] = (size >> 16) & 0xFF
    header[2] = (size >> 8) & 0xFF
    header[3] = (size >> 0) & 0xFF

    try:
      socket.sendall(header)
      socket.sendall(data)
    except OSError:
      return False

    return True

  def addConsumerSocket(self, socket, addr):
    self.consumers.append(PacketConsumer(socket, addr))

  def onDisconnect(self, consumer):
    self.consumers.remove(consumer)
    logger.debug('Client disconnected, {} connected consumers left'.format(len(self.consumers)))

  def processIncomingConsumerData(self):
    """
    Polls all connected consumer sockets for
    incoming data and responds if necessary
    """
    cs = self.consumers.copy()
    for c in cs:
      data = c.recv()

      if data == False:
        self.onDisconnect(c)
        continue

      if data == PacketStreamInfo.REQUEST_BODY:
        info = self.infoData
        response = json_dumps(info).encode('utf-8')

        if PacketService.sendDataAsPacket(c.socket, response):
          logger.info('Sent stream info request response to consumer ({} bytes): {}'.format(len(response), response))
        else:
          logger.warn('Failed to send stream info')


import platform
import os
import sys

# Set console colors
class colors:
    OKCYAN = '\033[96m'
    FAIL = '\033[91m'
    ENDC = '\033[0m'
    BOLD = '\033[1m'

def log_info(msg):
    print(colors.BOLD + colors.OKCYAN + "==> " + msg + colors.ENDC)

def log_fail(msg):
    print(colors.FAIL + msg + colors.ENDC)

def get_os():
    return platform.system()

def generate_project(generator, config, build_path):
    log_info(f"Generating {generator} project files...")

    # Generate project files
    if os.system(f"cmake -S . -B {build_path} -G \"{generator}\" -DCMAKE_BUILD_TYPE={config}") != 0:
        log_fail("Failed to generate project files.")
        exit()

    log_info("Successfully generated project files.")
    log_info("Building project...")

    # Build project
    if os.system(f"cmake --build {build_path} --config {config}") != 0:
        log_fail("Failed to build project.")
        exit()

    log_info("Build successful.")

    print("You can now launch Nightly with: python3 ./LaunchEditor.py")

def install_deps(os_name, deps, install_cmd, update_cmd):
    log_info(f"Installing dependencies for {os_name}...")
    print("Updating packages...")

    # Update packages
    if os.system(update_cmd) != 0:
        log_fail("Failed to update packages! Check your internet connection or try again later.")
        exit()

    failed_packages = []

    # Loop through all packages and install them
    for package in deps:
        log_info(f"Installing {package}...")
        if os.system(f"{install_cmd} " + package) != 0:
            log_fail(f"Failed to install package: {package}. Check your internet connection or try again later.")
            failed_packages.append(package)

    # Print failed packages if there are any
    if len(failed_packages) != 0:
        print(f"Failed to install {len(failed_packages)} packages: ")
        for pkg in failed_packages:
            print(pkg)

    log_info("Installation finished.")

def prompt_configuration():
    config = input("Which configuration? [Debug/Release]: ")
    if config != "Debug" and config != "Release":
        log_fail("Unknown configuration. Valid configurations: Debug, Release")
        return ""

    return config


from benchmark.SolverBenchmark import SolverBenchmark
from env.DepthFirstMazeEnv import DepthFirstMazeEnv
from solver.AStarMazeSolver import AStarMazeSolver
from solver.BestFirstMazeSolver import BestFirstMazeSolver
from solver.BodyRandomMazeSolver import BodyRandomMazeSolver
from solver.HeadRandomMazeSolver import HeadRandomMazeSolver
from solver.WallFollowerMazeSolver import WallFollowerMazeSolver
from env.KruskalMazeEnv import KruskalMazeEnv

import threading

class MyThread(threading.Thread):

    def __init__(self, env_list: list, solver_list: list, env_size_list: list, id: int):
        threading.Thread.__init__(self)
        self.env_list = env_list
        self.solver_list = solver_list
        self.env_size_list = env_size_list
        self.id = id

    def run(self) -> None:
        SolverBenchmark(self.env_list, self.solver_list, 40, self.env_size_list) \
            .get_metadata() \
            .to_csv('output' + str(self.id) + '.csv')

if __name__ == '__main__':
    env_list_1 = list()
    env_1 = DepthFirstMazeEnv()
    env_list_1.append(env_1)
    env_1 = KruskalMazeEnv()
    env_list_1.append(env_1)

    solver_list_1 = list()
    solver_list_1.append(AStarMazeSolver())
    solver_list_1.append(BestFirstMazeSolver())
    solver_list_1.append(BodyRandomMazeSolver())
    solver_list_1.append(HeadRandomMazeSolver())
    solver_list_1.append(WallFollowerMazeSolver())

    env_size_list_1 = list()
    env_size_list_1.append((8, 8))
    env_size_list_1.append((16, 16))
    env_size_list_1.append((32, 32))
    thread_1 = MyThread(env_list_1, solver_list_1, env_size_list_1, 1)
    thread_1.start()

    ########
    env_list_2 = list()
    env_2 = DepthFirstMazeEnv()
    env_list_2.append(env_2)
    env_2 = KruskalMazeEnv()
    env_list_2.append(env_2)

    solver_list_2 = list()
    solver_list_2.append(AStarMazeSolver())
    solver_list_2.append(BestFirstMazeSolver())
    solver_list_2.append(BodyRandomMazeSolver())
    solver_list_2.append(HeadRandomMazeSolver())
    solver_list_2.append(WallFollowerMazeSolver())

    env_size_list_2 = list()
    env_size_list_2.append((64, 64))
    thread_2 = MyThread(env_list_2, solver_list_2, env_size_list_2, 2)
    thread_2.start()
    ##############

    ########
    env_list_3 = list()
    env_3 = DepthFirstMazeEnv()
    env_list_3.append(env_3)
    env_3 = KruskalMazeEnv()
    env_list_3.append(env_3)

    solver_list_3 = list()
    solver_list_3.append(AStarMazeSolver())
    solver_list_3.append(BestFirstMazeSolver())
    solver_list_3.append(BodyRandomMazeSolver())
    solver_list_3.append(HeadRandomMazeSolver())
    solver_list_3.append(WallFollowerMazeSolver())

    env_size_list_3 = list()
    env_size_list_3.append((64, 64))
    thread_3 = MyThread(env_list_3, solver_list_3, env_size_list_3, 3)
    thread_3.start()
    ##############

    ########
    env_list_4 = list()
    env_4 = DepthFirstMazeEnv()
    env_list_4.append(env_4)
    env_4 = KruskalMazeEnv()
    env_list_4.append(env_4)

    solver_list_4 = list()
    solver_list_4.append(AStarMazeSolver())
    solver_list_4.append(BestFirstMazeSolver())
    solver_list_4.append(BodyRandomMazeSolver())
    solver_list_4.append(HeadRandomMazeSolver())
    solver_list_4.append(WallFollowerMazeSolver())

    env_size_list_4 = list()
    env_size_list_4.append((128, 128))
    thread_4 = MyThread(env_list_4, solver_list_4, env_size_list_4, 4)
    thread_4.start()
    ##############

    print("waiting for thread_1 ...")
    thread_1.join()
    print("waiting for thread_2 ...")
    thread_2.join()
    print("waiting for thread_3 ...")
    thread_3.join()
    print("waiting for thread_4 ...")
    thread_4.join()
    print("finished")


# -*- coding: utf-8 -*-
from __future__ import unicode_literals

from django.db import models, migrations

class Migration(migrations.Migration):

    dependencies = [
        ('tuanapp', '0004_tuan_crt_num'),
    ]

    operations = [
        migrations.AddField(
            model_name='tuan',
            name='progress',
            field=models.CharField(default=0, max_length=30),
        ),
    ]


import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(__file__) + "/../util"))
sys.path.append(os.path.dirname(os.path.dirname(__file__) + "/../game"))

from pyglet.window import Window, key
from pyglet import app, shapes
import pyglet
from util.constant import *
from game.entity.goblin import Goblin, State

window = Window(width=screen_width, height=screen_height)
window.set_location(2250, 150)
batch = pyglet.graphics.Batch()
fps_display = pyglet.window.FPSDisplay(window=window)

goblin = Goblin(batch)

keys = key.KeyStateHandler()
window.push_handlers(keys)

goblin.speed = 400.0

def update(dt):
    if keys[key.D]:
        goblin.move_right(goblin.speed * dt)
    elif keys[key.A]:
        goblin.move_left(goblin.speed * dt)
    else:
        goblin.set_state(state=State.IDLE)

pyglet.clock.schedule_interval(update, 1/60.0)

@window.event
def on_draw():
    window.clear();
    batch.draw();
    fps_display.draw()

app.run()


class CaixaEletronico:

    def __init__(self, nome):
        self.notas = [100, 50, 20, 10]
        self.nome_banco = nome

    def sacar(self,valor_saque):
       #valor_maximo = 1000
        valor = valor_saque
        resto = -1
        notas_entregues = []
        #if valor > valor_maximo:
          #print ("saldo insuficiente")

        for valor_nota in self.notas:
            qtd_notas = valor // valor_nota
            resto = valor % valor_nota
            valor = resto

        if qtd_notas > 0:
            notas_entregues.append(f'\n{qtd_notas} NOTAS DE R$ {valor_nota},00 REAIS')

    def imprimir_resultado(self, notas_entregues):
        print(', '.join(notas_entregues))

if __name__ == '__main__':
    caixa_eletronico = CaixaEletronico('Ultima Bank')
    valor = int(input('Valor do saque: '))
    caixa_eletronico.sacar(valor)


"""Meetup models"""

import datetime

from datetime import date

""" Initialize list to hold all meetups"""
Meetups = []
RSVPS =[]

"""Class model for storing meetups data"""

class MeetupsModel():
	"""Meetup instance variable"""
	def __init__(self,topic,details,location,happeningOn,Tags):
		self.topic=topic
		self.details=details
		self.location=location

		self.Tags=Tags
		createdOn=date.today()
		createdOn=createdOn.strftime("%d/%m/%Y")
		#happeningOn=date.today() + datetime.timedelta(days=7)
		self.happeningOn=happeningOn

		self.createdOn=createdOn
		'''self.images=images'''
		self.m_id=len(Meetups)+1
	def create_meetup(self):
		meetup = {
			"m_id":self.m_id,
			"topic":self.topic,
			"details":self.details,
			"createdOn":self.createdOn,
			"location":self.location,
			"happeningOn":self.happeningOn,

			"Tags":self.Tags

		}
		Meetups.append(meetup)
		return Meetups
	@classmethod
	def get_meetup(cls, m_id):
		"""check if meetup id is greater than length of dict"""

		if m_id>len(Meetups):
			return "The selected meetup record was not found"
		meetup = Meetups[m_id-1]
		meetup1 = {"m_id" : meetup["m_id"],"topic":meetup["topic"],"details":meetup["details"],"createdOn":meetup["createdOn"],"location":meetup["location"],

		"happeningOn":meetup["happeningOn"],"Tags":meetup["Tags"] }
		return meetup1

	@classmethod
	def get_upcoming(cls):
		"""check if there are any meetups"""
		if len(Meetups) == 0:
			return "No meetups"

		return Meetups

class MeetUpRsvps():

	@classmethod
	def add_rsvps(cls, m_id):
		if m_id<=len(Meetups):
			m_ide = Meetups[m_id-1]
			meetup1 = {"m_id" : m_ide["m_id"],
				"topic":m_ide["topic"]
				}
			return meetup1
		return "The selected meetup record was not found"


from django.core.exceptions import ValidationError
from django.shortcuts import render
from .models import Person
from rest_framework import serializers
from rest_framework import generics, status
from rest_framework.response import Response
from django.http import JsonResponse, HttpResponse
from rest_framework.decorators import api_view

# The model serializer class
class PersonSerializer(serializers.ModelSerializer):
    class Meta:
        model = Person
        fields = '__all__'

    def validate_fullname(self, value):
        # Check if the fullname contains only alphabetic characters and spaces
        if not value.replace(' ', '').isalpha():
            raise serializers.ValidationError(
                "Fullname should only contain letters and spaces.")
        return value

    def validate_gender(self, value):
        # Check if the gender is one of the allowed choices ('Male' or 'Female')
        if value not in ['Male', 'Female']:
            raise serializers.ValidationError(
                "Gender should be 'Male' or 'Female'.")
        return value

@api_view(['GET', 'POST'])
def apiview(request):
    if request.method == 'GET':
        if 'fullname' in request.GET:
            fullname = request.GET['fullname']
            data = Person.objects.filter(fullname=fullname)
            if not data:
                data = Person.objects.all()
                data = PersonSerializer(data, many=True)
                return Response(data.data, status=status.HTTP_204_NO_CONTENT)
            data = PersonSerializer(data, many=True)
            return Response(data.data, status=status.HTTP_200_OK)
        elif 'id' in request.GET:
            id = request.GET['id']
            data = Person.objects.filter(id=id)
            if not data:
                data = Person.objects.all()
                data = PersonSerializer(data, many=True)
                return Response(data.data, status=status.HTTP_204_NO_CONTENT)
            data = PersonSerializer(data, many=True)
            return Response(data.data, status=status.HTTP_200_OK)
        elif request.GET:
            data = Person.objects.all()
            data = PersonSerializer(data, many=True)
            return Response(data.data, status.HTTP_501_NOT_IMPLEMENTED)
        else:
            data = Person.objects.all()
            data = PersonSerializer(data, many=True)
            return Response(data.data)
    if request.method == 'POST':
        data = PersonSerializer(data=request.data)
        if data.is_valid():
            data.save()
            return Response(data.data, status=status.HTTP_201_CREATED)
        return Response({}, status.HTTP_400_BAD_REQUEST)

""" class PersonList(generics.ListCreateAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer """

class PersonDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = Person.objects.all()
    serializer_class = PersonSerializer


from flask import Flask
from flask import request
from flask import render_template
from flask_cors import CORS
import json
import sys
import pandas as pd
import numpy as np

app = Flask(__name__)

heatmap = pd.read_csv("heatmap.csv",header=None)
events = pd.read_csv("events.csv")

CORS(app,resources={r"/*": {"origins": "*"}})

@app.route("/")
def helloWorld():
  return "Hello, cross-origin-world!"

@app.route('/api', methods=['POST'])
def hello_python():
        js = request.get_json();
        #print(js, file=sys.stderr)
        #### do stuff
        for i in js['heatmap']:
          heatmap.iloc[round(i['y']/10),round(i['x']/10)] += 1
        for i in js['click']:
          events[i['element']][0]+=1
        events['scroll'][0]+=js['scroll']
        heatmap.to_csv("heatmap.csv",header=False,index=False)
        events.to_csv("events.csv",index=False)
        return "success"

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


# Simulador de Maquina de Turing ver 1.0.
# Desenvolvido como trabalho pratico para a disciplina de Teoria da Computacao.
# Python v.3.6.4
# Pedro Henrique Oliveira Veloso - 0002346, IFMG, 2018.

from pathlib import Path;
from TuringMachine import TM;

class Parser:

    def __init__(self, data):
        self.data      = data;
        self.inputFile = None;
        self.mt        = None;

    def parseProgramInput(self):
        """
            Recebe a linha de comando da execucao do programa,
            retorna uma array [modo de execucao, n.o de steps, delimitador],
            ou retorna None caso a linha esteja mal formatada.
        """
        # Precisa de ao menos duas entradas,
        # nome do programa e arquivo a ser simulado:
        if(len(self.data) < 2):
            print("\n> Argumentos insuficientes.");
            return None;

        # Checa se o arquivo informado existe:
        self.inputFile = self.data[len(self.data) - 1];
        filepath = Path(self.inputFile);
        if(filepath.is_file() == False):
            print("\n> Arquivo de entrada invalido.");
            return None;

        # Cria a array de configuracao:
        config = [TM.MODE_RESUME, TM.MAX_STEPS, TM.STD_DELIM];

        # Checa se o formato dos
        # demais argumentos e valido:
        foundHead  = False;
        foundSteps = False;
        for i in range(1, len(self.data) - 1):
            arg = self.data[i];

            if(((arg == "-resume") or (arg == "-r")) and (foundHead == False) and (foundSteps == False)):
                config[0] = TM.MODE_RESUME;
            elif(((arg == "-verbose") or (arg == "-v")) and (foundHead == False) and (foundSteps == False)):
                config[0] = TM.MODE_VERBOSE;
            elif(((arg == "-head") or (arg == "-h"))  and (foundHead == False) and (foundSteps == False)):
                foundHead = True;
            elif(((arg == "-step") or (arg == "-s"))  and (foundHead == False) and (foundSteps == False)):
                foundSteps = True;
            elif (foundSteps == True):
                config[1] = int(arg);
                foundSteps = False;
            elif (foundHead == True):
                delim = self.checkHead(arg);
                if(delim == None):
                    print("\nDelimitador " + arg + " invalido.");
                    return None;

                config[2] = delim;
                foundHead = False;

        return config;

    def checkHead(self, head):
        # Delimitador deve possuir exatamente dois caracteres:
        if(len(head) != 2):
            return None;

        return [head[0], head[1]];

    def parseInputFile(self, machine):
        """
            Le o arquivo de entrada e o interpreta,
            criando os blocos, estados, transicoes
            e o alfabeto que define a Maquina de Turing.
        """

        currentBlock = "";

        try:
            with open(self.inputFile) as file:
                # Itera sobre cada linha do arquivo:
                for fLine in file:

                    # Divide a linha em um vetor:
                    line = fLine.split();

                    # Comentario ou linha vazia:
                    if((len(line) == 0) or (line[0] == ';')):
                        continue;
                    # Inicio de um novo bloco:
                    elif(line[0] == 'bloco'):
                        currentBlock = self.addBlock(machine, line);
                    # Fim do bloco:
                    elif(line[0] == 'fim'):
                        continue;
                    # Transicao:
                    else:
                        self.addTransition(machine, line, currentBlock);

        except IOError:
            print("Erro ao abrir arquivo.\n");

    def addBlock(self, machine, line):
        name    = self.formatBlockName(line[1]);
        initial = self.formatState(line[2]);
        machine.addBlock(name, initial);
        return name;

    def addTransition(self, machine, line, block):
        """ Adiciona uma nova transicao a maquina. """

        # Transicao a ser adicionada
        # esta dentro do bloco atual
        if(line[2] == "--"):
            source = self.formatState(line[0]);
            read   = line[1];
            write  = line[3];
            mov    = line[4];
            target = self.formatState(line[5]);

            pausa = False;
            if(len(line) > 6):
                stopSign = line[6];
                if(stopSign == "!"):
                    pausa = True;

            machine.addTransition(source, target, read, write, mov, block, pausa);
        else:
            initial = self.formatState(line[0]);
            target  = self.formatBlockName(line[1]);
            returnS = self.formatState(line[2]);
            machine.addBlockTransition(initial, returnS, block, target);
        return;

    def formatState(self, state):

        if(state == "pare"):
            return "pare";
        elif(state == "retorne"):
            return "retorne";

        try:
            intState = int(state);
            strState = str(intState);
            for i in range(0, (4 - len(strState))):
                strState = "0" + strState;

            return strState;
        except ValueError:
            print("> (" + state + "). Nome de estado invalido. Estados devem ser numeros inteiros.");

        return;

    def formatBlockName(self, name):

        for i in range(0, (16 - len(name))):
            name = "." + name;

        return name;

    def prompt(self, iniConfig):
        print("\nAperte enter para continuar ou configure a maquina.");
        print("-v ou -verbose\n\tExecuta a maquina de turing e exibe cada passo." +
            "\n-r ou -resume\n\tExecuta a maquina de turing e exibe apenas o resultado." +
            "\n-s # ou -step #\n\tInforma o numero maximo de passos possivel." +
            "\n-h '[]' ou -head '[]'\n\tAltera o caractere delimitador do cabecote da fita.");
        line = input("Config: ");
        print("");
        aLine = line.split();
        config = [iniConfig[0], iniConfig[1], iniConfig[2]];

        foundSteps = False;
        foundHead  = False;
        for i in range(0, len(aLine)):
            arg = aLine[i];

            if(((arg == "-resume") or (arg == "-r")) and (foundHead == False) and (foundSteps == False)):
                config[0] = TM.MODE_RESUME;
            elif(((arg == "-verbose") or (arg == "-v")) and (foundHead == False) and (foundSteps == False)):
                config[0] = TM.MODE_VERBOSE;
            elif(((arg == "-head") or (arg == "-h"))  and (foundHead == False) and (foundSteps == False)):
                foundHead = True;
            elif(((arg == "-step") or (arg == "-s"))  and (foundHead == False) and (foundSteps == False)):
                foundSteps = True;
            elif (foundSteps == True):
                config[1] = int(arg);
                foundSteps = False;
            elif (foundHead == True):
                delim = [arg[1], arg[2]];
                config[2] = delim;
                foundHead = False;

        return config;


from typing import *
import numpy as np
from sklearn.linear_model import LinearRegression
from scipy.optimize import brentq

from src.data_source import PriceData
from src.option_pricing.black_scholes import price_BS
from src.option_pricing.smile import Smile
from src.utils import Uniform

def implied_vol(price: float, K: float, T: float, S0: float, r: float, ignore_warning=False) -> float:
    """Given a price, compute the vol sigma in BS formula that matches this price."""
    a, b = 1e-6, 10.0
    func = lambda s: price_BS(K, T, s, S0, r) - price

    if func(a) > 0 or func(b) < 0:
        if not ignore_warning:
            print("WARNING. Implied_vol solver could not find a solution.")
        return a if func(a) > 0 else b
    return brentq(func, a, b)

class HMCPricer:
    def __init__(self,
                 M: int,
                 ave: Optional = None,
                 detrend: Optional[bool] = False,
                 K_bounds: Optional[list] = None,
                 basis_func_method: str = 'piecewise_quadratic'):
        self.ave = ave or Uniform()
        self.M = M
        self.detrend = detrend
        self.K_bounds = K_bounds
        self.basis_func_method = basis_func_method

    def reset_Ks(self, dt: int) -> None:
        if self.K_bounds is not None:
            Ka, Kb = self.K_bounds
            self.Ks = np.linspace(Ka, Kb, self.M) + 10
        else:
            M_max = 8.0
            Ms = np.linspace(-M_max, M_max, self.M)
            self.Ks = Smile.from_M_to_K(0.2, dt, Ms)

    def get_price(self, x: np.ndarray) -> np.ndarray:
        """ Get prices C(xs). """
        if self.basis_func_method != 'piecewise_quadratic':
            raise ValueError("Unkwnown basis function method in HMC")

        return np.stack([(x > k) * (x - k) ** 2 for k in self.Ks])

    def get_hedge(self, x: np.ndarray) -> np.ndarray:
        """ Get hedges phi(xs). """
        if self.basis_func_method != 'piecewise_quadratic':
            raise ValueError("Unkwnown basis function method in HMC")

        return np.stack([2 * (x > k) * (x - k) for k in self.Ks])

    def perform_iteration(self,
                          it: int,
                          x_curr: np.ndarray,
                          x_prev: np.ndarray,
                          discount: float,
                          param_curr: Optional[np.ndarray],
                          price_curr: Optional[np.ndarray]
                          ) -> np.ndarray:
        """ From param_curr get param_prev. They are obtained by linear regression of price_curr
        against previous price + previous hedge. """
        if param_curr is None:
            Y = price_curr  # C_T(x)
        elif price_curr is None:
            Y = param_curr @ self.get_price(x_curr)  # C_{k+1}(x_{k+1})
        else:
            raise ValueError("Step in HMC requires price_curr or param_curr")

        self.reset_Ks(it)

        # C_k + phi_k (x_{k+1} - x_k)
        X = discount * self.get_price(x_prev) + self.get_hedge(x_prev) * (discount * x_curr - x_prev)[None, :]

        regr = LinearRegression(fit_intercept=False)
        regr.fit(X.T, Y)
        param_prev = regr.coef_

        # param_test = scipy.linalg.pinv(X @ X.T) @ X @ Y

        return param_prev

    def price(self,
              x: np.ndarray,
              strike: float
              ) -> Tuple[float, Callable, Callable]:
        """ Price a European Call option given several price paths X.

        Keyword arguments:
        X -- array of shape B x (N+1)
        """
        N = x.shape[-1] - 1

        y = x
        if self.detrend:
            dlny = np.diff(np.log(x), axis=-1)
            dlny -= dlny.mean(0, keepdims=True)
            y = PriceData(dlnx=dlny, x_init=100.0).x

        # iterate backward to determine current params in Longstaff-Schwartz
        price_curr = (y[:, -1] - strike) * (y[:, -1] > strike)
        param_curr = None
        it = 1
        for n in np.arange(1, N + 1)[::-1]:
            #             discount = np.exp(-r * T / N)  # TODO. Verify the discount, T / N or N / T ?
            discount = 1.0
            param_curr = self.perform_iteration(it, y[:, n], y[:, n - 1], discount, param_curr, price_curr if n == N else None)
            it += 1

        # go back to the price
        prices = param_curr @ self.get_price(y[:, 0])

        return self.ave.avg(prices, axis=-1), lambda x: param_curr @ self.get_price(x), lambda x: param_curr @ self.get_hedge(x)


import gym
import numpy as np
import torch.nn as nn
import torch
import random
import matplotlib.pyplot as plt
import time

def set_seed(seed):
    torch.manual_seed(seed)
    torch.cuda.manual_seed(seed)
    torch.backends.cudnn.deterministic=True
    torch.backends.cudnn.benchmark=False
    np.random.seed(seed)
    random.seed(seed)

# 建立一个网络
class Network(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(Network, self).__init__()

        self.layer1 = nn.Sequential(
            nn.Linear(input_dim, 32),
            nn.ReLU(),
            nn.Linear(32,64),
            nn.ReLU(),
            nn.Linear(64, output_dim),

        )

    def forward(self, input):
        output = self.layer1(input)
        return output

class DuelingNet(nn.Module):
    def __init__(self, input_dim, output_dim):
        super(DuelingNet, self).__init__()

        self.layer1 = nn.Sequential(
            nn.Linear(input_dim, 32),
            nn.ReLU(),
            nn.Linear(32,64),
            nn.ReLU(),
        )
        self.layer_a = nn.Sequential(
            nn.Linear(64,output_dim)
        )
        self.layer_v = nn.Sequential(
            nn.Linear(64,1)
        )

    def forward(self, input):
        output1 = self.layer1(input)
        output_a = self.layer_a(output1)
        output_v = self.layer_v(output1)
        output = output_v + (output_a - torch.mean(output_a))
        return output

# 建立DQN网络

class DQN(object):
    def __init__(self, input_dim,  output_dim, NetworkType):

        self.input_dim = input_dim
        self.output_dim = output_dim
        self.NetworkType = NetworkType  # 是否使用double DQN
        # replay buffer 设置
        self.BufferSize = 10000
        self.replayBuffer = np.zeros(self.BufferSize, dtype=object)
        self.currentpointer = 0  # 记录当前可以存放的位置
        self.batchSize = 1024
        # gpu
        self.device = torch.device("cuda:0" if torch.cuda.is_available() else "cpu")
        # 创建两个网络
        self.evalNet = None
        self.targetNet = None
        self.buildNetwork()

        self.targetNet.load_state_dict(self.evalNet.state_dict())
        self.loss_fun = torch.nn.MSELoss().to(self.device)
        self.optimizer = torch.optim.Adam(self.evalNet.parameters(), lr=0.0001)

        self.updateinterval = 20  # 每训练30次更新一次目标网络
        self.epsilon = 1  # 探索的概率
        self.gamma = 0.99
        self.learntimes = 0

    def buildNetwork(self):

        #DQN or double DQN
        if self.NetworkType == 0 or self.NetworkType == 1:
            self.evalNet = Network(self.input_dim, self.output_dim).to(self.device)
            self.targetNet = Network(self.input_dim, self.output_dim).to(self.device)

        # Dueling DQN or Dueling double DQN
        elif self.NetworkType == 2 or self.NetworkType == 3:
            self.evalNet = DuelingNet(self.input_dim, self.output_dim).to(self.device)
            self.targetNet = DuelingNet(self.input_dim, self.output_dim).to(self.device)

    # 根据状态获取下一个动作, 带探索的 或者 贪心的
    def get_action(self, obs, mode):

        obs = torch.from_numpy(np.array(obs)).float().to(self.device)
        q_val = self.targetNet(obs).detach().cpu().numpy()
        if mode:
            if random.random() < self.epsilon:
                return np.random.randint(self.output_dim)
            else:
                return np.argmax(q_val)
        else:
            return np.argmax(q_val)

     # 将数据存存入经验池 tans = (s, a, r, s1, d)
    def push_transition(self, trans):
        self.currentpointer = self.currentpointer % self.BufferSize
        self.replayBuffer[self.currentpointer] = trans
        self.currentpointer += 1

    # 从经验池中取出一个batch
    def get_traindata(self):
        Batch = np.random.choice(self.replayBuffer, self.batchSize, replace=False)
        obsBatch = []
        actionBatch = []
        rewardBatch = []
        nextObsBatch = []
        doneBatch = []

        for i in range(self.batchSize):
            obsBatch.append(Batch[i][0])
            actionBatch.append(Batch[i][1])
            rewardBatch.append(Batch[i][2])
            nextObsBatch.append(Batch[i][3])
            doneBatch.append(Batch[i][4])

        obsBatch = np.array(obsBatch)
        actionBatch = np.array(actionBatch)
        rewardBatch = np.array(rewardBatch)
        nextObsBatch = np.array(nextObsBatch)
        doneBatch = np.array(doneBatch)

        return obsBatch, actionBatch, rewardBatch, nextObsBatch, doneBatch

    def train(self):

        # 获取一批数据
        obsBatch, actionBatch, rewardBatch, nextObsBatch, doneBatch = self.get_traindata()
        obsBatch = torch.from_numpy(obsBatch).float().view(-1, 2).to(self.device)
        actionBatch = torch.LongTensor(actionBatch).view(-1, 1).to(self.device)
        rewardBatch = torch.from_numpy(rewardBatch).view(-1, 1).to(self.device)
        nextObsBatch = torch.from_numpy(nextObsBatch).float().view(-1, 2).to(self.device)
        doneBatch = torch.from_numpy(doneBatch).view(-1, 1).to(self.device)

        q_eval = self.evalNet(obsBatch).gather(1, actionBatch)
        q_next = self.targetNet(nextObsBatch).detach()

        # double DQN or dueling double DQN
        if self.NetworkType == 1 or self.NetworkType == 3:
            q_next_eval = self.evalNet(nextObsBatch).detach()
            max_action_indx = q_next_eval.max(1)[1].view(-1,1)
            q_target = (rewardBatch + (torch.logical_not(doneBatch))*self.gamma*q_next.gather(1, max_action_indx).view(-1, 1)).float()

        # Dueling DQN or DQN
        else:
            q_target = (rewardBatch + (torch.logical_not(doneBatch))*self.gamma*(q_next.max(1)[0]).view(-1, 1)).float()

        loss = self.loss_fun(q_eval, q_target)
        self.optimizer.zero_grad()
        loss.backward()
        self.optimizer.step()

        if self.learntimes % self.updateinterval == 0:
            #self.targetNet.load_state_dict(self.evalNet.state_dict())
            for target_param, param in zip(self.targetNet.parameters(), self.evalNet.parameters()):
                target_param.data.copy_(target_param.data * 0.98 + param.data * 0.02)

        self.learntimes += 1
        self.epsilon = self.epsilon*0.99  # 下降

def testNetwork(parameterpath, type):
    environment = gym.make('MountainCar-v0')

    testModel = DQN(input_dim=2, output_dim=3, NetworkType=type)
    testModel.targetNet.load_state_dict(torch.load(parameterpath))

    rewardList = []
    for i in range(100):
        obs = environment.reset()
        totalReward = 0
        while True:
            #environment.render()
            action = testModel.get_action(obs, False)

            nextobs, reward, done, info = environment.step(action)
            totalReward += reward
            obs = nextobs
            if done:
                rewardList.append(totalReward)

                break
    environment.close()

    return rewardList

def drawfig(reward, name):

    plt.plot(list(range(len(reward))), reward)
    plt.xlabel('Episodes')
    plt.ylabel('Reward')
    plt.title(name)
    plt.savefig('imgs/'+name+'.png')
    plt.close()

def training(type):

    environment = gym.make('MountainCar-v0')
    maxEpisodes = 600
    DQNModel = DQN(input_dim=2, output_dim=3, NetworkType=type)
    step = 0
    rewardlist = []
    maxReward = -300
    for episode in range(maxEpisodes):
        observation = environment.reset()
        totalreward = 0
        while True:
            step += 1
            action = DQNModel.get_action(observation, True)
            nextobservation, reward, done, info = environment.step(action)
            totalreward += reward
            reward = abs(nextobservation[0]+0.5)

            transtion = (observation, action, reward, nextobservation, done)
            DQNModel.push_transition(transtion)

            observation = nextobservation

            if step > DQNModel.BufferSize+1:  # 存满后开始训练
                DQNModel.train()

            if done:
                rewardlist.append(totalreward)
                print('type:', type ,'episode:', episode, 'totalreward', totalreward)
                if (totalreward > maxReward and totalreward > -125) or totalreward > -90:
                    currentTime = time.strftime("%Y_%m_%d_%H_%M_%S", time.localtime())
                    savepath = 'parameters/' + str(type) + '_'+currentTime + "_"+ str(episode) + str(totalreward) + '.pkl'
                    torch.save(DQNModel.targetNet.state_dict(), savepath)
                    if totalreward>maxReward:
                        maxReward = totalreward
                break

    environment.close()
    return rewardlist

def main():
    set_seed(2)
    DQN_Reward = training(0)
    Double_DQN_Reward = training(1)
    Dueling_DQN_Reward = training(2)
    Dueling_Double_DQN_Reward = training(3)

    drawfig(DQN_Reward, 'DQN_Reward')
    drawfig(Double_DQN_Reward,  'Double_DQN_Reward')
    drawfig(Dueling_DQN_Reward,  'Dueling_DQN_Reward')
    drawfig(Dueling_Double_DQN_Reward,  'Dueling_Double_DQN_Reward')

def testmain():
    set_seed(2)
    DQNparameter = 'parameters/DQN.pkl'
    Double_DQNparameter = 'parameters/DoubleDQN.pkl'
    Dueling_DQNparameter = 'parameters/DuelingDQN.pkl'
    Double_Dueling_DQNparameter = 'parameters/Double_Dueling_DQN.pkl'

    DQN_reward = testNetwork(DQNparameter,0)
    Double_DQN_reward = testNetwork(Double_DQNparameter,1)
    Dueling_DQN_reward = testNetwork(Dueling_DQNparameter,2)
    Double_Dueling_DQN_reward = testNetwork(Double_Dueling_DQNparameter, 3)

    lenth = len(DQN_reward)

    plt.figure(figsize=(20, 10), dpi=90)
    plt.plot(list(range(lenth)), DQN_reward, label='DQN')
    plt.plot(list(range(lenth)), Double_DQN_reward, label='Double DQN')
    plt.plot(list(range(lenth)), Dueling_DQN_reward, label='Dueling DQN')
    plt.plot(list(range(lenth)), Double_Dueling_DQN_reward, label='Dueling&Double DQN')
    plt.xlabel('Episodes')
    plt.ylabel('Reward')
    plt.legend(loc = 1)
    plt.savefig('imgs/testResult.png')
    plt.close()

    print("DQN average: ", sum(DQN_reward)/lenth)
    print("Double DQN average: ", sum(Double_DQN_reward) / lenth)
    print("Dueling DQN average: ", sum(Dueling_DQN_reward) / lenth)
    print("Dueling&Double DQN average: ", sum(Double_Dueling_DQN_reward) / lenth)

if __name__ == '__main__':

    # main() is to train the network
    # testmain() is to test trained network

    # main()
    testmain()


import random
import time
import socket
import asyncore
import threading
import argparse
from pprint import pprint

# Globals
ops = {}
numclients = 0
numkeys = 0
runtime = 0
clients = []

def _buildResp(*args):
    result = "*" + str(len(args)) + "\r\n"
    for v in args:
        result = result + "$" + str(len(v)) + "\r\n"
        result = result + v + "\r\n"
    return result.encode('utf-8')

class Client(asyncore.dispatcher):
    def __init__(self, host, port):
        asyncore.dispatcher.__init__(self)
        self.create_socket(socket.AF_INET, socket.SOCK_STREAM)
        self.connect((host, port))
        self.buf = b''
        self.inbuf = b''
        self.callbacks = list()
        self.client_id = 0
        self.get_client_id()

    def handle_connect(self):
        pass

    def handle_read(self):
        self.inbuf += self.recv(8192)
        self.parse_response()

    def handle_write(self):
        sent = self.send(self.buf)
        self.buf = self.buf[sent:]

    def handle_close(self):
        self.close()

    def writable(self):
        return len(self.buf) > 0

    def parse_array(self, startpos):
        assert(self.inbuf[startpos] == ord('*'))
        endrange = self.inbuf[startpos+1:].find(ord('\r')) + 1 + startpos
        assert(endrange > 0)
        numargs = int(self.inbuf[startpos+1:endrange])
        if numargs == -1: # Nil array, used in some returns
            startpos = endrange + 2
            return startpos, []
        assert(numargs > 0)
        args = list()
        startpos = endrange + 2 # plus 1 gets us to the '\n' and the next gets us to the start char

        while len(args) < numargs:
            # We're parsing entries of the form "$N\r\nnnnnnn\r\n"
            if startpos >= len(self.inbuf):
                return # Not the full response
            if self.inbuf[startpos] == ord('*'):
                startpos, arr = self.parse_array(startpos)
                args.append(arr)
            else:
                assert(self.inbuf[startpos] == ord('$'))
                startpos = startpos + 1
                endrange = self.inbuf[startpos:].find(b'\r')
                if endrange < 0:
                    return
                endrange += startpos
                assert(endrange <= len(self.inbuf))
                length = int(self.inbuf[startpos:endrange])
                if length < 0:
                    return
                startpos = endrange + 2
                assert((startpos + length) <= len(self.inbuf))
                assert(self.inbuf[startpos+length] == ord('\r'))
                assert(self.inbuf[startpos+length+1] == ord('\n'))
                args.append(self.inbuf[startpos:(startpos+length)])
                startpos += length + 2
        assert(len(args) == numargs)
        return startpos, args

    def parse_response(self):
        if len(self.inbuf) == 0:
            return

        while len(self.inbuf) > 0:
            if self.inbuf[0] == ord('+') or self.inbuf[0] == ord('-') or self.inbuf[0] == ord(':'):
                # This is a single line response
                endpos = self.inbuf.find(b'\n')
                if endpos < 0:
                    return  # incomplete response
                self.callbacks[0](self, self.inbuf[0:endpos-1])
                self.callbacks.pop(0)
                self.inbuf = self.inbuf[endpos+1:]

            elif self.inbuf[0] == ord('*'):
                #RESP response
                try:
                    startpos, args = self.parse_array(0)
                except:
                    return # Not all data here yet
                self.callbacks[0](self, args)
                self.callbacks.pop(0)
                self.inbuf = self.inbuf[startpos:]
            else:
                print("ERROR: Unknown response:")
                pprint(self.inbuf)
                assert(False)

    def default_result_handler(self, result):
        pprint(result)

    # Public Methods
    def set(self, key, val, callback = default_result_handler):
        self.buf += _buildResp("set", key, val)
        self.callbacks.append(callback)

    def lpush(self, key, val, callback = default_result_handler):
        self.buf += _buildResp("lpush", key, val)
        self.callbacks.append(callback)

    def blpop(self, *keys, timeout=0, callback=default_result_handler):
        self.buf += _buildResp("blpop", *keys, str(timeout))
        self.callbacks.append(callback)

    def delete(self, key, callback = default_result_handler):
        self.buf += _buildResp("del", key)
        self.callbacks.append(callback)

    def unblock(self, client_id, callback=default_result_handler):
        self.buf += _buildResp("client", "unblock", str(client_id))
        self.callbacks.append(callback)

    def scan(self, iter, match=None, count=None, callback = default_result_handler):
        args = ["scan", str(iter)]
        if match != None:
            args.append("MATCH")
            args.append(match)
        if count != None:
            args.append("COUNT")
            args.append(str(count))
        self.buf += _buildResp(*args)
        self.callbacks.append(callback)

    def get_client_id(self):
        self.buf += _buildResp("client", "id")
        self.callbacks.append(self.store_client_id)

    def store_client_id(self, c, resp):
        assert(resp[0] == ord(':'))
        self.client_id = int(resp[1:])
        assert(self.client_id == c.client_id)

    def get(self, key, callback = None):
       return

def getrandomkey():
    return str(random.randrange(0, numkeys))

def handle_lpush_response(c, resp=None):
    global ops
    if resp != None:
        ops['lpush'] += 1
        assert(resp[0] == ord(':'))
    c.lpush("list_" + getrandomkey(), 'bardsklfjkldsjfdlsjflksdfjklsdjflksd kldsjflksd jlkdsjf lksdjklds jrfklsdjfklsdjfkl', handle_lpush_response)

def handle_blpop_response(c, resp=None):
    global ops
    if resp != None:
        ops['blpop'] += 1
    c.blpop("list_" + getrandomkey(), callback=handle_blpop_response)

def handle_set_response(c, resp=None):
    global ops
    if resp != None:
        ops['set'] += 1
        assert(resp[0] == ord('+'))
    c.set("str_" + getrandomkey(), 'bardsklfjkldsjfdlsjflksdfjklsdjflksd kldsjflksd jlkdsjf lksdjklds jrfklsdjfklsdjfkl', handle_set_response)

def handle_del_response(c, resp=None):
    global ops
    if resp != None:
        ops['del'] += 1
    c.delete("list_" + getrandomkey(), handle_del_response)

def scan_callback(c, resp=None):
    global ops
    nextstart = int(resp[0])
    c.scan(nextstart, count=500, callback=scan_callback)
    ops['scan'] += 1

def clear_ops():
    global ops
    ops = {'lpush': 0, 'blpop': 0, 'del': 0, 'scan': 0, 'set': 0, 'get': 0}

def stats_thread():
    global ops
    global runtime
    i = 0
    while i < runtime or not runtime:
        time.sleep(1)
        print("Ops per second: " + str({k:v for (k,v) in ops.items() if v}))
        clear_ops()
        i += 1
    asyncore.close_all()

def flush_db_sync():
    server = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
    server.connect(('127.0.0.1', 6379))
    server.send(_buildResp("flushdb"))
    resp = server.recv(8192)
    assert(resp[:3] == "+OK".encode('utf-8'))

def init_blocking():
    global clients
    if numkeys > 100 * numclients:
        print("WARNING: High ratio of keys to clients. Most lpushes will not be popped and unblocking will take a long time!")
    for i in range(numclients):
        clients.append(Client('127.0.0.1', 6379))
        if i % 2:
            handle_blpop_response(clients[-1])
        else:
            handle_lpush_response(clients[-1])

def init_lpush():
    global clients
    for i in range(numclients):
        clients.append(Client('127.0.0.1', 6379))
        for i in range (10):
            handle_lpush_response(clients[-1])
        #handle_set_response(clients[-1], None)

    scan_client = Client('127.0.0.1', 6379)
    scan_client.scan(0, count=500, callback=scan_callback)

    del_client = Client('127.0.0.1', 6379)
    handle_del_response(del_client)

def main(test, flush):
    clear_ops()

    if flush:
        flush_db_sync()

    try:
        globals()[f"init_{test}"]()
    except KeyError:
        print(f"Test \"{test}\" not found. Exiting...")
        exit()
    except ConnectionRefusedError:
        print("Could not connect to server. Is it running?")
        print("Exiting...")
        exit()

    threading.Thread(target=stats_thread).start()
    asyncore.loop()
    print("Done.")

parser = argparse.ArgumentParser(description="Test use cases for KeyDB.")
parser.add_argument('test', choices=[x[5:] for x in filter(lambda name: name.startswith("init_"), globals().keys())], help="which test to run")
parser.add_argument('-c', '--clients', type=int, default=50, help="number of running clients to use")
parser.add_argument('-k', '--keys', type=int, default=100000, help="number of keys to choose from for random tests")
parser.add_argument('-t', '--runtime', type=int, default=0, help="how long to run the test for (default: 0 for infinite)")
parser.add_argument('-f', '--flush', action="store_true", help="flush the db before running the test")

if __name__ == "__main__":
    try:
        args = parser.parse_args()
    except:
        exit()
    numclients = args.clients
    numkeys = args.keys
    runtime = args.runtime
    main(args.test, args.flush)


def _phase_kuramoto(self, teta, t, argv):

        """!

        @brief Overrided method for calculation of oscillator phase.

        @param[in] teta (double): Current value of phase.

        @param[in] t (double): Time (can be ignored).

        @param[in] argv (uint): Index of oscillator whose phase represented by argument teta.

        @return (double) New value of phase of oscillator with index 'argv'.

        """

        index = argv;   # index of oscillator

        phase = 0.0;      # phase of a specified oscillator that will calculated in line with current env. states.

        neighbors = self.get_neighbors(index);

        for k in neighbors:

            conn_weight = 1.0;

            if (self._ena_conn_weight is True):

                conn_weight = self._conn_weight[index][k];

            phase += conn_weight * self._weight * math.sin(self._phases[k] - teta);

        divider = len(neighbors);

        if (divider == 0):

            divider = 1.0;

        return ( self._freq[index] + (phase / divider) );