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 / is_prime_recursion.py
Last active October 6, 2019 02:06
recursion関連
def is_prime_recursion(n, i=2):
# base case
## easiest case
if n == 0 or n == 1:
return False
## 終了条件
### iをインクリメントしていって最後まで割り切れなかったらprimeなのでTrueを返して終了
if n == i:
return True
@mizushou
mizushou / bubble_sort.py
Last active September 30, 2019 02:14
Sorting関連
# Bubble Sort - "Bubbling" the largest element to the right!
# (pseudo-code)
# list = [...]
# for each i from 1 to len(list)
# compare two adjacent elements
# if the first element is greater than the second element
# swap two elements
# Time Complexity
# O(n^2) algorithm
@mizushou
mizushou / binary_search.py
Last active September 30, 2019 02:02
Searching関連
"""
Binary Search
- how you look up a word in dictionary or a contact in phone book.
* Items have to be sorted!
"""
alist = ["Apple", "Banana", "Grapes",
"Kiwi", "Mango", "Mangosteen",
"Peach", "Pears", "Strawberry", "Watermelon"]
@mizushou
mizushou / array_front9.py
Last active October 31, 2019 05:33
A sample answer of Coding Bat
"""
CodingBat: Warmup-2
array_front9
"""
# I think it's okay in Python.
def array_front9(nums):
return 9 in nums[:4]
# Recursive case.
@mizushou
mizushou / lists_basics.py
Created September 20, 2019 17:01
Python listの使い方
"""
list: A sequence of items (elements)
- A list is mutable
- A list is not array in Python.
- A list can contain a variety of types.
"""
# 1. create a list
fruit = []
@mizushou
mizushou / lab2_list.py
Created September 20, 2019 07:22
Python list関連
"""
CICCC WMAD LAB2
Basic python list problems -- no loops.
出展は'Coding Bat'
"""
# まず、ほとんどがワンライナーで書ける
# loopを使わずに、logicで書くことでTrue,Falseをわざわざ書く必要がない、ってことに気づかないと冗長な書き方になる
def first_last6(nums):
@mizushou
mizushou / lab1_string.py
Last active September 20, 2019 15:38
Python string関連
"""
CICCC WMAD LAB1
Basic python string problems -- no loops.
"""
def hello_name(name):
"""
Given a string name, e.g. "Bob", return a greeting of the form "Hello Bob!".
"""
@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'
@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 / 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)