diff --git a/advanced-iterators.html b/advanced-iterators.html index 17a4770..f17b1e9 100755 --- a/advanced-iterators.html +++ b/advanced-iterators.html @@ -50,7 +50,7 @@ import itertools def solve(puzzle): words = re.findall('[A-Z]+', puzzle.upper()) - unique_characters = {c for c in ''.join(words)} + unique_characters = set(join(words)) assert len(unique_characters) <= 10 first_letters = {word[0] for word in words} n = len(first_letters) @@ -127,30 +127,30 @@ if __name__ == '__main__':
Set comprehensions make it trivial to find the unique items in a sequence. [FIXME-not sure if I’m going to cover set comprehensions in an earlier chapter; if not, this is certainly an abrupt and inadequate introduction to the topic.] +
Sets make it trivial to find the unique items in a sequence.
>>> a_list = ['a', 'c', 'b', 'a', 'd', 'b']
->>> {c for c in a_list} ①
+>>> set(a_list) ①
{'a', 'c', 'b', 'd'}
>>> a_string = 'EAST IS EAST'
->>> {c for c in a_string} ②
+>>> set(a_string) ②
{'A', ' ', 'E', 'I', 'S', 'T'}
>>> words = ['SEND', 'MORE', 'MONEY']
->>> ''.join(words) ③
+>>> ''.join(words) ③
'SENDMOREMONEY'
->>> {c for c in ''.join(words)} ④
+>>> set(''.join(words)) ④
{'E', 'D', 'M', 'O', 'N', 'S', 'R', 'Y'}
for loop. Take the first item from the list, put it in the set. Second. Third. Fourth — wait, that’s in the set already, so it only gets listed once. Fifth. Sixth — again, a duplicate, so it only gets listed once. The end result? All the unique items in the original list, without any duplicates. The original list doesn’t even need to be sorted first.
+set() function will return a set of unique strings from the list. This makes sense if you think of it like a for loop. Take the first item from the list, put it in the set. Second. Third. Fourth — wait, that’s in the set already, so it only gets listed once, because Python sets don’t allow duplicates. Fifth. Sixth — again, a duplicate, so it only gets listed once. The end result? All the unique items in the original list, without any duplicates. The original list doesn’t even need to be sorted first.
''.join(a_list) concatenates all the strings together into one.
-The alphametics solver uses this technique to get a list of all the unique characters in the puzzle. -
unique_characters = {c for c in ''.join(words)}
+unique_characters = set(''.join(words))
This list is later used to assign digits to characters as the solver iterates through the possible solutions. diff --git a/diveintopython3.org b/diveintopython3.org index 274626a..283cc02 100755 --- a/diveintopython3.org +++ b/diveintopython3.org @@ -7,6 +7,7 @@ * Closures & Generators * Classes & Iterators * TODO 2nd draft Advanced Iterators + SCHEDULED: <2009-07-15 Wed> * TODO 2nd draft Unit Testing * TODO 1st draft Advanced Unit Testing * TODO 2nd draft Refactoring diff --git a/examples/alphametics.py b/examples/alphametics.py old mode 100644 new mode 100755 index fbaae83..90840df --- a/examples/alphametics.py +++ b/examples/alphametics.py @@ -9,7 +9,7 @@ import itertools def solve(puzzle): words = re.findall('[A-Z]+', puzzle.upper()) - unique_characters = {c for c in ''.join(words)} + unique_characters = set(''.join(words)) assert len(unique_characters) <= 10 first_letters = {word[0] for word in words} n = len(first_letters)