Startup Data scientist Blog

データ分析系のテック情報を発信します

様々なプログラミング言語のコードがコンパイルできる Wandbox

f:id:xxUFOxx:20220204170308p:plain

対応しているプログラミング言語は、C++、C、D、Rill、HaskellC#PerlPython、R、RubyPHPErlang、Elixir、JavaScript、CoffeeScrpt、SQL、Rust、BashLua、Lazy K、LispPascalJava、swiftも対応しています。Rも対応しているので統計解析などもこちらのサイトから不要なダウンロードなく解析が可能となっています。wandbox.org

 

github.com

 

 

 

Pythonを使ったMemoizeとは

Memoize とはキャッシュを用いて関数呼び出しを高速化する手法

・関数が同じ引数で何度も呼び出される
・関数呼び出し 1 回あたりのコストが高い (実行時間が長い)

等で使用すると効率的に関数を呼び出すことが出来る。

MemoTable = {}

def MemoizedFib(n):
    if n <= 2:
        return 1
   
    if n in MemoTable:
        return MemoTable[n]
   
    MemoTable[n] = MemoizedFib(n-1) + MemoizedFib(n-2)
    return MemoTable[n]

res = MemoizedFib(10)

 

python 再帰呼出し n*fact(n-1)

python再帰呼出しにより階乗 n! の計算が可能になります。

 

factorialコード

def fact(n):
    if (n <= 1):
        return 1
    else:
        return n * fact(n - 1)
   
print(fact(5))

Output

>>

120

python inputを使った簡単なコード

python inputを使った文字列の入力

prefix = "Hello "
n1 = input("Enter your name ")
n2 = input("Enter your another name ")

res = prefix + n1 + " and " + n2
print(res)

Output

>>

Enter your name mike
Enter your another name tarou
Hello mike and tarou

python for elseのループコード

Pythonではwhile, forのループにelseを使用することが出来ます。他の言語と比較すると珍しい機能で、ありがちなケースでは「ループ処理で何かを探索して見つけたらbreakする、breakしなかったら見つからなかった」といったケースでフラグ変数を使う必要がなく処理が可能になります。

for n in range(2, 10):
    x_range = range(2, n)
    for x in x_range:
        if n % x == 0:
            break
    else:
        print(n)

Output

>>

2
3
5
7

pythonの挿入ソート(insertion sort)

シンプルで直感的なソートアルゴリズム

def InsertionSort(A):
    for j in range(1, len(A)):
        key = A[j]
        i = j - 1
        while (i >= 0) and (A[i] > key):
            i = i - 1
            A[i+1] = key
input = [8, 3, 9, 15, 29, 7, 10]
InsertionSort(input)
print(input)

Output

>>

[3, 3, 7, 7, 7, 7, 10]

 

Pythonで簡単な形態素解析

tokenize python

input = 'John, Doe, 1983, 4, 29, male'

tokens = input.split(',')
firstName = tokens[0]
lastName = tokens[1]
birthdate = (int(tokens[2]), int(tokens[3]), int(tokens[4]))
isMale = (tokens[5] == 'male')

print('Hi ' + firstName + ' ' + lastName)

Output

>>

Hi John  Doe

 

docs.python.org

 

python forとifを使ったフィルター処理

python forとifを使った簡単なフィルター処理

input = [("Mary", 27), ("Joe", 30), ("Ruth", 43), ("Boe", 17),("Jenny", 22)]

youngPeople = []

for (person, age) in input:
    if age < 30:
        youngPeople.append(person)
    else:
        print(person + 'is too old!')
       
print('There are ' + str(len(youngPeople)) + ' young people')

Output

>>

Joeis too old!
Ruthis too old!
There are3 young people

 

python stringsとnumberの基本的なコード

数字型

#number
age = 99
pi = 3.14
print(age)
print(pi)

Output

>>

99
3.14

 

文字列

#strings
s = 'Rutherford Birchard Hayas'
tokens = s.split()
firstName = tokens[0]
middleName = tokens[1]
lastName = tokens[2]
s2 = firstName + " " + middleName + " " + lastName

print(s)
print(tokens)
print(firstName)
print(middleName)
print(lastName)
print(s2)

Output

>>

Rutherford Birchard Hayas
['Rutherford', 'Birchard', 'Hayas']
Rutherford
Birchard
Hayas
Rutherford Birchard Hayas

 

s = 'Rutherford Birchard Hayas'
tokens = s.split()
firstName = tokens[0]
middleName = tokens[1]
lastName = tokens[2]
s2 = firstName + " " + middleName + " " + lastName

if s == s2:
    print("yes!")
else:
    print("No!")

Output

>>

yes!

 

リストとfor

#list
beatles = ['John', 'Paul', 'George']
beatles.append('Ringo')

for b in beatles:
    print('Hello' + b)

Output

>>

HelloJohn
HelloPaul
HelloGeorge
HelloRingo

 

How to sort

#tuple
ages = (18, 21,28, 21, 22, 18, 19, 34, 9)

#set
uniqueAges = set(ages)
uniqueAges.add(18)     #既に18は入っているので意味がはない
uniqueAges.remove(21)  #21が取り除かれる

for thisAge in uniqueAges:
    print(thisAge)
   
if 18 in uniqueAges:
    print('This is 18years old present!')
   
orderedUniqueAges = sorted(uniqueAges)
print(orderedUniqueAges)

Output

>>

34
9
18
19
22
28
This is 18years old present!
[9, 18, 19, 22, 28, 34]

docs.python.org

 

pythonの辞書形式

#dict 辞書形式
netWorth = {}
netWorth['Donald Trump'] = 3000000000
netWorth['Bill Gates'] = 58000000000
netWorth['Tom Cruise'] = 40000000
netWorth['Joe Postdoc'] = 20000

for (person, worth) in netWorth.items():
    if worth < 1000000:
        print(person + 'is not a millionaire')
       
       
if 'Tom Cruise' in netWorth:
    print('Show me the money!')

Output

>>

Joe Postdocis not a millionaire
Show me the money!