2014年10月3日金曜日

開発環境

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) 2.を解いてみる。

11.8(Exercises) 2.

コード(BBEdit)

sample2.py

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

def matingPairs(males, females):
    pairs = set()

    for i in range(len(males)):
        male = males.pop()
        female = females.pop()
        pair = (male, female)
        pairs.add(pair)

    return pairs
    

print(matingPairs({'a', 'b', 'c', 'd', 'e'}, {'A', 'B', 'C', 'D', 'E'}))

入出力結果(Terminal, IPython)

$ ./sample2.py
{('a', 'C'), ('c', 'A'), ('b', 'E'), ('e', 'D'), ('d', 'B')}
$

0 コメント:

コメントを投稿