forked from huangsam/ultimate-python
- Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathset.py
More file actions
Latest commit
89 lines (70 loc) · 3.26 KB
/
Copy pathset.py
File metadata and controls
89 lines (70 loc) · 3.26 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
"""
Sets are an unordered collection of unique values that can be modified at
runtime. This module shows how sets are created, iterated, accessed,
extended and shortened.
"""
defmain() ->None:
# Let's define one `set` for starters
simple_set= {0, 1, 2}
# A set is dynamic like a `list` and `tuple`
simple_set.add(3)
simple_set.remove(0)
assertsimple_set== {1, 2, 3}
# Unlike a `list and `tuple`, it is not an ordered sequence as it
# does not allow duplicates to be added
for_inrange(5):
simple_set.add(0)
simple_set.add(4)
assertsimple_set== {0, 1, 2, 3, 4}
# Use `pop` return any random element from a set
random_element=simple_set.pop()
assertrandom_elementin {0, 1, 2, 3, 4}
assertrandom_elementnotinsimple_set
# Now let's define two new `set` collections
multiples_two=set()
multiples_four=set()
# Fill sensible values into the set using `add`
foriinrange(10):
multiples_two.add(i*2)
multiples_four.add(i*4)
# As we can see, both sets have similarities and differences
assertmultiples_two== {0, 2, 4, 6, 8, 10, 12, 14, 16, 18}
assertmultiples_four== {0, 4, 8, 12, 16, 20, 24, 28, 32, 36}
# We cannot decide in which order the numbers come out - so let's
# look for fundamental truths instead, such as divisibility against
# 2 and 4. We do this by checking whether the modulus of 2 and 4
# yields 0 (i.e. no remainder from performing a division). We can
# also use `&` to perform set intersection
multiples_common=multiples_two.intersection(multiples_four)
multiples_common_shorthand=multiples_two&multiples_four
fornumberinmultiples_common:
assertnumber%2==0andnumber%4==0
fornumberinmultiples_common_shorthand:
assertnumber%2==0andnumber%4==0
# We can compute exclusive multiples. We can also use `-` to perform
# set difference
multiples_two_exclusive=multiples_two.difference(multiples_four)
multiples_two_exclusive_shorthand=multiples_two-multiples_four
multiples_four_exclusive=multiples_four.difference(multiples_two)
assertlen(multiples_two_exclusive) >0
assertlen(multiples_four_exclusive) >0
assertlen(multiples_two_exclusive_shorthand) >0
# Numbers in this bracket are greater than 2 * 9 and less than 4 * 10
fornumberinmultiples_four_exclusive:
assert18<number<40
# By computing a set union against the two sets, we have all integers
# in this program. We can also use `|` to perform set union
multiples_all=multiples_two.union(multiples_four)
multiples_all_shorthand=multiples_two|multiples_four
# Check if set A is a subset of set B
assertmultiples_four_exclusive.issubset(multiples_four)
assertmultiples_four.issubset(multiples_all)
# Check if set A is a subset and superset of itself
assertmultiples_all.issubset(multiples_all)
assertmultiples_all.issuperset(multiples_all)
assertmultiples_all_shorthand.issuperset(multiples_all_shorthand)
# Check if set A is a superset of set B
assertmultiples_all.issuperset(multiples_two)
assertmultiples_two.issuperset(multiples_two_exclusive)
if__name__=="__main__":
main()