import joblib
import pandas as pd
import numpy as np
from numpy import array
from pandas import DataFrame
from sklearn.preprocessing import LabelEncoder

def express_work_setup(data: DataFrame) -> DataFrame:
    """Generate all the minimal transformation to work with the Data

    Parameters
    ----------
    data : DataFrame:

    Returns
    -------
    DataFrame
    """
    df = data
    df = (df
          .dropna(subset=['colonia', 'm2_const', 'habitaciones', 'banos', 'autos', 'price', 'link_img', 'agent'])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_terreno.isna()))])
          .pipe(lambda df_: df_[(df_.m2_const > 20) & (df_.m2_const < 5000)])  # Remove values less than 10
          .pipe(lambda df_: df_[df_.price > 100])  # Remove listings below 100
          .pipe(lambda df_: df_[~((df_.tipo_oferta == 'Venta') & (df_.price < 100000))])
          .pipe(lambda df_: df_[df_.price < 100000000])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_terreno <= 50))])  # rm houses with less 50m2
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_terreno >= 5000))])
          .pipe(lambda df_: df_[~((df_.habitaciones == 0) | (df_.banos == 0))])
          .pipe(lambda df_: df_[df_.banos < 10])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Departamento') & (df_.habitaciones > 7))])
          .pipe(lambda df_: df_[df_.habitaciones < 10])
          .pipe(lambda df_: df_[df_.autos < 30])
          .pipe(lambda df_: df_[~((df_.autos == 0) & (df_.colonia.str.contains('Vallarta') == False))])
          # Renta
          .pipe(lambda df_: df_[~((df_.tipo_oferta == 'Renta') & (df_.price > 150000))])
          .assign(
        m2_price=lambda df_: df_.price / df_.m2_terreno,  # price / m2_terreno
        m2_const_price=lambda df_: df_.price / df_.m2_const,  # price / m2_const
        m2_abs_price=lambda df_: df_.price / (df_.m2_terreno + df_.m2_const),  # price / m2_terreno + m2_const
        fecha_pub=lambda df_: pd.to_datetime(df_.fecha_pub),
        descripcion=lambda df_: df_.descripcion.str.replace('"text":', '', regex=True))
          .pipe(lambda df_: df_[~(df_.m2_const_price > 100000)])
          .pipe(lambda df_: df_[~((df_.tipo_inmueble == 'Casa') & (df_.m2_price > 90000))])
          .pipe(create_sector_inmo)
          .astype(
        {'tipo_inmueble': 'category', 'sub_category': 'category', 'tipo_oferta': 'category', 'colonia': 'category',
         'estado': 'category', 'ciudad': 'category',
         'price': 'float32', 'm2_price': 'float32', 'm2_const_price': 'float32', 'm2_abs_price': 'float32',
         'm2_terreno': 'float32', 'm2_const': 'float32',
         'habitaciones': 'int16', 'banos': 'int16', 'autos': 'int16', 'antiguedad': 'float32'})
          .drop(columns=['divisa', 'total_habitaciones', 'link', 'link_img', 'agent'])
          ).reset_index().drop(columns='index')
    return df

def rm_outliers_by_colonia(data: DataFrame, columns: list = None) -> DataFrame:
    """ Remove the Outliers by Colonia in the DataFrame Selected.

    ## Use this list of colonies to Test: ['Puerta de Hierro', 'Valle Real', 'Americana', 'Providencia 1a Secc']

    Parameters
    ----------
    data : DataFrame:
        DataFrame that will be clean for its outliers.
    columns : list:
        List of the name of the features/columns that would remove the outliers.
         (Default value = None)

    Returns
    -------
    DataFrame
    """
    if columns is None:
        columns = ['price', 'm2_const', 'm2_terreno', 'habitaciones', 'banos', 'autos']

    df = data

    # Get Data Skew information Table
    skew_features_tb = identify_skew_features(df, columns)
    # Get Index Outliers
    skew_colonias_name = get_colonies_outliers_name(skew_features_tb)
    # Remove outliers by Colonia
    clean_data = rm_skew_outliers(df, skew_colonias_name, columns)

    return clean_data

def identify_skew_features(data: DataFrame, columns: list) -> DataFrame:
    """Create a table with the Skew Resume to identify the Observation that have one or more Outliers.

    Parameters
    ----------
    data :  DataFrame:

    columns : list:
        List of the name of the features/columns that would remove the outliers.

    groupby :
         (Default value = 'colonia')

    Returns
    -------

    """
    return (data
            .groupby('colonia', observed=True)[columns]
            .skew())

def get_colonies_outliers_name(skew_features_tb: DataFrame) -> array:
    """Create an Array of the Name of the Colonies

    Parameters
    ----------
    skew_features_tb : DataFrame:
        Array of the features name it would be removed the outliers.

    Returns
    -------
    array
    """
    positive_skew_features = skew_features_tb > 1
    negative_skew_features = skew_features_tb < -1
    # Select Positive or Negative Skew data
    skew_features = (
        skew_features_tb[(positive_skew_features) | (negative_skew_features)]
            .notna()  # Identify Only the Skew Observation
        # .any(axis=1)
    ).pipe(lambda df_: df_[df_.any(axis=1)]).index  # Select if any of the row have Outliers

    return skew_features

def rm_skew_outliers(data: DataFrame, skew_colonias_name: array, columns: list) -> DataFrame:
    """Remove the Outliers

    Parameters
    ----------
    data : DataFrame:
        DataFrame that will be clean for its outliers.

    skew_colonias_name : list:
        Array with the name of the Colonies with outliers.
    columns : list:
        List of the name of the features/columns that would remove the outliers.

    Returns
    -------
    DataFrame
    """
    df = data
    # iterate colonia names list
    for colonia in skew_colonias_name:
        # select the colonia subset DataFrame
        colonia_subset = df[df.colonia == colonia]
        # Iterate over the Colums with outliers
        for col in columns:
            # Second outliers Check
            # Positive +
            if colonia_subset[col].skew() > 1:
                # select low and hig quantiles value of the feautre
                low = colonia_subset[col].quantile(0.01)
                hig = colonia_subset[col].quantile(0.95)
                # Select only the outliers
                listings_outliers = colonia_subset.loc[(colonia_subset[col] <= low) | (colonia_subset[col] >= hig)]
                # Subset only not outliers
                df = df[~df.index.isin(listings_outliers.index)]

            # Negative -
            if colonia_subset[col].skew() < -1:
                # select low and hig quantiles value of the feautre
                low = colonia_subset[col].quantile(0.05)
                hig = colonia_subset[col].quantile(0.99)
                # Select only the outliers
                listings_outliers = colonia_subset.loc[(colonia_subset[col] <= low) | (colonia_subset[col] >= hig)]
                # Subset only not outliers
                df = df[~df.index.isin(listings_outliers.index)]

    return df.drop(columns=['sku'])

def set_dummies_features(data):
    df = data

    # Creating Dummi Variables
    offer_dummies = pd.get_dummies(df.tipo_oferta, prefix='dummie', drop_first=True)
    type_dummies = (pd.get_dummies(df.tipo_inmueble, prefix='dummie')
                    .drop(columns='dummie_Departamento'))
    # Concatenate the Tables
    df_result = (pd.concat([df, offer_dummies, type_dummies], axis=1)
                 .rename(columns={'dummie_Venta': 'venta', 'dummie_Casa': 'casa'}))

    return df_result

def colonies_encoder(data, save_file: str = None):
    df = data
    # Create Encoder
    encoder = LabelEncoder().fit(df['colonia'])
    df_result = df.assign(endocer_colonia=encoder.transform(df['colonia']))

    if save_file is not None:
        joblib.dump(encoder, save_file)

    return df_result

def create_sector_inmo(data: DataFrame) -> DataFrame:
    """Create a new Column call it 'sector_inmo' that contains the categories of the listings
            economical sectors.

    The categories are: 'Interés Social', 'Interés Medio', 'Residencial', 'Residencial Plus', 'Premium'.
    Depends on the type of offer (sell or rent) and the price that the label would be selected.

    Parameters
    ----------
    data : DataFrame:

    Returns
    -------

    """
    df = data
    # Create Sector Inmobiliario Column
    categories = ['Interés Social', 'Interés Medio', 'Residencial', 'Residencial Plus', 'Premium']
    df['sector_inmo'] = pd.Series(categories).astype('category')
    # Select Offer Types
    venta = (df.tipo_oferta == 'Venta')
    renta = (df.tipo_oferta == 'Renta')
    # Use a dict to iterate over the strings and subsets.
    offers = {'venta': venta, 'renta': renta}

    for offer_txt, offer_df in offers.items():

        if offer_txt == 'venta':
            interes_social = df.price.lt(1000001)
            interes_medio = df.price.between(1000001, 3000000)
            residencial = df.price.between(3000001, 7000000)
            residencial_plus = df.price.between(7000001, 15000000)
            premium = df.price.gt(15000000)
            sectors = [interes_social, interes_medio, residencial, residencial_plus, premium]

        else:  # renta
            interes_social = df.price.lt(5001)
            interes_medio = df.price.between(5001, 10000)
            residencial = df.price.between(10001, 15000)
            residencial_plus = df.price.between(15001, 30000)
            premium = df.price.gt(30000)
            sectors = [interes_social, interes_medio, residencial, residencial_plus, premium]

        for sector, category in zip(sectors, categories):
            df.loc[sector & offer_df, 'sector_inmo'] = category

    return df

def set_amenidades(data: DataFrame, column: str = 'amenidades') -> DataFrame:
    """Create N number of new Columns of the total amenities in the column 'amenidades' lists.

    This column contains list of amenities, and it is variable. The program don't know how many new columns
    will be created. So this proces is O(n2).

    Parameters
    ----------
    data : DataFrame:

    column : str : (Default value = 'amenidades')
        The column Name that contain the Raw List of amenities.

    Returns
    -------

    """
    df = data.reset_index()

    list_amenidades = list()
    # Unpacking the row amenities
    for i, row_list in enumerate(df[column]):
        if row_list is not None:  # Validate is not empty
            # Create/Add columns amenity value.
            for amenidad in row_list:
                df.loc[i, amenidad] = True
                # Add empty list
                if amenidad not in list_amenidades:
                    list_amenidades.append(amenidad)
    # Add Nan to the empty values to prevente errors with SQL DataBase
    for amenidad in list_amenidades:
        df[amenidad] = df[amenidad].replace(np.nan, False)

    return df


def black_tiles(v):
    tiles = {}
    for tile in v:
        nes = tile.count('ne')
        ses = tile.count('se')
        nws = tile.count('nw')
        sws = tile.count('sw')
        es = tile.count('e') - nes - ses
        ws = tile.count('w') - nws - sws
        i = 0.5 * (sws + ses - nes - nws)
        j = es - ws + 0.5 * (ses + nes - sws - nws)
        tiles[(i, j)] = not tiles.get((i, j), False)
    return set(t for t, is_black in tiles.items() if is_black)

def part1(v):
    print(len(black_tiles(v)))

def part2(v):
    dirs = [(-0.5, +0.5), (0, +1), (+0.5, +0.5), (+0.5, -0.5), (0, -1), (-0.5, -0.5)]

    def count_black_adj(d, i, j):
        return sum((i + di, j + dj) in d for di, dj in dirs)

    def adjs(i, j):
        return ((i + di, j + dj) for di, dj in dirs)

    today = black_tiles(v)
    for _ in range(100):
        tomorrow = set()
        for black_tile in today:
            if 1 <= count_black_adj(today, *black_tile) <= 2:
                tomorrow.add(black_tile)
            for adj in adjs(*black_tile):
                is_white = adj not in today
                if is_white and count_black_adj(today, *adj) == 2:
                    tomorrow.add(adj)
        today = tomorrow
    print(len(today))

with open("input/day24.txt") as f:
    v = [line.replace('\n', '') for line in f]
    part1(v)
    part2(v)


import numpy as np
import scipy.signal as sig
import matplotlib.pyplot as plt
import math
from mpl_toolkits.mplot3d import Axes3D
import os
import sys

direc = sys.argv[1]

final_data, files_list = {}, []
data_dir = 'Optimizations/{}/'.format(direc)
for f in os.listdir(data_dir):
    if f.endswith('.npy') and f.startswith('data_'):
        files_list.append([int(f.split('_')[1].split('-')[0]), f])
files_list.sort()

for i, fd in enumerate(files_list):
    files_list[i] = np.load(data_dir+fd[1], allow_pickle=True).item()

for key in files_list[0]:
    final_data[key] = np.concatenate(list(d[key] for d in files_list))
del files_list
print('----------------------------------------------------')
print('Water level is currently at: {}'.format(final_data['Water Levels'][-1]))
print('Best metric so far at: {}, {}/{} iterations in'.format(
    min(final_data['Metrics']), final_data['Iterations'][np.argmin(final_data['Metrics'])]+1, final_data['Iterations'][-1]+1))
print('Longest in a row without improvement: {}'.format(max(final_data['Stopping Iterations'])))
print('----------------------------------------------------')

plt.figure(dpi=300)
plt.plot(final_data['Iterations'], final_data['Metrics'], label='Metrics', alpha=0.5, color='grey')
plt.plot(final_data['Iterations'], final_data['Water Levels'], label='Water Level', linewidth=0.5, color='black')
plt.legend()
plt.xlabel('# Iterations')
plt.savefig(data_dir+'Plots/MIDRUN_Water_Level_Evolution.png',
            bbox_inches='tight', facecolor='white')
plt.close()
print('Saved midrun water level evolution plot')

fig, [ax1, ax2, ax3] = plt.subplots(nrows=3, sharex=True, dpi=300, figsize=[10,7])

ax1.plot(final_data['Iterations'], final_data['Q Metrics'], color='red', label='Cross Correlation')
ax1.legend(loc=1, fontsize=9)
ax2.plot(final_data['Iterations'], final_data['Sens Metrics'], color='green', label='Sensitivity')
ax2.legend(loc=1, fontsize=9)
ax3.plot(final_data['Iterations'], final_data['Flat Metrics'], color='blue', label='Flat Diff')
ax3.legend(loc=1, fontsize=9)
ax3.set_xlabel('# of Iterations')
fig.tight_layout()
plt.savefig(data_dir+'Plots/MIDRUN_Metrics_Evolution.png',
            bbox_inches='tight', facecolor='white')
plt.close()
print('Saved midrun metrics evolution plot')

plt.figure(dpi=300)
plt.plot(final_data['Iterations'], final_data['Stopping Iterations'], color='maroon')
plt.xlabel('# Iterations')
plt.ylabel('# Iterations without improvement')
plt.savefig(data_dir+'Plots/MIDRUN_stopping_iterations_Evolution_test.png',
            bbox_inches='tight', facecolor='white')
plt.close()
print('Saved midrun stopping iterations evolution')


from microbit import *
import neopixel
from math import sqrt, pow, ceil
import random

answers = [
    "It is certain",
    "It is decidedly so",
    "Without a doubt",
    "Yes, definitely",
    "You may rely on it",
    "As I see it, yes",
    "Most likely",
    "Outlook good",
    "Yes",
    "Signs point to yes",
    "Reply hazy try again",
    "Ask again later",
    "Better not tell you now",
    "Cannot predict now",
    "Concentrate and ask again",
    "Don't count on it",
    "My reply is no",
    "My sources say no",
    "Outlook not so good",
    "Very doubtful",
]

display.show('8')

numLEDs = 24
np = neopixel.NeoPixel(pin0, numLEDs)
while True:
    readingx = accelerometer.get_x()
    readingy = accelerometer.get_y()
    readingz = accelerometer.get_z()

    if accelerometer.was_gesture('shake'):
        display.clear()
        for i in range(24):
            np[i] = (0,0,0)
        np.show()
        sleep(2000)
        display.scroll(random.choice(answers))

    for i in range(24):
        np[i] = (
            min(abs(readingx//10),255),
            min(abs(readingy//10), 255),
            min(abs(readingz//10),255)
            )
        np.show()

#


def replace(self, year=None, month=None, day=None, hour=None,
                minute=None, second=None, microsecond=None, tzinfo=True):
        """Return a new datetime with new values for the specified fields."""
        if year is None:
            year = self.year
        if month is None:
            month = self.month
        if day is None:
            day = self.day
        if hour is None:
            hour = self.hour
        if minute is None:
            minute = self.minute
        if second is None:
            second = self.second
        if microsecond is None:
            microsecond = self.microsecond
        if tzinfo is True:
            tzinfo = self.tzinfo
        return datetime.__new__(type(self),
                                year, month, day, hour, minute, second,
                                microsecond, tzinfo)


'''
DTMF detection based on FFT.
This is a pedagogical tool. The implemented algorithm here is not as efficient,
as an implementation based on Goertzel algorithm [1,2].

Inspired on Ali Jamali Zavare's code: https://github.com/alijamaliz/DTMF-detector
I removed the dependency on PyAudio, which may require a C compiler.
And changed the algorithm to use peak picking.

[1] https://en.wikipedia.org/wiki/Goertzel_algorithm
[2] https://github.com/EliasOenal/multimon-ng/ and https://github.com/EliasOenal/multimon-ng/blob/master/demod_dtmf.c

October 10, 2020 - Aldebaro Klautau - www.lasse.ufpa.br/aldebaro
'''
from scipy.io import wavfile as wav
from scipy.fftpack import fft
from scipy.io import wavfile
#import wave
import numpy as np
import matplotlib.pyplot as plt
from scipy.signal import find_peaks

audio_file_name = 'dial2.wav' #input file in WAV format
should_plot = False #use True if want to see auxiliary plots or False otherwise
silence_duration = 80e-3 #in seconds, interval between two DTMF tones. It depends on the DTMF standard
window_duration =  50e-3 # in seconds, analysis windows. Suggestion: use as half of the DTMF duration
shift_duration = 10e-3 # in seconds, window shift
window_power_threshold = 6 # in dB, below this threshold from the maximum the window is considered silence
frequencies_power_threshold = 10 # in dB, below this threshold from the maximum the FFT value is considered zero
spectrum_resolution = 4 #in Hz, spectrum (FFT) resolution. The smaller this number, the larger the FFT size will be
frequency_error_tolerance = 5 #in Hz, tolerance to error when comparing estimated DTMF frequencies with nominal values

def nextpow2(x):
    return int(np.ceil(np.log2(np.abs(x))))

def extract_block(x, num_block, window_length, window_shift):
    first_sample = num_block * window_shift
    last_sample = first_sample + window_length
    return x[first_sample:last_sample]

def isNumberInArray(array, number, tolerance=5):
    for i in range(number - frequency_error_tolerance, number + frequency_error_tolerance):
        if i in array:
            return True
    return False

DTMF_TABLE = {
    '1': [1209, 697],
    '2': [1336, 697],
    '3': [1477, 697],
    'A': [1633, 697],

    '4': [1209, 770],
    '5': [1336, 770],
    '6': [1477, 770],
    'B': [1633, 770],

    '7': [1209, 852],
    '8': [1336, 852],
    '9': [1477, 852],
    'C': [1633, 852],

    '*': [1209, 941],
    '0': [1336, 941],
    '#': [1477, 941],
    'D': [1633, 941],
}

# reading audio
Fs, signal = wav.read(audio_file_name) #change for your file name
Ts = 1.0/Fs #sampling interval = 1 / (sampling frequency)

window_length = round(window_duration / Ts)
window_shift = round(shift_duration / Ts)
silence_length = round(silence_duration / Ts)

print('Some information about file: ', audio_file_name)
print('Sampling frequency = ', Fs, 'Hz')
print('Sampling interval = ', Ts, 's')
print('Window length = ', window_length, 'samples')
print('Window shift = ', window_shift, 'samples')
print('Silence length = ', silence_length, 'samples')
print('')

#choose a strategy to determine the frequency (FFT) resolution
if False:
    num_fft = 2 ** nextpow2(window_length) #use FFT with size power of 2 for speed
    spectrum_resolution = Fs / num_fft #in Hz
else:
    num_fft = np.ceil(Fs/spectrum_resolution)
    num_fft = 2 ** nextpow2(num_fft) #use FFT with size power of 2 for speed
    spectrum_resolution = Fs / num_fft #in Hz

signal_length = len(signal)
#number of windows to analyze
num_windows = int( (signal_length - window_length) / (window_shift)) + 1

#detect the power in dB of each analysis window. The goal is to detect silence between DTMF tones
window_power = np.zeros((num_windows,), np.float)
for i in range(num_windows):
    signal_window = extract_block(signal, i, window_length, window_shift) #get one window for analysis
    energy = np.linalg.norm(signal_window) ** 2 #squared norm is the energy
    average_power = energy / window_length #average power is normalized energy
    if average_power > 0: #avoid log of zero
        window_power[i] = 10.0 * np.log10(average_power) #convert from Watts to dB

if should_plot:
    #plot the signal and the power per window
    plt.subplot(311)
    plt.plot(signal)
    plt.ylabel('Signal amplitude')
    plt.subplot(312)
    plt.plot(window_power)
    plt.ylabel('Power per window')

max_power = np.max(window_power) #get maximum power over all windows
min_power = max_power - window_power_threshold #required minimum power in dB

#use binary array to distinguish DTMF (element = 1) or silence (element = 0)
#when finding consecutive runs of ones, choose a single representative: the one
#with maximum power. For instance, transform:
#0 1 1 1 0 0 0
#into:
#0 0 0 1 0 0 0    <= a single 1 represents the sequence of 3 1's above
#assuming the fourth window has more power than the second and third.
window_represents_DTMF = np.zeros((num_windows,), np.int) #initialize
interval_best_index = -1 #initialize, best index for the current "run" of ones
interval_max_power = -np.inf #initialize
for window_index in range(num_windows):
    if window_power[window_index] < min_power:
        #it is a "silence", reset:
        interval_best_index = -1
        interval_max_power = -np.inf
    else: #it is a window with enough power to be a potential DTMF
        if window_power[window_index] > interval_max_power:
            #current power is larger than the best up to this moment
            if interval_best_index != -1:
                window_represents_DTMF[interval_best_index] = 0 #erase previous decision
            #update who is the "best":
            interval_max_power = window_power[window_index]
            interval_best_index = window_index
            window_represents_DTMF[interval_best_index] = 1

#filter very short silence segments such as 0 0 1 0 0, to turn it
#into 0 0 0 0 0 based on silence_duration in seconds, specified by the user,
#which was alredy converted to silence_length in samples
last_found_DTMF = -100000 #initialize with negative number
#each element of window_represents_DTMF represents a window, not
#a sample. Therefore, we need to define how many windows corresponds to the duration of silence:
silence_interval_in_windows = int(silence_length / window_shift)
if silence_interval_in_windows < 1:
    silence_interval_in_windows = 1 #should be at least 1
for window_index in range(num_windows):
    if window_represents_DTMF[window_index] == 1:
        #check how far is the current index 1 from previous index 1
        difference_in_indices = window_index - last_found_DTMF
        if difference_in_indices  <= silence_interval_in_windows:
            window_represents_DTMF[window_index] = 0 #flip the current 1, to indicate silence now
        else:
            last_found_DTMF = window_index #update for next iteration

if should_plot:
    #plot the signal and the power per window
    plt.subplot(313)
    plt.plot(window_represents_DTMF)
    plt.ylabel('DTMF candidates')
    plt.show()

#At this stage we have a single window representing each DTMF tone.
#That is, if a tone of duration 200 ms was mapped to 4 windows of 50 ms each,
#we chose one among these four windows to represent the tone.
#
#The next stage is to recognize the frequencies using peak picking of the
#spectrum calculated for each window
for window_index in range(num_windows):
    if window_represents_DTMF[window_index] == 0:
        continue #skip the windows that are not representing a DTMF tone

    #get a single window of the signal to analyze
    signal_window = extract_block(signal, window_index, window_length, window_shift)

    #Calculate fourier trasform of this frame of the signal
    fourier_transform = np.fft.fft(signal_window, num_fft)
    spectrum_magnitude = np.abs(fourier_transform) / num_fft #normalize by the FFT length
    spectrum_magnitude_dB = 20 * np.log10(spectrum_magnitude) #convert to dB
    spectrum_magnitude_dB = spectrum_magnitude_dB[0:int(num_fft / 2)] #discard "negative" frequencies

    #Discard (make zero) all magnitudes above specified threshold
    max_frequency_amplitude = np.max(spectrum_magnitude_dB)
    zeroed_indices = spectrum_magnitude_dB < max_frequency_amplitude - frequencies_power_threshold
    spectrum_magnitude_dB[zeroed_indices] = 0

    #find the two strongest peaks
    peaks, _ = find_peaks(spectrum_magnitude_dB, height=0)  #only positive frequencies
    sortead_peak_index = np.argsort(spectrum_magnitude_dB[peaks])
    first_frequency_index = peaks[sortead_peak_index[-1]] #sort worked in ascending order, get last
    second_frequency_index = peaks[sortead_peak_index[-2]]

    #make sure first_frequency < second_frequency (this is not really necessary)
    if first_frequency_index > second_frequency_index:
        temp = first_frequency_index
        first_frequency_index = second_frequency_index
        second_frequency_index = temp

    #convert from index to frequency in Hertz
    first_frequency = first_frequency_index * spectrum_resolution
    second_frequency = second_frequency_index * spectrum_resolution

    if should_plot:
        plt.subplot(311)
        plt.plot(signal_window)
        plt.subplot(312)
        #generate frequency axis for plots
        f_axis = np.arange(0,len(spectrum_magnitude_dB))*spectrum_resolution
        plt.plot(f_axis, spectrum_magnitude_dB)
        #indicate the location of the 2 peaks
        plt.plot(first_frequency, spectrum_magnitude_dB[first_frequency_index], "x")
        plt.plot(second_frequency, spectrum_magnitude_dB[second_frequency_index], "x")
        plt.show()

    #pack the two frequencies in a list
    detectedFrequencies = list()
    detectedFrequencies.append(int(round(second_frequency)))
    detectedFrequencies.append(int(round(first_frequency)))
    print('Detected frequencies:', detectedFrequencies)

    #check if the 2 detected frequencies compose a DTMF tone, and print it if the test is positive
    for char, frequency_pair in DTMF_TABLE.items():
        if (isNumberInArray(detectedFrequencies, frequency_pair[0],tolerance=frequency_error_tolerance) and
            isNumberInArray(detectedFrequencies, frequency_pair[1],tolerance=frequency_error_tolerance)):
            print ('Decoded as ', char, '(nominal frequencies: ', frequency_pair,')')
            print('')
            break


def add(path):
    '''Add an environment to the cache. Allows you to activate the environment
    by name instead of by full path'''

    click.echo('\nAdding {} to cache......'.format(path), nl=False)
    try:
        r = cpenv.resolve(path)
    except Exception as e:
        click.echo(bold_red('FAILED'))
        click.echo(e)
        return

    if isinstance(r.resolved[0], cpenv.VirtualEnvironment):
        EnvironmentCache.add(r.resolved[0])
        EnvironmentCache.save()
        click.echo(bold_green('OK!'))


import tweepy, time
from Codes import *

'''So, i don't know what I am doing, let's hope it works'''

auth = tweepy.OAuthHandler(CONSUMER_KEY, CONSUMER_SECRET)
auth.set_access_token(ACCESS_KEY, ACCESS_SECRET)
api = tweepy.API(auth)

filename = open('QUOTES.txt','r', encoding='utf8')
tweettext = filename.readlines()
filename.close()

for line in tweettext[0:100]:
    api.update_status(line)
    print(line)
    time.sleep(86400) # Sleep for 1 day

print("All done!")


import click
import json
import requests

from bs4 import BeautifulSoup

from libs import stock_service
from libs.constants import DB_NAME, COL_STOCKS
from libs.mongo_service import MongoService

class StockData:

    def __init__(self, rank, name, ticker, weight):
        self.rank = rank
        self.name = name
        self.ticker = ticker
        self.weight = weight

@click.group(name="web")
def web_cmd():
    pass

@web_cmd.command(name="update")
def update_sp500():
    headers = {'User-Agent': 'Mozilla/5.0 (Windows NT 6.1; WOW64; rv:50.0) Gecko/20100101 Firefox/50.0'}
    res = requests.get("https://www.slickcharts.com/sp500", headers=headers)
    if res.status_code == 200:
        stock_data = []
        soup = BeautifulSoup(res.text, "lxml")
        table = soup.find('table', attrs={'class': 'table table-hover table-borderless table-sm'})
        table_body = table.find('tbody')
        rows = table_body.find_all('tr')
        for row in rows:
            cols = row.find_all('td')
            cols = [ele.text.strip() for ele in cols]

            rank = int(cols[0])
            name = cols[1]
            ticker = cols[2].upper()
            weight = float(cols[3])
            stock_data.append(StockData(rank, name, ticker, weight).__dict__)

        with open("data/sp500.json", "w") as f:
            f.write(json.dumps(stock_data, indent=2))
        return
    raise Exception(f"status code {res.status_code}")


input = open("inputs/day11.txt", "r").readlines()

# Parse input
SZ = 10
grid = [list(map(int, line[:-1])) for line in input]

# Utility function to obtain adjacent tiles
def adjacent(x, y):
    for dx in -1, 0, 1:
        for dy in -1, 0, 1:
            if dx == 0 == dy:
                continue
            if 0 <= x + dx < SZ and \
               0 <= y + dy < SZ:
                yield (x + dx, y + dy)

# Utility function to simulate a step
def simulate(grid):
    queue = []
    flashed = set() # Tracks all flashing octopuses
    # Increment all octopuses by 1
    for x in range(SZ):
        for y in range(SZ):
            grid[x][y] += 1
            if grid[x][y] > 9:
                queue.append((x, y))
                flashed.add((x, y))
    # BFS to flash the remaining octopuses
    for x, y in queue:
        for nx, ny in adjacent(x, y):
            grid[nx][ny] += 1
            if grid[nx][ny] > 9 and (nx, ny) not in flashed:
                queue.append((nx, ny))
                flashed.add((nx, ny))
    # Mark all flashed to 0
    for x, y in flashed:
        grid[x][y] = 0
    return len(flashed)

step = 1
count = []
while True:
    count.append(simulate(grid))
    if count[-1] == SZ * SZ:
        print(sum(count[:100]))
        print(step)
        break
    step += 1


import pandas as pd

pd.options.mode.chained_assignment = (
    None  # ignore all warnings like SettingWithCopyWarning
)

import numpy as np
from typing import Callable
import os, glob
import shutil
import pickle
from ml_collections.config_dict import ConfigDict

def _flatten_dict(input_dict, parent_key="", sep="."):
    """
    Based on https://github.com/google/flax/blob/main/flax/metrics/tensorboard.py
    Flattens and simplifies dict such that it can be used by hparams.
    Args:
        input_dict: Input dict, e.g., from ConfigDict.
        parent_key: String used in recursion.
        sep: String used to separate parent and child keys.
    Returns:
        Flattened dict.
    """
    items = []
    for k, v in input_dict.items():
        new_key = parent_key + sep + k if parent_key else k

        # Valid types according to https://github.com/tensorflow/tensorboard/blob/1204566da5437af55109f7a4af18f9f8b7c4f864/tensorboard/plugins/hparams/summary_v2.py
        valid_types = (
            bool,
            int,
            float,
            str,
            np.bool_,
            np.integer,
            np.floating,
            np.character,
        )

        if isinstance(v, dict) or isinstance(v, ConfigDict):
            # Recursively flatten the dict.
            if isinstance(v, ConfigDict):
                v = v.to_dict()
            items.extend(_flatten_dict(v, new_key, sep=sep).items())
            continue
        elif not isinstance(v, valid_types):
            # Cast any incompatible values as strings such that they can be handled by hparams
            v = str(v)
        items.append((new_key, v))
    return dict(items)

def walk_through(
    path: str,
    metric: str,
    query_fn: Callable,
    start: int,
    end: int,
    steps: int,
    window: int,
    cutoff: float = 0.9,
    delete: bool = False,
):
    def isnan(number):
        return np.isnan(float(number))

    def smooth(df):
        try:
            df = df.dropna(subset=[metric])  # remove NaN rows
        except KeyError:
            print("!!key error csv", run)
            if delete:
                shutil.rmtree(run)
                print("deleted")
            return None

        if isnan(df["env_steps"].iloc[-1]) or df["env_steps"].iloc[-1] < cutoff * end:
            # an incomplete run
            print("!!incomplete csv", run, "num steps", df["env_steps"].iloc[-1])
            if delete:
                shutil.rmtree(run)
                print("deleted")
            else:
                print("\n")
            return None

        # smooth by moving average
        df[metric] = df[metric].rolling(window=window, min_periods=1).mean()

        # update the columns with interpolated values and aligned steps
        aligned_step = np.linspace(start, end, steps).astype(np.int32)
        ## we only do interpolation, not extrapolation
        aligned_step = aligned_step[aligned_step <= df["env_steps"].iloc[-1]]
        aligned_value = np.interp(aligned_step, df["env_steps"], df[metric])

        # enlarge or reduce to same number of rows
        print(run, df.shape[0], df["env_steps"].iloc[-1])

        extracted_df = pd.DataFrame(
            data={
                "env_steps": aligned_step,
                metric: aligned_value,
            }
        )

        return extracted_df

    dfs = []
    i = 0

    runs = sorted(glob.glob(os.path.join(path, "*")))

    for run in runs:
        flags = pickle.load(open(os.path.join(run, "flags.pkl"), "rb"))

        if not query_fn(flags):
            continue

        csv_path = os.path.join(run, "progress.csv")
        try:
            df = pd.read_csv(open(csv_path))
        except pd.errors.EmptyDataError:
            print("!!empty csv", run)
            if delete:
                shutil.rmtree(run)
                print("deleted")
            continue

        df = smooth(df)
        if df is None:
            continue
        i += 1

        # concat flags (dot)
        pd_flags = pd.json_normalize(_flatten_dict(flags))
        df_flag = pd.concat([pd_flags] * df.shape[0], axis=0)  # repeat rows
        df_flag.index = df.index  # copy index
        df = pd.concat([df, df_flag], axis=1)
        dfs.append(df)
        # print(flags)

    print("\n in total:", i)
    dfs = pd.concat(dfs, ignore_index=True)
    return dfs


import os

from jwt_decoder import decode_token
from jwt_validator import signature_is_valid

def authorizer(event: dict, context: dict, callback: staticmethod) -> None:
    secret = os.getenv('SECRET', "")
    token = event['authorizationToken']
    header, payload, signature = decode_token(token)

    if signature_is_valid(header, payload, signature, secret):
        try:
            callback(None, generate_policy_for(payload))
        except:
            raise Exception('Deny')
    else:
        raise Exception('Unauthorized')

def generate_policy_for(payload: dict) -> dict:
    policy = {
        "principalId": '',
        "policyDocument": {
            "Version": "2012-10-17",
            "Statement": {
                "Action": "execute-api:Invoke",
                "Effect": "Allow",
                "Resource": _get_allowed_resource(payload)
            }
        },
        'principalId': payload.get('username', 'anon')}

    return policy

def _get_allowed_resource(payload: dict) -> list:
    resources = []
    is_admin = payload.get('admin', False)
    if is_admin:
        resources.append('*')

    else:
        # Handle custom role
        pass

    return resources


from django.test import TestCase
from django.contrib.auth import get_user_model

from django.conf import settings

class UserTestCase(TestCase):

    def test_anonymous_user_should_exist(self):
        User = get_user_model()   # pylint: disable=invalid-name
        anon = User.get_anonymous()

        self.assertTrue(anon.is_anonymous,
                        msg=f'Anonymous user should exist and his/her name should be {settings.ANONYMOUS_USER_NAME}')

    def test_admin_user_should_exist(self):
        User = get_user_model()   # pylint: disable=invalid-name
        admin = User.objects.filter(is_staff=True, is_active=True)

        self.assertTrue(admin.exists(), msg='There should be at least one superuser')


from collections import defaultdict

import matplotlib.pyplot as plt
from scml.scml2020 import SCML2020Agent, SCML2021World
from scml.scml2020.agents import RandomAgent, MarketAwareDecentralizingAgent, DecentralizingAgent
from scml.scml2020.components.production import DemandDrivenProductionStrategy, ProductionStrategy
from scml.scml2020.components.trading import PredictionBasedTradingStrategy
from scml.scml2020.components.prediction import MarketAwareTradePredictionStrategy
from negmas import LinearUtilityFunction
from negmas import SAOMetaNegotiatorController
from negmas import Contract
import numpy as np
from typing import List, Dict, Optional, Tuple, Any
from scml.scml2020 import TIME, QUANTITY, UNIT_PRICE
# from myagent.steady_mgr import SteadyMgr
from scml.scml2020.common import is_system_agent

__all__ = ["MyPaibiu"]
# class YetAnotherNegotiationManager:
#     """My new negotiation strategy
#
#     Args:
#         price_weight: The relative importance of price in the utility calculation.
#         time_range: The time-range for each controller as a fraction of the number of simulation steps
#     """
#

class MyPaibiuAgent(MarketAwareTradePredictionStrategy, PredictionBasedTradingStrategy,
               DemandDrivenProductionStrategy, SCML2020Agent):
    def __init__(self, *args, price_weight=0.7, time_horizon=0.1, utility_threshold=0.9, time_threshold=0.9, **kwargs,):
        super().__init__(*args, **kwargs)
        self.index: List[int] = None
        self.time_horizon = time_horizon
        self._price_weight = price_weight
        self._time_threshold = time_threshold
        self._utility_threshold = utility_threshold
        self._current_end = -1
        self._current_start = -1

    def step(self):
        super().step()
        # find the range of steps about which we plan to negotiate
        step = self.awi.current_step
        production_cost = np.max(self.awi.profile.costs[:, self.awi.my_input_product])
        if step == 0:
            self.output_price = (
                self.awi.catalog_prices[self.awi.my_input_product] + production_cost
            ) * np.ones(self.awi.n_steps, dtype=int)
            self.input_cost = (self.awi.catalog_prices[self.awi.my_output_product] - production_cost
                              ) * np.ones(self.awi.n_steps, dtype=int)
        else:
            self.output_price[step:] = (
                max(self.awi.catalog_prices[self.awi.my_input_product] + production_cost, self.awi.trading_prices[self.awi.my_output_product]))
            self.input_cost[step:] = min(self.awi.catalog_prices[self.awi.my_output_product] - production_cost, self.awi.trading_prices[self.awi.my_output_product] - production_cost)
        self._current_start = step + 1
        self._current_end = min(
            self.awi.n_steps - 1,
            self._current_start + max(1, int(self.time_horizon * self.awi.n_steps)),
        )
        if self._current_start >= self._current_end:
            return

        for seller, needed, secured, product in [
            (False, self.inputs_needed, self.inputs_secured, self.awi.my_input_product),
            (True, self.outputs_needed, self.outputs_secured, self.awi.my_output_product),
        ]:
            # find the maximum amount needed at any time-step in the given range
            needs = np.max(
                needed[self._current_start: self._current_end]
                - secured[self._current_start: self._current_end]
            )
            if needs < 1:
                continue
            # set a range of prices
            if seller:
                # for selling set a price that is at least the catalog price
                min_price = max(self.awi.catalog_prices[product], self.awi.trading_prices[product])
                price_range = (min_price, 2 * min_price)
                controller = SAOMetaNegotiatorController(ufun=LinearUtilityFunction({
                    TIME: 0.0, QUANTITY: (1 - self._price_weight), UNIT_PRICE: self._price_weight
                }))
            else:
                # for buying sell a price that is at most the catalog price
                production_cost = np.max(self.awi.profile.costs[:, self.awi.my_input_product])
                price_range = (0,  min(self.awi.catalog_prices[product] - production_cost, self.awi.trading_prices[product] - production_cost))
                controller = SAOMetaNegotiatorController(ufun=LinearUtilityFunction({
                    TIME: 0.0, QUANTITY: (1 - self._price_weight), UNIT_PRICE: -self._price_weight
                }))

            self.awi.request_negotiations(
                not seller,
                product,
                (1, needs),
                price_range,
                time=(self._current_start, self._current_end),
                controller=controller,
            )

    def sign_all_contracts(self, contracts: List[Contract]) -> List[Optional[str]]:
        # sort contracts by time and then put system contracts first within each time-step
        signatures = [None] * len(contracts)
        contracts = sorted(
            zip(contracts, range(len(contracts))),
            key=lambda x: (
                x[0].agreement["unit_price"],
                x[0].agreement["time"],
                0
                if is_system_agent(x[0].annotation["seller"])
                or is_system_agent(x[0].annotation["buyer"])
                else 1,
                x[0].agreement["unit_price"],
            ),
        )

        sold, bought = 0, 0  # count the number of sold/bought products during the loop
        s = self.awi.current_step

        for contract, indx in contracts:
            is_seller = contract.annotation["seller"] == self.id
            q, u, t = (
                contract.agreement["quantity"],
                contract.agreement["unit_price"],
                contract.agreement["time"],
            )
            if t < s and len(contract.issues) == 3:
                continue

            if is_seller:
                # Sign the first contract when the final process is assigned
                if s == 0 and self.awi.my_output_product == (self.awi.n_products - 1):
                    signatures[indx] = self.id
                # I don't sign contracts for less than the selling price
                if u < self.output_price[t]:
                    continue

                est = 0  # Estimated number of products
                # Calculate the maximum production possible before delivery date
                for i in range(1, t - s + 1):
                    est += min(self.inputs_secured[t - i], i * self.awi.n_lines)
                est = min(est, (t - s) * self.awi.n_lines)

                available = (
                    est
                    + self.internal_state["_output_inventory"]
                    - (self.outputs_secured[s:]).sum()
                )  # Add stock and sub contracted
                # Only sign contracts that ensure production is on time.
                if available - sold > q:
                    signatures[indx] = self.id
                    sold += q

            else:
                # I don't make contracts to buy at the end of the game.
                if t > self.awi.n_steps * 3 // 4:
                    continue

                # I don't sign contracts over the buying price
                if u > self.input_cost[t]:
                    continue

                needed = self.inputs_needed[
                    self.awi.n_steps - 1
                ]  # Maximum number of products that can be produced
                if needed - bought > q:
                    signatures[indx] = self.id
                    bought += q

        return signatures

    def on_agent_bankrupt(
        self,
        agent: str,
        contracts: List[Contract],
        quantities: List[int],
        compensation_money: int,
    ) -> None:
        super().on_agent_bankrupt(agent, contracts, quantities, compensation_money)
        for contract, new_quantity in zip(contracts, quantities):
            q = contract.agreement["quantity"]
            if new_quantity == q:
                continue
            t = contract.agreement["time"]
            missing = q - new_quantity
            if t < self.awi.current_step:
                continue
            if contract.annotation["seller"] == self.id:
                self.outputs_secured[t] -= missing
                if t > 0:
                    self.outputs_needed[t - 1 :] += missing
            else:
                self.inputs_secured[t] -= missing
                self.inputs_needed[t:] += missing

    def respond_to_negotiation_request(
            self,
            initiator: str,
            issues: List["Issue"],
            annotation: Dict[str, Any],
            mechanism: "AgentMechanismInterface",
    ) -> Optional["Negotiator"]:
        # refuse to negotiate if the time-range does not intersect
        # the current range
        if not (
                issues[TIME].min_value < self._current_end
                or issues[TIME].max_value > self._current_start
        ):
            return None
        if self.id == annotation["seller"]:
            controller = SAOMetaNegotiatorController(ufun=LinearUtilityFunction({
                TIME: 0.0, QUANTITY: (1 - self._price_weight), UNIT_PRICE: self._price_weight
            }))
        else:
            if self.awi.current_step == 0:
                try:
                    needs = np.max(
                        self.inputs_needed[0: self.awi.n_steps - 1]
                        - self.inputs_secured[0: self.awi.n_steps - 1]
                        , initial=0)
                except:
                    return None
            else:
                try:
                    needs = np.max(
                        self.inputs_needed[self._current_start: self._current_end]
                        - self.inputs_secured[self._current_start: self._current_end]
                        , initial=0)
                except:
                    return None
            if needs < 1:
                return None
            controller = SAOMetaNegotiatorController(ufun=LinearUtilityFunction({
                TIME: 0.0, QUANTITY: (1 - self._price_weight), UNIT_PRICE: -self._price_weight
            }))
        return controller.create_negotiator()
    """My agent"""

    # def target_quantity(self, step: int, sell: bool) -> int:
    #     """A fixed target quantity of half my production capacity"""
    #     return self.awi.n_lines // 2
    #
    # def acceptable_unit_price(self, step: int, sell: bool) -> int:
    #     """The catalog price seems OK"""
    #     return self.awi.catalog_prices[self.awi.my_output_product] if sell else self.awi.catalog_prices[
    #         self.awi.my_input_product]
    #
    # def create_ufun(self, is_seller: bool, issues=None, outcomes=None):
    #     """A utility function that penalizes high cost and late delivery for buying and and awards them for selling"""
    #     if is_seller:
    #         return LinearUtilityFunction((0, 0.25, 1))
    #     return LinearUtilityFunction((0, -0.5, -0.8))

def show_agent_scores(world):
    scores = defaultdict(list)
    for aid, score in world.scores().items():
        scores[world.agents[aid].__class__.__name__.split(".")[-1]].append(score)
    scores = {k: sum(v) / len(v) for k, v in scores.items()}
    plt.bar(list(scores.keys()), list(scores.values()), width=0.2)
    plt.show()
#
#
# world = SCML2021World(
#     **SCML2021World.generate([RandomAgent, MarketAwareDecentralizingAgent, DecentralizingAgent, MyPaibiuAgent, SteadyMgr], n_steps=10),
#     construct_graphs=True,
# )
# world.run()
# # # world.draw(steps=(0, world.n_steps), together=False, ncols=2, figsize=(20, 20))
# # # plt.show()
# show_agent_scores(world)
# #
# # world.draw(steps=(0, world.n_steps), together=False, ncols=2, figsize=(20, 20))
# # plt.show()
#
# def run(
#         competition="std",
#         reveal_names=True,
#         n_steps=10,
#         n_configs=2,
# ):
#     """
#     **Not needed for submission.** You can use this function to test your agent.
#
#     Args:
#         competition: The competition type to run (possibilities are std,
#                      collusion).
#         n_steps:     The number of simulation steps.
#         n_configs:   Number of different world configurations to try.
#                      Different world configurations will correspond to
#                      different number of factories, profiles
#                      , production graphs etc
#
#     Returns:
#         None
#
#     Remarks:
#
#         - This function will take several minutes to run.
#         - To speed it up, use a smaller `n_step` value
#
#     """
#     competitors = [
#         RandomAgent, MarketAwareDecentralizingAgent, DecentralizingAgent, MyPaibiu
#     ]
#     start = time.perf_counter()
#     if competition == "std":
#         results = anac2021_std(
#             competitors=competitors,
#             verbose=True,
#             n_steps=n_steps,
#             n_configs=n_configs,
#         )
#     elif competition == "collusion":
#         results = anac2021_collusion(
#             competitors=competitors,
#             verbose=True,
#             n_steps=n_steps,
#             n_configs=n_configs,
#         )
#     elif competition == "oneshot":
#         # Standard agents can run in the OneShot environment but cannot win
#         # the OneShot track!!
#         from scml.oneshot.agents import GreedyOneShotAgent, RandomOneShotAgent
#
#         competitors = [
#             MyPaibiuAgent,
#             RandomOneShotAgent,
#             GreedyOneShotAgent,
#         ]
#         results = anac2021_oneshot(
#             competitors=competitors,
#             verbose=True,
#             n_steps=n_steps,
#             n_configs=n_configs,
#         )
#     else:
#         raise ValueError(f"Unknown competition type {competition}")
#     # just make agent types shorter in the results
#     results.total_scores.agent_type = results.total_scores.agent_type.str.split(
#         "."
#     ).str[-1]
#     # show results
#     print(tabulate(results.total_scores, headers="keys", tablefmt="psql"))
#     print(f"Finished in {humanize_time(time.perf_counter() - start)}")
#
#
# if __name__ == "__main__":
#     # will run a short tournament against two built-in agents. Default is "std"
#     # You can change this from the command line by running something like:
#     # >> python3 paibiu.py collusion
#     import sys
#
#     run(sys.argv[1] if len(sys.argv) > 1 else "std")


from outputRequestBlock import OutputRequestBlock
from spooling_output_process import *
from outputSpool import *
from userProcess import *
if __name__ == "__main__":
    output_spool = OutputSpool(capacity=100, max_request_blocks=10)
    output_service_semaphore = threading.Semaphore(0)
    messages0=['abc','cnjd','dkjfdk','sldkolf','dkfokol','jfidjfi','kdfkl','kfkss','vjkjvkd','sklko','vkdvm']
    messages1=['dkk','lkld']
    user_processes = [UserProcess(0, output_spool,messages0), UserProcess(1, output_spool,messages1)]
    spooling_output_process = SpoolingOutputProcess(output_spool, user_processes, output_service_semaphore)
    output_service = OutputService(output_spool, user_processes, spooling_output_process)
    output_service.start()
    output_service.join()


import sys

def read_file(input_pdb):
    tmp_file = open(input_pdb,'r')
    file = tmp_file.readlines()
    tmp_file.close()
    return file

input_pdb = sys.argv[1]

pdb_file = read_file(input_pdb)

dict_his = {
    "28" : "HIE",
    "87" : "HIE",
    "107": "HIE",
    "110": "HID",
    "117": "HID",
    "118": "HID",
    "119": "HIE",
    "141": "HID"
}

new_file = open('new_file.pdb','w')

for line in pdb_file:

    if(line[17:19] == "HI"):

        for k,v in dict_his.iteritems():

            if k == line[23:26].strip():

                line = str(line[0:17])+v+str(line[20:])
    new_file.write(line)


import subprocess
import os.path
import re

class PhotoHandler:

    def __init__(self):
        self.TIF_PATH = "data/input.tif"
        self.TXT_PATH = "data/output"
        self.AIRPORT_CODES_PATH = "data/airport_codes.txt"
        self.AIRLINE_INDICATORS_PATH = "data/airline_indicators.txt"

        self.AIRPORT_CODES = self.buildAirportCodesSet()
        self.AIRLINE_INDICATORS = self.buildAirlineIndicatorsSet()

    def buildAirlineIndicatorsSet(self):
        text_file = open(self.AIRLINE_INDICATORS_PATH, "r")
        text = text_file.read()

        airline_indicators = set(text.split())

        return airline_indicators

    def buildAirportCodesSet(self):
        text_file = open(self.AIRPORT_CODES_PATH, "r")
        text = text_file.read()

        airport_codes = set()

        for i, character in enumerate(text):
            if character == '(':
                code = text[i+1:i+4]
                airport_codes.add(code)

        return airport_codes

    def convertPhotoToTextFile(self, path):
        delete_cmd = "rm " + self.TXT_PATH
        convert_cmd = "convert " + path + " -resize 200% -type Grayscale " + self.TIF_PATH
        ocr_cmd = "tesseract -l eng " + self.TIF_PATH + " " + self.TXT_PATH

        # delete old txt just in case
        process = process = subprocess.Popen(delete_cmd.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
        # convert downloaded image to .tif format
        process = process = subprocess.Popen(convert_cmd.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()
        # find text in .tif and put in .txt
        process = process = subprocess.Popen(ocr_cmd.split(), stdout=subprocess.PIPE)
        output, error = process.communicate()

        # !! should probably handle errors and stuff here

    def findDataInPicture(self, path):
        self.convertPhotoToTextFile(path)

        # something went wrong with OCR
        if not os.path.exists(self.TXT_PATH + ".txt"):
            print("ERROR: no output.txt")
            return None

        text_file = open(self.TXT_PATH + ".txt", "r")
        text = text_file.read()

        confirmationNumber = self.searchTextForConfirmation(text)

        airportCodes = self.searchTextForAirports(text)

        airline = self.searchTextForAirline(text)

        return confirmationNumber, airportCodes, airline

    def searchTextForAirports(self, text):
        matches = re.finditer(r'[A-Z]{3}', text)

        airports = set()

        for match in matches:
            possible_airport = match.group()
            if possible_airport in self.AIRPORT_CODES:
                airports.add(possible_airport)

        return airports

    def searchTextForAirline(self, text):
        for airline in self.AIRLINE_INDICATORS:
            airline = airline.lower()
            if airline in text.lower():
                return airline

        return None

    def searchTextForConfirmation(self, text):
        matches = re.finditer(r'\b[0-9A-Z]{6}\b', text)

        potential_codes = set()

        # matches with all 6 character capital alphanumeric strings
        for match in matches:
            possible_code = match.group()
            num_seen = False
            letter_seen = False

            # if code contains at least 1 num and 1 letter, this could be a confirmation code
            for character in possible_code:
                if character.isdigit():
                    num_seen = True
                else:
                    letter_seen = True

            if num_seen and letter_seen:
                potential_codes.add(possible_code)

        if len(potential_codes) > 0:
            return potential_codes
        else:
            return None


def dist(src, tar, method=sim_levenshtein):
    """Return a distance between two strings.

    This is a generalized function for calling other distance functions.

    Parameters
    ----------
    src : str
        Source string for comparison
    tar : str
        Target string for comparison
    method : function
        Specifies the similarity metric (:py:func:`sim_levenshtein` by default)
        -- Note that this takes a similarity metric function, not a distance
        metric function.

    Returns
    -------
    float
        Distance according to the specified function

    Raises
    ------
    AttributeError
        Unknown distance function

    Examples
    --------
    >>> round(dist('cat', 'hat'), 12)
    0.333333333333
    >>> round(dist('Niall', 'Neil'), 12)
    0.6
    >>> dist('aluminum', 'Catalan')
    0.875
    >>> dist('ATCG', 'TAGC')
    0.75

    """
    if callable(method):
        return 1 - method(src, tar)
    else:
        raise AttributeError('Unknown distance function: ' + str(method))


import numpy as np

lab_idx = 7     # Characteristics [DiseaseState]
min_sample_num = 20
sample_num = 5896
gene_num = 22283

data_in = './Gene_Chip_Data/microarray.original.txt'
label_in = './Gene_Chip_Data/E-TABM-185.sdrf.txt'

def data_process():
    # read label
    with open(label_in, 'r') as fl:
        line = fl.readline()
        print('select label: ', line.split('\t')[lab_idx])

        label = []
        label_dist = {}     # label: num

        for i in range(sample_num):
            sample = fl.readline()
            lab = sample.split('\t')[lab_idx]
            label.append(lab)
            if lab.strip():
                if lab in label_dist:
                    label_dist[lab] += 1
                else:
                    label_dist[lab] = 1

    # select label
    selected_label = []
    for lab, num in label_dist.items():
        if num > min_sample_num:
            selected_label.append(lab)
    print('select %d types of disease' % len(selected_label))

    # load data
    data = []
    with open(data_in, 'r') as fd:
        fd.readline()
        for i in range(gene_num):
            line = fd.readline().split('\t')[1:]
            line = [float(x) for x in line]
            data.append(line)
    data = np.array(data).T

    # select data
    label_idx = []
    remind_data_idx = []
    for i in range(sample_num):
        lab = label[i]
        if lab in selected_label:
            label_idx.append(selected_label.index(lab))
            remind_data_idx.append(i)
    data = data[remind_data_idx, :]
    print('select %d samples' % data.shape[0])

    # write into files
    np.save('data.npy', data)
    np.save('label_idx.npy', label_idx)

    with open('label.txt', 'w') as f:
        for lab in selected_label:
            f.write(lab)
            f.write('\n')

def split_train_test(data_file, test_ratio):
    label_idx = np.load('label_idx.npy')
    data = np.load(data_file)

    n_samples = label_idx.size
    n_test = int(n_samples * test_ratio)
    order = np.random.permutation(n_samples)
    train_order = order[n_test:]
    test_order = order[:n_test]
    print('train set: %d' % (n_samples - n_test))
    print('test set: %d' % n_test)

    np.save('train_x.npy', data[train_order, :])
    np.save('train_y.npy', label_idx[train_order])
    np.save('test_x.npy', data[test_order, :])
    np.save('test_y.npy', label_idx[test_order])

if __name__ == '__main__':
    # data_process()
    split_train_test('data.npy', 0.1)


# +
# Imports

import torch
from torch import nn
from torch import optim
from torchvision import datasets, transforms, models

import numpy as np
import matplotlib.pyplot as plt

import os
import json
import time
import argparse

from collections import OrderedDict
from PIL import Image

from flowers_common import load_checkpoint, recreate_model_from_checkpoint, \
                           create_model_densenet121, create_model_resnet18, \
                           load_category_names, class_to_name_lookup, pred_class_idx_to_flower_name

#----------------------------------------------------------------------------------------------------
#----------------------------------------------------------------------------------------------------

def process_image_via_numpy(image):
    ''' Scales, crops, and normalizes a PIL image for a PyTorch model,
        returns an Numpy array
    '''

    # DONE: Process a PIL image for use in a PyTorch model

    # open PIL image
    with Image.open(image) as im:
        # reize and crop
        pil_image = im.resize((256, 256))
        pil_image = pil_image.crop((16, 16, 240, 240))

        # transform to numpy with float values 0.0-1.0
        np_image = np.array(pil_image)
        np_image = np_image / 255

    # normalization
    mean  = np.array([0.485, 0.456, 0.406])
    std   = np.array([0.229, 0.224, 0.225])
    np_image = (np_image - mean) / std

    # transpone - color channel to be 1st dimention of the array
    np_image = np_image.transpose((2,0,1))

    return np_image

# credits: https://discuss.pytorch.org/t/pytorch-pil-to-tensor-and-vice-versa/6312/9
def process_image_via_torch(image):

    with Image.open(image) as im:
        pil_to_tensor = transforms.ToTensor()(im).unsqueeze_(0)

    inference_transforms = transforms.Compose([transforms.Resize(255),
                                               transforms.CenterCrop(224),
                                               transforms.Normalize([0.485, 0.456, 0.406],
                                                                    [0.229, 0.224, 0.225])])

    pil_to_tensor = inference_transforms(pil_to_tensor)
    return pil_to_tensor

#----------------------------------------------------------------------------------------------------

# display numpy
def imshow_numpy(image, ax=None, title=None):
    """Imshow for Tensor."""
    if ax is None:
        fig, ax = plt.subplots()

    # PyTorch tensors assume the color channel is the first dimension
    # but matplotlib assumes is the third dimension
    image = image.transpose((1, 2, 0))

    # Undo preprocessing
    mean = np.array([0.485, 0.456, 0.406])
    std = np.array([0.229, 0.224, 0.225])
    image = std * image + mean

    # Image needs to be clipped between 0 and 1 or it looks like noise when displayed
    image = np.clip(image, 0, 1)

    ax.imshow(image)

    return ax

# credits: https://discuss.pytorch.org/t/simple-way-to-inverse-transform-normalization/4821
def inverse_normalize(tensor, mean, std):
    for t, m, s in zip(tensor, mean, std):
        t.mul_(s).add_(m)
    return tensor

# display tensor
# credits: https://pytorch.org/vision/stable/auto_examples/plot_scripted_tensor_transforms.html
def imshow_tensor(image_tensor):
    fix, axs = plt.subplots(ncols=len(image_tensor), squeeze=False)
    for i, img in enumerate(image_tensor):
        input_tensor = inverse_normalize(tensor=img,
                                         mean=(0.485, 0.456, 0.406),
                                         std=(0.229, 0.224, 0.225))
        input_tensor = transforms.ToPILImage()(input_tensor.to('cpu'))

        axs[0, i].imshow(np.asarray(input_tensor))
        axs[0, i].set(xticklabels=[], yticklabels=[], xticks=[], yticks=[])

def show_probablities_chart(probabilities, classes):

    # get list of labels
    labels = class_to_name_lookup(cat_to_name, classes)

    fig, ax = plt.subplots()
    ax.barh(np.arange(len(classes)), probabilities)
    ax.set_yticks(np.arange(len(classes)))
    ax.set_yticklabels(labels)

    plt.show()

#----------------------------------------------------------------------------------------------------

def predict(image_path_or_tensor, model, device, topk=5):
    ''' Predict the class (or classes) of an image using a trained deep learning model.
    '''

    # DONE: Implement the code to predict the class from an image file

    if type(image_path_or_tensor) == torch.Tensor:
        input_data = image_tensor
    else:
        input_data = process_image_via_torch(image_path_or_tensor)

    # move model to target device
    model.to(device)

    # switch model to evaluation mode
    model.eval()
    # with gradient calculation turned off
    with torch.no_grad():

        if type(input_data) != torch.Tensor:
            print("processing non-tensor array to tensor")
            input_data = torch.from_numpy(input_data)
            input_data.unsqueeze_(0)
            input_data = input_data.float()

        # move data to device
        input_data = input_data.to(device)

        logps = model.forward(input_data)

        # get top topk classes
        ps = torch.exp(logps)
        top_p, top_class = ps.topk(topk, dim=1)

    # move tensors to cpu to transform them into numpy and list
    top_p, top_class = top_p.cpu(), top_class.cpu()

    # torch.Tensor to numpy of float
    top_p = top_p.data.numpy().squeeze(axis=0)

    # torch.Tensor to list of ints
    top_class = top_class.squeeze().tolist()
    if type(top_class) != list:
        top_class = [top_class]

    return top_p, top_class

#----------------------------------------------------------------------------------------------------

def parse_arguments():
    # parse cmd line argument
    parser = argparse.ArgumentParser(prog='predict',
                                     description='Predict species of given flower',)

    parser.add_argument('image_to_predict',
                        help='The path to image for prediction',
                        action='store', type=str)

    parser.add_argument('model_checkpoint',
                        help='The path to trained network checkpoint',
                        action='store', type=str)

    parser.add_argument('-t', '--top_k',
                        choices=range(1, 11),
                        default='1',
                        help='Return top K most likely classes',
                        action='store', type=int, required=False)

    parser.add_argument('-c', '--category_names',
                        help='Path to category-to-name file',
                        action='store', type=str, required=False)

    parser.add_argument('-g', '--gpu',
                        help='Inference on GPU, if available',
                        action='store_true', required=False)

    parser.add_argument('-v', '--verbose',
                        help='Show extra logs',
                        action='store_true', required=False)

    args = parser.parse_args()
    return args

def show_arguments(parsed_args):
    print("==== ==== ==== ==== ====")
    print('image_to_predict ', parsed_args.image_to_predict)
    print('model_checkpoint ', parsed_args.model_checkpoint)
    print('top_k            ', parsed_args.top_k)
    print('category_names   ', parsed_args.category_names)
    print('gpu              ', parsed_args.gpu)

#----------------------------------------------------------------------------------------------------

def main():
    args = parse_arguments()
    if args.verbose:
        show_arguments(args)

    # load parsed arguments
    image_to_predict = args.image_to_predict
    model_checkpoint = args.model_checkpoint
    category_names   = args.category_names
    top_k = args.top_k

    # check if provided image exists
    if not os.path.exists(image_to_predict):
        print('ERROR: The \'image_to_predict\' specified ({}) does not exist'.format(image_to_predict))
        exit()

    # check if provided checkpoint exists
    if not os.path.exists(model_checkpoint):
        print('ERROR: The \'model_checkpoint\' specified ({}) does not exist'.format(model_checkpoint))
        exit()

    cat_to_name_dict = {}
    # check if provided category file exists
    if not category_names:
        print('INFO: The \'category_names\' NOT specified.')
    elif not os.path.exists(category_names):
        print('WARNING: The \'category_names\' specified ({}) does not exist'.format(category_names))
    else:
        cat_to_name_dict = load_category_names(category_names)

    # load checkpoint and build the model
    loaded_checkpoint = load_checkpoint(model_checkpoint)

    loaded_model_dict = recreate_model_from_checkpoint(loaded_checkpoint)

    if args.verbose:
        print("==== ==== ==== ==== ====")
        print('NN MODEL:')
        print(loaded_model_dict['model'])

    # Use GPU if available and if allowed by paramter "GPU"
    if args.gpu:
        device = torch.device("cuda" if torch.cuda.is_available() else "cpu")
    else:
        device = torch.device("cpu")

    if args.verbose:
        print("==== ==== ==== ==== ====")
        print('Device selected for training: ', device)

    # predict class idx of given flower image
    predP, predCI = predict(image_to_predict, loaded_model_dict['model'], device, topk=top_k)
    predCL, predN = pred_class_idx_to_flower_name(loaded_checkpoint['class_idx_to_class'], cat_to_name_dict, predCI)

    print("==== ==== ==== ==== ==== ==== ==== ====")
    print("Input image: ", image_to_predict)
    if not predN:
        for cl, pr in zip(predCL, predP):
            print("Prediction:  class {:>3}   probability: {:.3f}".format(cl, pr))
    else:
        for cl, pr, la in zip(predCL, predP, predN):
            print("Prediction:  class {:>3}   probability: {:.3f}   label: {}".format(cl, pr, la))
    print("==== ==== ==== ==== ==== ==== ==== ====")

if __name__ == "__main__":
    main()


# The edit distance between two strings is the minimum number of operations required to transform one string into the other.

# The allowed operations are:

#     Add one character to the string.
#     Remove one character from the string.
#     Replace one character in the string.

# For example, the edit distance between LOVE and MOVIE is 2, because you can first replace L with M, and then add I.

import sys

sys.stdout = open("out.txt", "w")
sys.stdin = open("in.txt", "r")

def solver(n, m):
    dp = [[0 for i in range(len(m) + 1)] for j in range(len(n) + 1)]
    for i in range(len(n) + 1):
        dp[i][0] = i
    for i in range(len(m) + 1):
        dp[0][i] = i
    for i in range(1, len(n) + 1):
        for j in range(1, len(m) + 1):
            if n[i - 1] == m[j - 1]:
                dp[i][j] = dp[i - 1][j - 1]
            else:
                dp[i][j] = min(dp[i - 1][j - 1], dp[i][j - 1], dp[i - 1][j]) + 1
    return dp[len(n)][len(m)]

if __name__ == "__main__":
    n = sys.stdin.readline()
    m = sys.stdin.readline()
    print(solver(n, m))

# Input

# LOVE
# MOVIE

# oOUUTput
# 2


from module import *  #Importation des modules nécessaires à partir d'un fichier externe.

def main(): # Définition de la fonction principale "main()".
    date = ['', '', ''] # Initialisation d'un tableau vide appelé "date" qui va stocker une date entrée par l'utilisateur.
    for i in range(3): # Utilisation d'une boucle "for" pour itérer 3 fois et demander à l'utilisateur d'entrer chaque élément de la date (jour, mois, année) qui sera stocké dans le tableau "date".
        date[i]=input("Entrer la date dans l'ordre JJ/MM/AAAA : ")

    # Définition d'une variable "url" en utilisant une chaîne de formatage f. Cette variable contient l'URL d'un fichier CSV en ligne qui comprend la date entrée par l'utilisateur
    url = (f"https://files.data.gouv.fr/lcsqa/concentrations-de-polluants-atmospheriques-reglementes/temps-reel/{date[2]}/FR_E2_{date[2]}-{date[1]}-{date[0]}.csv")
    df = donnees(url) #Utilisation de la fonction "donnees()" pour récupérer les données à partir de l'URL et les stocker dans une variable "df".
    df = graph(df) # Utilisation de la fonction "graph()" pour traiter les données contenues dans "df" et les préparer pour une visualisation.
    return df # Retourne le dataframe "df" traité # Utilisation de la fonction "donnees()" pour récupérer les données à


def canPlaceFlowers(fb: list[int], n: int) -> bool:
    if n == 0: return True

    #To ensure we dont go out of bounds + flowerless spaces at both ends outside list
    fb = [0] + fb +[0]
    for i, e in enumerate(fb[1:-1], 1):

        if n == 0:
            return True

        elif [fb[i-1], e, fb[i+1]] == [0, 0, 0]: #Adjacent checking
            fb[i] = 1
            n -= 1

    return True if n == 0 else False

if __name__ == "__main__":
    canPlaceFlowers([1,0,0,0,1,0,1], 1)


# encoding=utf8
import socket
import time

host = ''
port = 22222
buffsize = 1024
addr = (host, port)

tcpsocket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
tcpsocket.bind(addr)
tcpsocket.listen(10)

while True:
    print('Waiting for connection...')
    sub_sock, addr = tcpsocket.accept()
    print('Connected from ', addr)

    while True:
        data = sub_sock.recv(buffsize)
        if not data:
            break

        new_data = '[%s] '%(time.ctime()) + data.decode()
        sub_sock.send(new_data.encode())
        # sub_sock.send('[{}] {}'.format(time.ctime().encode(), data))

    sub_sock.close()


import twitter
from decouple import config

class RONBWrapper:
    def __init__(self):
        self.api = twitter.Api(
            consumer_key=config("TWITTER_API_KEY"),
            consumer_secret=config("TWITTER_API_KEY_SECRET"),
            access_token_key=config("TWITTER_ACCESS_TOKEN"),
            access_token_secret=config("TWITTER_ACCESS_TOKEN_SECRET"),
            tweet_mode="extended",
        )

    def get_updates(self, screen_name: str = "RONBupdates", count: int = 10) -> list:
        """Given a twitter username returns `count` number of tweets

        Args:
            screen_name (str, optional): Twitter username. Defaults to "RONBupdates".
            count (int, optional): Number of tweets to fetch. Defaults to 10.

        Returns:
            list: List of tweets
        """
        return self.api.GetUserTimeline(screen_name=screen_name, count=count)


import discord
import asyncio
import requests
import threading
import sys
import os
from typing import Optional
from colorama import Fore
import time

def Clear():
  if sys.platform in ["linux", "linux2"]:
    os.system("clear")
  else:
    os.system("cls")

Clear()

def setTitle(title: Optional[any]=None):
  os.system(f"title {title}")

setTitle("Multiple Token Hoster - [KaramveerPlayZ#1337]")

session = requests.Session()
tokens = []
with open("tokens.txt", "r") as f:
  tokens_ = f.read().split("\n")

if len(tokens_) == 0:
  print("KaramveerPlayZ#1337 | They're Are No Tokens, Please Add Some Tokens To Host In tokens.txt File.")
  exit()

def Check_Token(Token):
  response = requests.get(f"https://discord.com/api/v9/users/@me", headers={"Authorization": Token})
  if response.status_code in [204, 200, 201]:
    print(f"KaramveerPlayZ#1337 | {Token} Is Vaild.")
    tokens.append(Token)
  if "need to verify" in response.text:
    print(f"KaramveerPlayZ#1337 | {Token} Is On Verification.")
  elif response.status_code in [404, 401, 400]:
    print(f"KaramveerPlayZ#1337 | {Token} Invaild Token Or Rate Limited.")

for tk in tokens_:
  Check_Token(tk)

if len(tokens) == 0:
  print("KaramveerPlayZ#1337 | All Tokens Were Invaild, Try Again Later With Working Tokens.")
  exit()

time.sleep(2)
Clear()
menu = f"""{Fore.RED}[-]{Fore.RESET} Created by KaramveerPlayZ#1337\n"""

print(menu)

st = input('KaramveerPlayZ#1337 | Accounts Status Idle, Dnd, Online, Mobile: ')
akks = []
stl = st.lower()
if stl == "dnd":
  status = discord.Status.dnd
elif stl == "idle":
  status = discord.Status.idle
elif stl == "online":
  status = discord.Status.online
ty = input("KaramveerPlayZ#1337 | Activity Type Playing, Streaming, Listening, Watching: ")
tyy = ty.lower()
if tyy == "streaming":
  name = input("KaramveerPlayZ#1337 | Streaming Name: ")
  acttt = discord.Streaming(name=name, url="https:/twitch/KaramveerPlayZ")
elif tyy == "playing":
  name = input("KaramveerPlayZ#1337 | Playing Name: ")
  acttt = discord.Game(name=name)
elif tyy == "listening":
  name = input("KaramveerPlayZ#1337 | Listening Name: ")
  acttt=discord.Activity(type=discord.ActivityType.listening, name=name)
elif tyy == "watching":
  name = input("KaramveerPlayZ#1337 | Watching Name: ")
  acttt=discord.Activity(type=discord.ActivityType.watching, name=name)
loop = asyncio.get_event_loop()
for tk in tokens:
  client = discord.Client(status=status, activity=acttt)
  loop.create_task(client.start(tk, bot=False))
  akks.append(client)
  print(" ")
  print("KaramveerPlayZ#1337 | {} Is Hosted.\n".format(tk))

threading.Thread(target=loop.run_forever).start()

while True:
  idk = 0
  idk += 1


testcases = int(input())
while testcases!=0:
    Ax, Ay = 0, 0
    Bx, By = input().split()
    Bx, By = int(Bx), int(By)
    Cx, Cy = 0, 0

    if (Bx+By)%2==0:
        if Bx%2!=0 and By%2!=0:
            if Bx>By:
                Cx = (Bx+By)//2
                Cy = 0
            else:
                Cx = 0
                Cy = (By+Bx)//2
        else:
            Cx = Bx//2
            Cy = By//2
    else:
        Cx, Cy = -1, -1

    print(Cx,Cy)
    testcases -= 1

#https://codeforces.com/problemset/problem/1621/B


# -*- coding: utf-8 -*-

from my_game.models import FactoryPattern, HullPattern, ArmorPattern, ShellPattern, ShieldPattern, \
    GeneratorPattern, WeaponPattern, EnginePattern, ModulePattern, DevicePattern, FuelPattern
from space_forces.hold_and_tank.unloading import unloading
from space_forces.hold_and_tank.unload_hold_resources import unload_hold_resources

def unload_hold(*args):
    session_user = args[0]
    session_user_city = args[1]
    fleet = args[2]
    my_dictionary = args[3]

    amount_factory = my_dictionary.get('amount_factory')
    if amount_factory is not None:
        shipment_id_fac = my_dictionary.get('hidden_factory')
        len_factory_amount = len(amount_factory)
        for i in range(len_factory_amount):
            factory_amount = int(amount_factory[i])
            if factory_amount != 0:
                shipment_id = int(shipment_id_fac[i])
                class_shipment = 10
                factory = FactoryPattern.objects.filter(id=shipment_id).first()
                size = int(factory.factory_size) * factory_amount
                amount_shipment = factory_amount
                mass_shipment = int(factory.factory_mass) * factory_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_hull = my_dictionary.get('amount_hull')
    if amount_hull is not None:
        shipment_id_hull = my_dictionary.get('hidden_hull')
        len_hull_amount = len(amount_hull)
        for i in range(len_hull_amount):
            hull_amount = int(amount_hull[i])
            if hull_amount != 0:
                shipment_id = int(shipment_id_hull[i])
                class_shipment = 1
                hull = HullPattern.objects.filter(id=shipment_id).first()
                size = int(hull.hull_size) * hull_amount
                amount_shipment = hull_amount
                mass_shipment = int(hull.hull_mass) * hull_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_armor = my_dictionary.get('amount_armor')
    if amount_armor is not None:
        shipment_id_arm = my_dictionary.get('hidden_armor')
        len_armor_amount = len(amount_armor)
        for i in range(len_armor_amount):
            armor_amount = int(amount_armor[0])
            if armor_amount != 0:
                shipment_id = int(shipment_id_arm[i])
                class_shipment = 2
                armor = ArmorPattern.objects.filter(id=shipment_id).first()
                size = 15 * armor_amount
                amount_shipment = armor_amount
                mass_shipment = int(armor.armor_mass) * armor_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_shield = my_dictionary.get('amount_shield')
    if amount_shield is not None:
        shipment_id_shield = my_dictionary.get('hidden_shield')
        len_shield_amount = len(amount_shield)
        for i in range(len_shield_amount):
            shield_amount = int(amount_shield[i])
            if shield_amount != 0:
                shipment_id = int(shipment_id_shield[i])
                class_shipment = 3
                shield = ShieldPattern.objects.filter(id=shipment_id).first()
                size = int(shield.shield_size) * shield_amount
                amount_shipment = shield_amount
                mass_shipment = int(shield.shield_mass) * shield_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_engine = my_dictionary.get('amount_engine')
    if amount_engine is not None:
        shipment_id_eng = my_dictionary.get('hidden_engine')
        len_engine_amount = len(amount_engine)
        for i in range(len_engine_amount):
            engine_amount = int(amount_engine[i])
            if engine_amount != 0:
                shipment_id = int(shipment_id_eng[i])
                class_shipment = 4
                engine = EnginePattern.objects.filter(id=shipment_id).first()
                size = int(engine.engine_size) * engine_amount
                amount_shipment = engine_amount
                mass_shipment = int(engine.engine_mass) * engine_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_generator = my_dictionary.get('amount_generator')
    if amount_generator is not None:
        shipment_id_gen = my_dictionary.get('hidden_generator')
        len_generator_amount = len(amount_generator)
        for i in range(len_generator_amount):
            generator_amount = int(amount_generator[i])
            if generator_amount != 0:
                shipment_id = int(shipment_id_gen[i])
                class_shipment = 5
                generator = GeneratorPattern.objects.filter(id=shipment_id).first()
                size = int(generator.generator_size) * generator_amount
                amount_shipment = generator_amount
                mass_shipment = int(generator.generator_mass) * generator_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_weapon = my_dictionary.get('amount_weapon')
    if amount_weapon is not None:
        shipment_id_weap = my_dictionary.get('hidden_weapon')
        len_weapon_amount = len(amount_weapon)
        for i in range(len_weapon_amount):
            weapon_amount = int(amount_weapon[i])
            if weapon_amount != 0:
                shipment_id = int(shipment_id_weap[i])
                class_shipment = 6
                weapon = WeaponPattern.objects.filter(id=shipment_id).first()
                size = int(weapon.weapon_size) * weapon_amount
                amount_shipment = weapon_amount
                mass_shipment = int(weapon.weapon_mass) * weapon_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_shell = my_dictionary.get('amount_shell')
    if amount_shell is not None:
        shipment_id_shell = my_dictionary.get('hidden_shell')
        len_shell_amount = len(amount_shell)
        for i in range(len_shell_amount):
            shell_amount = int(amount_shell[i])
            if shell_amount != 0:
                shipment_id = int(shipment_id_shell[i])
                class_shipment = 7
                shell = ShellPattern.objects.filter(id=shipment_id).first()
                size = int(shell.shell_size) * shell_amount
                amount_shipment = shell_amount
                mass_shipment = int(shell.shell_mass) * shell_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_module = my_dictionary.get('amount_module')
    if amount_module is not None:
        shipment_id_mod = my_dictionary.get('hidden_module')
        len_module_amount = len(amount_module)
        for i in range(len_module_amount):
            module_amount = int(amount_module[i])
            if module_amount != 0:
                shipment_id = int(shipment_id_mod[i])
                class_shipment = 8
                module = ModulePattern.objects.filter(id=shipment_id).first()
                size = int(module.module_size) * module_amount
                amount_shipment = module_amount
                mass_shipment = int(module.module_mass) * module_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_device = my_dictionary.get('amount_device')
    if amount_device is not None:
        shipment_id_device = my_dictionary.get('hidden_device')
        len_device_amount = len(amount_device)
        for i in range(len_device_amount):
            device_amount = int(amount_device[i])
            if device_amount != 0:
                shipment_id = int(shipment_id_device[i])
                class_shipment = 9
                device = DevicePattern.objects.filter(id=shipment_id).first()
                size = int(device.device_size) * device_amount
                amount_shipment = device_amount
                mass_shipment = int(device.device_mass) * device_amount
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)

    amount_fuel = my_dictionary.get('amount_fuel')
    if amount_fuel is not None:
        shipment_id_fuel = my_dictionary.get('hidden_fuel')
        len_fuel_amount = len(amount_fuel)
        for i in range(len_fuel_amount):
            amount_fuel = int(amount_fuel[i])
            if amount_fuel != 0:
                shipment_id = int(shipment_id_fuel[i])
                class_shipment = 14
                fuel = FuelPattern.objects.filter(id=shipment_id).first()
                size = int(fuel.fuel_size) * amount_fuel
                amount_shipment = amount_fuel
                mass_shipment = int(fuel.fuel_mass) * amount_fuel
                unloading(session_user, session_user_city, fleet, class_shipment, shipment_id, amount_shipment,
                          mass_shipment, size)
    unload_hold_resources(session_user, session_user_city, fleet, my_dictionary)
    message = 'Ресурсы выгружено'
    return message


#!/usr/bin/env python

try:
    from setuptools import setup, Extension
    setup, Extension
except ImportError:
    from distutils.core import setup
    from distutils.extension import Extension
    setup, Extension

import os
import re
import sys

if sys.argv[-1] == "publish":
    os.system("python setup.py sdist upload")
    sys.exit()

vre = re.compile("__version__ = \"(.*?)\"")
m = open(os.path.join(os.path.dirname(os.path.abspath(__file__)),
                      "daft.py")).read()
version = vre.findall(m)[0]

setup(
    name="daft",
    version=version,
    description="PGM rendering at its finest.",
    long_description=open("README.rst").read(),
    author="David W. Hogg & Daniel Foreman-Mackey",
    author_email="danfm@nyu.edu",
    url="http://daft-pgm.org",
    py_modules=["daft"],
    package_data={"": ["LICENSE.rst"]},
    include_package_data=True,
    install_requires=[
        "numpy",
        "matplotlib"
    ],
    classifiers=[
        "Development Status :: 3 - Alpha",
        "Intended Audience :: Developers",
        "Intended Audience :: Science/Research",
        "License :: OSI Approved :: MIT License",
        "Operating System :: OS Independent",
        "Programming Language :: Python",
    ],
)


# from operator import le
# import re
# from turtle import right

def binary_search(keys, query):
    # write your code here
    # n = keys[0]
    # seq = keys[1:]

    lt  = 0
    rt = len(keys) -1

    while (lt <= rt):
        mid = (int)(lt + (rt - lt)//2)
        # if keys[lt] == keys[lt +1]:
        #     lt = lt + 1
        if query > keys[mid]:
            lt = mid +1
        elif query < keys[mid]:
            rt = mid -1
        else:
            return mid
    return -1

    # pass
    # # print(keys)
    # # print(query)

if __name__ == '__main__':
    num_keys = int(input())
    # print(num_keys)
    input_keys = list(map(int, input().split()))
    # print(input_keys)
    assert len(input_keys) == num_keys

    num_queries = int(input())
    # print(num_queries)
    input_queries = list(map(int, input().split()))
    # print(input_queries)
    assert len(input_queries) == num_queries

    for q in input_queries:
        print(binary_search(input_keys, q), end=' ')


from gdl.main import app
from gdl.schemas import GameBaseSchema, PlatformGameBaseSchema

def test_search_games(test_client):
    with test_client(app) as client:
        response = client.get("/games/search?search=example&limit=5")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "success"
        assert "results" in data
        assert "games" in data

def test_get_post_by_name_no_result(test_client):
    with test_client(app) as client:
        response = client.get("/games/name/non-existing game")
        assert response.status_code == 404

def test_get_post_by_id_no_result(test_client):
    with test_client(app) as client:
        response = client.get("/games/id/999")
        assert response.status_code == 404

def test_create_game(test_game, test_client):
    with test_client(app) as client:
        payload = test_game.__dict__
        response = client.post("/games/", json=payload)
        assert response.status_code == 201
        data = response.json()
        assert data["status"] == "success"
        assert "game" in data

def test_get_post_by_name(test_game, test_client):
    with test_client(app) as client:
        response = client.get(f"/games/name/{test_game.name}")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "success"
        assert "game" in data
        assert data.get("game").get("name") == test_game.name
        test_game.id = data.get("game").get("id")

def test_get_post_by_id(test_game, test_client):
    with test_client(app) as client:
        response = client.get(f"/games/id/{test_game.id}")
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "success"
        assert "game" in data

def test_update_game(test_client, test_game):
    with test_client(app) as client:
        new_name = "Updated Game"
        payload = {"name": new_name}
        response = client.patch(f"/games/{str(test_game.id)}", json=payload)
        assert response.status_code == 200
        data = response.json()
        assert data["status"] == "success"
        assert "game" in data
        db_game = client.get(f"/games/id/{test_game.id}")
        assert db_game.json().get("game", {}).get("name") == new_name

def test_delete_game(test_client, test_game):
    with test_client(app) as client:
        response = client.delete(f"/games/{test_game.id}")
        assert response.status_code == 204
        db_game = client.get(f"/games/id/{test_game.id}")
        assert db_game.status_code == 404

def test_platform_games_attribute(test_client, another_game, a_platform_game):
    with test_client(app) as client:
        response = client.get(f"/games/id/{another_game.id}")
        db_game = GameBaseSchema(**response.json()["game"])
        assert db_game == another_game
        assert any(
            [
                PlatformGameBaseSchema(**platform_game) == a_platform_game
                for platform_game in db_game.platform_games
            ]
        )


class Solution:
    def minDistance(self, word1: str, word2: str) -> int:
        size1, size2 = len(word1), len(word2)

        # 创建一个(size1+1) * (size2+1)的dp数组，初始化为最大值
        # 这个dp数组的意义是dp[i][j]表示长度为i的word1和长度为j的word2的最小编辑距离
        dp = [[float('inf') for x in range(size2+1)] for y in range(size1+1)]

        # 然后初始化base情况
        for row in range(size1+1): dp[row][0] = row # 行代表word1
        for col in range(size2+1): dp[0][col] = col # 列代表word2

        # 开始计算，每次基于已有的历史。从上到下，从左到右
        for row in range(1,size1+1):
            for col in range(1,size2+1):
                # 每个位置有三种选择：替换word1，插入到word1，删除word1，选择修改最小的情况
                if word1[row-1] != word2[col-1]:
                    # dp[i][j] = min(dp[i-1][j-1]+1, dp[i][j-1]+1, dp[i-1][j]+1) index别大意啊！！
                    dp[row][col] = min(dp[row-1][col-1]+1, dp[row][col-1]+1, dp[row-1][col]+1)
                else:
                    # dp[i][j] = dp[i-1][j-1]+1 index别大意啊！！
                    dp[row][col] = dp[row-1][col-1]
                    # 因为只加1，所以对角线的替换应该是最少的，否则或是要考虑垂直和水平情况

        # 最后的dp[size1][size2]就是所得
        return dp[size1][size2]

if __name__ == "__main__":
  gua = Solution()
  word1, word2 = "horse", "ros"
  rtn = gua.minDistance(word1, word2)
  print("expect result = 3, actual result = %d " % rtn )

  word1, word2 = "intention", "execution"
  rtn = gua.minDistance(word1, word2)
  print("expect result = 5, actual result = %d " % rtn )


"""
The main training script for the CIFAR-10 image classification task. This script integrates functionalities
from the associated modules, orchestrating the end-to-end training process. From data loading, preprocessing,
model construction, to the actual training phase, this script provides a holistic approach to model training.
"""

import sys
import numpy as np
import os
import tensorflow as tf
from datetime import datetime
from keras.callbacks import ModelCheckpoint, ReduceLROnPlateau, EarlyStopping, CSVLogger
from keras.preprocessing.image import ImageDataGenerator

from data import load_train_and_test
from config import SHAPE, EPOCHS, CSVPATH, NUMBERSOFATTRIBUTES, LOSS, MODELSAVELOCATION, MODELEXTENSTION, CSVEXTENSION

from model import build_model as builder

def args_handler(args):
    if "-test" in args:
        return 1
    else:
        return 2

if __name__ == "__main__":
    """ Arguments """
    args = sys.argv[1:]
    model_choice = args_handler(args)

    path = os.getcwd()
    now = datetime.now()
    dt_string = now.strftime("%d_%m_%Y %H_%M_%S")

    """ Seeding """
    np.random.seed(42)
    tf.random.set_seed(42)

    """ Dataset """
    (X_train, y_train), (X_test, y_test) = load_train_and_test()
    X_train = X_train.astype('float32') / 255
    X_test = X_test.astype('float32') / 255
    print(
        f"Dataset: Train: {len(X_train)} - Test: {len(X_test)}")

    """ Hyperparameters """
    shape = SHAPE
    epochs = EPOCHS
    csv_path = CSVPATH
    csv_ext = CSVEXTENSION
    num_attributes = NUMBERSOFATTRIBUTES
    loss = LOSS
    model_path = MODELSAVELOCATION
    model_ext = MODELEXTENSTION

    """ Model """
    if(model_choice == 1):
        exit()
    if(model_choice == 2):
        model = builder(shape, num_attributes)

    """ Data Augmentation """
    datagen = ImageDataGenerator(rotation_range=20, width_shift_range=0.1, height_shift_range=0.1,
                         zoom_range=0.1, horizontal_flip=True,
                         fill_mode="nearest")

    validgen = ImageDataGenerator()

    """ Data Configuration """
    model.compile(loss=loss,
                  optimizer=tf.keras.optimizers.Adam(), metrics=["acc", "categorical_accuracy"])

    callbacks = [
        ModelCheckpoint(path + model_path + str(model_choice) + " " + dt_string + model_ext, verbose=1, save_best_model=True),
        ReduceLROnPlateau(monitor="loss", patience=3,
                          factor=0.1, verbose=1, min_lr=1e-6),
        CSVLogger(path + csv_path + str(model_choice) +" " + dt_string + csv_ext),
        EarlyStopping(monitor="loss", patience=5, verbose=1)
    ]

    """ Training """
    model.fit(datagen.flow(X_train, y_train, batch_size=32),
              validation_data = validgen.flow(X_test, y_test, batch_size=8),
              steps_per_epoch=len(X_train) // 32,
              validation_steps=len(X_test) // 8,
              epochs=epochs,
              callbacks=callbacks
              )


import json
import os
import random
from PIL import Image
import torch
from diffusers import StableDiffusionPipeline
from multiprocessing import Pool, cpu_count

model_id = "CompVis/stable-diffusion-v1-4"
device = "cuda"

# Read concepts from JSONL file
with open("/home/wangyu/iNLG_v1/data/concept2text/commongen_inhouse_with_image_ofa/test.jsonl", "r") as f:
    concepts_list = [json.loads(line) for line in f]

# Shuffle the concepts_list
random.shuffle(concepts_list)

# Determine the number of concepts to select (20% of total)
num_to_select = int(len(concepts_list) * 0.05)

# Select a subset of the concepts_list
selected_concepts = concepts_list[:num_to_select]

# Initialize the pipeline
pipe = StableDiffusionPipeline.from_pretrained(model_id, torch_dtype=torch.float16)
pipe = pipe.to(device)

# Define a helper function to generate images for a given concept
def generate_images(concepts_dict, i):
    concepts = concepts_dict["concepts"]
    target = concepts_dict["target"]
    img_path_1 = f"/home/wangyu/iNLG_v1/images/commongen_inhouse_iamges/test_{i}_0.png"
    img_path_2 = f"/home/wangyu/iNLG_v1/images/commongen_inhouse_iamges/test_{i}_1.png"

    # Generate first image
    image_1 = pipe(concepts).images[0]
    image_1.save(img_path_1)

    # Generate second image
    image_2 = pipe(concepts).images[0]
    image_2.save(img_path_2)

    # Update the concepts_dict with the image paths
    concepts_dict["img_paths"] = [img_path_1, img_path_2]

    return concepts_dict

# Use multiprocessing to generate images for all selected concepts in parallel
with Pool(processes=4) as p:
    selected_concepts = p.starmap(generate_images, zip(selected_concepts, range(num_to_select)))

# Write the selected concepts with their image paths to JSONL file
with open("/home/wangyu/iNLG_v1/data/concept2text/commongen_inhouse_with_image_ofa/test_mul_images.jsonl", "w") as f:
    for line in selected_concepts:
        f.write(json.dumps(line) + "\n")