# My
import sys
input = sys.stdin.readline

n = int(input())
board = [list(map(int, input().split())) for _ in range(n)]

# 폭탄이 터지는 횟수
bomb_count = 0
for b in board:
    bomb_count += b.count(1)

dir_mapper = {
    1: [[1, 2, -1, -2], [0, 0, 0, 0]],
    2: [[1, 0, -1, 0], [0, 1, 0, -1]],
    3: [[-1, -1, 1, 1], [-1, 1, -1, 1]]
}

def explode(nums, visited):
    for num in nums:
        dx, dy = dir_mapper[num]
        for x in range(n):
            for y in range(n):
                if board[x][y] == 1:
                    for i in range(4):
                        nx = x + dx[i]
                        ny = y + dy[i]

                        if nx < 0 or nx >= n or ny < 0 or ny >= n or visited[nx][ny]:
                            continue

                        visited[nx][ny] = True
    for k in visited:
        print(*k)
    print()
    return

nums = []

def Choose(cnt):
    if cnt == bomb_count + 1:
        visited = [[False] * n for _ in range(n)]
        a = [[0] * n for _ in range(n)]
        # print(nums)
        explode(nums, visited)
        return

    for i in range(1, 4):
        nums.append(i)
        Choose(cnt + 1)
        nums.pop()

Choose(1)

for b in board:
    print(*b)

# Sol 1
n = int(input())
bomb_type = [
    [0 for _ in range(n)]
    for _ in range(n)
]
bombed = [
    [False for _ in range(n)]
    for _ in range(n)
]

ans = 0

bomb_pos = list()

def in_range(x, y):
    return 0 <= x and x < n and 0 <= y and y < n

def bomb(x, y, b_type):
    # 폭탄 종류마다 터질 위치를 미리 정의합니다.
    bomb_shapes = [
        [],
        [[-2, 0], [-1, 0], [0, 0], [1, 0], [2, 0]],
        [[-1, 0], [1, 0], [0, 0], [0, -1], [0, 1]],
        [[-1, -1], [-1, 1], [0, 0], [1, -1], [1, 1]]
    ]

    # 격자 내 칸에 대해서만 영역을 표시합니다.
    for i in range(5):
        dx, dy = bomb_shapes[b_type][i];
        nx, ny = x + dx, y + dy
        if in_range(nx, ny):
            bombed[nx][ny] = True

def calc():
    # Step1. 폭탄이 터진 위치를 표시하는 배열을
    # 초기화합니다.
    for i in range(n):
        for j in range(n):
            bombed[i][j] = False

    # Step2. 각 폭탄의 타입에 따라
    # 초토화 되는 영역을 표시합니다.
    for i in range(n):
        for j in range(n):
            if bomb_type[i][j]:
                bomb(i, j, bomb_type[i][j])

    # Step3. 초토화된 영역의 수를 구합니다.
    cnt = 0
    for i in range(n):
        for j in range(n):
            if bombed[i][j]:
                cnt += 1

    return cnt

def find_max_area(cnt):
    global ans

    if cnt == len(bomb_pos):
        ans = max(ans, calc())
        return

    for i in range(1, 4):
        x, y = bomb_pos[cnt]

        bomb_type[x][y] = i
        find_max_area(cnt + 1)
        bomb_type[x][y] = 0

for i in range(n):
    given_row = list(map(int, input().split()))
    for j, bomb_place in enumerate(given_row):
        if bomb_place:
            bomb_pos.append((i, j))

find_max_area(0)

print(ans)

# Sol2
from copy import deepcopy

def explode_and_count(board):
    global _max

    visited = [[False] * N for _ in range(N)]
    tot = len(booms)

    for cx,cy in booms: # 폭탄 위치 일단 체크
        visited[cx][cy] = True

    for sx,sy in booms: # 폭탄 위치 꺼내오기
        lst = dics[data[sx][sy]] # 해당 폭탄 위치 이동해야 하는 현재 +- 위치들ㅑ

        for x,y in lst:
            nx, ny = sx + x, sy + y

            if 0 <= nx < N and 0 <= ny <N and not visited[nx][ny]:
                visited[nx][ny] = True
                tot += 1
    _max = max(tot, _max)

def write_boom():  # booms -> 폭탄 위치, match_pos -> lst 속에 1,2,2 이런식의 폭탄이 들어있음
    for idx in range(len(booms)):
        x, y = booms[idx]
        data[x][y] = match_pos[idx]

    explode_and_count(deepcopy(data))

def back(cur_num):
    global boom_size

    if cur_num == boom_size + 1:
        write_boom()
        return

    for a in range(1, 4):
        match_pos.append(a)
        back(cur_num + 1)
        match_pos.pop()

N = int(input())
data = [list(map(int, input().split())) for _ in range(N)]
_max = 0
match_pos = []
booms = []
dics = {1: [(-1, 0), (-2, 0), (1, 0), (2, 0)], 2: [(-1, 0), (1, 0), (0, -1), (0, 1)],
        3: [(-1, -1), (-1, 1), (1, -1), (1, 1)]}

for i in range(N):  # 폭탄 위치 찾기
    for j in range(N):
        if data[i][j] != 0:
            booms.append((i, j))
            data[i][j] = 0

boom_size = len(booms)

back(1)
print(_max)

Algorithm/CodeTree/backtracking/K개 중 하나를 N번 선택하기/강력한 폭발.py


import os
import random
import numpy as np
import tensorflow as tf

seed = 1234
os.environ['PYTHONHASHSEED'] = str(seed)
os.environ['TF_DETERMINISTIC_OPS'] = '1'
random.seed(seed)
np.random.seed(seed)
tf.random.set_seed(seed)

import argparse
parser = argparse.ArgumentParser()
parser.add_argument("--tfrecs", nargs="+", default=[])
parser.add_argument("--weights", nargs="+", default=[])
parser.add_argument("--vocab", type=str, required=True)
parser.add_argument("--batch-size", type=int, required=False, default=64)
parser.add_argument("--eval-step", type=int, required=False, default=100)
parser.add_argument("--save-step", type=int, required=False, default=1000)
parser.add_argument("--val-step", type=int, required=False, default=1000)
parser.add_argument("--train-step", type=int, required=False, default=100000)
parser.add_argument("--begin-lr", type=float, required=False, default=1e-4)
parser.add_argument("--val-lr-update", type=float, required=False, default=3)
parser.add_argument("--output", type=str, required=True)
args = parser.parse_args()

if len(args.tfrecs) == 0: sys.exit(0)
assert len(args.tfrecs) == len(args.weights)
weights = [int(e) for e in args.weights]
weights = weights / np.sum(weights)

import json
samp_len = None
mix_lens = []

for tfrec in args.tfrecs:
  tfrec_args = os.path.join(tfrec, "ARGS")
  with open(tfrec_args, "r") as f:
    _json = json.loads(f.readlines()[-1])
    _samp_len = _json["samp_len"]

    _mix_len = 0
    if "mixup_multiplier" in _json:
      _mix_len = _json["mixup_multiplier"]

    if samp_len is None: samp_len = _samp_len
    assert samp_len == _samp_len
    mix_lens.append(_mix_len)

vocab = [e.strip() for e in open(args.vocab).readlines()]

import types
import sys
from stat import S_IREAD, S_IRGRP, S_IROTH, S_IWUSR

args_file = os.path.join(args.output, "ARGS")
if os.path.isdir(args.output):
  msg = 'directory {} exists. Do you want to proceed?'.format(args.output)
  cont = input("%s (y/N) " % msg).lower() == 'y'
  if not cont: sys.exit(0)
  if os.path.isfile(args_file):
    os.chmod(args_file, S_IWUSR|S_IREAD)

os.makedirs(args.output, exist_ok=True)
with open(args_file, "w") as f:
  f.write(" ".join([sys.executable] + sys.argv))
  f.write("\n")
  f.write(json.dumps(vars(args)))
os.chmod(args_file, S_IREAD|S_IRGRP|S_IROTH)

gpus = tf.config.list_physical_devices('GPU')
if gpus:
  try:
    # Currently, memory growth needs to be the same across GPUs
    for gpu in gpus:
      tf.config.experimental.set_memory_growth(gpu, True)
    logical_gpus = tf.config.list_logical_devices('GPU')
    print(len(gpus), "Physical GPUs,", len(logical_gpus), "Logical GPUs")
  except RuntimeError as e:
    # Memory growth must be set before GPUs have been initialized
    print(e)

import parse_data
import glob

datasets = []
for tfrec, mix_len in zip(args.tfrecs, mix_lens):
  tfrec_list = glob.glob(os.path.join(tfrec, "train-*.tfrecord"))

  if mix_len == 0:
    dataset = parse_data.gen_train_v1(tfrec_list,
      samp_len, mix_len, len(vocab),
      batch_size=args.batch_size, seed=seed)
  else:
    dataset = parse_data.gen_train_v2(tfrec_list,
      samp_len, mix_len, len(vocab),
      batch_size=args.batch_size, seed=seed)

  datasets.append(dataset)

dataset = tf.data.Dataset.sample_from_datasets(datasets, weights=weights, seed=seed)
dataset = dataset.batch(args.batch_size)
dataset = dataset.prefetch(buffer_size=tf.data.experimental.AUTOTUNE)

lr = tf.Variable(args.begin_lr, trainable=False)
# lr_schedule = tf.keras.optimizers.schedules.ExponentialDecay(
#   lr, decay_steps=1000, decay_rate=0.96, staircase=False)
opt = tf.keras.optimizers.Adam(learning_rate=lr)

import model
m = model.tdnn(len(vocab))

specs = [val.__spec__ for name, val in sys.modules.items() \
  if isinstance(val, types.ModuleType) and not ('_main_' in name)]
origins = [spec.origin for spec in specs if spec is not None]
origins = [e for e in origins if e is not None and os.getcwd() in e]

import shutil
for origin in origins + [os.path.abspath(__file__)]:
  shutil.copy(origin, args.output)

import datetime
logdir = os.path.join(args.output, "logs")
log_writer = tf.summary.create_file_writer(logdir)
log_writer.set_as_default()

@tf.function
def run_step(step, pcm, target, training=True):
  with tf.GradientTape() as tape, log_writer.as_default():
    loss = m((pcm, target), training=training)
    loss = tf.math.reduce_mean(loss)
    tf.summary.scalar("loss", loss, step=step)

  if training:
    grads = tape.gradient(loss, m.trainable_weights)
    grads, _ = tf.clip_by_global_norm(grads, 5.)
    opt.apply_gradients(zip(grads, m.trainable_weights))

  return loss

import logging
logger = tf.get_logger()
logger.setLevel(logging.INFO)

logfile = os.path.join(args.output, "train.log")
if os.path.isfile(logfile): os.remove(logfile)
fh = logging.FileHandler(logfile)
logger.addHandler(fh)

ckpt = tf.train.Checkpoint(model=m)
prev_val_loss = None; stall_cnt = 0

for idx, data in enumerate(dataset):
  if idx > args.train_step: break

  loss = run_step(tf.cast(idx, tf.int64),
    data["pcm"], data["target"])
  log_writer.flush()

  if idx > 0 and idx % args.eval_step == 0:
    logger.info("gstep[{}] loss[{:.2f}] lr[{:.2e}]".format(
      idx, loss, lr.numpy()))

  if idx > 0 and idx % args.save_step == 0:
    modelname = "model-{}.ckpt".format(idx)
    modelpath = os.path.join(args.output, modelname)
    ckpt.write(modelpath)

    optname = "adam-{}-weight".format(idx)
    optpath = os.path.join(args.output, optname)
    np.save(optpath, opt.get_weights())

    cfgname = "adam-{}-config".format(idx)
    cfgpath = os.path.join(args.output, cfgname)
    np.save(cfgpath, opt.get_config())

    logger.info("model is saved as {}".format(modelpath))


# encoding: utf-8

# library
import sys
import time
import os
import threading
from tabulate import tabulate

# import custom module
from mymodule.drive import quickstart
from mymodule.drive.DriveFile import DriveFile

def cmd_list():
    list_commands = '''
          [ login - login to google drive ] [ exit ]

    ls   - list directory            mkdir - make directory
    rm   - remove file/directory     get   - download file
    help - list commands             put   - upload file
    link - view file (link)          !     - local command
    '''
    return list_commands

def ls_command(cmd):
    cmd_split = cmd.split(' ')
    trash = 'true' if len(cmd_split) > 1 and cmd_split[1] == '-trash' else 'false'
    path = cmd_split[1] if len(cmd_split) == 2 and cmd_split[1] != '-trash' else 'root'
    path = cmd_split[2] if len(cmd_split) == 3 else path
    # print(f"'{path}' in parents and trashed={trash}")
    try:
        ls = DriveFile(credentials_module).searchFile(f"'{path}' in parents and trashed={trash}")
        dataFile = {
            'name': [i['title'] for i in ls],
            # 'size': [i['size'] for i in ls],
            'id': [i['id'] for i in ls]
        }
        table = tabulate(dataFile, headers=['name', 'id'], showindex=True)
        return table
    except:
        return 'ls: error'

def mkdir_command(cmd):
    cmd_split = cmd.split(' ')
    if len(cmd_split) != 2 and len(cmd_split) != 3:
        return 'mkdir: missing operand. mkdir <folder_id> <name>'
    try:
        folder_id = cmd_split[1] if len(cmd_split) != 2 else 'root'
        path = cmd_split[2] if len(cmd_split) != 2 else cmd_split[1]
        DriveFile(credentials_module).createFolder(folder_id, path)
        return f'mkdir success crete folder [{path}]'
    except:
        return f'error create file'

def rm_command(cmd):
    cmd_split = cmd.split(' ')
    if len(cmd_split) != 2 and len(cmd_split) != 3:
        return 'rm: missing operand. rm <mode> <file_id>'
    try:
        mode = 'permanent' if str(cmd_split[1]) == '-p' else 'trash'
        mode = 'outTrash' if str(cmd_split[1]) == '-outTrash' else mode
        string_IDs = cmd_split[2] if len(cmd_split) != 2 else cmd_split[1]
        list_IDs = string_IDs.split(',')
        list_thread = list()
        for id_file in list_IDs:
            thread = threading.Thread(target=DriveFile(credentials_module).removeFile, args=(id_file, mode))
            list_thread.append(thread)
            thread.start()
            # DriveFile(credentials_module).removeFile(id_file, mode)
        for thread in list_thread:
            thread.join()
        return f'rm success remove file {string_IDs}'
    except:
        return f'error remove'

def get_command(cmd):
    cmd_split = cmd.split(' ')
    if len(cmd_split) != 2 and len(cmd_split) != 3:
        return 'get: missing operand. get <id_file> <path>'
    try:
        string_IDs = cmd_split[1]
        list_IDs = string_IDs.split(',')
        path = cmd_split[2] if len(cmd_split) != 2 else './'
        path = path.replace('#', ' ')
        list_thread = list()
        for id_file in list_IDs:
            thread = threading.Thread(target=DriveFile(credentials_module).downloadFile, args=(id_file, path))
            list_thread.append(thread)
            thread.start()
            # DriveFile(credentials_module).downloadFile(id_file, path)
        for thread in list_thread:
            thread.join()
        return f'get: download success'
    except:
        return f'get: error download'

def put_command(cmd):
    cmd_split = cmd.split(' ')
    if len(cmd_split) != 2 and len(cmd_split) != 3:
        return 'put: missing operand. put <id_folder> <path>'
    try:
        id_folder = cmd_split[1] if len(cmd_split) != 2 else 'root'
        string_path = cmd_split[2] if len(cmd_split) != 2 else cmd_split[1]
        string_path = string_path.replace('#', ' ')
        list_path = string_path.split(',')
        list_thread = list()
        for path in list_path:
            thread = threading.Thread(target=DriveFile(credentials_module).uploadFile, args=(id_folder, path))
            list_thread.append(thread)
            thread.start()
            # DriveFile(credentials_module).uploadFile(id_folder, path)
        for thread in list_thread:
            thread.join()
        return f'put: upload success'
    except:
        return f'put: error upload'

def link_command(cmd):
    cmd_split = cmd.split(' ')
    path = cmd_split[1] if len(cmd_split) != 1 else 'root'
    try:
        ls = DriveFile(credentials_module).searchFile(f"'{path}' in parents and trashed=false")
        dataFile = {
            'name': [i['title'] for i in ls],
            # 'id': [i['id'] for i in ls],
            'link': [i['link'] for i in ls]
        }
        table = tabulate(dataFile, headers=['name', 'link'], showindex=True)
        return table
    except:
        return 'error'

def local_command(cmd):
    command = cmd.replace('!', '')
    try:
        os.system(command)
        return f'!: local command [{command}]'
    except:
        return '!: error local command'

def commands(cmd):
    command = cmd.split(' ')[0]
    if command == 'login':
        quickstart.login()
        return 'login success'
    elif command == 'exit':
        sys.exit(0)
    elif command == 'ls':
        return ls_command(cmd)
    elif command == 'help':
        return cmd_list()
    elif command == 'mkdir':
        return mkdir_command(cmd)
    elif command == 'rm':
        return rm_command(cmd)
    elif command == 'get':
        return get_command(cmd)
    elif command == 'put':
        return put_command(cmd)
    elif command == 'link':
        return link_command(cmd)
    elif str(command[0:1]) == '!':
        return local_command(cmd)
    else:
        return 'Invalid command'

def main():
    cmd = input('> ')
    out_command = commands(cmd)
    print(out_command)

if __name__ == '__main__':
    global credentials_module
    credentials_module = 'credentials_module.json'

    print(cmd_list())
    while True:
        try:
            main()
            time.sleep(0.1)
        except Exception as e:
            print(f'ERROR:\n{e}')


from django.shortcuts import render, redirect, get_object_or_404
from django.contrib.auth.decorators import login_required
from .models import Soru, Yorum
from .forms import SoruForm, YorumForm
from django.db.models import Q
from django.contrib.auth.forms import UserCreationForm
from django.contrib.auth import login
from django.http import HttpResponseRedirect
from django.urls import reverse
from django.http import JsonResponse
from django.core.paginator import Paginator

def anasayfa(request):
    sorular = Soru.objects.all().order_by('-tarih')
    return render(request, 'sorular/anasayfa.html', {'sorular': sorular})

def soru_detay(request, soru_id):
    soru = get_object_or_404(Soru, pk=soru_id)
    yorumlar_listesi = soru.yorum_set.order_by("-tarih")

    # Yorumlar için sayfalama
    paginator = Paginator(yorumlar_listesi, 10)  # Her sayfada 10 yorum göster
    page = request.GET.get("page")
    yorumlar = paginator.get_page(page)

    return render(
        request,
        "sorular/soru_detay_ajax.html",
        {"soru": soru, "yorumlar": yorumlar},
    )
@login_required
def soru_ekle(request):
    if request.method == 'POST':
        form = SoruForm(request.POST)
        if form.is_valid():
            yeni_soru = form.save(commit=False)
            yeni_soru.kullanici = request.user
            yeni_soru.save()
            return redirect('soru_detay', soru_id=yeni_soru.id)
    else:
        form = SoruForm()
    return render(request, 'sorular/soru_ekle.html', {'form': form})

@login_required
def yorum_ekle(request, soru_id):
    soru = get_object_or_404(Soru, id=soru_id)
    if request.method == 'POST':
        form = YorumForm(request.POST)
        if form.is_valid():
            yeni_yorum = form.save(commit=False)
            yeni_yorum.kullanici = request.user
            yeni_yorum.soru = soru
            yeni_yorum.save()
            return redirect('soru_detay', soru_id=soru.id)
    else:
        form = YorumForm()
    return render(request, 'sorular/yorum_ekle.html', {'form': form, 'soru': soru})

def arama(request):
    arama_sorgusu = request.GET.get('q')
    eslesen_sorular = Soru.objects.filter(baslik__icontains=arama_sorgusu)

    tam_eslesme = Soru.objects.filter(baslik=arama_sorgusu).first()
    if tam_eslesme:
        return redirect('soru_detay', soru_id=tam_eslesme.id)
    else:
        return render(request, 'sorular/arama_sonuclari.html', {'sorular': eslesen_sorular, 'arama_sorgusu': arama_sorgusu})

def signup(request):
    if request.method == 'POST':
        form = UserCreationForm(request.POST)
        if form.is_valid():
            user = form.save()
            login(request, user)
            return redirect('anasayfa')
    else:
        form = UserCreationForm()
    return render(request, 'sorular/signup.html', {'form': form})

@login_required
def baslik_olustur(request):
    if request.method == 'POST':
        baslik = request.POST.get('soru')
        yeni_baslik = Soru.objects.create(baslik=baslik, kullanici=request.user)
        return redirect('soru_detay', soru_id=yeni_baslik.id)
    else:
        return redirect('anasayfa')

def yorum_ekle(request, soru_id):
    if request.method == "POST":
        form = YorumForm(request.POST)
        if form.is_valid():
            yorum = form.save(commit=False)
            yorum.kullanici = request.user
            yorum.soru = get_object_or_404(Soru, pk=soru_id)
            yorum.save()

            # JSON response oluşturma
            return JsonResponse({
    'success': True,
    'kullanici': request.user.username,
    'yorum': yorum.icerik,  # 'yorum' yerine 'icerik' kullanın
    'tarih': yorum.tarih.strftime('%Y-%m-%d %H:%M')
})

    return HttpResponseRedirect(reverse("soru_detay", args=(soru_id,)))


# toNumbers_function.py

def toNumbers(strList):
    for i in range(len(strList)):
        number = eval(strList[i])
        strList[i] = number

def test():
    strList1 = ["1", "2", "3"]
    print(strList1)
    toNumbers(strList1)
    print(strList1)

test()


import random
import math

NUMBER_PER_BOX = 3

# indices representation ### CONSTANTS ###
x = 0  # x index in box
y = 1  # y index in box
z = 2  # z index in box
v = 3  # v index in box
number = 3  # number index in box
offset = 4  # offset index in box
nn = 5  # nn index in box

random.seed(7)

def lavaMD(alpha, boxes1d_arg, number_boxes, box, box_nei, rv, qv, fv):
    a2 = 2.0*alpha*alpha

    #	PROCESS INTERACTIONS
    for ii in range(boxes1d_arg):
        #  home boxes in y direction
        for jj in range(boxes1d_arg):
            #  home boxes in x direction
            for kk in range(boxes1d_arg):
                #	home box - box parameters
                l = ii * boxes1d_arg * boxes1d_arg + jj * boxes1d_arg + kk
                first_i = l * NUMBER_PER_BOX  # offset to common arrays
                #	Do for the # of (home+neighbor) boxes
                for k in range((1+box[l * 6 + nn])):
                    #	neighbor box - get pointer to the right box
                    if (k == 0):
                        pointer = l  # set first box to be processed to home box
                    else:
                        # remaining boxes are neighbor boxes
                        pointer = box_nei[l * 5 * 26 + (k-1) * 5 + number]

                    #	neighbor box - box parameters
                    first_j = box[pointer * 6 + offset]
                    #	Do for the # of particles in home box
                    for i in range(NUMBER_PER_BOX):
                        # do for the # of particles in current (home or neighbor) box
                        for j in range(NUMBER_PER_BOX):
                            #   coefficients			DOT product
                            r1 = rv[(first_i + i) * 4 + x] * \
                                rv[(first_j + j) * 4 + x]
                            r1 += rv[(first_i + i) * 4 + y] * \
                                rv[(first_j + j) * 4 + y]
                            r1 += rv[(first_i + i) * 4 + z] * \
                                rv[(first_j + j) * 4 + z]
                            r2 = rv[(first_i + i) * 4 + v] + \
                                rv[(first_j + j) * 4 + v]
                            r2 -= r1
                            u2 = a2*r2
                            vij = math.exp(-u2)
                            fs = 2.0*vij
                            fxij = fs*(rv[(first_i + i) * 4 + x] -
                                       rv[(first_j + j) * 4 + x])
                            fyij = fs*(rv[(first_i + i) * 4 + y] -
                                       rv[(first_j + j) * 4 + y])
                            fzij = fs*(rv[(first_i + i) * 4 + z] -
                                       rv[(first_j + j) * 4 + z])

                            # forces
                            fv[(first_i + i) * 4 + v] += qv[(first_j + j)]*vij
                            fv[(first_i + i) * 4 + x] += qv[(first_j + j)]*fxij
                            fv[(first_i + i) * 4 + y] += qv[(first_j + j)]*fyij
                            fv[(first_i + i) * 4 + z] += qv[(first_j + j)]*fzij

def initializeHomeBoxes(boxes1d_arg, box_py, box_py_nei):
    #  home boxes in z direction
    for i in range(boxes1d_arg):
        #  home boxes in y direction
        for j in range(boxes1d_arg):
            #  home boxes in x direction
            for k in range(boxes1d_arg):
                #  current home box
                nh = i * boxes1d_arg * boxes1d_arg + j * boxes1d_arg + k
                box_py[nh * 6 + x] = k
                box_py[nh * 6 + y] = j
                box_py[nh * 6 + z] = i
                box_py[nh * 6 + number] = nh
                box_py[nh * 6 + offset] = nh * NUMBER_PER_BOX
                #  initialize number of neighbor boxes
                box_py[nh * 6 + nn] = 0
                #  neighbor boxes in z direction
                for l in range(-1, 2):
                    #  neighbor boxes in y direction
                    for m in range(-1, 2):
                        #  neighbor boxes in x direction
                        for n in range(-1, 2):
                            #  check if (this neighbor exists) and (it is not the same as home box)
                            if((((i+l) >= 0 and (j+m) >= 0 and (k+n) >= 0) and ((i+l) < boxes1d_arg and (j+m) < boxes1d_arg and (k+n) < boxes1d_arg)) and not (l == 0 and m == 0 and n == 0)):
                                #  current neighbor box
                                nnn = box_py[nh * 6 + nn]
                                idx = nh * 5 * 26 + nnn * 5
                                box_py_nei[idx + x] = (k+n)
                                box_py_nei[idx + y] = (j+m)
                                box_py_nei[idx + z] = (i+l)
                                box_py_nei[idx + number] = (box_py_nei[idx + z] * boxes1d_arg * boxes1d_arg) + (
                                    box_py_nei[idx + y] * boxes1d_arg) + box_py_nei[idx + x]
                                box_py_nei[idx + offset] = box_py_nei[nh *
                                                                      5 * 26 + nnn * 5 + number] * NUMBER_PER_BOX

                                #  increment neighbor box
                                box_py[nh * 6 + nn] += 1

class Data:
    def __init__(self):
        self.box_py = None
        self.box_py_nei = None
        self.rv_py = None
        self.qv_py = None
        self.fv_py = None

data = Data()

def measure(boxes1d_arg, alpha=0.5):
    print("Initializing...")
    initializeHomeBoxes(boxes1d_arg, data.box_py, data.box_py_nei)

    #  total number of boxes
    number_boxes = boxes1d_arg * boxes1d_arg * boxes1d_arg

    print("Starting...")
    lavaMD(alpha, boxes1d_arg, number_boxes, data.box_py,
           data.box_py_nei, data.rv_py, data.qv_py, data.fv_py)

default_size = 32

def __benchmark__(boxes1d_arg=default_size):
    measure(boxes1d_arg)

def __setup__(boxes1d_arg=default_size):
    #  Print configuration
    print("Configuration used: boxes1d = %d\n" % (boxes1d_arg))
    #  total number of boxes
    number_boxes = boxes1d_arg * boxes1d_arg * boxes1d_arg

    #  how many particles space has in each direction
    space_elem = number_boxes * 3

    # 	BOX
    #  allocate boxes
    data.box_py = [0 for i in range(6 * number_boxes)]
    data.box_py_nei = [0 for i in range(5 * 26 * number_boxes)]

    #  input (distances): get a number in the range 0.1 - 1.0
    data.rv_py = [(random.randint(0, 10) + 1) /
                  10.0 for i in range(4 * space_elem)]

    #  input (charge): get a number in the range 0.1 - 1.0
    data.qv_py = [(random.randint(0, 10) + 1) /
                  10.0 for i in range(space_elem)]

    #  output (forces): set to 0, because kernels keeps adding to initial value
    data.fv_py = [0. for i in range(4 * space_elem)]

def __cleanup__(boxes1d_arg=default_size):
    # clean up written data
    number_boxes = boxes1d_arg * boxes1d_arg * boxes1d_arg
    space_elem = number_boxes * 3
    for i in range(4 * space_elem):
        data.fv_py[i] = 0.


import re

from drozer import android
from drozer.modules import common, Module

class AttackSurface(Module, common.Filters, common.PackageManager):

    name = "Get attack surface of package"
    description = "Examine the attack surface of an installed package."
    examples = """Finding the attack surface of the built-in browser

    dz> run app.package.attacksurface com.android.browser

    6 activities exported
    4 broadcast receivers exported
    1 content providers exported
    0 services exported"""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2012-11-06"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("package", help="the identifier of the package to inspect")

    def execute(self, arguments):
        if arguments.package != None:
            package = self.packageManager().getPackageInfo(arguments.package, common.PackageManager.GET_ACTIVITIES | common.PackageManager.GET_RECEIVERS | common.PackageManager.GET_PROVIDERS | common.PackageManager.GET_SERVICES)
            application = package.applicationInfo

            activities = self.match_filter(package.activities, 'exported', True)
            receivers = self.match_filter(package.receivers, 'exported', True)
            providers = self.match_filter(package.providers, 'exported', True)
            services = self.match_filter(package.services, 'exported', True)

            self.stdout.write("Attack Surface:\n")
            self.stdout.write("  %d activities exported\n" % len(activities))
            self.stdout.write("  %d broadcast receivers exported\n" % len(receivers))
            self.stdout.write("  %d content providers exported\n" % len(providers))
            self.stdout.write("  %d services exported\n" % len(services))

            if (application.flags & application.FLAG_DEBUGGABLE) != 0:
                self.stdout.write("    is debuggable\n")

            if package.sharedUserId != None:
                self.stdout.write("    Shared UID (%s)\n" % package.sharedUserId)
        else:
            self.stdout.write("No package specified\n")

class Info(Module, common.Filters, common.PackageManager, common.IntentFilter):

    name = "Get information about installed packages"
    description = "List all installed packages on the device with optional filters. Specify optional keywords to search for in the package information, or granted permissions."
    examples = """Finding all packages with the keyword "browser" in their name:

    dz> run app.package.info -f browser

    Package: com.android.browser
      Process name: com.android.browser
      Version: 4.1.1
      Data Directory: /data/data/com.android.browser
      APK path: /system/app/Browser.apk
      UID: 10014
      GID: [3003, 1015, 1028]
      Shared libraries: null
      Permissions:
      - android.permission.ACCESS_COARSE_LOCATION
      - android.permission.ACCESS_DOWNLOAD_MANAGER
      - android.permission.ACCESS_FINE_LOCATION
      ...

Finding all packages with the "INSTALL_PACKAGES" permission:

    dz> run app.package.info -p INSTALL_PACKAGES

    Package: com.android.packageinstaller
      Process Name: com.android.packageinstaller
      Version: 4.1.1-403059
      Data Directory: /data/data/com.android.packageinstaller
      APK Path: /system/app/PackageInstaller.apk
      UID: 10003
      GID: [1028]
      Shared Libraries: null
      Shared User ID: null
      Permissions:
      - android.permission.INSTALL_PACKAGES
      - android.permission.DELETE_PACKAGES
      - android.permission.CLEAR_APP_CACHE
      - android.permission.READ_PHONE_STATE
      - android.permission.CLEAR_APP_USER_DATA
      - android.permission.READ_EXTERNAL_STORAGE"""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2012-11-06"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("-a", "--package", default=None, help="the identifier of the package to inspect")
        parser.add_argument("-d", "--defines-permission", default=None, help="filter by the permissions a package defines")
        parser.add_argument("-f", "--filter", default=None, help="keyword filter conditions")
        parser.add_argument("-g", "--gid", default=None, help="filter packages by GID")
        parser.add_argument("-p", "--permission", default=None, help="permission filter conditions")
        parser.add_argument("-u", "--uid", default=None, help="filter packages by UID")
        parser.add_argument("-i", "--show-intent-filters", action="store_true", default=False , help="show intent filters")

    def execute(self, arguments):
        if arguments.package == None:
            for package in self.packageManager().getPackages(common.PackageManager.GET_PERMISSIONS | common.PackageManager.GET_CONFIGURATIONS | common.PackageManager.GET_GIDS | common.PackageManager.GET_SHARED_LIBRARY_FILES | common.PackageManager.GET_ACTIVITIES):
               self.__get_package(arguments, package)
        else:
            package = self.packageManager().getPackageInfo(arguments.package, common.PackageManager.GET_PERMISSIONS | common.PackageManager.GET_CONFIGURATIONS | common.PackageManager.GET_GIDS | common.PackageManager.GET_SHARED_LIBRARY_FILES | common.PackageManager.GET_ACTIVITIES)
            self.__get_package(arguments, package)

    def get_completion_suggestions(self, action, text, **kwargs):
        if action.dest == "permission":
            return android.permissions

    def __print_intent_filters(self, intent_filters):
        for intent_filter in intent_filters:
            if len(intent_filter.actions) > 0:
                self.stdout.write("    - Actions:\n")
                for action in intent_filter.actions:
                    self.stdout.write("      - %s\n" % action)
            if len(intent_filter.categories) > 0:
                self.stdout.write("    - Categories:\n")
                for category in intent_filter.categories:
                    self.stdout.write("      - %s\n" % category)
            if len(intent_filter.datas) > 0:
                self.stdout.write("    - Data:\n" )
                for data in intent_filter.datas:
                    self.stdout.write("      - %s\n" % data)

    def __get_package(self, arguments, package):
        application = package.applicationInfo
        activities = package.activities
        services = package.services

        intent_matches = not (arguments.show_intent_filters and arguments.filter)

        if not intent_matches and arguments.filter != None:
            if activities != None:
                for activity in activities:
                    if not intent_matches:
                        for intent_filter in self.find_intent_filters(activity, 'activity'):
                            if len(intent_filter.actions) > 0:
                                for action in intent_filter.actions:
                                    if action != None and action.upper().find(arguments.filter.upper()) >= 0:
                                        intent_matches = True
                                        break

                            if len(intent_filter.datas) > 0:
                                for data in intent_filter.datas:
                                    if data.scheme != None and data.scheme != False and data.scheme.upper().find(arguments.filter.upper()) >= 0:
                                        intent_matches = True
                                        break

                            if len(intent_filter.categories) > 0:
                                for category in intent_filter.categories:
                                    if category != None and category.upper().find(arguments.filter.upper()) >= 0:
                                        intent_matches = True
                                        break
                    else:
                        break

            if services != None:
                for service in services:
                    if not intent_matches:
                        for intent_filter in self.find_intent_filters(service, 'service'):
                            if len(intent_filter.actions) > 0:
                                for action in intent_filter.actions:
                                    if action != None and action.upper().find(arguments.filter.upper()) >= 0:
                                        intent_matches = True
                                        break

                            if len(intent_filter.uris) > 0:
                                for data in intent_filter.datas:
                                    if data.scheme != None and data.scheme != False and data.scheme.upper().find(arguments.filter.upper()) >= 0:
                                        intent_matches = True
                                        break

                            if len(intent_filter.categories) > 0:
                                for category in intent_filter.categories:
                                    if category != None and category.upper().find(arguments.filter.upper()) >= 0:
                                        intent_matches = True
                                        break
                    else:
                        break

        if (arguments.defines_permission == None or package.permissions != None and True in map(lambda p: p.name.upper().find(arguments.defines_permission.upper()) >= 0, package.permissions)) and (arguments.filter == None or package.packageName.upper().find(arguments.filter.upper()) >= 0 or self.packageManager().getApplicationLabel(package.packageName).upper().find(arguments.filter.upper()) >= 0) and (arguments.gid == None or package.gids != None and True in map(lambda g: g == int(arguments.gid), package.gids)) and (arguments.permission == None or package.requestedPermissions != None and True in map(lambda p: p.upper().find(arguments.permission.upper()) >= 0, package.requestedPermissions)) and (arguments.uid == None or arguments.uid == str(package.applicationInfo.uid)) and intent_matches:
            self.stdout.write("Package: %s\n" % application.packageName)
            self.stdout.write("  Application Label: %s\n" % self.packageManager().getApplicationLabel(application.packageName))
            self.stdout.write("  Process Name: %s\n" % application.processName)
            self.stdout.write("  Version: %s\n" % package.versionName)
            self.stdout.write("  Data Directory: %s\n" % application.dataDir)
            self.stdout.write("  APK Path: %s\n" % application.publicSourceDir)
            self.stdout.write("  UID: %s\n" % application.uid)
            if package.gids != None:
                self.stdout.write("  GID: %s\n" % package.gids)
            else:
                self.stdout.write("  GID: None\n")
            self.stdout.write("  Shared Libraries: %s\n" % application.sharedLibraryFiles)
            self.stdout.write("  Shared User ID: %s\n" % package.sharedUserId)
            self.stdout.write("  Uses Permissions:\n")
            if package.requestedPermissions != None:
                for permission in package.requestedPermissions:
                    self.stdout.write("  - %s\n" % permission)
            else:
                self.stdout.write("  - None\n")
            self.stdout.write("  Defines Permissions:\n")
            if package.permissions != None:
                for permission in package.permissions:
                    self.stdout.write("  - %s\n" % permission.name)
            else:
                self.stdout.write("  - None\n")
            if arguments.show_intent_filters:
                ifcount = 0
                self.stdout.write("  Intent Filters:\n")

                if activities != None:
                    for activity in activities:
                       intent_filters = self.find_intent_filters(activity, 'activity')
                       if len(intent_filters) > 0:
                           ifcount += len(intent_filters)

                           self.stdout.write("  - %s\n" % activity.name)
                           self.__print_intent_filters(intent_filters)

                if services!= None:
                    for service in services:
                        intent_filters = self.find_intent_filters(service, 'service')
                        if len(intent_filters) > 0:
                            ifcount += len(intent_filters)

                            self.stdout.write("  - %s\n" % service.name)
                            self.__print_intent_filters(intent_filters)

                if ifcount==0:
                    self.stdout.write("  - None")
            self.stdout.write("\n")

class LaunchIntent(Module, common.PackageManager):

    name = "Get launch intent of package"
    description = "Get the launch intent of an installed package."
    examples = """Finding the launch intent of the Android browser package:

    dz> run app.package.launchintent com.android.browser

    Intent:
      Action: android.intent.action.MAIN
      Component: {com.android.browser/com.android.browser.BrowserActivity}
      Data: null
      Categories:
        android.intent.category.LAUNCHER
      Flags: [ACTIVITY_NEW_TASK]
      Mime Type: null
      Extras: null"""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2013-03-06"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("package", help="the identifier of the package to inspect")
        parser.add_argument("-r", "--raw", action="store_true", default=False, help="display the raw output of the intent (as if you were calling intent.toString())")

    def execute(self, arguments):
        intent = self.packageManager().getLaunchIntentForPackage(arguments.package)

        if intent != None:
            if not arguments.raw:
                self.processIntent(intent)
            else:
                self.stdout.write("%s\n\n" % str(intent.toString()))
        else:
            self.stdout.write("No Launch Intent found.\n\n")

    def processIntent(self, intent):

        self.stdout.write("Launch Intent:\n")
        self.stdout.write("  Action: %s\n"%intent.getAction())
        self.stdout.write("  Component: %s\n"%intent.getComponent().toShortString())
        self.stdout.write("  Data: %s\n"%intent.getDataString())
        if intent.getCategories() == None:
            self.stdout.write("  Categories: null\n")
        else:
            self.stdout.write("  Categories: \n")
            for category in intent.getCategories().toArray():
                self.stdout.write("     - %s\n"%str(category.toString()))

        self.stdout.write("  Flags: %s\n"%self.processFlags(intent.getFlags()))
        self.stdout.write("  Mime Type: %s\n"%intent.getType())

        extras = intent.getExtras()
        if extras != None:
            if not extras.isEmpty():
                self.stdout.write("  Extras: \n")
                for extra in extras.keySet():
                    self.stdout.write("    - %s\n"%extras.get(extra))
        else:
            self.stdout.write("  Extras: null\n")

    def processFlags(self, flags):

        out = ""
        #flags are a bit mask
        for key in android.Intent.flags.keys():
            for i in range(0, 8):
                flag = flags & (0x0000000F << i*4)
                if android.Intent.flags.get(key) == flag:
                    out = out + "%s, "%key
        if out is not "":
            return "[%s]"%out[:-2]
        else:
            return "null"

class List(Module, common.PackageManager):

    name = "List Packages"
    description = "List all installed packages on the device. Specify optional keywords to search for in the package name."
    examples = """Finding all packages with the keyword "browser" in their name:

    dz> run app.package.list -f browser

    com.android.browser"""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2012-11-06"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("-d", "--defines-permission", default=None, help="filter by the permissions a package defines")
        parser.add_argument("-f", "--filter", default=None, help="keyword filter conditions")
        parser.add_argument("-g", "--gid", default=None, help="filter packages by GID")
        parser.add_argument("-p", "--permission", default=None, help="permission filter conditions")
        parser.add_argument("-u", "--uid", default=None, help="filter packages by UID")
        parser.add_argument("-n", "--no_app_name", action="store_true", default=False, help="do not print the app name")

    def execute(self, arguments):
        for package in self.packageManager().getPackages(common.PackageManager.GET_PERMISSIONS | common.PackageManager.GET_CONFIGURATIONS | common.PackageManager.GET_GIDS | common.PackageManager.GET_SHARED_LIBRARY_FILES):
            self.__get_package(arguments, package)

    def __get_package(self, arguments, package):
        application = package.applicationInfo

        if (arguments.defines_permission == None or package.permissions != None and True in map(lambda p: p.name.upper().find(arguments.defines_permission.upper()) >= 0, package.permissions)) and (arguments.filter == None or package.packageName.upper().find(arguments.filter.upper()) >= 0 or self.packageManager().getApplicationLabel(application.packageName).upper().find(arguments.filter.upper()) >= 0) and (arguments.gid == None or package.gids != None and True in map(lambda g: g == int(arguments.gid), package.gids)) and (arguments.permission == None or package.requestedPermissions != None and True in map(lambda p: p.upper().find(arguments.permission.upper()) >= 0, package.requestedPermissions)) and (arguments.uid == None or arguments.uid == str(package.applicationInfo.uid)):
            if arguments.no_app_name:
                self.stdout.write("%s\n" % application.packageName)
            else:
                try:
                    self.stdout.write("%s (%s)" % (application.packageName, self.packageManager().getApplicationLabel(application.packageName)))
                    self.stdout.write("\n")
                except IOError as e:
                    self.stdout.write(" (Warning: Cannot decode application label)\n")

class Manifest(Module, common.Assets):

    name = "Get AndroidManifest.xml of package"
    description = "Retrieves AndroidManifest.xml from an installed package."
    examples = """Getting the manifest for drozer

    dz> run app.package.manifest com.mwr.dz

    <manifest versionCode="2" versionName="1.1" package="com.mwr.dz">
      <uses-sdk minSdkVersion="8" targetSdkVersion="4">
      </uses-sdk>
      <uses-permission name="android.permission.INTERNET">
      </uses-permission>

      ...
    </manifest>"""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2012-11-06"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("package", help="the identifier of the package")

    def execute(self, arguments):
        if arguments.package == None or arguments.package == "":
            self.stderr.write("No package provided.\n")
        else:
            self.__write_manifest(self.getAndroidManifest(arguments.package))

    def __write_manifest(self, manifest):
        lines = manifest.split("\n")
        level = 0

        for line in lines:
            if line.startswith("</"):
                level -= 1

            if line.startswith("<") and not line.startswith("</"):
                pattern = re.compile("(.*)<([^>]+)>(.*)")
                match = pattern.match(line)

                contents = match.group(2).split(" ", 1)

                self.stdout.write("%s%s<[color green]%s[/color]" % ("  " * level, match.group(1), contents[0]))
                if len(contents) > 1:
                    self.stdout.write(("\n%s%s" % ("  " * level, " " * (len(contents[0]) + 1))).join(map(lambda m: " [color purple]%s[/color]=[color red]\"%s\"[/color]" % m, re.compile("([^=]+)=\"([^\"]+)\"\s*").findall(contents[1]))))

                self.stdout.write(">\n")
            else:
                self.stdout.write("%s%s\n" % ("  " * level, line))

            if line.startswith("<") and not "</" in line:
                level += 1

class Native(Module, common.ClassLoader, common.PackageManager):

    name = "Find Native libraries embedded in the application."
    description = "Find Native libraries embedded in the application."
    examples = ""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2013-03-23"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("package", help="the identifier of the package")

    def execute(self, arguments):
        Native = self.loadClass("common/Native.apk", "Native")

        package = self.packageManager().getPackageInfo(arguments.package, common.PackageManager.GET_PROVIDERS)
        libraries = Native.list(package.applicationInfo)

        if len(libraries) > 0:
            self.stdout.write("Package: %s\n" % package.packageName)
            self.stdout.write("  Native Libraries:\n")

            for library in libraries:
                self.stdout.write("   - %s\n"%library)
            self.stdout.write("\n")
        else:
            self.stdout.write("Package: %s\n" % package.packageName)
            self.stdout.write("  No Native Libraries.\n")
            self.stdout.write("\n")

class SharedUID(Module, common.PackageManager):

    name = "Look for packages with shared UIDs"
    description = "Finds packages that have shared UIDs and gives their accumulated permissions."
    examples = """Finding packages that share the UID 10011

    dz> run app.package.shareduid -u 10011

    UID: 10011 (com.motorola.blur.uid.provider_authenticator:10011)
    Package Name: com.motorola.blur.provider.photobucket
    Package Name: com.motorola.blur.provider.picasa
    Package Name: com.motorola.blur.provider.yahoo
    Package Name: com.motorola.blur.provider.twitter
    Package Name: com.motorola.blur.provider.fixedemail
    Package Name: com.motorola.blur.provider.motorola.app
    Package Name: com.motorola.blur.provider.orkut
    Package Name: com.motorola.blur.provider.email
    Package Name: com.motorola.blur.provider.facebook
    Package Name: com.motorola.blur.provider.lastfm
    Package Name: com.motorola.blur.provider.linkedin
    Package Name: com.motorola.blur.provider.youtube
    Package Name: com.motorola.blur.provider.skyrock
    Package Name: com.motorola.blur.provider.activesync
    Package Name: com.motorola.blur.provider.flickr
    Accumulated permissions: com.motorola.blur.setupprovider.Permissions.ACCESS_ACCOUNTS; ..."""
    author = "MWR InfoSecurity (@mwrlabs)"
    date = "2012-11-06"
    license = "BSD (3 clause)"
    path = ["app", "package"]
    permissions = ["com.mwr.dz.permissions.GET_CONTEXT"]

    def add_arguments(self, parser):
        parser.add_argument("-u", "--uid", default=None, help="specify uid")

    def execute(self, arguments):
        uids = set([])

        if arguments.uid == None:
            for package in self.packageManager().getPackages(common.PackageManager.GET_PERMISSIONS):
                uids.add(int(package.applicationInfo.uid))
        else:
            uids.add(int(arguments.uid))

        for uid in uids:
            self.stdout.write("UID: %d (%s)\n"%(uid, self.packageManager().getNameForUid(uid)))

            packages = self.packageManager().getPackagesForUid(uid)

            if packages != None:
                permissions = set([])

                for packageName in packages:
                    try:
                        package = self.packageManager().getPackageInfo(packageName, common.PackageManager.GET_PERMISSIONS)
                    except:
                        continue
                    self.stdout.write("  Package: %s\n"%packageName)

                    if package.requestedPermissions != None:
                        for permission in package.requestedPermissions:
                            permissions.add(permission)

                self.stdout.write("  Permissions: %s\n"%", ".join(map(lambda p: str(p), permissions)))
                self.stdout.write("\n")
            else:
                self.stdout.write("No such UID.\n")


import copy
import inspect

import torch

from ...utils import Registry, build_from_cfg

OPTIMIZERS = Registry('optimizer')
OPTIMIZER_BUILDERS = Registry('optimizer builder')

def register_torch_optimizers():
    torch_optimizers = []
    for module_name in dir(torch.optim):
        if module_name.startswith('__'):
            continue
        _optim = getattr(torch.optim, module_name)
        if inspect.isclass(_optim) and issubclass(_optim,
                                                  torch.optim.Optimizer):
            OPTIMIZERS.register_module()(_optim)
            torch_optimizers.append(module_name)
    return torch_optimizers

TORCH_OPTIMIZERS = register_torch_optimizers()

def build_optimizer_constructor(cfg):
    return build_from_cfg(cfg, OPTIMIZER_BUILDERS)

def build_optimizer(model, cfg):
    optimizer_cfg = copy.deepcopy(cfg)
    constructor_type = optimizer_cfg.pop('constructor',
                                         'DefaultOptimizerConstructor')
    paramwise_cfg = optimizer_cfg.pop('paramwise_cfg', None)
    optim_constructor = build_optimizer_constructor(
        dict(
            type=constructor_type,
            optimizer_cfg=optimizer_cfg,
            paramwise_cfg=paramwise_cfg))
    optimizer = optim_constructor(model)
    return optimizer


import sys
input = sys.stdin.readline

T = int(input())

for _ in range(T):
    n = int(input())

    clothes = []  # 의상 종류를 저장
    for i in range(n):
        clothes.append(input().split()[-1])

    kind = []   # 의상 종류를 저장 (중복X)
    count = []  # 같은 의상 종류의 개수를 저장
    for i in range(n):
        if not clothes[i] in kind:
            kind.append(clothes[i])
            count.append(clothes.count(clothes[i]))

    # 전체 = (의상 종류1 개수 + 1) x (의상 종류2 개수 + 1) x ... x (의상 종류k 개수 + 1) - 1  단, 여기서 k는 kind의 크기
    answer = 1
    for i in count:
        answer *= i+1

    print(answer-1)

[백준] 단계별로풀어보기/14 정수론 및 조합론/9375.py


from FASTA import ReadFASTA

def find_overlaps(arr, acc=''):
    if len(arr) == 0:
        return acc

    elif len(acc) == 0:
        acc = arr.pop(0)
        return find_overlaps(arr, acc)

    else:

        for i in range(len(arr)):
            a = arr[i]
            l = len(a)

            for p in range(l // 2):
                q = l - p

                if acc.startswith(a[p:]):
                    arr.pop(i)
                    return find_overlaps(arr, a[:p] + acc)

                if acc.endswith(a[:q]):
                    arr.pop(i)
                    return find_overlaps(arr, acc + a[q:])

if __name__ == "__main__":
    dna_list = ReadFASTA('data/rosalind_long.txt')
    reads = []
    for x in dna_list:
        reads.append(x[1])
    with open('output/rosalind_long.txt', "w") as fout:
        result = find_overlaps(reads)
        fout.write(result)


#
#   42 - Bleichenbacher's e=3 RSA Attack
#

from cryptopals.s06.c42.solution_c42 import Oracle, bleichenbacher_signature

def test_c42() -> None:
    # RSA Oracle
    oracle = Oracle(e=3)

    # Message
    message = b"\x00\x00\x2e\x36"

    # Forged Signature
    forged_signature = bleichenbacher_signature(
        n_size=oracle.parameters.size_in_bits(),
        e=oracle.parameters.e,
        message=message
    )

    assert oracle.verify(signature=forged_signature, message=message)


import gym
import itertools
import matplotlib.pyplot as plt
from IPython import display
import time

###################################################################################################################################################
# Creation of the environment
# Environment on which to train
# https://gym.openai.com/envs/CartPole-v1/
env = gym.make("CartPole-v1")

# Actions :
env.action_space.n
env.observation_space.sample()
# State : x, x_dot, theta, theta_dot = state
# position, velocity, angular_position, angular_velocity

###################################################################################################################################################
# Play a game in which actions are chosen randomly

env.reset()
img = plt.imshow(env.render(mode='rgb_array')) # only call this once
total_reward = 0
for timestep in itertools.count():
    img.set_data(env.render(mode='rgb_array')) # just update the data
    display.display(plt.gcf())
    display.clear_output(wait=True)
    action = env.action_space.sample()
    next_state, reward, done, info =env.step(action)
    total_reward += reward
    time.sleep(0.1)
    if done:
        print("The duration of the episode/game is : {} timestemps.".format(timestep+1))
        print("The total reward for the game is : {}.".format(total_reward))
        break
plt.clf()
plt.close("all")
env.close()

###################################################################################################################################################


from django.shortcuts import render, redirect, get_object_or_404
from .models import Imagem
from .forms import ImagemForm
from main import settings
from django.views.generic import ListView, DetailView
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.image as img
from sklearn.cluster import KMeans

# Generico

def index(request):
    num_imagens = Imagem.objects.all().count()

    context = {
        'num_imagens': num_imagens,
    }
    template_name = 'home.html'
    return render(request, template_name, context)

# Imagem

class listaImagem(ListView):
    template_name = 'listaImagem.html'
    context_object_name = 'imagem_list'

    def get_queryset(self):
        return Imagem.objects.all()

class detalhesImagem(DetailView):
    model = Imagem
    template_name ='detalhesImagem.html'

def cadastroImagem(request):
    form = ImagemForm()
    if request.method == 'POST':
        form = ImagemForm(request.POST, request.FILES)
        if form.is_valid():
            Imagem = form.save()

            img_arr = img.imread(Imagem.img)

            (h,w,c) = img_arr.shape

            img2D = img_arr.reshape(h*w,c)

            kmeans_model = KMeans(n_clusters=int(Imagem.clusters), init="random")
            clusters_labels = kmeans_model.fit_predict(img2D)

            centroid = kmeans_model.cluster_centers_

            rgb_colors = centroid.round(0).astype(int)

            labels = (kmeans_model.labels_)

            img_quant = np.reshape(rgb_colors[clusters_labels],(h,w,c))

            labels=list(kmeans_model.labels_)

            percent=[]
            for i in range(len(centroid)):
                j=labels.count(i)
                j=j/(len(labels))
                percent.append(j)
                print(percent)

            fig, ax = plt.subplots(1,3, figsize=(20,12))
            ax[0].imshow(img_arr)
            ax[0].set_title('Imagem Original')
            ax[1].imshow(img_quant)
            ax[1].set_title('Imagem Quantizada')
            ax[2].pie(percent,colors=np.array(centroid/255),labels=np.arange(len(centroid)))
            ax[2].set_title('Paleta de cores')
            imagem_name = str(Imagem.img).split('/')[1]

            fig.savefig(str(settings.MEDIA_ROOT) + '/uploads/' + imagem_name)
            return redirect('ImagemDetalhes', Imagem.pk)
        else:
            print(request.FILES)

    return render(request,'cadastro.html',{'form': form})

def excluirImagem(request, pk, template_name='confirm_delete.html'):
    imagem = get_object_or_404(Imagem, pk=pk)
    if request.method=='POST':
        imagem.delete()
        return redirect('Imagem')
    return render(request, template_name, {'object':imagem})


from pyspark import SparkContext, SparkConf
import sys

#input & output
inputs = sys.argv[1]
output = sys.argv[2]

conf = SparkConf().setAppName('Wikipedia_popular')
sc = SparkContext(conf=conf)
assert sys.version_info >= (3, 5)  # make sure we have Python 3.5+
assert sc.version >= '2.3'

def func(line):
	string_arr = line.split()

	datetime = string_arr[0]
	lan = string_arr[1]
	name = string_arr[2]
	cnt = int(string_arr[3])

	return (datetime,lan,name,cnt)

def maximum(x,y):
	if x[0]>=y[0]:
		return x
	else:
		return y

def get_key(kv):
	return kv[0]

def tab_separated(kv):
	k,v = kv
	return "%s\t%s" % (k,v)

text = sc.textFile(inputs)

words = text.map(func)

words2 = words.filter(lambda x: x[1]=="en" and x[2] != "Main_Page" and not (x[2].startswith("Special:")))

pairs = words2.map(lambda x: (x[0],(x[3], x[2])))

redu = pairs.reduceByKey(maximum)

outdata = redu.sortBy(get_key)
outdata.map(tab_separated).saveAsTextFile(output)

print(outdata.take(10))


import pygame
from random import random, choice, randint, uniform

class particle():
    """HERES YOUR PARTICLES DR MARC"""

    def __init__(self, game, image, wiggle_room, min_speed, max_speed, x_position, y_position, min_duration, max_duration):
        self.game = game
        self.wiggle_room = wiggle_room
        self.min_speed = min_speed
        self.max_speed = max_speed
        self.min_duration = float(min_duration)
        self.max_duration = float(max_duration)
        self.duration = 0.0
        self.total_duration = uniform(self.min_duration, self.max_duration)
        self.x_position = float(x_position) + float(randint(0, self.wiggle_room))
        self.y_position = float(y_position) + float(randint(0, self.wiggle_room))
        self.rect = pygame.Rect(self.x_position, self.y_position, 5, 5)
        self.image = image.copy() #pygame.image.load("img/")
        self.x_multiplier = random()
        self.y_multiplier = 1 - self.x_multiplier
        self.x_direction = choice([-1, 1])
        self.y_direction = choice([-1, 1])
        self.speed = randint(self.min_speed, self.max_speed)
        self.x_vel = self.speed * self.x_multiplier * self.x_direction
        self.y_vel = self.speed * self.y_multiplier * self.y_direction
        self.dead = False

    def update(self, time_since_last_frame):
        if (not self.dead):
            self.x_position += float(self.x_vel) * float(time_since_last_frame)/float(1000)
            self.y_position += float(self.y_vel) * float(time_since_last_frame)/float(1000)
            self.rect.x = self.x_position
            self.rect.y = self.y_position
            self.duration += time_since_last_frame
            if (self.duration >= self.total_duration):
                self.dead = True
            self.image.set_alpha(255 - 255*(self.duration / self.total_duration))

    def draw(self, surface):
        if (not self.dead):
            surface.blit(self.image, self.rect)


mas = ['привет', 'пока', 'досвидания']
i = 0
while True:
    while i < len(mas):
        print(i, "-", mas[i])
        i += 1
    x = input("Введите индекс элемента который хотите посмотреть ")
    x = int(x)
    if x < 0 or x >= len(mas):
      print("нах пошел")
    else:
      print(mas[x])
      break

Программирование на Python с Нуля до Гуру/Lesson_9.py


import numpy as np
import copy
from scipy.optimize import minimize
import time
from .nelder_mead import nelder_mead
from numpy import linalg as LA

d = 3
class PLS1Regression():
    def __init__(self, _X, _Y, _components, mode='classic'):
        # Кол-во компонени должно быть меньше количества признаков, иначе нельзя раскладывать
        if _X.shape[1] < _components:
            print("error")
        else:
            self.X = _X  # matrix of predictors
            self.Y = _Y  # vector of real data
            self.B = 0
            self.B0 = 0
            self.components = _components  # number of latent variable
            self.n = _X.shape[0]  # number of testing people
            self.p = _X.shape[1]  # number of properties
            try:
                if(mode == 'robust'):
                    self.PLS1Robust()
                else:
                    self.PLS1()
            except Exception:
                raise Exception

    def Hiuber(self, x):
        if abs(x) < d:
            return 1.0 / 2.0 * x ** 2
        else:
            return d * (abs(x) - 1.0 / 2.0 * d)

    def MinimazeFunc(self, b):
        f = np.zeros(self.n)
        multiply = np.dot(self.X, b)
        dif = self.Y - multiply
        for i in range(len(self.Y)):
            f[i] = self.Hiuber(dif[i])
        f = np.sum(f)
        return f

    # def ExploratorySearch(self, startB, delta):
    #     trialStep = copy.copy(startB)
    #     isStepMade = 0
    #     for i in range(len(startB)):
    #         trialStep[i] = startB[i] + delta[i]
    #         if self.MinimazeFunc(trialStep) > self.MinimazeFunc(startB):
    #             trialStep[i] = startB[i] - delta[i]
    #             if self.MinimazeFunc(trialStep) > self.MinimazeFunc(startB):
    #                 isStepMade = 0
    #                 # keep
    #             else:
    #                 startB[i] = trialStep[i]
    #                 isStepMade = 1
    #         else:
    #             startB[i] = trialStep[i]
    #             isStepMade = 1
    #
    #     return isStepMade, startB
    #
    # def HookaJivsa(self, startB, delta, step, error, maxiter):
    #     if step < 2:
    #         print("the step should be larger")
    #     else:
    #         x = copy.copy(startB)
    #         for i in range(maxiter):
    #             funcValue = self.MinimazeFunc(x)
    #             isStepMade, x = self.ExploratorySearch(startB, delta)
    #             while (not isStepMade):
    #                 delta /= step
    #                 isStepMade, x = self.ExploratorySearch(startB, delta)
    #                 if abs(self.MinimazeFunc(x) - funcValue) < error:
    #                     return x
    #         return x

    def centerscale(self, scale=True):
        self.X -= np.amin(self.X, axis=(0, 1))
        self.X /= np.amax(self.X, axis=(0, 1))

    def PLS1(self):
        # self.centerscale()
        # init X0 & y0
        Xk = self.X
        y = self.Y

        # n - size of testee
        # p - size of properties
        # W - help matrix of weights
        # P, q - Load matrix

        W = np.zeros((self.p, self.components))
        P = np.zeros((self.p, self.components))
        b = np.zeros(self.components)
        t = np.zeros((self.n, self.components))  # x_scores
        znam = np.linalg.norm(np.dot(Xk.T, y))
        W[:, 0] = np.divide(np.dot(Xk.T, y), znam)

        for k in range(0, self.components):
            t[:, k] = np.dot(Xk, W[:, k])  # x_scores
            tk_scalar = np.dot(t[:, k].T, t[:, k])
            # print(tk_scalar)
            t[:, k] = np.divide(t[:, k], tk_scalar)

            P[:, k] = np.dot(Xk.T, t[:, k])
            b[k] = np.dot(y.T, t[:, k])

            if b[k] == 0:
                break

            if k < self.components - 1:
                help1 = tk_scalar * t[:, k]
                help2 = np.outer(help1, P[:, k].T)

                Xk = Xk - help2
                W[:, k + 1] = np.dot(Xk.T, y)

        helpPW = np.dot(P.T, W)
        f = open('result_graph_P.txt', 'w')
        for i in range(len(P)):
            for j in range(len(P[0])):
                f.write(str(P[i][j]) + '\t')
            f.write('\n')
        # print(b)
        self.B = np.dot((W.dot(np.linalg.inv(helpPW))), b)
        self.B0 = b[0] - np.dot(P[:, 0].T, self.B)

    def PLS1Robust(self):
        # init X0 & y0
        Xk = self.X
        y = self.Y

        # n - size of testee
        # p - size of properties
        # W - help matrix of weights
        # P, q - Load matrix

        W = np.zeros((self.p, self.components))
        P = np.zeros((self.p, self.components))
        b = np.zeros(self.components)
        t = np.zeros((self.n, self.components))  # x_scores
        znam = np.linalg.norm(np.dot(Xk.T, y))
        W[:, 0] = np.divide(np.dot(Xk.T, y), znam)

        for k in range(0, self.components):
            t[:, k] = np.dot(Xk, W[:, k])  # x_scores
            # t[:, k] = ne.evaluate("Xk*W[:, k]")  # x_scores
            tk_scalar = np.dot(t[:, k].T, t[:, k])
            t[:, k] = np.divide(t[:, k], tk_scalar)

            P[:, k] = np.dot(Xk.T, t[:, k])
            b[k] = np.dot(y.T, t[:, k])

            if b[k] == 0:
                break

            if k < self.components - 1:
                help1 = tk_scalar * t[:, k]
                help2 = np.outer(help1, P[:, k].T)

                Xk = Xk - help2
                W[:, k + 1] = np.dot(Xk.T, y)

        helpPW = np.dot(P.T, W)
        Bpls = np.dot((W.dot(np.linalg.inv(helpPW))), b)
        resMinimization = minimize (self.MinimazeFunc, Bpls, method="nelder-mead")
        rez = nelder_mead(self.MinimazeFunc, Bpls)
        self.B = resMinimization.x
        self.B = rez[0]
        self.B0 = b[0] - np.dot(P[:, 0].T, self.B)

    def Predict(self, X):
        return np.dot(X, self.B) + self.B0


#!/usr/bin/env python3
import re
import csv

def normalize_space(s):
    return ' '.join(s.split())

def isfloat(s):
  try :
     float(s)
     return True
  except :
     return False

absolute_path = open(r'output_files/ALU.map.rpt', 'r')
contents = absolute_path.read()

res = re.search('\;\s+Analysis & Synthesis Resource Usage(.*\s[0-9.]*)\;\s+Average fan-out\s+\;\s+[0-9.]*\s+\;$',
                    contents,flags = re.S | re.M).group(0)
my_list = res.split(";")

my_list[0:6] = ''

remove_space = [x.strip(' ') for x in my_list]
remove_space = [x.strip('\n') for x in my_list]
newlist = [ele for ele in my_list if ele.strip()]
special_char = "--"
out_list = [''.join(x for x in string if not x in special_char) for string in newlist]

replaced_list = [normalize_space(i) for i in out_list]
#print(replaced_list)

#partitioning list to number and text
row = []
column = []
for i in replaced_list:
    if(i.isdigit() or isfloat(i)):
       column.append(i)
    elif (i == "op[0]"):
        column.append(i)
    else:
       row.append(i)
del row[4]
del row[9]

#print(row)
#print(column)

filename = "new.csv"
with open(filename, 'w') as csvfile:
    csvwriter = csv.writer(csvfile)
    csvwriter.writerow(row)
    csvwriter.writerow(column)
absolute_path.close()


import numpy as np
import matplotlib.pyplot as plt
import seaborn as sns
import hist, mplhep
from hist import  Hist

cont = np.zeros((360, 180))

h  = Hist(
    hist.axis.Regular(90, 0, 180, name="theta"),
    hist.axis.Regular(180, 0, 360, name="phi"),
)

h1 = Hist(
    hist.axis.Regular(90, 0, 180, name="angle"),
)

i = 0
for theta in range(0, 180, 2):
    j = 0
    for phi in range(0, 360, 2):
        h[i, j] = (1-np.sin(theta/180*np.pi)**2*np.cos(phi/180.*np.pi)**2)
        j = j + 1
    i = i+1

j = 0
for i in range(0, 180, 2):
    h1[j] = np.cos(i/180*np.pi)**2
    j += 1

fig, ax = plt.subplots()
mplhep.histplot(h1, ax=ax, yerr=0)

#h.plot2d_full(
#    main_cmap="coolwarm",
#    top_ls="--",
#    top_color="orange",
#    top_lw=1,
#    side_ls=":",
#    side_lw=1,
#    side_color="steelblue",
#)

plt.savefig("genpdf1.pdf")
plt.show()

"""

ax.set_xlabel(r"$\theta$ [deg]", fontsize=15)
ax.set_ylabel(r"$\phi$ [deg]", fontsize=15)

plt.tight_layout()

plt.savefig("genpdf.pdf")

plt.show()

"""


DEPS = [
    'build/file',
    'depot_tools/bot_update',
    'depot_tools/gclient',
    'recipe_engine/path',
    'recipe_engine/raw_io',
    'recipe_engine/step',
    'recipe_engine/time',
]

# Service account for push access to container registry, deployed by Puppet.
_CONTAINER_REGISTRY_CREDENTIAL_PATH = (
    '/creds/service_accounts/service-account-container_registry_pusher.json')
_CONTAINER_REGISTRY_PROJECT = 'chromium-container-registry'

def RunSteps(api):
  api.gclient.set_config('infra')
  api.bot_update.ensure_checkout()
  api.gclient.runhooks()

  # Ensure docker is installed.
  # FIXME: Move docker logic here into its own module if it's ever worth it.
  try:
    find_docker_step = api.step(
        'Find docker bin', ['which', 'docker'], stdout=api.raw_io.output(),
        step_test_data=lambda: api.raw_io.test_api.stream_output(
            '/usr/bin/docker'))
  except api.step.StepFailure as f:
    f.result.presentation.step_text = (
        'Error: is docker installed on this machine?')
    raise
  docker_bin = find_docker_step.stdout.strip()

  # Get version.
  docker_version_step = api.step(
      'Get docker version', [docker_bin, 'version'], stdout=api.raw_io.output(),
      step_test_data=lambda: api.raw_io.test_api.stream_output(
          'Version: 1.2.3'))
  for line in docker_version_step.stdout.splitlines():
    if 'Version' in line:
      docker_version_step.presentation.step_text = line.strip()
      break
  else:
    docker_version_step.presentation.step_text = 'Version unknown?'

  # To prevent images from accumulating over time on the slave, dumbly delete
  # all local images. This has the unfortunate side effect of making every build
  # a full clobber, but this recipe shouldn't be run too frequently and doesn't
  # require fast cycle times.
  # TODO(bpastene): Be smarter about this.
  with api.step.nest('Clear all local images'):
    get_images_step = api.step(
        'Get images', [docker_bin, 'images', '-q'], stdout=api.raw_io.output(),
        step_test_data=lambda: api.raw_io.test_api.stream_output('img1'))
    for image_id in get_images_step.stdout.splitlines():
      try:
        api.step(
            'Delete image %s' % image_id,
            [docker_bin, 'rmi', '-f', image_id.strip()])
      except api.step.StepFailure as f:
        f.result.presentation.status = api.step.WARNING

  # Run the build script. It names the resulting image 'android_docker' and
  # tags it with 'latest'.
  build_script = api.path['checkout'].join(
      'docker', 'android_devices', 'build.sh')
  api.step('Build image', ['/bin/bash', build_script])

  # Read service account creds.
  service_account_creds = api.file.read(
      'Read service account creds', _CONTAINER_REGISTRY_CREDENTIAL_PATH)

  # Login to the container registry. Pass the contents of the credentials file
  # as the password. Probably want to run this only internally.
  # See https://cloud.google.com/container-registry/docs/advanced-authentication
  login_cmd = [
      docker_bin, 'login',
      '-u', '_json_key',
      '-p', '%s' % service_account_creds,
      'https://gcr.io'
  ]
  api.step('Login to registry', login_cmd)

  # Tag the image with the registry's url and the date.
  registry_url = 'gcr.io/%s' % _CONTAINER_REGISTRY_PROJECT
  date_tag = api.time.utcnow().strftime('%Y-%m-%d-%H-%M')
  registry_image_name = '%s/android_docker:%s' % (registry_url, date_tag)
  tag_cmd = [
      docker_bin,
      'tag',
      'android_docker:latest',
      registry_image_name
  ]
  api.step('Tag image', tag_cmd)

  # Push the image to the registry.
  upload_step = api.step(
      'Push image', [docker_bin, 'push', registry_image_name],
      stdout=api.raw_io.output(),
      step_test_data=lambda: api.raw_io.test_api.stream_output(
          '1-2-3: digest: sha256:deadbeef size:123'))
  image_spec = 'unknown'
  for line in upload_step.stdout.splitlines():
    if 'digest' in line:
      image_spec = 'image = android_docker:' + line.strip()
      break
  upload_step.presentation.step_text = image_spec

def GenTests(api):
  yield (api.test('full_build'))
  yield (
      api.test('no_docker') +
      api.step_data('Find docker bin', retcode=1)
  )
  yield (
      api.test('unknown_version') +
      api.step_data('Get docker version', stdout=api.raw_io.output(''))
  )
  yield (
      api.test('failed_image_deletion') +
      api.step_data('Clear all local images.Delete image img1', retcode=1)
  )


def nolamda(x, y):
    return 3 * x + 2 * y

x, y = 3, 5

result = nolamda(x, y)
print('일반 함수 방식 : %d' % (result))

yeslamda = lambda x, y : 3 * x + 2 * y
result = yeslamda(x, y)
print("람다 방식 1 : %d" % (result))

result = yeslamda(5, 7)
print("람다 방식 2 : %d" % (result))


import numpy as np
import pandas as pd
import cmath

from Newton_raphson.NR_functions import Q_violated, printing_jacobian, P_Calc, Q_Calc, get_PQ_calc, delta_VD, updateVD, updateVD_vec, P_Updated, Q_Updated, Q_max_violation, VD_vec_Qmax, Q_calc_violated
from Newton_raphson.NR_network import Network

# Spezialized function for creating Ybus for the DCLF method

def Ybus_dclf(file, shape):
    df_impedances = pd.read_csv(file, sep=";")
    Z_values = np.zeros((shape,shape), dtype=complex)
    Y_bus = np.zeros((shape,shape), dtype=complex)

    for x in range(df_impedances.shape[0]):
        from_line = df_impedances['From_line'][x]
        to_line = df_impedances['To_line'][x]

        # Adding the diagonal elements to the Y-bus
        Y_bus[from_line-1][from_line-1] += 1/(df_impedances['X'][x]*1j)
        Y_bus[to_line-1][to_line-1] += 1/(df_impedances['X'][x]*1j)

        # Z values for off diagonal elements
        Z_values[from_line-1][to_line-1] = df_impedances['X'][x]*1j
        Z_values[to_line-1][from_line-1] = df_impedances['X'][x]*1j
    # Adding off diagonal elements
    for i in range(shape):
        for j in range(shape):
            if(Z_values[i][j] != 0. +0.j):
                if(i != j):
                    Y_bus[i][j] = - 1/Z_values[i][j]
            else:
                if(i != j):
                    Y_bus[i][j] = Z_values[i][j]
    return Y_bus

# Function to print the Y bus
def printing_Y_bus(Ybus):
    df = pd.DataFrame(Ybus)
    df.index = np.arange(1, len(df)+1)
    df.columns = np.arange(1, len(df)+1)
    print('Ybus: \n', df, '\n')
    return

# Function creating the jacobian matrix for the DCLF method. Returning the nxn matrix
def make_jacobian_dclf(VD_jacobian, PQ_jacobian, PQ_vec, num_buses, V, delta, Ybus):
    j = np.zeros((len(PQ_vec),len(PQ_vec)), dtype=complex)

    for x in range(len(PQ_vec)):
        for y in range(len(PQ_vec)):
            if (PQ_jacobian[x].Bus_num == VD_jacobian[y].Bus_num):
                if (PQ_jacobian[x].Bus_type == 'P' and VD_jacobian[y].Bus_type == 'D'):
                    for i in range(num_buses):
                        if (i==PQ_jacobian[x].Bus_num):
                            j[x,y] += 0
                        else:
                            j[x,y] += V[PQ_jacobian[x].Bus_num]*V[i]*(-np.real(Ybus[PQ_jacobian[x].Bus_num,i])*cmath.sin(delta[PQ_jacobian[x].Bus_num]-delta[i])+np.imag(Ybus[PQ_jacobian[x].Bus_num,i])*cmath.cos(delta[PQ_jacobian[x].Bus_num]-delta[i]))

                if (PQ_jacobian[x].Bus_type == 'P' and VD_jacobian[y].Bus_type == 'V'):
                    for i in range(num_buses):
                        if (i==PQ_jacobian[x].Bus_num):
                            j[x,y] += 0
                        else:
                            j[x,y] += 0

                if (PQ_jacobian[x].Bus_type == 'Q' and VD_jacobian[y].Bus_type == 'D'):
                    for i in range(num_buses):
                        if (i==PQ_jacobian[x].Bus_num):
                            j[x,y] += 0
                        else:
                            j[x,y] += 0

                if (PQ_jacobian[x].Bus_type == 'Q' and VD_jacobian[y].Bus_type == 'V'):
                    for i in range(num_buses):
                        if (i==PQ_jacobian[x].Bus_num):
                            j[x,y] += -2*V[i]*np.imag(Ybus[PQ_jacobian[x].Bus_num,i])*cmath.cos(delta[PQ_jacobian[x].Bus_num]-delta[i])
                        else:
                            j[x,y] += V[i]*(np.real(Ybus[PQ_jacobian[x].Bus_num,i])*cmath.sin(delta[PQ_jacobian[x].Bus_num]-delta[i])-np.imag(Ybus[PQ_jacobian[x].Bus_num,i])*cmath.cos(delta[PQ_jacobian[x].Bus_num]-delta[i]))
            else:
                if (PQ_jacobian[x].Bus_type == 'P' and VD_jacobian[y].Bus_type == 'D'):
                    j[x,y] += V[PQ_jacobian[x].Bus_num]*V[VD_jacobian[y].Bus_num]*(np.real(Ybus[PQ_jacobian[x].Bus_num,VD_jacobian[y].Bus_num])*cmath.sin(delta[PQ_jacobian[x].Bus_num]-delta[VD_jacobian[y].Bus_num])-np.imag(Ybus[PQ_jacobian[x].Bus_num,VD_jacobian[y].Bus_num])*cmath.cos(delta[PQ_jacobian[x].Bus_num]-delta[VD_jacobian[y].Bus_num]))

                if (PQ_jacobian[x].Bus_type == 'P' and VD_jacobian[y].Bus_type == 'V'):
                    j[x,y] += 0

                if (PQ_jacobian[x].Bus_type == 'Q' and VD_jacobian[y].Bus_type == 'V'):
                    j[x,y] += V[PQ_jacobian[x].Bus_num]*np.real(Ybus[PQ_jacobian[x].Bus_num,VD_jacobian[y].Bus_num]*cmath.sin(delta[PQ_jacobian[x].Bus_num]-delta[VD_jacobian[y].Bus_num])-np.imag(Ybus[PQ_jacobian[x].Bus_num,VD_jacobian[y].Bus_num]*cmath.cos(delta[PQ_jacobian[x].Bus_num]-delta[VD_jacobian[y].Bus_num])))

                if (PQ_jacobian[x].Bus_type == 'Q' and VD_jacobian[y].Bus_type == 'D'):
                    j[x,y] += 0
    printing_jacobian(j)
    return j

# Function with all the steps for one iteration of DCLF. To be used in a loop in the main function
def iterate_dclf(VD_jacobian, PQ_jacobian, PQ_vec, num_buses, V, delta, V_vec, delta_vec, Ybus, bus_num_init, P_init, Q_init, VD_vec_current, power_network, bus_type_init, Q_max, Q_limit, reactive_limits_method):

    #1 Calculates new values for P and Q separately
    P_calc = P_Calc(V_vec, Ybus, bus_num_init, delta_vec, P_init)
    Q_calc = Q_Calc(V_vec, Ybus, bus_num_init, delta_vec, Q_init)

    #2 Updates RHS of inverted-jacobi-equation
    PQ_calc_updated = get_PQ_calc(P_calc, Q_calc)

    #3 Creating new jacobian
    j = make_jacobian_dclf(VD_jacobian, PQ_jacobian, PQ_calc_updated, num_buses, V_vec, delta_vec, Ybus)

    #4 Inverting jacobian
    j_inv = np.linalg.inv(j)

    #5 Updates LHS of  inverted-jacobi-equation
    delta_vd = delta_VD(PQ_vec, PQ_calc_updated, j_inv)

    #6 Updates values for V and delta
    VD_vec_current = updateVD_vec(VD_vec_current, delta_vd, bus_type_init, bus_type_init, delta, V)

    #7 Updates V_values and delta_values in separate vectores tougether with given values.
    delta_updated, V_updated = updateVD(VD_vec_current,delta, V , bus_type_init, bus_type_init)

    #8 New P and Q vectors without nan.
    P_updated = P_Updated(V_updated, Ybus, bus_num_init, delta_updated)     #Returns P vector updated with calculated values for unknown P's instead of nan.
    Q_updated = Q_Updated(V_updated, Ybus, bus_num_init, delta_updated)     #Returns Q vector updated with calculated values for unknown Q's instead of nan.

    #8 Checking Q_max
    bus_type = bus_type_init
    if (Q_limit):
        if(Q_violated(Q_max, Q_updated, bus_type) and reactive_limits_method == 'before'):
            Q_updated, power_network = Q_max_violation(Q_updated, Q_max, bus_num_init, V, power_network)
            bus_type = power_network.get_bus_type_vec()
            VD_vec, VD_jacobian = power_network.get_VD_jacobian()
            PQ_vec, PQ_jacobian = power_network.get_PQ_vec()

            VD_vec_current = VD_vec_Qmax(VD_vec, VD_vec_current, bus_type, bus_type_init, V)

            V  = power_network.get_V_vec()
            VD_vec_current = updateVD_vec(VD_vec_current, delta_vd, bus_type_init, bus_type, delta, V)

            delta_updated, V_updated = updateVD(VD_vec_current,delta, V, bus_type_init, bus_type)
            Q_calc = Q_calc_violated(bus_type_init,bus_type, Q_updated, Q_calc)

    return delta_updated, V_updated, VD_vec_current, P_calc, Q_calc, P_updated, Q_updated, bus_type, power_network, VD_jacobian, PQ_jacobian, PQ_vec, bus_type, delta_vd, V

#PQ_to_PV
def PQ_to_PV(bus_type_init, bus_type, power_network, V_updated):
    Buses = power_network.buses
    for i in range (len(bus_type_init)):
        if (bus_type_init[i] != bus_type[i]):
            Buses[i].bus_type = 1
            Buses[i].Q = np.nan
            Buses[i].V = V_updated[i]
        else:
            continue
        power_network = Network(Buses)
    return power_network


#from InputNsdpDatabase import InputNsdpDatabase
from sdg.inputs.InputYamlMeta import InputYamlMeta
from sdg.inputs.InputCsvData import InputCsvData
from sdg.inputs.InputCsvMeta import InputCsvMeta

def get_inputs():
    data_input = InputCsvData(path_pattern='data/*.csv')
    meta_input = InputCsvMeta(path_pattern='meta/*.csv', git=False)
    indicator_config_input = InputYamlMeta(path_pattern='indicators/*.yml', git=False)
    progress_input = InputYamlMeta(path_pattern='progress/*.yml', git=False)
    return [
        data_input,
        meta_input,
        indicator_config_input,
        progress_input,
    ]

def alter_meta(meta):
    if 'NSDPIndicatorID' in meta:
        # Auto-calculate settings based on 'NSDPIndicatorID'.
        open_sdg_id = meta['NSDPIndicatorID'].replace(' ', '').replace('.', '-')
        meta['indicator_number'] = open_sdg_id
        meta['goal_number'] = open_sdg_id.split('-')[0]
        meta['goal_name'] = 'global_goals.' + meta['goal_number'] + '-title'
        meta['target_number'] = open_sdg_id.split('-')[0] + '-' + open_sdg_id.split('-')[1]
        meta['target_name'] = 'global_targets.' + meta['target_number'] + '-title'
        meta['indicator_name'] = 'indicators.' + open_sdg_id + '-title'
        meta['national_geographical_coverage'] = 'Vanuatu'
    return meta


"""
The script reprojects 10" resolution C-band image (provided by Dr. Delaney)
into a common WCS grid. Chandra observation WCS grid is used as a template
"""

import time
import matplotlib.pyplot as plt
import numpy as np
from astropy.io import fits
from astropy.wcs import WCS
from matplotlib.colors import LogNorm
from reproject import reproject_exact

outpath = '../data/intermediate_products/'
Chandra_infile = '1_chandra_bin3_4.2-6.2_flux_jy.fits'

hdu1 = fits.open((outpath + Chandra_infile))
hdu2_C10 = fits.open('../data/original/C10.FITS')
# BEAM TO JY
# 17 comes from ratio between beam surface area (7.28'') and 2.5" image pixel surface area (0.43")
# 16 comes as correction from smoothing from 2.5" to 10". Difference of 4x turns into 16 in surface area
beam_to_jy = 17*16 # only works for this specific case

array_C10, footprint_C10 = reproject_exact((hdu2_C10[0].data[0, 0, :, :]/(beam_to_jy), WCS(hdu2_C10[0].header).sub(2)),
                                               hdu1[0].header)

xray_pixel_size_in_arcsec = hdu1[0].header['CDELT2'] * 3600
radio_pixel_size_in_arcsec = hdu2_C10[0].header['CDELT2'] * 3600
ratio = xray_pixel_size_in_arcsec ** 2 / radio_pixel_size_in_arcsec ** 2

print('Ratio:', ratio)
# correcting for the pixel change
print('Pixel size in arcsec', hdu1[0].header['CDELT2'] * 3600)
array_C10 *= ratio

new_secondary_hdu = fits.PrimaryHDU()
# copy spitzer header into new secondary
new_secondary_hdu.header = hdu2_C10[0].header
# copy chandra header projection part into new primary
hdu2_C10[0].header = hdu1[0].header[-80:-54]
hdu2_C10[0].data = array_C10

# create new HDU list
new_hdul = fits.HDUList()
new_hdul.append(hdu2_C10[0])
new_hdul.append(new_secondary_hdu)
new_hdul[0].verify('fix')
# new_hdul[1].verify('fix')

header = new_hdul[0].header
header['history'] = '--------------------------------------------------'
header['history'] = 'Used script:    3_reproject_C10.py'
header['history'] = 'Author:         Vladimir Domcek'
header['history'] = 'Date and time:  ' + time.strftime('%X %x %Z')
header['history'] = 'Main changes: '
header['history'] = 'Delaney radio data reprojected to chandra coord system and binsize (1.5'')'
header['history'] = 'Previous radio header moved into 2nd extension of header'
header['history'] = 'Reprojected file: radio/C10.FITS'
header['history'] = 'Reproject according to file: ' + Chandra_infile
header['history'] = 'Units Jy/px'

new_hdul.writeto(outpath + '3_C10_bin3_xheader.fits', overwrite=True)


import urllib.request
from datetime import datetime
from dateutil import tz
import json
import os

from . import cache

BASE_DIR = os.path.dirname(os.path.realpath(__file__))
with open(os.path.join(BASE_DIR, 'wx_source.json')) as json_data_file:
    wx_data = json.load(json_data_file)

def wx_read_json(f_string):
    f = urllib.request.urlopen(f_string)

    json_string = f.read()
    json_parsed = json.loads(json_string.decode('utf-8'))
    f.close()

    return json_parsed

def wx_connect(station_name, flag, source):
    global wx_data
    # connect to the <source> forecast api
    # Time format required = 2019-02-08T21:00:00+11:00
    # Done: http://api.wunderground.com/api/a0288eb1235df557/hourly/q/-37.914,145.369.json

    list_split = station_name.split(',')
    latlon = False
    if len(list_split) == 2:
        try:
            float(list_split[0])
            latlon = True
        except ValueError:
            pass

    # need to modify slightly if the lat lon notation is being used
    # Can combine call to reduce access requirements
    # http://api.wunderground.com/api/<key>/forecast10day/hourly/q/AU/Selby.json

    # request = urllib.request.urlopen(
    #     'https://api.aerisapi.com/forecasts/ferny creek,australia?&format=json&filter=day&limit=10&
    #     fields=periods.dateTimeISO,loc,periods.maxTempC,periods.minHumidity,periods.windSpeedMaxKPH,
    #     periods.windSpeedMinKPH&client_id=xxx&client_secret=XXXXXXX')
    # response = request.read()
    # json = json.loads(response)
    # if json['success']:
    #     print
    #     json
    # else:
    #     print("An error occurred: %s" % (json['error']['description']))
    #     request.close()

    # if latlon:
    final = []
    cache_hour = 30
    cache_daily = 90
    cache_time = None
    if source == "aeris":
        if not latlon:
            station_name = "%s, australia" % station_name
        tail = ""
        if flag == "hourly":
            cache_time = cache_hour
            tail = "&filter=1hr&limit=36"
        if flag == 'forecast10day':
            cache_time = cache_daily
            tail = "&filter=day&limit=10"
        url = "{}{}?format=json&client_id={}&client_secret={}&fields={}{}".format(wx_data[source]['base_url'],
                                                                                station_name,
                                                                                wx_data[source]['key'],
                                                                                wx_data[source]['secret'],
                                                                                wx_data[source]['fields'],
                                                                                tail)
        # url = "https://api.aerisapi.com/forecasts/%s?format=json&client_id=%s&client_secret=%s&fields=%s%s" \
        #                            % (station_name, "xxx", "XXXXXXXXX",
        #                               "periods.dateTimeISO,loc,periods.maxTempC,periods.minHumidity,
        #                               periods.windSpeedMaxKPH,periods.windSpeedMinKPH", tail)
        # print(url)
        result = wx_read_json(url)

        # print(result)

        if result['success']:
            for value in result['response'][0]['periods']:
                tmp = dict()
                tmp['maxTemp'] = value['maxTempC']
                tmp['minRH'] = value['minHumidity']
                # tmp['avgWindSpd'] = (value['windSpeedMaxKPH'] + value['windSpeedMinKPH']) / 2.0
                tmp['avgWindSpd'] = value['windSpeedMaxKPH']
                tmp['datetime'] = value['dateTimeISO']
                final.append(tmp)
        else:
            final.append({"error": result['error']['description']})
    elif source == "darksky":
        # https://api.darksky.net/forecast/xxxxxxx/-37.9176,145.3782?units=ca&exclude=[currently,minutely,alerts,flags]
        #
        tail = ""
        if flag == "hourly":
            cache_time = cache_hour
        if flag == 'forecast10day':
            cache_time = cache_daily

        url = "{}{}/{}?{}{}".format(wx_data[source]['base_url'], wx_data[source]['key'], station_name,
                                 wx_data[source]['fields'], tail)
        # print(url)
        result = wx_read_json(url)

        if result:
            if flag == "hourly":
                for value in result['hourly']['data']:
                    tmp = dict()
                    tmp['maxTemp'] = value['temperature']
                    tmp['avgWindSpd'] = value['windSpeed']
                    tmp['minRH'] = int(value['humidity'] * 100)
                    tmp['datetime'] = datetime.utcfromtimestamp(value['time']).replace(tzinfo=tz.tzutc())\
                        .astimezone(tz.tzlocal()).isoformat()
                    final.append(tmp)
            if flag == "forecast10day":
                for value in result['daily']['data']:
                    tmp = dict()
                    tmp['maxTemp'] = value['temperatureMax']
                    tmp['avgWindSpd'] = value['windSpeed']
                    tmp['minRH'] = int(value['humidity'] * 100)
                    tmp['datetime'] = datetime.utcfromtimestamp(value['time']).replace(tzinfo=tz.tzutc())\
                        .astimezone(tz.tzlocal()).isoformat()
                    final.append(tmp)
        else:
            final.append({"error": result})

    elif source == "wunder":
        url = 'http://api.wunderground.com/api/a0288eb1235df557/{}/{}/q/{}.json'.format('forecast10day',
                                                                                'hourly', station_name)

        result = wx_read_json(url)
        print(result)

    else:
        tmp = {'error': 'Unable to find file!'}
        final.append(tmp)

    wx_cache_write('%s_%s_%s' % (source, flag, station_name), final, cache_time)

    return final
    #  ********************************************************************************************************* #

    # seperate the single file into seperate cache files
    # if flag == "forecast10day":
    #     wx_cache_write('%s_%s_%s' % (source, 'forecast10day', station_name),
    #     json_parsed['forecast']['simpleforecast']['forecastday'], 90)
    #
    # elif flag == "hourly":
    #     wx_cache_write('%s_%s_%s' % (source, 'hourly', station_name), json_parsed['hourly_forecast'], 30)
    #
    # else:
    #     return {'ERROR': 'Unable to find file!'}

    # print(json_parsed['forecast']['simpleforecast'])
    # print(json_parsed['hourly_forecast'])

    # if flag == 'hourly':
    #     return json_parsed['hourly_forecast']
    # elif flag == 'forecast10day':
    #     return json_parsed['forecast']['simpleforecast']['forecastday']
    # else:
    #     return {'ERROR': 'Unable to find file!'}

def wx_check_cache(station_name, flag, source):
    # check to see if the required file has been downloaded in the last 5 minutes
    # if not connect to the ftp server and download, otherwise use cached version

    print("checking for file: %s_%s_%s in cache" % (source, flag, station_name))
    res = cache.get('%s_%s_%s' % (source, flag, station_name))
    if not res:
        print("File %s_%s_%s not in cache" % (source, flag, station_name))
        res = wx_connect(station_name, flag, source)
    # print(res)
    return res

def wx_cache_write(station_name, io, cache_time_minutes):
    # Write the variable to the cache based on the filename
    # this will probably work due to the low file count, otherwise might need some other method.
    # probably set cache time to 30 minutes as forecast data is only 1 hour intervals.
    # cache time for 10 day forecast is 3 hours

    cache.set(station_name, io, timeout=int(cache_time_minutes)*60)

def fetch_forecast(station_name, flag, source):
    # fetch the hourly or 10 day forecast... for given location
    return wx_check_cache(station_name, flag, source)


import numpy as np

# class Hedge:

#     def __init__ (self, beta = 0.5, time_steps: int = 150):
#         self. __time_steps = time_steps
#         self.__beta = beta

#     def fit (self, X: np.ndarray, y: np.ndarray):

#         # Initialise weights
#         self.__weights = np.ones_like(y)

#         # Run through time steps
#         for t in range (self.__time_steps):

#             # Calculate distribution p
#             distrib = self.__weights / np.sum(self.__weights)

#             # Update weights
#             weights_multiplier = np.power(
#                 np.ones_like(self.__weights) * self.__beta,
#                 1 - np.abs(self.__hypotheses[t](X) - y)
#             )
#             self.__weights *= weights_multiplier

def weaklearn (weights, x, y):
    """
    X -> y\in[0,1]
    :return: hypothesis function
    """

    return

class Adaboost:

    # TBC

    def __init__ (self, time_steps = 150):
        self.__time_steps = time_steps
        self.__hypotheses = list()
        self.__betas = list()

    def fit (self, X: np.ndarray, y: np.ndarray, weaklearn = None):

        # Initialise weights
        self.__weights = np.ones_like(y)

        # Run through time steps
        for t in range (self.__time_steps):

            # Calculate distribution p
            distrib = self.__weights / np.sum(self.__weights)

            # Calculate weak hypothesis function
            self.__hypotheses .append(weaklearn(distrib))

            # Calculate hypothesis error
            error = np.dot( distrib, np.abs(self.__hypotheses[t](X) - y) )
            self.__betas .append(error / (1 - error))

            # Update weights
            weights_multiplier = np.power(
                np.ones_like(self.__weights) * self.__betas[t],
                1 - np.abs(self.__hypotheses[t](X) - y)
            )
            self.__weights *= weights_multiplier

        def decision_func (x):
            a = np.sum(np.multiply(
                -1 * np.log(self.__betas),
                [self.__hypotheses[t](x) for t in range(self.__time_steps)]
            ))
            b = 0.5 * np.sum(-1 * np.log(self.__betas))

            return int(a >= b)

        self.__decision_func = decision_func

    def predict (self, X):
        return self.__decision_func(X)

# what is weaklearn?
# how to "receive loss", "suffer loss" in Hedge?