import numpy
import pickle

pkl_file = open('data_l.pkl', 'rb')

data1 = pickle.load(pkl_file)

bestresults = numpy.array(list(map(lambda x: x[1], data1)))

import matplotlib.pyplot as plt

fig = plt.figure()
# plt.yscale('log')

labels = [0.75, 0.375, 0.25, 0.75/4, 0.75/5, 0.75/6, 0.75/7, 0.75/8, 0.75/9, 0.075]

z = []
for i in range(10):
    seq = bestresults[i: 101: 10]
    cmap = plt.get_cmap('jet')
    colors = [cmap(i) for i in numpy.linspace(0, 1, 10)]

    q = []

    for j in range(10):
        q.append(seq[j].flatten())
        '''if 0 == j:
            plt.plot(seq[j].flatten(), color=colors[i], label='T(initial)=' + str(labels[i]))
        else:
            plt.plot(seq[j].flatten(), color=colors[i])
    '''
    qm = []
    qe = []
    offsets = [999]
    for offs in offsets:
        qv = list(map(lambda x: x[offs], q))
        qm.append(numpy.mean(qv))
        qe.append(numpy.std(qv))
    z.append((qm[0]))
    plt.errorbar([labels[i]], qm, numpy.sqrt(qe))

plt.plot(labels, z, 'k')

plt.xlabel('t')
plt.ylabel('E')
#plt.legend()
fig.show()

input("exit>")


from urllib import request, parse, error
import hmac
import hashlib
import json
from collections import namedtuple

API_KEY = ''

def _json_hook(d):
    return namedtuple('X', list(d.keys()))(*list(d.values()))

def p_object(data):
    return json.loads(data, object_hook=_json_hook).result

class CryptoPayments:
    url = 'https://www.coinpayments.net/api.php'

    def __init__(self, public_key, private_key, ipn_url):
        self.public_key = public_key
        self.private_key = private_key
        self.ipn_url = ipn_url
        self.format = 'json'
        self.version = 1

    def create_hmac(self, **params):
        """ Generate an HMAC based upon the url arguments/parameters

            We generate the encoded url here and return it to Request because
            the hmac on both sides depends upon the order of the parameters, any
            change in the order and the hmacs wouldn't match
        """
        encoded = parse.urlencode(params).encode('utf-8')
        return encoded, hmac.new(bytearray(self.private_key, 'utf-8'), encoded, hashlib.sha512).hexdigest()

    def request(self, request_method, **params):
        """The basic request that all API calls use

            the parameters are joined in the actual api methods so the parameter
            strings can be passed and merged inside those methods instead of the
            request method. The final encoded URL and HMAC are generated here
        """
        encoded, sig = self.create_hmac(**params)

        headers = {'hmac': sig}

        req = None
        if request_method == 'get':
            req = request.Request(self.url, headers=headers)
        elif request_method == 'post':
            headers['Content-Type'] = 'application/x-www-form-urlencoded'
            req = request.Request(self.url, data=encoded, headers=headers)

        try:
            response = request.urlopen(req)
            # status_code = response.getcode()
            response_body = response.read()
        except error.HTTPError as e:
            # status_code = e.getcode()
            response_body = e.read()

        return p_object(response_body)

    def create_transaction(self, params=None):
        """ Creates a transaction to give to the purchaser
            https://www.coinpayments.net/apidoc-create-transaction
        """
        if params is None:
            params = {}
        params.update({'cmd': 'create_transaction',
                       'ipn_url': self.ipn_url,
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_basic_info(self, params=None):
        """Gets merchant info based on API key (callee)
           https://www.coinpayments.net/apidoc-get-basic-info
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_basic_info',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def rates(self, params=None):
        """Gets current rates for currencies
           https://www.coinpayments.net/apidoc-rates
        """
        if params is None:
            params = {}
        params.update({'cmd': 'rates',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def balances(self, params=None):
        """Get current wallet balances
            https://www.coinpayments.net/apidoc-balances
        """
        if params is None:
            params = {}
        params.update({'cmd': 'balances',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_deposit_address(self, params=None):
        """Get address for personal deposit use
           https://www.coinpayments.net/apidoc-get-deposit-address
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_deposit_address',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_callback_address(self, params=None):
        """Get a callback address to receive info about address status
           https://www.coinpayments.net/apidoc-get-callback-address
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_callback_address',
                       'ipn_url': self.ipn_url,
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def create_transfer(self, params=None):
        """Not really sure why this function exists to be honest, it transfers
            coins from your addresses to another account on coinpayments using
            merchant ID
           https://www.coinpayments.net/apidoc-create-transfer
        """
        if params is None:
            params = {}
        params.update({'cmd': 'create_transfer',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def create_withdrawal(self, params=None):
        """Withdraw or masswithdraw(NOT RECOMMENDED) coins to a specified address,
        optionally set a IPN when complete.
            https://www.coinpayments.net/apidoc-create-withdrawal
        """
        if params is None:
            params = {}
        params.update({'cmd': 'create_withdrawal',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def convert_coins(self, params=None):
        """Convert your balances from one currency to another
            https://www.coinpayments.net/apidoc-convert
        """
        if params is None:
            params = {}
        params.update({'cmd': 'convert',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_withdrawal_history(self, params=None):
        """Get list of recent withdrawals (1-100max)
            https://www.coinpayments.net/apidoc-get-withdrawal-history
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_withdrawal_history',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_withdrawal_info(self, params=None):
        """Get information about a specific withdrawal based on withdrawal ID
            https://www.coinpayments.net/apidoc-get-withdrawal-info
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_withdrawal_info',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def get_conversion_info(self, params=None):
        """Get information about a specific withdrawal based on withdrawal ID
            https://www.coinpayments.net/apidoc-get-conversion-info
        """
        if params is None:
            params = {}
        params.update({'cmd': 'get_conversion_info',
                       'key': self.public_key,
                       'version': self.version,
                       'format': self.format})
        return self.request('post', **params)

    def validate_mac(self, uuid, price, currency, test_hash):
        to_check = API_KEY + '_' + uuid + '_' + str(int(price * 100)) + currency
        computed_hash = hashlib.sha256(to_check).hexdigest()
        return computed_hash == test_hash


import yfinance as yf
import pymysql
import pandas as pd
import os
from yahoo_fin import stock_info as si
db = pymysql.connect("localhost", "stockuser", "123456", "stock_advisor")
cursor = db.cursor()
cursor.execute("SELECT symbol FROM symbols WHERE active=1")
symbols=cursor.fetchall()

def main():
    print('Starting market profits checking module')

    SUMM()

def SUMM():
    open('data/out2_tmp.csv', 'w').close()
    open('data/out3_tmp.csv', 'w').close()
    open('data/out4_tmp.csv', 'w').close()
    for symbol in symbols: #Loop trough the stock summary
          market=(symbol[0])
          #print (symbol)
          #market = yf.Ticker(symbol)
          print (market, summ_serf(market))
          if summ_serf(market)!=0:
              #print (market, summ_serf(market), count(market))
              f= open('data/out2_tmp.csv', 'a')
              print (str(market), file=f)
              f1=open('data/out3_tmp.csv', 'a')
              print (summ_serf(market), file=f1)
              f2 = open('data/out4_tmp.csv', 'a')
              print (count(market), file=f2)
    os.rename('data/out2_tmp.csv', 'data/out2.csv')
    os.rename('data/out3_tmp.csv', 'data/out3.csv')
    os.rename('data/out4_tmp.csv', 'data/out4.csv')

def summ_serf(marketname):
    db = pymysql.connect("localhost", "stockuser", "123456", "stock_advisor")
    cursor = db.cursor()
    market=marketname
    cursor.execute("SELECT SUM(percent_serf) FROM orders where active=0 and market= '%s'" % market)
    r = cursor.fetchall()
    for row in r:
        if row[0] is not None:
            return row[0]

    return 0

def count(marketname):
    db = pymysql.connect("localhost", "stockuser", "123456", "stock_advisor")
    cursor = db.cursor()
    market=marketname
    cursor.execute("SELECT COUNT(*) FROM orders where active=0 and market= '%s'" % market)
    r = cursor.fetchall()
    for row in r:
        if row[0] is not None:
            return row[0]

    return 0

if __name__ == "__main__":
    main()


#!/usr/bin/python
# coding=utf-8

import sys
import re

# 功能说明：
# 统计内容中含有“@…… ”或者“[……]”、“#……#”特殊格式的内容和数量

####################以下是参数######################

COLUMN = 1  # 要清洗的数据在第几列
FILE_NAME = "8.23-8.31.csv"  # 数据文件，必须是立方导出的数据
COLUMN_TOTAL = 1  # 总列数
PATTERNTYPE = 3  # 选择匹配规则patternTemp1，patternTemp2，patternTemp3
OUTPUT_FILE = "result.csv"  # 输出结果文件名
COUNT_FILE = "8.6-8.21t.txt"  # 统计结果文件名

####################以上是参数######################
DELIMITER = ","
CODING = "gbk"
patternTemp1 = "@[^,，：:\s@()/]+"
patternTemp2 = "\[.*?\]"
patternTemp3 = "#.*?#"

i = 0
j = 0
result = dict()

def utf8_2_gbk(src):
    res = src.decode("utf-8").encode("gbk", "ignore")
    return res

if __name__ == "__main__":
    ResultWriter = file(OUTPUT_FILE, "w+")
    reader = open(FILE_NAME, 'rb')
    count_file = file(COUNT_FILE, "w+")

    next(reader)

    for line in reader:
        content = line.split(DELIMITER)[COLUMN - 1].strip().decode(CODING,'ignore')

        if PATTERNTYPE == 1:
            pattern = re.compile(r'' + patternTemp1)
        elif PATTERNTYPE == 2:
            pattern = re.compile(r'' + patternTemp2)
        elif PATTERNTYPE == 3:
            pattern = re.compile(r'' + patternTemp3)
        matches = pattern.findall(content)
        if len(matches) != 0:
            for ele in matches:
                if ele in result:
                    result[ele] = result[ele] + 1
                    i = i + 1
                else:
                    result[ele] = 1
                    j = j + 1
    # 排序
    result = sorted(result.iteritems(), key=lambda asd: asd[1], reverse=True)
    for key, value in result:
        out = str(key.encode(CODING)) + "," + str(value) + "\n"
        ResultWriter.write(out)

    # 输出统计结果
    count_file.write(utf8_2_gbk("统计结果：") + str(i + j))

    ResultWriter.close()
    reader.close()

14 【数据统计】统计内容中含有“@…… ”或者“[……]”、“#……#”特殊格式的内容和数量/match_count.py


import numpy as np
from matplotlib import pyplot as plt
import pylab

file = open("predictions.txt")
numarray = []
while 1:
	line = file.readline()
	if not line:
		break
	numarray.append(float(line))

x = range(len(numarray))
plt.scatter(x,numarray)

plt.savefig("figure.pdf")


import numpy as np
import math

class Dnode:
    def __init__(self,parent = None, position = None):
        self.parent = parent
        self.position = position
        self.f=0 #cost

    def __eq__(self,other):
        return self.position == other.position

    def solve(maze,cost,start,end,steps=8) ->np.ndarray:

        start_node = Dnode(None,tuple(start))
        start_node.f=0
        end_node = Dnode(None,tuple(end))
        end_node.f = 0

        unvisited_list = []
        visited_list = []

        unvisited_list.append(start_node)

        outer_iterations = 0
        max_iterations = (len(maze) // 2) ** 4

        if steps == 8:
            move = [[-1, 0],  # go up
                    [-1,-1],
                    [0, -1],  # go left
                    [-1,+1],
                    [ 1, 0],  # go down
                    [ 1, 1],
                    [ 0, 1],  # go right
                    [ 1,-1]]
        elif steps == 4 :
            move = [[-1, 0],  # go up
                    [0, -1],  # go left
                    [ 1, 0],  # go down
                    [ 0, 1]]  # go right
        else:
            print("error!")
            return None

        no_rows, no_columns,_ = maze.shape
        maze[start_node.position]=[0,0,0]
        maze[end_node.position] = [0,0,0]

        while len(unvisited_list) > 0:

            outer_iterations += 1

            current_node = unvisited_list[0]
            current_index = 0
            for index, item in enumerate(unvisited_list):
                if item.f < current_node.f:
                    current_node = item
                    current_index = index

            # if we hit this point return the path such as it may be no solution or
            # computation cost is too high
            if outer_iterations > max_iterations:
                print("giving up on pathfinding too many iterations")
                break

            unvisited_list.pop(current_index)
            visited_list.append(current_node)

            if current_node == end_node:
                print("Computation Complete!")
                break
            children = []

            for new_position in move:

                node_position = (
                    current_node.position[0] + new_position[0], current_node.position[1] + new_position[1])

                if (node_position[0] > (no_rows - 1) or
                    node_position[0] < 0 or
                    node_position[1] > (no_columns - 1) or
                        node_position[1] < 0):
                    continue

                if (maze[node_position[0]][node_position[1]] != [0,0,0]).all():
                    continue

                new_node = Dnode(current_node, node_position)
                maze[node_position[0]][node_position[1]] = [50,88,239]
                children.append(new_node)

            for child in children:

                if child in visited_list:
                    continue

                child.f = current_node.f + cost

                if child in unvisited_list:
                    continue

                unvisited_list.append(child)

                count=0
        # we update the path of start to end found by Dijkstra search with every step incremented by 1
        while current_node is not None:
            count+=1
            maze[current_node.position[0],current_node.position[1]] = [239,88,50]
            current_node = current_node.parent
        return (maze, count)


"""
Module for functions that analyse text
"""

import pandas as pd
from utils.text_preprocessing_utils import normalise, tokenise

def get_cluster(row, kmeans):
    return kmeans.predict(row.values.reshape(1, -1))[0]

def get_common_words(string):
    text_df = pd.DataFrame(normalise(tokenise(string)))
    text_df["count"] = 1
    grouped_df = text_df.groupby([0]).count()
    grouped_df = grouped_df.sort_values(["count"], ascending=False)
    grouped_df = grouped_df[:20]
    grouped_df.plot.bar()


from docx import Document
from log import logger

def read_sheets_from_template(template_file):
    wordDoc = Document(template_file)
    tables = wordDoc.tables
    sheets = []

    for t_idx, table in enumerate(tables):
        sheet = []

        for r_idx, row in enumerate(table.rows):
            row_data = []
            for c_idx, cell in enumerate(row.cells):
                if r_idx == 0:
                    if len(cell.text) >= 0:
                        row_data.append(cell.text)
                else:
                    row_data.append(cell.text)
            if len(row_data) > 0:
                sheet.append(row_data)
        if len(sheet) > 0:
            sheets.append(sheet)
    print(sheets)
    return sheets

def write_to_docx(sheets,docx_template):
    wordDoc = Document(docx_template)
    # para = wordDoc.add_paragraph()
    tables = wordDoc.tables
    sStr2 = "Tr"
    for t_idx, table in enumerate(tables):
        if table is not None:
            for r_idx, row in enumerate(table.rows):
                if row is not None:
                    for c_idx, cell in enumerate(row.cells):
                        if r_idx >= 0 and c_idx >= 0 and len(cell.text) >= 0:
                            try:
                                if cell is not None:
                                    cell.text = str(sheets[t_idx][r_idx][c_idx])
                                    # <----remove flag string --->
                                    sStr1 =cell.text
                                    # print(sStr1)
                                    if sStr2 in sStr1:
                                        nPos = sStr1.index(sStr2)
                                        cell.text=sStr1[0:nPos]
                                        # print (nPos)
                                    # <--------------------------->
                            except Exception as err:
                                logger.error(
                                    "t_idx  " + str(t_idx) + "   r_idx  " + str(r_idx) + "    c_idx  " + str(c_idx))
                                logger.error(err)

    wordDoc.save(docx_template)

if __name__ == '__main__':

    sheets1 = read_sheets_from_template("../docx/test1.docx")
    write_to_docx(sheets1,"../docx/test1.docx")


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

# In[3]:

import numpy as np
import pandas as pd
from pandas import Series,DataFrame
import matplotlib.pyplot as plt
import seaborn as sns

import scipy.stats as stats

# In[10]:

data=sns.load_dataset('iris')

# In[11]:

data.head()

# In[35]:

data.size

# In[6]:

data['sepal_length'].mean()

# In[10]:

data['sepal_length'].median()

# In[11]:

data['sepal_length'].mode()

# In[7]:

data['sepal_width'].mean()

# In[8]:

data['petal_length'].mean()

# In[9]:

data['petal_width'].mean()

# In[13]:

range_=max(data['sepal_length'])-min(data['sepal_length'])
range_

# In[14]:

q2=data['sepal_length'].quantile(0.50)
q2

# In[18]:

q1=data['sepal_length'].quantile(0.25)
q1

# In[19]:

q3=data['sepal_length'].quantile(0.75)
q3

# In[27]:

st=data['sepal_length'].std()
st

# In[22]:

si=data['sepal_length'].var()
si

# In[23]:

mn=data['sepal_length'].mean()

# In[24]:

mn

# In[30]:

coefficient_of_variation=st/mn*100

# In[31]:

coefficient_of_variation

# In[41]:

co=data.corr()
co

# In[32]:

from scipy.stats import zscore

# In[33]:

zscore(data['sepal_length'])

# In[34]:

sns.pairplot(data)
plt.show()

# In[36]:

sns.countplot(data['sepal_length'])
plt.show()

# In[38]:

sns.boxplot(data['sepal_length'])
plt.plot()

# In[39]:

plt.scatter(data['sepal_length'],data['sepal_width'])
plt.show()

# In[42]:

sns.heatmap(co)
plt.show()

# In[44]:

data.isnull().sum()

# In[49]:

sns.violinplot(data['sepal_length'])
plt.show()

# In[6]:

plt.hist(data['sepal_length'],bins=4)
plt.show()

# In[8]:

data.hist()

# In[52]:

sns.violinplot(x='petal_length',y='petal_length',hue='species',data=data)

# In[4]:

dt=pd.read_csv('titanictrain.csv')
dt

# In[6]:

dt.size

# In[8]:

a=pd.get_dummies(dt['Sex'])
a.head()#we have to add the columns

# In[13]:

pd.get_dummies(dt,columns=['Sex']).head()# it direct add the columns to the dataframe skipping the original one

# In[74]:

pd.merge(dt,a,on=dt.index)# adding the columns by merge method

# In[14]:

import sklearn
from sklearn.preprocessing import OneHotEncoder
hotencoder=OneHotEncoder()
encoded=hotencoder.fit_transform(dt.Sex.values.reshape(-1,1)).toarray()
encoded

# In[69]:

aa=pd.get_dummies(data,prefix='park',columns=['species'])
aa.head()

# In[75]:

from sklearn.preprocessing import StandardScaler

# In[76]:

std_scale=StandardScaler()
std_scale

# In[81]:

dt['age_stdscl']=std_scale.fit_transform(dt[['Age']])# it takes

# In[82]:

dt['age_stdscl']

# In[86]:

from sklearn.preprocessing import MinMaxScaler

# In[88]:

minmax_scale=MinMaxScaler()
minmax_scale

# In[89]:

dt['t']=minmax_scale.fit_transform(dt[['Age']])# it takes only numerical value

# In[90]:

dt['t']

# In[ ]:


import numpy as np

def perceptron_average(X,Y,r,epoch):
    n,m = X.shape
    Y = modify_label(Y)
    w = np.zeros(m)
    a = np.zeros(m)
    for t in range(epoch):
        for ind,x in enumerate(X):
            y = Y[ind]
            pred = y*(w.dot(x))
            if pred<=0:
                w += r*y*x
            a += w

    return a

def prediction_average(a,X):

    return np.sign(X.dot(a))

def modify_label(Y):
    for ind,y in enumerate(Y):
        if y == 0:
            Y[ind] = -1
    return Y


city =input("Enter The City Name:")
scity=city.strip()
if scity=='Hyderabad':
    print("Hello Hyderbadi..adab")
elif scity=='Chennai':
    print("Hello Madrasi...Vanakkam")
elif scity=="Bangalore":
    print("Hello Kannadiga.....Shubhodaya")
else:
    print("your enter city is invalid")


#
# @lc app=leetcode id=316 lang=python3
#
# [316] Remove Duplicate Letters
#

# @lc code=start
class Solution:
    def removeDuplicateLetters(self, s: str) -> str:
        countList = [0] * 26
        booleanList = [False] * 26
        stack = []
        for char in s:
            countList[ord(char) - ord('a')] += 1

        for char in s:
            countList[ord(char) - ord('a')] -= 1
            if booleanList[ord(char) - ord('a')]:
                continue
            while stack and  char < stack[-1] and countList[ord(stack[-1]) - ord('a')] > 0:
                booleanList[ord(stack[-1]) - ord('a')] = False
                stack.pop()
            stack.append(char)
            booleanList[ord(stack[-1]) - ord('a')] = True
        return ''.join(stack)
# @lc code=end


#Implementing and testing a Bubble Sort Algorithm

def bubblesort(arr):

    '''Returns a sorted array
       input: an array of integers
       output: an array of integers'''

    nl = []
    for i in arr:
        arr2 = arr
        for j in arr:
            print(i,'        ',j)
            if i >= j[0]:
                nl.append(i)
                arr2.remove(j[0])
    return nl
    print(arr)
    print('')
    correct = False

#Attempt 2
def bubblesortNew(arr):

    '''Returns a sorted array
       input: an array of integers
       output: an array of integers'''

    print(arr)
    print('Sorted array below')
    correct = False

    while correct == False:
        correct = True

        for n in arr:
            k = arr.index(n)
            if k == len(arr) -1:
                pass
            elif k != len(arr) -1:
                if n > arr[k+1]:
                    correct = False
                    arr[k+1], arr[k] = arr[k], arr[k+1]
    return arr

array = [2,4,5,1,39,7,7]
array2 = [1,4,3,8,8,5]

print(bubblesort(array))
print(bubblesortNew(array2))


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

# 转为u8类型
def restore1(u, sigma, v, k):
    m = len(u)
    n = len(v)
    a = np.zeros((m, n))
    a = np.dot(u[:, :k], np.diag(sigma[:k])).dot(v[:k, :])
    # s1 =  np.size(u[:, :k])
    # s1+= np.size(np.diag(sigma[:k]))
    # s1+= np.size(np.diag(v[:k, :]))
    # s2 = np.size(a)
    # print("压缩率：",s1/s2)
    a[a < 0] = 0
    a[a > 255] = 255
    return np.rint(a).astype('uint8')

def SVD(frame, K=10):
    a = np.array(frame)
    # 由于是彩色图像，所以3通道。a的最内层数组为三个数，分别表示RGB，用来表示一个像素
    u_r, sigma_r, v_r = np.linalg.svd(a[:, :, 0])
    u_g, sigma_g, v_g = np.linalg.svd(a[:, :, 1])
    u_b, sigma_b, v_b = np.linalg.svd(a[:, :, 2])

    R = restore1(u_r, sigma_r, v_r, K)
    G = restore1(u_g, sigma_g, v_g, K)
    B = restore1(u_b, sigma_b, v_b, K)
    I = np.stack((R, G, B), axis=2)
    return I

if __name__ == "__main__":
    mpl.rcParams['font.sans-serif'] = [u'simHei']
    mpl.rcParams['axes.unicode_minus'] = False
    frame = cv2.imread("test.png", -1)
    I = SVD(frame, 40)
    plt.imshow(I)
    cv2.imwrite("out.png", I)


#!/usr/bin/env python
__author__ = 'uwe'

from sys import argv
from os import listdir, makedirs, system
from os.path import exists

from bc.eodata.beam_processing.conf.paths import gptProcessor, ECOHAM_graph_file
from utils.utilities import getDOY, ensureTrailingSlash, exit_on_empty_list
from nasa.modis.seadas_processing.conf.paths import modisL3_TSMBasePath, modisL3_ECOHAMBasePath
from nasa.modis.seadas_processing.conf.params import _site

def printUsage():
    print("Usage: ", argv[0], "<date>")
    print("where date is a string representing the date to process,")
    print("e.g. 20120607 for June 7, 2012.")

if len(argv) != 2:
    printUsage()
    exit(1)

back_date = argv[1]
if len(back_date)!=8:
    print("****************************")
    print("* date parameter malformed *")
    print("****************************")
    printUsage()
    exit(1)

_year  = back_date[:4]
_month = back_date[4:6]
_day   = back_date[6:]
_doy   = getDOY(_year, _month, _day)
print("Processing date " + back_date + " (DOY = " + str(_doy)+ ").")

modisL3_TSMPath  = ensureTrailingSlash(ensureTrailingSlash(ensureTrailingSlash(modisL3_TSMBasePath  + _year) + _month) + _day)
modisL3_ECOHAMPath  = ensureTrailingSlash(modisL3_ECOHAMBasePath  + _year)

for _path in [modisL3_TSMPath, modisL3_ECOHAMPath]:
    if not exists(_path):
        print("Making directory: ", _path, " ...")
        makedirs(_path)

try:
    srcList = listdir(modisL3_TSMPath)
except OSError:
    print("Cannot open ", modisL3_TSMPath+ "! Now exiting...")
    exit(1)
else:
    listSize = exit_on_empty_list(srcList)
    print(listSize)

# Liste bereinigen:
for a in range(listSize):
    for item in srcList:
        if not item.startswith('cb_' + _site + '_' + back_date) or not item.endswith('.dim'):
            srcList.remove(item)

listSize = exit_on_empty_list(srcList)
srcList.sort()

# reprojected L3 products,        Name e.g. cb_ns_20130603_eo_ecoham.dim
outputProductPath = modisL3_ECOHAMPath + 'cb_' + _site + '_' + back_date + '_eo_bc_lat_lon_ecoham.dim'

print(srcList, outputProductPath)

for item in srcList:
    reproj_processingCall = gptProcessor + ' ' + ECOHAM_graph_file + ' -Ssource=' + modisL3_TSMPath + item + ' -Pfile=' + outputProductPath
    system(reproj_processingCall)


#!/usr/bin/env python3

# Follow the wall which way leads to the longest possible path

from LightningBot import LightningBot
from random import randint

bot = LightningBot(
  bot_name = 'Waller' + '%04d' % randint(0, 9999),
)

while bot.waitForNextTurnDirections():

  bot.move(bot.directionToLongestWallPath())


from django.http import JsonResponse

def json_from_object(obj):
    data = {
        'id': obj.id
    }

    if hasattr(obj, 'name'):
        data['name'] = obj.name
    elif hasattr(obj, 'address'):
        data['address'] = obj.address
    else:
        data['name'] = str(obj)

    if hasattr(obj, 'phone'):
        data['phone'] = obj.percent
    return JsonResponse(data)


import os
import shutil

import boto3

from data_refinery_common.enums import PipelineEnum
from data_refinery_common.logging import get_and_configure_logger
from data_refinery_common.models import Pipeline, ProcessorJob, Sample
from data_refinery_common.utils import get_env_variable
from data_refinery_workers.processors import utils

LOCAL_ROOT_DIR = get_env_variable("LOCAL_ROOT_DIR", "/home/user/data_store")
logger = get_and_configure_logger(__name__)

# Default to us-east-1 if the region variable can't be found
AWS_REGION = get_env_variable("AWS_REGION", "us-east-1")

# 100 is the maximum number of jobIds you can pass to a AWS Batch's
# describe_jobs.
DESCRIBE_JOBS_PAGE_SIZE = 100

batch = boto3.client("batch", region_name=AWS_REGION)

def _find_and_remove_expired_jobs(job_context):
    """Finds expired jobs and removes their working directories"""

    job_context["deleted_items"] = []

    items_to_delete = []
    jobs_to_check = []
    for item in os.listdir(LOCAL_ROOT_DIR):

        # There may be successful processors
        if "SRP" in item or "ERP" in item or "DRP" in item:
            sub_path = os.path.join(LOCAL_ROOT_DIR, item)
            for sub_item in os.listdir(sub_path):
                try:
                    sample = Sample.objects.get(accession_code=sub_item)
                    if sample.computed_files.count() == 0:
                        # This doesn't have any associated computed files - leave it be.
                        continue
                except Sample.DoesNotExist:
                    # Interesting. This shouldn't happen at all.
                    continue
                except Exception:
                    # We can't contact the DB right now, skip deletion.
                    continue

                try:
                    sub_item_path = os.path.join(sub_path, sub_item)
                    logger.debug(
                        "Janitor deleting " + sub_item_path, contents=str(os.listdir(sub_item_path))
                    )
                    shutil.rmtree(sub_item_path)
                    job_context["deleted_items"].append(sub_item_path)
                except Exception:
                    # This job is likely vanished. No need for this directory.
                    pass

        # Processor job working directories
        if "processor_job_" in item:

            # TX Index jobs are the only ones who are allowed to hang around
            # after their jobs are finished. They're marked with an _index in their path.
            if "_index" in item:
                continue

            # Okay, does this job exist?
            try:
                job = ProcessorJob.objects.get(id=item.split("processor_job_")[1])

                jobs_to_check.append(job)
            except ProcessorJob.DoesNotExist:
                # This job has vanished from the DB - clean it up!
                logger.error("Janitor found no record of " + item + " - why?")
                items_to_delete.append(item)

    for page_start in range(0, len(jobs_to_check), DESCRIBE_JOBS_PAGE_SIZE):
        try:
            page_end = page_start + DESCRIBE_JOBS_PAGE_SIZE
            page = jobs_to_check[page_start:page_end]

            job_ids = [job.batch_job_id for job in page if job.batch_job_id]
            batch_jobs = batch.describe_jobs(jobs=job_ids)["jobs"]

            running_ids = {job["jobId"] for job in batch_jobs if job["status"] == "RUNNING"}

            for job in page:
                if not job.batch_job_id or job.batch_job_id not in running_ids:
                    # Reconstitute the path we broke apart previously.
                    items_to_delete.append("processor_job_" + str(job.id))

        except Exception:
            # We're unable to connect to the DB or Batch right
            # now, so hold onto it for right now.
            logger.exception("Problem finding job record for " + item + " - why?")
            continue

    for item in items_to_delete:
        to_delete = LOCAL_ROOT_DIR + "/" + item
        logger.debug("Janitor deleting " + to_delete)
        # This job is likely vanished. Not a problem, it's go
        shutil.rmtree(to_delete, ignore_errors=True)
        job_context["deleted_items"].append(to_delete)

    job_context["success"] = True
    return job_context

def run_janitor(job_id: int) -> None:
    pipeline = Pipeline(name=PipelineEnum.JANITOR.value)
    job_context = utils.run_pipeline(
        {"job_id": job_id, "pipeline": pipeline},
        [utils.start_job, _find_and_remove_expired_jobs, utils.end_job],
    )
    return job_context


import argparse
import os
import random
import sys
import time
import torch
from torch.backends import cudnn
from torch.utils.data import DataLoader
from torchvision.datasets import CIFAR100

sys.path.append("/share/home/kcli/CL_research/iCaRL_ILtFA")
from CIFAR.alg_model import resnetforcifar
from public.util_models import FE_cls, FE_2fc_cls, FE_3fc_cls

BASE_DIR = os.path.dirname(
    os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
sys.path.append(BASE_DIR)
from CIFAR.alg_model.config.config import Config

from public.data import get_dataset
from public.utils import AverageMeter, accuracy, get_logger

def parse_args():
    parser = argparse.ArgumentParser(description='PyTorch CIFAR10 Training')
    parser.add_argument('--model_name', type=str, default=Config.model_name, help=' CNN model_name')
    parser.add_argument('--dataset_root',
                        type=str,
                        default=Config.dataset_root,
                        help='cifar dataset path')
    parser.add_argument('--lr',
                        type=float,
                        default=Config.lr,
                        help='learning rate')
    parser.add_argument('--momentum',
                        type=float,
                        default=Config.momentum,
                        help='momentum')
    parser.add_argument('--weight_decay',
                        type=float,
                        default=Config.weight_decay,
                        help='weight decay')
    parser.add_argument('--gamma',
                        type=float,
                        default=Config.gamma,
                        help='gamma')
    parser.add_argument('--milestones',
                        type=list,
                        default=Config.milestones,
                        help='optimizer milestones')
    parser.add_argument('--epochs',
                        type=int,
                        default=Config.epochs,
                        help='num of training epochs')
    parser.add_argument('--batch_size',
                        type=int,
                        default=Config.batch_size,
                        help='batch size')
    parser.add_argument('--num_classes',
                        type=int,
                        default=Config.num_classes,
                        help='model classification num')
    parser.add_argument('--feature_dim',
                        type=int,
                        default=Config.feature_dim,
                        help='feature_dim')
    parser.add_argument('--num_workers',
                        type=int,
                        default=Config.num_workers,
                        help='number of worker to load data')
    parser.add_argument('--checkpoints',
                        type=str,
                        default=Config.checkpoint_path,
                        help='path for saving trained models')
    parser.add_argument('--log',
                        type=str,
                        default=Config.log,
                        help='path to save log')
    parser.add_argument('--evaluate',
                        type=str,
                        default=Config.evaluate,
                        help='path for evaluate model')
    parser.add_argument('--rate', type=float, default=Config.rate, help='model size')
    parser.add_argument('--seed', type=int, default=Config.seed, help='seed')
    parser.add_argument('--print_interval',
                        type=bool,
                        default=Config.print_interval,
                        help='print interval')
    parser.add_argument('--availabel_cudas',
                        type=str,
                        default=Config.availabel_cudas,
                        help='print interval')

    return parser.parse_args()

class cifar_train:
    def __init__(self, model_name, dataset_root, epochs, logger, lr, momentum, weight_decay, batch_size, num_workers,
                 milestones,
                 gamma, checkpoints, availabel_cudas, num_classes, feature_dim,
                 seed=0, rate=1):
        self.model_name = model_name
        self.root = dataset_root
        self.epochs = epochs
        self.logger = logger
        self.num_classes = num_classes
        self.feature_dim = feature_dim
        self.lr = lr
        self.momentum = momentum
        self.weight_decay = weight_decay
        self.seed = seed
        self.batch_size = batch_size
        self.num_workers = num_workers
        self.milestones = milestones
        self.gamma = gamma
        self.rate = rate
        self.checkpoints = checkpoints
        self.train_dataset = get_dataset("CIFAR100", 'train', dir=self.root)
        self.val_dataset = get_dataset("CIFAR100", 'test', dir=self.root)
        self.availabel_cudas = availabel_cudas
        self.net = self.construct_model(rate)
        # self.net = self.load_model("/share/home/kcli/CL_research/iCaRL_ILtFA/log/FE_cls_cifar10_preTrain_log/"
        #                               "resnet34/rate_/"
        #                               "cifar10_resnet34_rate_1_FE_cls.pth")
        # self.net = self.load_model("/share/home/kcli/CL_research/iCaRL_ILtFA/checkpoints/cifar10_resnet34_rate_0.125_FEcls.pth")
        # self.net = self.load_FE_2fc_cls_model("/share/home/kcli/CL_research/iCaRL_ILtFA/checkpoints/cifar10_resnet34_rate_1_FE_2fc_cls_64.pth")

    def construct_model(self, rate):
        # model = torch.nn.DataParallel(
        #     FE_3fc_cls(resnetforcifar.__dict__[self.model_name](rate=rate, get_feature=True), int(512 * rate),
        #                self.feature_dim, self.num_classes))

        # model = torch.nn.DataParallel(
        #     FE_2fc_cls(resnetforcifar.__dict__[self.model_name](rate=rate, get_feature=True), int(512 * rate),
        #                self.feature_dim, self.num_classes))

        model = torch.nn.DataParallel(FE_cls(resnetforcifar.__dict__[self.model_name](rate=rate, get_feature=True),
                                             int(512 * rate), self.num_classes))
        model = model.cuda()
        return model

    def load_model(self, model_path):
        if self.availabel_cudas:
            # os.environ['CUDA_VISIBLE_DEVICES'] = self.availabel_cudas
            # device_ids = [i for i in range(len(self.availabel_cudas.strip().split(',')))]
            # model = torch.nn.DataParallel(FE_cls(resnetforimagenet.__dict__[self.model_name](rate=rate, get_feature=True),
            #                                      self.hidden_size, self.num_classes),
            #                               device_ids=device_ids).cuda()
            model = FE_cls(resnetforcifar.__dict__[self.model_name](rate=self.rate, get_feature=True),
                           int(512 * self.rate),
                           self.num_classes)
            model.load_state_dict(
                torch.load(model_path))
            model = torch.nn.DataParallel(model).cuda()
            # model = torch.nn.DataParallel(model, device_ids=device_ids).cuda()
            # model = torch.nn.DataParallel(model, device_ids=device_ids).cuda()
        else:
            model = FE_cls(resnetforcifar.__dict__[self.model_name](rate=self.rate, get_feature=True),
                           int(512 * self.rate),
                           self.num_classes)
            model.load_state_dict(
                torch.load(model_path))
        return model
        pass

    def load_FE_2fc_cls_model(self, model_path):
        if self.availabel_cudas:
            # os.environ['CUDA_VISIBLE_DEVICES'] = self.availabel_cudas
            # device_ids = [i for i in range(len(self.availabel_cudas.strip().split(',')))]
            # model = torch.nn.DataParallel(FE_cls(resnetforimagenet.__dict__[self.model_name](rate=rate, get_feature=True),
            #                                      self.hidden_size, self.num_classes),
            #                               device_ids=device_ids).cuda()
            model = FE_2fc_cls(resnetforcifar.__dict__[self.model_name](rate=self.rate, get_feature=True),
                               int(512 * self.rate),
                               self.feature_dim, self.num_classes)
            model.load_state_dict(
                torch.load(model_path))
            model = torch.nn.DataParallel(model).cuda()
            # model = torch.nn.DataParallel(model, device_ids=device_ids).cuda()
            # model = torch.nn.DataParallel(model, device_ids=device_ids).cuda()
        else:
            model = FE_2fc_cls(resnetforcifar.__dict__[self.model_name](rate=self.rate, get_feature=True),
                               int(512 * self.rate),
                               self.feature_dim, self.num_classes)
            model.load_state_dict(
                torch.load(model_path))
        print(type(model))
        print(model)
        return model
        pass

    def load_FE_3fc_cls_model(self, model_path):
        if self.availabel_cudas:
            # os.environ['CUDA_VISIBLE_DEVICES'] = self.availabel_cudas
            # device_ids = [i for i in range(len(self.availabel_cudas.strip().split(',')))]
            # model = torch.nn.DataParallel(FE_cls(resnetforimagenet.__dict__[self.model_name](rate=rate, get_feature=True),
            #                                      self.hidden_size, self.num_classes),
            #                               device_ids=device_ids).cuda()
            model = FE_3fc_cls(resnetforcifar.__dict__[self.model_name](rate=self.rate, get_feature=True),
                               int(512 * self.rate),
                               self.feature_dim, self.num_classes)
            model.load_state_dict(
                torch.load(model_path))
            model = torch.nn.DataParallel(model).cuda()
            # model = torch.nn.DataParallel(model, device_ids=device_ids).cuda()
            # model = torch.nn.DataParallel(model, device_ids=device_ids).cuda()
        else:
            model = FE_3fc_cls(resnetforcifar.__dict__[self.model_name](rate=self.rate, get_feature=True),
                               int(512 * self.rate),
                               self.feature_dim, self.num_classes)
            model.load_state_dict(
                torch.load(model_path))
        print(type(model))
        print(model)
        return model
        pass

    def get_dataset(self, dataset_name, dataset_type):
        return get_dataset(dataset_name, type=dataset_type, download=True, capacity=None, dir=self.root)

    def validate_main(self, args):
        print("seed:", self.seed)
        print("model:", self.net)
        if self.seed is not None:
            random.seed(self.seed)
            torch.manual_seed(self.seed)
            torch.cuda.manual_seed_all(self.seed)
            cudnn.deterministic = True

        gpus = torch.cuda.device_count()
        self.logger.info(f'use {gpus} gpus')
        self.logger.info(f"args: {args}")

        cudnn.benchmark = True
        cudnn.enabled = True
        start_time = time.time()
        train_loader = DataLoader(self.train_dataset,
                                  batch_size=self.batch_size,
                                  shuffle=True,
                                  num_workers=self.num_workers,
                                  pin_memory=True)
        val_loader = DataLoader(self.val_dataset,
                                batch_size=self.batch_size,
                                num_workers=self.num_workers,
                                pin_memory=True)
        # for name, param in self.net.named_parameters():
        #     self.logger.info(f"{name},{param.requires_grad}")
        print("len(train_loader):", len(train_loader))
        acc1, acc5, throughput = self.validate(val_loader)
        rlt = f"val: top1 acc: {acc1:.2f}%, top5 acc: {acc5:.2f}%, throughput: {throughput:.2f}sample/s"
        self.logger.info(
            rlt
        )
        print(rlt)

    def train_main(self, args):
        print("seed:", self.seed)
        print("model:", self.net)
        if self.seed is not None:
            random.seed(self.seed)
            torch.manual_seed(self.seed)
            torch.cuda.manual_seed_all(self.seed)
            cudnn.deterministic = True

        gpus = torch.cuda.device_count()
        self.logger.info(f'use {gpus} gpus')
        self.logger.info(f"args: {args}")

        cudnn.benchmark = True
        cudnn.enabled = True
        start_time = time.time()
        train_loader = DataLoader(self.train_dataset,
                                  batch_size=self.batch_size,
                                  shuffle=True,
                                  num_workers=self.num_workers,
                                  pin_memory=True)
        val_loader = DataLoader(self.val_dataset,
                                batch_size=self.batch_size,
                                num_workers=self.num_workers,
                                pin_memory=True)
        print("len(train_loader):", len(train_loader))
        for name, param in self.net.named_parameters():
            self.logger.info(f"{name},{param.requires_grad}")
        criterion = torch.nn.CrossEntropyLoss().cuda()
        optimizer = torch.optim.SGD(self.net.parameters(),
                                    self.lr,
                                    momentum=self.momentum,
                                    weight_decay=self.weight_decay)

        scheduler = torch.optim.lr_scheduler.MultiStepLR(
            optimizer, milestones=self.milestones, gamma=self.gamma)
        best_acc = 0.0
        start_epoch = 1
        if not os.path.exists(self.checkpoints):
            os.makedirs(self.checkpoints)

        self.logger.info('start training')
        for epoch in range(start_epoch, args.epochs + 1):
            acc1, acc5, losses = self.train(train_loader, criterion, optimizer,
                                            scheduler, epoch, args)
            self.logger.info(
                f"train: epoch {epoch:0>3d}, top1 acc: {acc1:.2f}%, top5 acc: {acc5:.2f}%, losses: {losses:.2f}"
            )

            acc1, acc5, throughput = self.validate(val_loader)
            rlt = f"val: epoch {epoch:0>3d}, top1 acc: {acc1:.2f}%, top5 acc: {acc5:.2f}%, throughput: {throughput:.2f}sample/s"
            self.logger.info(
                rlt
            )
            print(rlt)
            if acc1 > best_acc:
                torch.save(self.net.module.state_dict(),
                           os.path.join(args.checkpoints, "best.pth"))
                best_acc = acc1
            # remember best prec@1 and save checkpoint
            # torch.save(
            #     {
            #         'epoch': epoch,
            #         'best_acc': best_acc,
            #         'acc1': acc1,
            #         'loss': losses,
            #         'lr': scheduler.get_lr()[0],
            #         'model_state_dict': self.net.state_dict(),
            #         'optimizer_state_dict': optimizer.state_dict(),
            #         'scheduler_state_dict': scheduler.state_dict(),
            #     }, os.path.join(args.checkpoints, 'latest.pth'))

        self.logger.info(f"finish training, best acc: {best_acc:.2f}%")
        training_time = (time.time() - start_time) / 3600
        self.logger.info(
            f"finish training, total training time: {training_time:.2f} hours")
        # torch.save(self.net, "reset34_rate1_8_cifar10_pretrain.pth")
        # checkpoint_file = os.path.join(self.checkpoints,
        #                                "cifar10_resnet34_rate_{}_FE_3fc_cls_{}.pth".format(self.rate, self.feature_dim))
        checkpoint_file = os.path.join(self.checkpoints,
                                       "cifar100_resnet34_rate_{}_FE_cls.pth".format(self.rate))
        torch.save(self.net.module.state_dict(), checkpoint_file)

    def train(self, train_loader, criterion, optimizer, scheduler, epoch, args):
        top1 = AverageMeter()
        top5 = AverageMeter()
        losses = AverageMeter()

        # switch to train mode
        self.net.train()

        iters = len(train_loader.dataset) // args.batch_size
        iter_index = 1

        for i, (inputs, labels) in enumerate(train_loader):
            inputs, labels = inputs.cuda(), labels.cuda()

            _, outputs = self.net(inputs)
            loss = criterion(outputs, labels)
            loss.backward()
            optimizer.step()
            optimizer.zero_grad()

            # measure accuracy and record loss
            acc1, acc5 = accuracy(outputs, labels, topk=(1, 5))
            top1.update(acc1.item(), inputs.size(0))
            top5.update(acc5.item(), inputs.size(0))
            losses.update(loss.item(), inputs.size(0))
            if iter_index % args.print_interval == 0:
                self.logger.info(
                    f"train: epoch {epoch:0>3d}, iter [{iter_index:0>4d}, {iters:0>4d}], lr: "
                    f"{scheduler.get_last_lr()[0]:.6f}, top1 acc: {acc1.item():.2f}%, "
                    f"top5 acc: {acc5.item():.2f}%, loss_total: {loss.item():.2f}"
                )

            iter_index += 1
        scheduler.step()
        return top1.avg, top5.avg, losses.avg

    def validate(self, val_loader):
        batch_time = AverageMeter()
        data_time = AverageMeter()
        top1 = AverageMeter()
        top5 = AverageMeter()

        # switch to evaluate mode
        self.net.eval()

        with torch.no_grad():
            end = time.time()
            total = 0
            correct = 0
            for inputs, labels in val_loader:
                data_time.update(time.time() - end)
                inputs, labels = inputs.cuda(), labels.cuda()
                _, outputs = self.net(inputs)
                acc1, acc5 = accuracy(outputs, labels, topk=(1, 5))
                top1.update(acc1.item(), inputs.size(0))
                top5.update(acc5.item(), inputs.size(0))
                batch_time.update(time.time() - end)
                end = time.time()
                total += labels.size(0)
                _, predicted = torch.max(outputs.data, 1)
                correct_temp = (predicted == labels).sum()
                correct += correct_temp
        throughput = 1.0 / (batch_time.avg / inputs.size(0))
        prect = 100. * correct / total
        return top1.avg, top5.avg, throughput

def main():
    args = parse_args()
    logger = get_logger(args.log)
    cifar_model = cifar_train(model_name=args.model_name, dataset_root=args.dataset_root, epochs=args.epochs,
                              logger=logger, lr=args.lr,
                              momentum=args.momentum, weight_decay=args.weight_decay, batch_size=args.batch_size,
                              num_workers=args.num_workers, milestones=args.milestones, gamma=args.gamma,
                              checkpoints=args.checkpoints, availabel_cudas=args.availabel_cudas,
                              num_classes=args.num_classes,
                              feature_dim=args.feature_dim,
                              seed=args.seed, rate=args.rate)
    cifar_model.train_main(args)
    # cifar_model.validate_main(args)

if __name__ == "__main__":
    # model = resnetforcifar.__dict__["resnet18"](rate=1/4, get_feature=True)
    # print(model.get_feature)
    # # print(model)
    # model2 = resnetforcifar.__dict__["resnet18"](rate=1 / 4)
    # print(model2.get_feature)
    main()


T = eval(input())
for i in range(T):
    K = eval(input())
    N = eval(input())
    prices = [int(x) for x in input().split()]
    dp = [[[0, 0] for j in range(K + 1)] for k in range(N)]
    for j in range(N):
        for k in range(1, K + 1):
            if j - 1 == -1:
                # dp[j][k][0] = 0
                dp[j][k][1] = -prices[j]
            else:
                dp[j][k][0] = max(dp[j - 1][k][0], dp[j - 1][k][1] + prices[j])
                dp[j][k][1] = max(dp[j - 1][k][1], dp[j - 1][k - 1][0] - prices[j])
    print(dp[N - 1][K][0])


from bs4 import BeautifulSoup
import lxml
from selenium import webdriver
import requests
from time import sleep

GOOGLE_FORM = "YOUR_GOOGLE_FORM_LINK"

# -------------------- BeautifulSoup ------------------ #
ZILLOW_URL = "YOUR_ZILLOW_URL"

HEADERS = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:86.0) Gecko/20100101 Firefox/86.0",
    "Accept-Language": "en-US,en;q=0.5"
}
response = requests.get(url=ZILLOW_URL, headers=HEADERS)
zillow_page = response.text

soup = BeautifulSoup(zillow_page, "lxml")

all_link_elements = soup.select(selector=".list-card-top a")
all_links = []
for link in all_link_elements:
    href = link.get("href")
    if "http" not in href:
        all_links.append(f"https://www.zillow.com{href}")
    else:
        all_links.append(href)

all_address_elements = soup.select(selector=".list-card-info address")
all_addresses = [address.text.split(" | ")[-1] for address in all_address_elements]

all_price_elements = soup.select(".list-card-heading")
all_prices = []
for element in all_price_elements:
    # Get the prices. Single and multiple listings have different tag & class structures
    try:
        # Price with only one listing
        price = element.select(".list-card-price")[0].contents[0]
    except IndexError:
        print('Multiple listings for the card')
        # Price with multiple listings
        price = element.select(".list-card-details li")[0].contents[0]
    finally:
        all_prices.append(price)

# --------------- Selenium ------------ #

FIREFOX_DRIVER_PATH = "geckodriver-v0.29.0-win64/geckodriver.exe"
driver = webdriver.Firefox(executable_path=FIREFOX_DRIVER_PATH)

for i in range(len(all_links)):
    driver.get(url=GOOGLE_FORM)
    sleep(3)

    address = driver.find_element_by_xpath("/html/body/div/div[2]/form/div[2]/div/div[2]/div[1]/div/div/div[2]/div/div[1]/div/div[1]/input")
    address.send_keys(all_addresses[i])
    price = driver.find_element_by_xpath("/html/body/div/div[2]/form/div[2]/div/div[2]/div[2]/div/div/div[2]/div/div[1]/div/div[1]/input")
    price.send_keys(all_prices[i])

    property_link = driver.find_element_by_xpath("/html/body/div/div[2]/form/div[2]/div/div[2]/div[3]/div/div/div[2]/div/div[1]/div/div[1]/input")
    property_link.send_keys(all_links[i])

    submit = driver.find_element_by_xpath("/html/body/div/div[2]/form/div[2]/div/div[3]/div[1]/div/div/span/span")
    submit.click()


# 바닥 장식
# 23.01.26
# 그래프 탐색
# https://www.acmicpc.net/problem/1388

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

N, M = map(int, input().split())
shape = []
for _ in range(N):
    shape.append(input().rstrip())

cnt = 0

for i in range(N):
    for j in range(M):
        if shape[i][j] == '-':
            if j + 1 < M and shape[i][j + 1] == '-':
                continue
            cnt += 1
        elif shape[i][j] == '|':
            if i + 1 < N and shape[i + 1][j] == '|':
                continue
            cnt += 1

print(cnt)


import numpy as np
import pandas as pd
import cv2
import os

from random import shuffle

""" WHAT'S THE FORMAT I WANT? METADATA (accident/no_accident)  SEPARATELY, PATHS TO IMAGES ELSEWHERE """

# Get the frames, videos, ... for the videos with accidents
acc_paths = []
acc_times = []

duos = []
i = 0 # Current output video
frame_opath = "/home/dd/arcelormittalvideos/accident_frames/final/"
with open("accident_times.txt", "r") as f:
    for line in f.readlines():
        vid, duration = line.strip().split(" ")

        # ENH: Also detect 1 class and 0 class types
        if duration in ["-1","many", "none", "0:00", "None"]: print("Skipping{}".format(vid));continue
        duos.append((vid, duration))

    # OK, we got the duos. Now shuffle and iterate again
    shuffle(duos)
    print("We have {} videos".format(len(duos)))
    for vid_idx, (vid, duration) in enumerate(duos):

        ipath = "/home/dd/arcelormittalvideos/accident{}".format(vid)
        if not os.path.exists(ipath): print("{} not existing".format(ipath))
        cap = cv2.VideoCapture(ipath)
        video_length = int(cap.get(cv2.CAP_PROP_FRAME_COUNT)) - 1
        def getFrame(sec, i, vid, cls):
            cap.set(cv2.CAP_PROP_POS_MSEC,sec*1000)
            hasFrames,image = cap.read()
            if hasFrames:
                print("Writing")
                this_o_path = frame_opath
                if vid_idx < len(duos) * 0.8:
                    subdir = "train"
                else:
                    subdir = "val"
                this_o_path = this_o_path + os.sep + subdir
                if not os.path.exists(this_o_path): os.mkdir(this_o_path)
                # Also add class
                this_o_path = this_o_path + os.sep + str(cls)
                if not os.path.exists(this_o_path): os.mkdir(this_o_path)

                cv2.imwrite(this_o_path + os.sep + vid +"_"+str(i)+"_"+str(cls)+".jpg", image)     # save frame as JPG file
            return hasFrames

        sec = 0
        frameRate = 1

        minutes, seconds = duration.split(":")
        total_seconds = int(minutes) * 60 + int(seconds)
        print(total_seconds)

        # Get before the accident
        success = True
        while success and sec < total_seconds:
            sec = sec + frameRate
            sec = round(sec, 2)
            i = i + 1
            success = getFrame(sec, i, vid, 0)

        # Get after the accident
        while success and sec < 2 * total_seconds:
            sec = sec + frameRate
            sec = round(sec, 2)
            i = i + 1
            success = getFrame(sec, i, vid, 1)

        cap.release()