Skip to content

Instantly share code, notes, and snippets.

@murat-to
murat-to / mysql_connector_sample.py
Last active November 11, 2016 23:39
Python module mysql.connector sample
import mysql.connector
try:
database_connection = mysql.connector.connect(
host = '<host_name>',
port = <port_number>,
db = '<database_name>',
user = '<database_user>',
passwd = '<database_password>',
charset = '<database_character_set>',
@murat-to
murat-to / Max
Last active October 9, 2016 01:47
[Scala] 整数リスト ints 内の最大数を返す関数
def max(ints: List[Int]): Int = ints match{
case Nil => 0
case head :: tail => if (head > max(tail)) head else max(tail)
}
@murat-to
murat-to / ProductOfList
Last active October 9, 2016 01:47
[Scala] 整数リスト ints 内の数をすべて乗算した値を返す関数
def productOfList(ints: List[Int]): Int = {
def loop(ints: List[Int]): Int = ints match{
case Nil => 1
case head :: tail => head * loop(tail)
}
if (ints.isEmpty) 0
else loop(ints)
}
@murat-to
murat-to / SumOfList
Last active October 9, 2016 01:48
[Scala] 引数として与えられた整数リスト ints の合計を返す関数
def sumOfList(ints: List[Int]): Int = ints match{
case Nil => 0
case head :: tail => head + sumOfList(tail)
}
@murat-to
murat-to / FibonacciNumberOf
Last active October 9, 2016 01:48
[Scala] フィボナッチ数列の n 番目を返す関数
def fibonacciNumberOf(n: Int): Int = n match{
case 0 => 1
case 1 => 1
case _ => fibonacciNumberOf(n - 2) + fibonacciNumberOf(n - 1)
}
@murat-to
murat-to / Factorial
Last active October 9, 2016 01:49
[Scala] 非負整数 n の階乗を返す関数
def factorial(n: Int): Int = {
if (n == 0) 1
else factorial(n - 1) * n
}
@murat-to
murat-to / SumOfLessthan
Last active October 9, 2016 01:42
[Scala] 自然数 n を引数に与えると、n 以下の自然数合計を返す関数
def sumOfLessthan(n: Int): Int = {
def loop(n: Int): Int = {
if (n == 1) 1
else sumOfLessthan(n - 1) + n
}
if (n < 1) 0
else loop(n)
}