Harigami
ログイン
Harigamiは、ログイン不要のコード共有サービスです。コードの投稿後に発行されるURLでコードを共有できます。
PythonやRubyなど一部の言語は、投稿後にオンラインで実行することもできます。
誰でも無料で使えて、広告もありません。コードレビューやスニペットの代わりに使ってみてください。


詳細設定
+
anonymous タイトルなし
Python
import tkinter as tk
from tkinter.ttk import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox as msgbox
import pyodbc
import pandas as pd
import os
import re

test = True


class FormMain(tk.Frame):

    def __init__(self, master):
        super().__init__(master)

        master.title('データインポートプログラム')
        master.resizable(False, False)
        self.propagate(False)
        self.pack(expand=True, fill=tk.BOTH)
        pad = {"padx": 10, "pady": 10}

        lbl1 = Label(self, text='取込ファイル種別')
        lbl1.grid(row=0, column=0, cnf=pad)

        self.ftype_var = tk.IntVar(value=1)
        ftype1 = Radiobutton(self, text='Shipment Report', variable=self.ftype_var, value=1)
        ftype1.grid(row=0, column=1, cnf=pad)
        # New Product Shipment Reportボタン作成
        ftype2 = Radiobutton(self, text='New Product Shipment Report', variable=self.ftype_var, value=2)
        ftype2.grid(row=0, column=2, cnf=pad, sticky=tk.W)

        lbl2 = Label(self, text='取込年月')
        lbl2.grid(row=1, column=0, cnf=pad)
        vcmd = (self.register(self.date_entry_validate), '%P')
        self.date_entry_var = tk.StringVar()
        date_entry = Entry(self, textvariable=self.date_entry_var)
        date_entry.configure(validate="key", validatecommand=vcmd)
        date_entry.grid(row=1, column=1, cnf=pad, sticky=tk.W)

        lbl3 = Label(self, text='取込ファイル')
        lbl3.grid(row=2, column=0, cnf=pad)
        self.file_entry_var = tk.StringVar()
        file_entry = Entry(self, textvariable=self.file_entry_var)
        file_entry.grid(row=2, column=1, columnspan=3, cnf=pad, sticky=tk.E + tk.W)

        file_sel_btn = Button(self, text='参 照', width=16, command=self.file_sel_clicked)
        file_sel_btn.grid(row=2, column=4, cnf=pad, sticky=tk.W)

        lbl4 = Label(self, text='取込件数')
        lbl4.grid(row=3, column=0, cnf=pad)
        self.num_entry_var = tk.StringVar()
        num_entry = Entry(self, textvariable=self.num_entry_var)
        num_entry.grid(row=3, column=1, cnf=pad, sticky=tk.W)
        lbl5 = Label(self, text='件')
        lbl5.grid(row=3, column=2, cnf=pad, sticky=tk.W)

        exec_button = Button(self, text='実 行 ', width=16, command=self.exec_clicked)
        exec_button.grid(row=4, column=4, cnf=pad, sticky=tk.W)

        if test:
            self.add_test_frame()

    def date_entry_validate(self, text):
        return (not text) or len(text) <= 6 and text.isdecimal()

    def file_sel_clicked(self):
        path = askopenfilename(initialdir=r'C:\Python', filetypes=[('Excel File', '*xlsx')])
        if path:
            self.file_entry_var.set(path)

    def exec_clicked(self):
        if not re.match("[0-9]{6}", self.date_entry_var.get()):
            msgbox.showwarning(message="date format error")
            return

        if not os.path.exists(self.file_entry_var.get()):
            msgbox.showwarning(message="file not found")
            return

        rowcount = 0
        if self.ftype_var.get() == 1:
            data = self.read_book1(self.file_entry_var.get(), self.date_entry_var.get())
            rowcount = self.insert(data)
        else:
            msgbox.showwarning(message="not implemented")

        self.num_entry_var.set(rowcount)
        msgbox.showinfo(message="ok")

    def add_test_frame(self):
        test_frame = tk.Frame(self, bg="gray")
        test_frame.grid(row=5, column=0, columnspan=5, sticky=tk.E + tk.W)
        cnf = {"ipadx": 5, "side": tk.LEFT}
        crete_table = Button(test_frame, text='CREATE TABLE', command=self.test_create_table)
        crete_table.pack(cnf)
        insert = Button(test_frame, text='INSERT', command=self.test_insert)
        insert.pack(cnf)
        createbook = Button(test_frame, text='CREATE BOOK', command=self.test_create_book)
        createbook.pack(cnf)

    def read_book1(self, path, yyyymm):
        df = pd.read_excel(path, index=False)
        data = df.query("item1=='{}'".format(yyyymm))
        return data

    def insert(self, data):
        con = self.get_connection()
        cur = con.cursor()
        ret = 0
        try:
            for i, row in data.iterrows():
                sql = "INSERT INTO test_a VALUES('{}', '{}', '{}')"
                sql = sql.format(row["item1"], row["item2"], row["item3"])
                cur.execute(sql)
                ret += cur.rowcount

            cur.commit()
        except:
            cur.rollback()
        finally:
            con.close()

        return ret

    def get_conn_str(self):
        driver = "{SQL Server}"
        server = r'localhost\SQLEXPRESS'
        user = 'test'
        password = 'test'
        database = 'sample'

        con_str = "DRIVER={};SERVER={};UID={};PWD={};DATABASE={};"
        return con_str.format(driver, server, user, password, database)

    def get_connection(self):
        conn = pyodbc.connect(self.get_conn_str())
        return conn

    def test_insert(self):
        sql = "INSERT INTO test_a VALUES('202001', 'AAA', 'aaa')"
        con = self.get_connection()
        try:
            con.execute(sql)
            con.commit()
        except:
            con.rollback()
            msgbox.showinfo(message="rollback")
        else:
            msgbox.showinfo(message="ok")
        finally:
            con.close()

    def test_create_table(self):
        sql = """
            DROP TABLE IF EXISTS test_a;
            CREATE TABLE test_a(
                id int IDENTITY(1, 1) NOT NULL,
                item1 varchar(10) NULL,
                item2 nvarchar(50) NULL,
                item3 nvarchar(50) NULL
            );
        """
        con = self.get_connection()
        con.execute(sql)
        con.commit()
        con.close()
        msgbox.showinfo(message="ok")

    def test_create_book(self):
        data = [
            ["1", "202001", "AAAA", "aaaa"],
            ["2", "202001", "BBBB", "bbbb"],
            ["3", "202002", "CCCC", "cccc"],
            ["4", "202002", "DDDD", "dddd"],
        ]
        df = pd.DataFrame(data=data, columns=["no.", "item1", "item2", "item3"])
        fname = "test.xlsx"
        fpath = os.path.join(os.path.dirname(__file__), fname).replace(os.path.sep, "/")
        df.to_excel(fpath, index=False)
        self.file_entry_var.set(fpath)
        msgbox.showinfo(message="ok")


def main():
    app = tk.Tk()
    FormMain(app)
    app.mainloop()


if __name__ == "__main__":
    main()
  • 0
  • 1
anonymous タイトルなし
Python
import tkinter as tk
from tkinter.ttk import *
from tkinter.filedialog import askopenfilename
from tkinter import messagebox as msgbox
import pyodbc
import pandas as pd
import os
import re

test = True


class FormMain(tk.Frame):

  def __init__(self, master):
    super().__init__(master)

    master.title('データインポートプログラム')
    master.resizable(False, False)
    self.propagate(False)
    self.pack(expand=True, fill=tk.BOTH)
    pad = {"padx": 10, "pady": 10}

    lbl1 = Label(self, text='取込ファイル種別')
    lbl1.grid(row=0, column=0, cnf=pad)

    self.ftype_var = tk.IntVar(value=1)
    ftype1 = Radiobutton(self, text='Shipment Report', variable=self.ftype_var, value=1)
    ftype1.grid(row=0, column=1, cnf=pad)
    # New Product Shipment Reportボタン作成
    ftype2 = Radiobutton(self, text='New Product Shipment Report', variable=self.ftype_var, value=2)
    ftype2.grid(row=0, column=2, cnf=pad, sticky=tk.W)

    lbl2 = Label(self, text='取込年月')
    lbl2.grid(row=1, column=0, cnf=pad)
    vcmd = (self.register(self.date_entry_validate), '%P')
    self.date_entry_var = tk.StringVar()
    date_entry = Entry(self, textvariable=self.date_entry_var)
    date_entry.configure(validate="key", validatecommand=vcmd)
    date_entry.grid(row=1, column=1, cnf=pad, sticky=tk.W)

    lbl3 = Label(self, text='取込ファイル')
    lbl3.grid(row=2, column=0, cnf=pad)
    self.file_entry_var = tk.StringVar()
    file_entry = Entry(self, textvariable=self.file_entry_var)
    file_entry.grid(row=2, column=1, columnspan=3, cnf=pad, sticky=tk.E + tk.W)

    file_sel_btn = Button(self, text='参 照', width=16, command=self.file_sel_clicked)
    file_sel_btn.grid(row=2, column=4, cnf=pad, sticky=tk.W)

    lbl4 = Label(self, text='取込件数')
    lbl4.grid(row=3, column=0, cnf=pad)
    self.num_entry_var = tk.StringVar()
    num_entry = Entry(self, textvariable=self.num_entry_var)
    num_entry.grid(row=3, column=1, cnf=pad, sticky=tk.W)
    lbl5 = Label(self, text='件')
    lbl5.grid(row=3, column=2, cnf=pad, sticky=tk.W)

    exec_button = Button(self, text='実 行 ', width=16, command=self.exec_clicked)
    exec_button.grid(row=4, column=4, cnf=pad, sticky=tk.W)

    if test:
      self.add_test_frame()

  def date_entry_validate(self, text):
    return (not text) or len(text) <= 6 and text.isdecimal()

  def file_sel_clicked(self):
    path = askopenfilename(initialdir=r'C:\Python', filetypes=[('Excel File', '*xlsx')])
    if path:
      self.file_entry_var.set(path)

  def exec_clicked(self):
    if not re.match("[0-9]{6}", self.date_entry_var.get()):
      msgbox.showwarning(message="date format error")
      return

    if not os.path.exists(self.file_entry_var.get()):
      msgbox.showwarning(message="file not found")
      return

    rowcount = 0
    if self.ftype_var.get() == 1:
      data = self.read_book1(self.file_entry_var.get(), self.date_entry_var.get())
      rowcount = self.insert(data)
    else:
      msgbox.showwarning(message="not implemented")

    self.num_entry_var.set(rowcount)
    msgbox.showinfo(message="ok")

  def add_test_frame(self):
    test_frame = tk.Frame(self, bg="gray")
    test_frame.grid(row=5, column=0, columnspan=5, sticky=tk.E + tk.W)
    cnf = {"ipadx": 5, "side": tk.LEFT}
    crete_table = Button(test_frame, text='CREATE TABLE', command=self.test_create_table)
    crete_table.pack(cnf)
    insert = Button(test_frame, text='INSERT', command=self.test_insert)
    insert.pack(cnf)
    createbook = Button(test_frame, text='CREATE BOOK', command=self.test_create_book)
    createbook.pack(cnf)

  def read_book1(self, path, yyyymm):
    df = pd.read_excel(path, index=False)
    data = df.query("item1=='{}'".format(yyyymm))
    return data

  def insert(self, data):
    con = self.get_connection()
    cur = con.cursor()
    ret = 0
    try:
      for i, row in data.iterrows():
        sql = "INSERT INTO test_a VALUES('{}', '{}', '{}')"
        sql = sql.format(row["item1"], row["item2"], row["item3"])
        cur.execute(sql)
        ret += cur.rowcount

      cur.commit()
    except:
      cur.rollback()
    finally:
      con.close()

    return ret

  def get_conn_str(self):
    driver = "{SQL Server}"
    server = r'localhost\SQLEXPRESS'
    user = 'test'
    password = 'test'
    database = 'sample'

    con_str = "DRIVER={};SERVER={};UID={};PWD={};DATABASE={};"
    return con_str.format(driver, server, user, password, database)

  def get_connection(self):
    conn = pyodbc.connect(self.get_conn_str())
    return conn

  def test_insert(self):
    sql = "INSERT INTO test_a VALUES('202001', 'AAA', 'aaa')"
    con = self.get_connection()
    try:
      con.execute(sql)
      con.commit()
    except:
      con.rollback()
      msgbox.showinfo(message="rollback")
    else:
      msgbox.showinfo(message="ok")
    finally:
      con.close()

  def test_create_table(self):
    sql = """
      DROP TABLE IF EXISTS test_a;
      CREATE TABLE test_a(
        id int IDENTITY(1, 1) NOT NULL,
        item1 varchar(10) NULL,
        item2 nvarchar(50) NULL,
        item3 nvarchar(50) NULL
      );
    """
    con = self.get_connection()
    con.execute(sql)
    con.commit()
    con.close()
    msgbox.showinfo(message="ok")

  def test_create_book(self):
    data = [
      ["1", "202001", "AAAA", "aaaa"],
      ["2", "202001", "BBBB", "bbbb"],
      ["3", "202002", "CCCC", "cccc"],
      ["4", "202002", "DDDD", "dddd"],
    ]
    df = pd.DataFrame(data=data, columns=["no.", "item1", "item2", "item3"])
    fname = "test.xlsx"
    fpath = os.path.join(os.path.dirname(__file__), fname).replace(os.path.sep, "/")
    df.to_excel(fpath, index=False)
    self.file_entry_var.set(fpath)
    msgbox.showinfo(message="ok")


def main():
  app = tk.Tk()
  FormMain(app)
  app.mainloop()


if __name__ == "__main__":
  main()
  • 0
  • 0
anonymous タイトルなし
Python
  • 0
  • 0
anonymous タイトルなし
Python
def main():
    import sys
    import itertools
    N=int(sys.stdin.readline())
    PL=list(map(int,sys.stdin.readline().split()))
    QL=list(map(int,sys.stdin.readline().split()))

    for i,L in enumerate(itertools.permutations(range(1,N+1))):
        if tuple(L)==tuple(PL):
            a=i
        if tuple(L)==tuple(QL):
            b=i

    print(abs(a-b))
main()
  • 0
  • 1
anonymous タイトルなし
Python
n=int(input())
p=list(map(int,input().split()))
q=list(map(int,input().split()))

def solve(x):
    if(x==1):
        return 1
    else:
        return x*solve(x-1)

def solve2(ls):
    x=ls[0]
    data=sorted(ls)
    return data.index(x)


ans1=0
ans2=0
for i in range(1,n):
    ans1+=solve(n-i)*solve2(p)
    del p[0]

    ans2+=solve(n-i)*solve2(q)
    del q[0]

print(abs(ans1-ans2))
  • 0
  • 0
エビです タイトルなし
Python
def blizzard(defenses,offense):
    for index in range(0,len(defenses),1):
        if (offense-defenses[index]>=0):
            print("コカトリス{}に{}のダメージを与えた!".format(index+1,offense-defenses[index]))
        else:
            print("コカトリス{}はダメージを受け付けない!".format(index+1))
defenses =[38,42,22,120,27]
print("魔法使いの魔力を入力して下さい。")
offense=int(input("魔法使いの魔力:"))
for index in range(1,len(defenses),1):
    print("コカトリスが現れた!".format(index))
print("魔法使いはブリザードを唱えた!")
blizzard(defenses,offense)
  • 0
  • 1
anonymous タイトルなし
VisualBasic
printe
  • 0
  • 1
anonymous タイトルなし
HTML
</2019>
<2020>
  <h1>HELLO 2020!!</h1>
  • 1
  • 1
anonymous タイトルなし
C#
using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class Player : MonoBehaviour
{
	class BulletType
	{
		public enum Types
		{
			Small,
			TwoWay,
			Length
		}

		public GameObject Prefab;
		public Vector2 minOffset;
		public Vector2 maxOffset;

		public BulletType(GameObject prefab, float minOffsetX, float maxOffsetX, float minOffsetY, float maxOffsetY)
		{
			this.Prefab = prefab;
			this.minOffset = new Vector2(minOffsetX, minOffsetY);
			this.maxOffset = new Vector2(maxOffsetX, maxOffsetY);
		}
	}
	BulletType[] BulletTypes;
	BulletType CurrentBulletType;

	void Start()
	{
		InitBulletTypes();
		CurrentBulletType = BulletTypes[(int)BulletType.Types.Small];
	}

	void InitBulletTypes()
	{
		BulletTypes = new BulletType[(int)BulletType.Types.Length];
		BulletTypes[(int)BulletType.Types.Small] = new BulletType((GameObject)Resources.Load("Prefabs/Player_Bullet_Small"), 0.5f, 0.6f, -0.15f, 0.15f);
		BulletTypes[(int)BulletType.Types.TwoWay] = new BulletType((GameObject)Resources.Load("Prefabs/Player_Bullet_2way"), 0.5f, 0.6f, -0.15f, 0.15f);
	}

	void Fire1_Push()
	{
		ShootBullet(CurrentBulletType);
	}

	void ShootBullet( BulletType bullet ){
		Vector3 position = transform.position;
		position += transform.up * Random.Range(bullet.minOffset.y, bullet.maxOffset.y);
		position += transform.right * Random.Range(bullet.minOffset.x, bullet.maxOffset.x);
		Instantiate(bullet.Prefab, transform.position, transform.rotation);
	}
}
  • 0
  • 1
Kohei Arai UVa 12392 - Guess the Numbers
Java
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.*;

public class Main {

	static int[] dx = {0, 1, 0, -1};
	static int[] dy = {-1, 0, 1, 0};
	static long MOD = (long) (1e9 + 7);
	  
	static boolean isAnswerFound = false;
	
	public static void main(String[] args) {
		FastReader in = new FastReader();
		
		while (true) {
			int N = in.nextInt();
			
			int[] nums = new int[N];
			for (int i = 0; i < N; i++) {
				nums[i] = in.nextInt();
			}
			
			int M = in.nextInt();
			if (N == 0 && M == 0) {
				break;
			}
			
			
			String s = in.nextLine();
			isAnswerFound = false;
			System.out.println(solve(nums, M, s) ? "YES" : "NO");
		}
	}
	
	public static boolean solve(int[] nums, int M, String s) {
		int[] newnums = new int[nums.length];
		rec(nums, newnums, 0, s, M);
		return isAnswerFound;
	}
	
	public static void rec(int[] nums, int[] newnums, int index, String s, int M) {
		if (isAnswerFound) {
			return;
		}
		if (index == nums.length) {
			if (isValid(newnums, s, M)) {
				isAnswerFound = true;
			}
		} else {
			for (int i = 0; i < nums.length; i++) {
				if (nums[i] >= 0) {
					int tmp = nums[i];
					nums[i] = -1;
					newnums[index] = tmp;
					rec(nums, newnums, index + 1, s, M);
					nums[i] = tmp;
				}
			}
		}
	}
	
	public static boolean isValid(int[] nums, String s, int M) {
//		printNumArray(nums);
		HashMap<Character, Long> unknownMap = new HashMap<>();
		Stack<CustomClass> stack = new Stack<CustomClass>();
		// 1 => number, 2 => op, 3 => parenthesis 
		for (int i = 0; i < s.length(); i++) {
			char c = s.charAt(i);
			// No number in string, so just look at character by character
			if (c == '(') {
				CustomClass cc = new CustomClass(3);
				stack.add(cc);
			} else if (c == ')') {
				CustomClass keep = stack.pop();
				CustomClass tmp = stack.pop();
				// If it's an opening parenthesis, just replace it with the result of the expression
				if (tmp.type == 3) {
					stack.add(keep);
				}
				// It was an op
				else if (tmp.type == 2) {
					keep.n = exp(stack.pop().n, keep.n, tmp.op);
					// Remove the starting parenthesis
					stack.pop();
					stack.add(keep);
				} else {
					// Should not reach here
					out("This is unexpected");
				}
			} else if (c == '+' || c == '-' || c == '*') {
				CustomClass cc = new CustomClass(2);
				cc.op = c;
				stack.add(cc);
			} else {
				long n = -1;
				if (unknownMap.containsKey(c)) {
					n = unknownMap.get(c);
				} else {
					n = nums[unknownMap.size()];
					unknownMap.put(c, n);
				}
				
				// If we can compute the result, compute the result
				if (stack.size() > 0 && stack.peek().type == 2) {
					CustomClass op = stack.pop();
					n = exp(stack.pop().n, n, op.op);
					// Add the result of expression back to the result
					CustomClass cc = new CustomClass(1);
					cc.n = n;
					stack.add(cc);
				} else {
					CustomClass cc = new CustomClass(1);
					cc.n = n;
					stack.add(cc);
				}
			}
		}
		
		return stack.pop().n == M;
	}
	
	public static long exp(long a, long b, char op) {
		if (op == '+') {
			return a + b;
		} else if (op == '-') {
			return a - b;
		} else if (op == '*') {
			return a * b;
		} else {
			out("Unknown operation");
			return a;
		}
	}
	
	public static int maxSideLength(int[][] mat, int threshold) {
		if (mat == null || mat.length == 0 || mat[0].length == 0) {
			return 0;
		}
		
		int M = mat.length;
		int N = mat[0].length;
		
		// Precompute the sum
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				mat[i][j] = getAt(mat, i, j) + getAt(mat, i - 1, j) + getAt(mat, i, j - 1) - getAt(mat, i - 1, j - 1);
			}
		}
		
		int ans = 0;
		for (int i = 0; i < M; i++) {
			for (int j = 0; j < N; j++) {
				for (int k = 1; i + k < M && j + k < N; k++) {
					int area = getAt(mat, i + k, j + k) - getAt(mat, i + k, j - 1) - getAt(mat, i - 1, j + k) + getAt(mat, i - 1, j - 1);
					if (area <= threshold) {
						ans = Math.max(ans, k + 1);
					}
				}
			}
		}
		
		return ans;
	}
	
	public static int getAt(int[][] mat, int i, int j) {
		if (i < 0 || j < 0) return 0;
		return mat[i][j];
	}
	
    public static List<Integer> findAnagrams(String s, String p) {
    	List<Integer> ans = new ArrayList<Integer>();
        
    	int[] pfreq = new int[26];
        for (int i = 0; i < p.length(); i++) {
            pfreq[p.charAt(i) - 'a']++;
        }
        
        int[] sfreq = new int[26];
        for (int i = 0; i < p.length() && i < s.length(); i++) {
        	sfreq[s.charAt(i) - 'a']++;
        }
        
        // key is in the format character###,
        String pkey = "";
        String skey = "";
        
        for (int i = 0; i < 26; i++) {
        	pkey += String.format("%c%d,", i + 'a', pfreq[i]);
        	skey += String.format("%c%d,", i + 'a', sfreq[i]);
        }
        
        for (int i = p.length(); i < s.length(); i++) {
        	char removed = s.charAt(i - p.length());
        	char added = s.charAt(i);
        	
        	
        }
        
        return ans;
    }

	public static int count(String[] arr, Map<String, Map<Character, TreeSet<Integer>>> map, String s) {
		int count = 0;
		for (int i = 0; i < arr.length; i++) {
			if (subsequence(s, map.get(arr[i]))) {
				count++;
			}
		}
		return count;
	}
	
	public static boolean subsequence(String s, Map<Character, TreeSet<Integer>> map) {
		char f = s.charAt(0);
		char m = s.charAt(1);
		char e = s.charAt(2);
		if (!map.containsKey(f) || !map.containsKey(m) || !map.containsKey(e)) {
			return false;
		}
		
		Iterator<Integer> it = map.get(m).iterator();
		while (it.hasNext()) {
			int index = it.next();
			if (map.get(f).lower(index) != null && map.get(e).higher(index) != null) {
				return true;
			}
		}
		
		return false;
	}
	
	public static String str(int i, int j, int k) {
		return String.format("%c%c%c", c(i), c(j), c(k));
	}
	
	public static char c(int i) {
		if (i < 26) {
			return (char) ('A' + i);
		} else {
			i -= 26;
			return (char) ('a' + i);
		}
	}
	
	public static int sol(int N, int k) {
		if (N == 1 && k == 1) return 1;
		int parent = sol(N - 1, (k + 1) / 2);
		if (k % 2 == 0) {
			return (parent + 1) % 2;
		} else {
			return parent;
		}
	}
	
	public static double calculate(double base, double n) {
        return Math.pow(Math.E, Math.log(base)/n);
    }
	
	public static int partition(int[] nums, int low, int high) { // [1,2], 0, 1
		// Pick the first element as pivot
		// int pivot = nums[low]; // 1
		//
		// int left = low + 1; // 2
		// int right = high; // 1
		//
		//
		// while (left <= right) { // This equal to is important to move right
		// cursor left cursor correct position
		// if (nums[left] <= pivot) {
		// left++;
		// continue;
		// }
		// if (pivot <= nums[right]) {
		// right--;
		// continue;
		// }
		// int tmp = nums[left];
		// nums[left] = nums[right];
		// nums[right] = tmp;
		// }
		// int tmp = nums[low];
		// nums[low] = nums[right];
		// nums[right] = tmp;
		//
		// return right;

		// Pick the last element as pivot
		int pivot = nums[high];

		int left = low;
		int right = high - 1;

		while (left <= right) {
			if (nums[left] <= pivot) {
				
				left++;
				continue;
			}
			if (pivot <= nums[right]) {
				right--;
				continue;
			}
			int tmp = nums[left];
			nums[left] = nums[right];
			nums[right] = tmp;
		}
		int tmp = nums[high];
		nums[high] = nums[left];
		nums[left] = tmp;

		return left;
	}

	// Pivot is the first element
	public static int partitionFront(int[] nums, int low, int high) {
		int pivot = nums[low];

		int left = low + 1;

		while (left <= high) {
			// Increment left until it reaches the element greater than pivot
			while (left <= high && nums[left] <= pivot)
				left++;
			while (left <= high && nums[high] >= pivot)
				high--;

			if (left < high) {
				int tmp = nums[left];
				nums[left] = nums[high];
				nums[high] = tmp;
			}
		}
		int tmp = nums[low];
		nums[low] = nums[high];
		nums[high] = tmp;

		return high;
	}

	// Pivot is the last element
	public static int partitionBack(int[] nums, int low, int high) {
		int pivot = nums[high];

		int right = high - 1;

		while (low <= right) {
			// Increment low until it is greater than pivot
			while (low <= right && nums[low] <= pivot)
				low++;
			// Decrement right until it is less than pivot
			while (low <= right && nums[right] >= pivot)
				right--;

			if (low < right) {
				int tmp = nums[low];
				nums[low] = nums[right];
				nums[right] = tmp;
			}
		}

		int tmp = nums[low];
		nums[low] = nums[high];
		nums[high] = tmp;

		return low;
	}

	public static void out(Object o) {
		System.out.println(o);
	}

	public static void print(Object o) {
		System.out.print(o);
	}

	public static void printNumArray(int[] nums) {
		System.out.printf("[");
		for (int i = 0; i < nums.length; i++) {
			System.out.printf("%d%s", nums[i], i != nums.length - 1 ? ", " : "");
		}
		System.out.println("]");
	}

	public static void printNumArray(long[] nums) {
		System.out.printf("[");
		for (int i = 0; i < nums.length; i++) {
			System.out.printf("%d%s", nums[i], i != nums.length - 1 ? ", " : "");
		}
		System.out.println("]");
	}

	public static void printDoubleArray(double[] nums) {
		System.out.printf("[");
		for (int i = 0; i < nums.length; i++) {
			System.out.printf("%f%s", nums[i], i != nums.length - 1 ? ", " : "");
		}
		System.out.println("]");
	}

	public static void printDoubleList(List<List<Integer>> nums) {
		for (int i = 0; i < nums.size(); i++) {
			printNumList(nums.get(i));
		}
	}

	public static void printNumList(List<Integer> nums) {
		System.out.printf("[");
		for (int i = 0; i < nums.size(); i++) {
			System.out.printf("%d%s", nums.get(i), i != nums.size() - 1 ? ", " : "");
		}
		System.out.println("]");
	}

	public static void printStringList(List<String> list) {
		System.out.printf("[");
		for (int i = 0; i < list.size(); i++) {
			System.out.printf("%s%s", list.get(i), i != list.size() - 1 ? ", " : "");
		}
		System.out.println("]");
	}

	public static void printStringArray(String[] arr) {
		print("[");
		for (int i = 0; i < arr.length; i++) {
			print(String.format("%s%s", arr[i], i != arr.length - 1 ? ", " : ""));
		}
		out("]");
	}

	public static void printStringListOfStringList(List<List<String>> list) {
		for (int i = 0; i < list.size(); i++) {
			printStringList(list.get(i));
		}
	}

	public static void printNumListOfNumList(List<List<Integer>> num) {
		for (int i = 0; i < num.size(); i++) {
			printNumList(num.get(i));
		}
	}
	
	public static void printNumListOfNumArray(List<int[]> list) {
		for (int i = 0; i < list.size(); i++) {
			printNumArray(list.get(i));
		}
	}

	public static void printNum2DArray(int[][] map) {
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				System.out.printf("%2d ", map[i][j]);
			}
			System.out.println();
		}
	}

	public static void printNum2DArray(long[][] map) {
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[i].length; j++) {
				System.out.printf("%2d ", map[i][j]);
			}
			System.out.println();
		}
	}
	
	public static void printCharArray(char[] arr) {
		print("[");
		for (int i = 0; i < arr.length; i++) {
			System.out.printf("%c%s", arr[i], i == arr.length - 1 ? "" : ", ");
		}
		out("]");
	}

	public static void printChar2DArray(char[][] map) {
		for (char[] arr : map) {
			for (char c : arr) {
				print(String.format("%c ", c));
			}
			out("");
		}
	}

	public static void printBoolean2DArray(boolean[][] map) {
		for (int i = 0; i < map.length; i++) {
			for (int j = 0; j < map[0].length; j++) {
				print(map[i][j] ? "T " : "F ");
			}
			out("");
		}
	}

	public static void printListNode(ListNode node) {
		boolean first = true;
		while (node != null) {
			if (!first) {
				print("->");
			}
			print(node.val);
			node = node.next;
			first = false;
		}
	}

	public static void printStringSet(Set<String> set) {
		for (String s : set) {
			out(s);
		}
	}

	/**
	 * Receives an integer and print in numBits from the right
	 * 
	 * @param n
	 * @param numBits
	 */
	public static void printBinary(int n, int numBitsFromRight) {
		String s = "";
		for (int i = numBitsFromRight - 1; i >= 0; i--) {
			s += (n & (1 << i)) > 0 ? "1" : "0";
		}
		out(s);
	}

	// Taken from
	// https://www.geeksforgeeks.org/c-program-find-gcd-hcf-two-numbers/
	public static long gcd(long a, long b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}
	
	public static int gcd(int a, int b) {
		if (b == 0) {
			return a;
		}
		return gcd(b, a % b);
	}

	public static long lcm(long a, long b) {
		return a * b / gcd(a, b);
	}
	
	public static int lcm(int a, int b) {
		return a * b / gcd(a, b);
	}
}

class TreeNode {
	int val;
	TreeNode left;
	TreeNode right;

	TreeNode(int x) {
		val = x;
	}
}

class CustomClass {
	int type; // 1 => number, 2 => op, 3 => parenthesis
	// parenthesis will be stored in op
	char op;
	long n;
	
	public CustomClass(int type) {
		this.type = type;
	}
}

class TreeDrawer {
	public static String treeNodeToString(TreeNode root) {
		if (root == null) {
			return "[]";
		}

		String output = "";
		Queue<TreeNode> nodeQueue = new LinkedList<>();
		nodeQueue.add(root);
		while (!nodeQueue.isEmpty()) {
			TreeNode node = nodeQueue.remove();

			if (node == null) {
				output += "null, ";
				continue;
			}

			output += String.valueOf(node.val) + ", ";
			nodeQueue.add(node.left);
			nodeQueue.add(node.right);
		}
		return "[" + output.substring(0, output.length() - 2) + "]";
	}

	public static TreeNode stringToTreeNode(String input) {
		input = input.trim();
		input = input.substring(1, input.length() - 1);
		if (input.length() == 0) {
			return null;
		}

		String[] parts = input.split(",");
		String item = parts[0];
		TreeNode root = new TreeNode(Integer.parseInt(item));
		Queue<TreeNode> nodeQueue = new LinkedList<>();
		nodeQueue.add(root);

		int index = 1;
		while (!nodeQueue.isEmpty()) {
			TreeNode node = nodeQueue.remove();

			if (index == parts.length) {
				break;
			}

			item = parts[index++];
			item = item.trim();
			if (!item.equals("null")) {
				int leftNumber = Integer.parseInt(item);
				node.left = new TreeNode(leftNumber);
				nodeQueue.add(node.left);
			}

			if (index == parts.length) {
				break;
			}

			item = parts[index++];
			item = item.trim();
			if (!item.equals("null")) {
				int rightNumber = Integer.parseInt(item);
				node.right = new TreeNode(rightNumber);
				nodeQueue.add(node.right);
			}
		}
		return root;
	}

	public static void prettyPrintTree(TreeNode node, String prefix, boolean isLeft) {
		if (node == null) {
			System.out.println("Empty tree");
			return;
		}

		if (node.right != null) {
			prettyPrintTree(node.right, prefix + (isLeft ? "│   " : "    "), false);
		}

		System.out.println(prefix + (isLeft ? "└── " : "┌── ") + node.val);

		if (node.left != null) {
			prettyPrintTree(node.left, prefix + (isLeft ? "    " : "│   "), true);
		}
	}

	public static void draw(TreeNode node) {
		prettyPrintTree(node, "", true);
	}
}

class Interval {
	int start;
	int end;

	Interval() {
		start = 0;
		end = 0;
	}

	Interval(int s, int e) {
		start = s;
		end = e;
	}
}

class ListNode {
	int val;
	ListNode next;

	ListNode(int x) {
		val = x;
	}
}

class FastReader { 
    BufferedReader br; 
    StringTokenizer st; 

    public FastReader() 
    { 
        br = new BufferedReader(new
                 InputStreamReader(System.in)); 
    } 

    String next() 
    { 
        while (st == null || !st.hasMoreElements()) 
        { 
            try
            { 
                st = new StringTokenizer(br.readLine()); 
            } 
            catch (IOException  e) 
            { 
                e.printStackTrace(); 
            } 
        } 
        return st.nextToken(); 
    } 

    int nextInt() 
    { 
        return Integer.parseInt(next()); 
    } 

    long nextLong() 
    { 
        return Long.parseLong(next()); 
    } 

    double nextDouble() 
    { 
        return Double.parseDouble(next()); 
    } 

    String nextLine() 
    { 
        String str = ""; 
        try
        { 
            str = br.readLine(); 
        } 
        catch (IOException e) 
        { 
            e.printStackTrace(); 
        } 
        return str; 
    } 
} 
  • 0
  • 1