2014年10月13日月曜日

開発環境

Practical Programming: An Introduction to Computer Science Using Python 3 (Pragmatic Programmers) (Paul Gries (著)、Jennifer Campbell (著)、Jason Montojo (著)、Lynn Beighley (編集)、Pragmatic Bookshelf)のChapter 11(Storing Data Using Other Collection Types)、11.8(Exercises) 11-a, b, c.を解いてみる。

11.8(Exercises) 11-a, b, c.

コード(BBEdit)

sample11.py

#!/usr/bin/env python3
#-*- coding: utf-8 -*-

# a.
def sparseAdd(v1, v2):
    result = {}
    s1 = set(v1.keys())
    s2 = set(v2.keys())
    
    for k in s1.intersection(s2):
        result[k] = v1[k] + v2[k]
    for k in s1.symmetric_difference(s2):
        if k in s1:
            result[k] = v1[k]
        else:
            result[k] = v2[k]
            
    return result
        
# b.
def sparseDot(v1, v2):
    result = 0
    s1 = set(v1.keys())
    s2 = set(v2.keys())
    
    for k in s1.intersection(s2):
        result += v1[k] * v2[k]
        
    return result
    
if __name__ == '__main__':
    d1 = {0:1, 1:2, 2:3}
    d2 = {0:4, 1:5, 2:6}
    d3 = {0:5, 1:7, 2:9}
    d4 = {0:1, 6:3}
    dicts = [d1, d2, d3, d4]
    for x in dicts:
        for y in dicts:
            print('vector1={0}, vector2={1}'.format(x, y))
            print('vector1 + vector2 = {0}'.format(sparseAdd(x, y)))
            print('vector1・vector2 = {0}'.format(sparseDot(x, y)))

入出力結果(Terminal, IPython)

$ ./sample11.py
vector1={0: 1, 1: 2, 2: 3}, vector2={0: 1, 1: 2, 2: 3}
vector1 + vector2 = {0: 2, 1: 4, 2: 6}
vector1・vector2 = 14
vector1={0: 1, 1: 2, 2: 3}, vector2={0: 4, 1: 5, 2: 6}
vector1 + vector2 = {0: 5, 1: 7, 2: 9}
vector1・vector2 = 32
vector1={0: 1, 1: 2, 2: 3}, vector2={0: 5, 1: 7, 2: 9}
vector1 + vector2 = {0: 6, 1: 9, 2: 12}
vector1・vector2 = 46
vector1={0: 1, 1: 2, 2: 3}, vector2={0: 1, 6: 3}
vector1 + vector2 = {0: 2, 1: 2, 2: 3, 6: 3}
vector1・vector2 = 1
vector1={0: 4, 1: 5, 2: 6}, vector2={0: 1, 1: 2, 2: 3}
vector1 + vector2 = {0: 5, 1: 7, 2: 9}
vector1・vector2 = 32
vector1={0: 4, 1: 5, 2: 6}, vector2={0: 4, 1: 5, 2: 6}
vector1 + vector2 = {0: 8, 1: 10, 2: 12}
vector1・vector2 = 77
vector1={0: 4, 1: 5, 2: 6}, vector2={0: 5, 1: 7, 2: 9}
vector1 + vector2 = {0: 9, 1: 12, 2: 15}
vector1・vector2 = 109
vector1={0: 4, 1: 5, 2: 6}, vector2={0: 1, 6: 3}
vector1 + vector2 = {0: 5, 1: 5, 2: 6, 6: 3}
vector1・vector2 = 4
vector1={0: 5, 1: 7, 2: 9}, vector2={0: 1, 1: 2, 2: 3}
vector1 + vector2 = {0: 6, 1: 9, 2: 12}
vector1・vector2 = 46
vector1={0: 5, 1: 7, 2: 9}, vector2={0: 4, 1: 5, 2: 6}
vector1 + vector2 = {0: 9, 1: 12, 2: 15}
vector1・vector2 = 109
vector1={0: 5, 1: 7, 2: 9}, vector2={0: 5, 1: 7, 2: 9}
vector1 + vector2 = {0: 10, 1: 14, 2: 18}
vector1・vector2 = 155
vector1={0: 5, 1: 7, 2: 9}, vector2={0: 1, 6: 3}
vector1 + vector2 = {0: 6, 1: 7, 2: 9, 6: 3}
vector1・vector2 = 5
vector1={0: 1, 6: 3}, vector2={0: 1, 1: 2, 2: 3}
vector1 + vector2 = {0: 2, 1: 2, 2: 3, 6: 3}
vector1・vector2 = 1
vector1={0: 1, 6: 3}, vector2={0: 4, 1: 5, 2: 6}
vector1 + vector2 = {0: 5, 1: 5, 2: 6, 6: 3}
vector1・vector2 = 4
vector1={0: 1, 6: 3}, vector2={0: 5, 1: 7, 2: 9}
vector1 + vector2 = {0: 6, 1: 7, 2: 9, 6: 3}
vector1・vector2 = 5
vector1={0: 1, 6: 3}, vector2={0: 1, 6: 3}
vector1 + vector2 = {0: 2, 6: 6}
vector1・vector2 = 10
$

0 コメント:

コメントを投稿