Skip to content

Instantly share code, notes, and snippets.

View mizushou's full-sized avatar
:octocat:
Working from home

Shouhei.Mizuno mizushou

:octocat:
Working from home
View GitHub Profile
@mizushou
mizushou / AdditionAndMultiplication.java
Last active October 29, 2017 04:01
# AtCoder Beginner Contest 076 問題文 square1001 は、電光掲示板に整数 1 が表示されているのを見ました。 彼は、電光掲示板に対して、以下の操作 A, 操作 B をすることができます。 操作 A: 電光掲示板に表示する整数を「今の電光掲示板の整数を 2 倍にしたもの」に変える。 操作 B: 電光掲示板に表示する整数を「今の電光掲示板の整数に K を足したもの」に変える。 square1001 は、操作 A, 操作 B 合計で N 回 行わなければなりません。 そのとき、N 回の操作後の、電光掲示板に書かれている整数として考えられる最小の値を求めなさい。 制約 1≤N,K≤10 入力はすべて整数である
import java.util.Scanner;
class AdditionAndMultiplication {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
int n = sc.nextInt();
int k = sc.nextInt();
sc.close();
int number = 1;
for(int i=0; i<n; i++){
@mizushou
mizushou / Fibonacci.java
Created October 29, 2017 23:25
# フィボナッチ数列
class Fibonacci {
static int fib(int n) {
//引数でn=0,1を指定した際は再帰処理を行わず、即座に1を返す
if(n<=1) return 1;
return fib(n-1) + fib(n-2);
}
public static void main(String[] args) {
System.out.println(fib(0));
}
}
import java.util.Scanner;
class BinarySearch {
static int binarySearch(int[] A, int key) {
int left = 0;
int right = A.length;
while(left < right) {
int mid = (left + right)/2;
if(A[mid] == key) {
@mizushou
mizushou / BinarySearch.java
Last active November 9, 2017 03:22
* 二分木探索(バイナリサーチ)
import java.util.Scanner;
class BinarySearch2 {
static int binarySearch(int[] A, int key) {
int left = 0;
int right = A.length;
while(left < right) {
int mid = (left + right)/2;
if(A[mid] == key) {
@mizushou
mizushou / GoodInterger2.java
Created November 19, 2017 20:27
Good Integerの別解。こちらはより一般的な解法。4桁に限らず判定できる。
import java.util.Scanner;
class GoodInterger2 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
char[] c = sc.next().toCharArray();
sc.close();
for(int i=0; i<c.length; i++){
int j = i;
while(j<c.length) {
public class ThePalindrome {
public int find(String s) {
// 答えの文字数を決定する. 条件は指定しない(無限ループ. returnでの強制終了)
for(int i=s.length(); ;i++){
boolean flag = true;
for(int j=0; j<s.length(); j++){
if ((i - j - 1) < s.length() && s.charAt(j) != s.charAt(i - j - 1)){
flag = false;
break;
}
@mizushou
mizushou / dictionary-comprehension.py
Last active July 29, 2019 06:09
list, dictionary comprehensionのサンプル
"""
dictionary-comprehension
"""
# Pythonはdictionary comprehensionもサポートしている
# syntax: <variable> = { key:new_value for (key, value) in <dictionary>.items() }
grades = {'Nora': 90, 'Lulu': 15, 'Gino': 60}
grades
# ---> {'Nora': 90, 'Lulu': 15, 'Gino': 60}
@mizushou
mizushou / rectified_linear_unit.py
Last active July 30, 2019 08:08
NNで使うactivation functionのサンプル
import numpy as np
# ReLu activation function, which computes the ReLu of a scalar.
def rectified_linear_unit(x):
""" Returns the ReLU of x, or the maximum between 0 and x. """
return np.maximum(0, x)
@mizushou
mizushou / swap_with_tuple.py
Last active August 3, 2019 03:37
小ネタサンプル
"""
swap with tuple
"""
x, y = 1, 0
# swap with tuple
# tmpなどの変数を使わずにワンラインでswapできる
(x, y) = (y, x)
@mizushou
mizushou / advanced_slicing.py
Last active September 9, 2019 00:26
sliceサンプル集
"""
Advanced slicing
"""
# 応用的な使い方1
# third parameterはstep size.この例ではstep size = 2
s = 'Python is Fun!'
s[1:12:2]
# ---> 'yhni u'