mu chance or much chance ?

日々の戯れ言

pythonでprocessing

Processingの勉強とpythonの勉強を兼ねて,

以下の本を見ながら,

pythonモードでprocessingを操作しました.

Processingをはじめよう 第2版 (Make: PROJECTS)

Processingをはじめよう 第2版 (Make: PROJECTS)

おそらく実行時に,ネットに接続していないと機能しないと思います.

  • 本に書かれているプログラムをpythonで書き直したもの
class Robot:
    yOffset = 0.0
    def __init__(self, shape, tempX, tempY):
        self.botShape = shape
        self.xPos = tempX
        self.yPos = tempY
        self.angle = random(0, TWO_PI)
    def update(self):
        self.angle += 0.05
        self.yOffset = sin(self.angle) * 20
    def display(self):
        shape(self.botShape, self.xPos, self.yPos + self.yOffset)
from robot import Robot
bots = []
num = 20

def setup():
    size(720, 480)
    robotShape = loadShape("robot2.svg")
    for i in range(num):
        x = random(-40, width - 40);
        y = map(i, 0, num, -100, height - 200)
        bots.append(Robot(robotShape, x, y))

def draw():
    background(0, 153, 204)
    for i in range(num):
        bots[i].update()
        bots[i].display()
  • 実行結果

f:id:muchance:20170416192213p:plain

時間があれば,以下の本で勉強したいですね.

[普及版]ジェネラティブ・アート―Processingによる実践ガイド

[普及版]ジェネラティブ・アート―Processingによる実践ガイド

Nature of Code -Processingではじめる自然現象のシミュレーション-

Nature of Code -Processingではじめる自然現象のシミュレーション-

ラズパイ

Raspberry Pi3 Model Bが欲しいです.

強いて言えば,Raspberry Pi Mouse V2が欲しい.

Raspberry Pi Mouse V2 フルキット

Raspberry Pi Mouse V2 フルキット

物欲が止まりません.

統計学

仕事で使う必要があったので,

以下の本で勉強しました.

挫折しない統計学入門 数学苦手意識を克服する

挫折しない統計学入門 数学苦手意識を克服する

状況に応じてどの検定を使うべきか少し分かったので,

とても勉強になりました.

ただ分散分析に関しては正直まだ分かりません.

言語処理100本ノック 2015 09

  • 問題

09. Typoglycemia
スペースで区切られた単語列に対して,各単語の先頭と末尾の文字は残し,それ以外の文字の順序をランダムに並び替えるプログラムを作成せよ.ただし,長さが4以下の単語は並び替えないこととする.適当な英語の文(例えば"I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind .")を与え,その実行結果を確認せよ.

  • 解答例
import random

def wordShuffle(word):
    if len(word) <= 4:
        return word
    shuffleWord = list(word)[1:-1]
    random.shuffle(shuffleWord)
    return word[0] + ''.join(shuffleWord) + word[-1]

def typoglycemia(str):
    wordList = []
    for word in str.split():
        wordList.append(wordShuffle(word))
    return wordList

str = "I couldn't believe that I could actually understand what I was reading : the phenomenal power of the human mind ."
print(' '.join(typoglycemia(str)));
  • コメント

これまでの総まとめを感じた問題でした.
random.shuffleで簡単にランダムに並び替えられるので,
とても楽ですね.

2章もそのうち解こうと思います.

言語処理100本ノック 2015 08

  • 問題

08. 暗号文
与えられた文字列の各文字を,以下の仕様で変換する関数cipherを実装せよ.
英小文字ならば(219 - 文字コード)の文字に置換
その他の文字はそのまま出力
この関数を用い,英語のメッセージを暗号化・復号化せよ.

  • 解答例
def cipher(str):
    decode = ""
    for i in range(len(str)):
        decode += chr(219 - ord(str[i])) if str[i].islower() else str[i]
    return decode

print(cipher("irk low hob hold holy horn glow grog all"))
print(cipher(cipher("irk low hob hold holy horn glow grog all")))
  • コメント

「chr」と「ord」の使い方を学びました.

言語処理100本ノック 2015 07

  • 問題

07. テンプレートによる文生成
引数x, y, zを受け取り「x時のyはz」という文字列を返す関数を実装せよ.さらに,x=12, y="気温", z=22.4として,実行結果を確認せよ.

  • 解答例
def template(a, b ,c):
    return str(a) + "時の" + str(b) + "は" + str(c)

x = 12
y = "気温"
z = 22.4

print(template(x, y ,z))
  • コメント

一番簡単だった気がします.
ただメソッド引数に型名を書く必要が無いので,
他人にプログラムを共有するときは,
きちんとコメントを書く必要があります.

言語処理100本ノック 2015 06

  • 問題

06. 集合
"paraparaparadise"と"paragraph"に含まれる文字bi-gramの集合を,それぞれ, XとYとして求め,XとYの和集合,積集合,差集合を求めよ.さらに,'se'というbi-gramがXおよびYに含まれるかどうかを調べよ.

  • 解答例
def ngram(n, sentence):
    gram = []
    for i in range(len(sentence) - n + 1):
        gram.append(''.join(sentence[i:i + n]))
    return gram

str1 = "paraparaparadise"
str2 = "paragraph"

setX = set(ngram(2, str1))
setY = set(ngram(2, str2))

# 集合X・Y
print(setX)
print(setY)

# 和集合 積集合 差集合
print(setX.union(setY))
print(setX.intersection(setY))
print(setX.difference(setY))

# 集合に含まれているかどうか
print("se" in setX)
print("se" in setY)
  • コメント

問題05で作成したメソッドを利用しています.
リストから集合に変換することで,
重複している中身を消すことができるので便利です.