Practical Programming
An Introduction to Computer Science
Using Python 3
(Pragmatic Programmers)
(Pragmatic Bookshelf)
Paul Gries (著) Jennifer Campbell (著)
Jason Montojo (著) Lynn Beighley (編集)
開発環境
- OS X Mavericks - Apple(OS)
- Emacs (CUI)、BBEdit - Bare Bones Software, Inc. (GUI) (Text Editor)
- Python 3.4 (プログラミング言語)
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) 1.を解いてみる。
11.8(Exercises) 1.
コード(BBEdit)
sample1.py
#!/usr/bin/env python3 #-*- coding: utf-8 -*- import random def findDups(nums): s1 = set() s2 = set() for num in nums: if num in s1: s2.add(num) else: s1.add(num) return s2 nums = [random.randrange(10) for x in range(20)] print(nums) print(findDups(nums))
入出力結果(Terminal, IPython)
$ ./sample1.py [8, 8, 0, 3, 8, 3, 5, 9, 5, 9, 9, 3, 1, 9, 3, 2, 9, 9, 9, 3] {8, 9, 3, 5} $ ./sample1.py [2, 3, 7, 6, 6, 9, 2, 0, 8, 3, 9, 7, 5, 4, 5, 0, 0, 3, 9, 3] {0, 2, 3, 5, 6, 7, 9} $
0 コメント:
コメントを投稿