This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
CodingBat: Warmup-2 | |
array_front9 | |
""" | |
# I think it's okay in Python. | |
def array_front9(nums): | |
return 9 in nums[:4] | |
# Recursive case. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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 = [] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
CICCC WMAD LAB2 | |
Basic python list problems -- no loops. | |
出展は'Coding Bat' | |
""" | |
# まず、ほとんどがワンライナーで書ける | |
# loopを使わずに、logicで書くことでTrue,Falseをわざわざ書く必要がない、ってことに気づかないと冗長な書き方になる | |
def first_last6(nums): |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
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!". | |
""" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
Advanced slicing | |
""" | |
# 応用的な使い方1 | |
# third parameterはstep size.この例ではstep size = 2 | |
s = 'Python is Fun!' | |
s[1:12:2] | |
# ---> 'yhni u' |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
""" | |
swap with tuple | |
""" | |
x, y = 1, 0 | |
# swap with tuple | |
# tmpなどの変数を使わずにワンラインでswapできる | |
(x, y) = (y, x) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) |
NewerOlder