Modify set tests to print sorted sets directly
instead of creating temporary lists and sorting those in-place
This commit is contained in:
parent
2bfd2dc770
commit
1ff6011abc
@ -1,5 +1,3 @@
|
||||
s = {1, 2, 3, 4}
|
||||
print(s.add(5))
|
||||
l = list(s)
|
||||
l.sort()
|
||||
print(l)
|
||||
print(sorted(s))
|
||||
|
@ -1,30 +1,26 @@
|
||||
def r(s):
|
||||
l = list(s)
|
||||
l.sort()
|
||||
return l
|
||||
sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
|
||||
for s in sets:
|
||||
for t in sets:
|
||||
print(r(s), '|', r(t), '=', r(s | t))
|
||||
print(r(s), '^', r(t), '=', r(s ^ t))
|
||||
print(r(s), '&', r(t), '=', r(s & t))
|
||||
print(r(s), '-', r(t), '=', r(s - t))
|
||||
print(sorted(s), '|', sorted(t), '=', sorted(s | t))
|
||||
print(sorted(s), '^', sorted(t), '=', sorted(s ^ t))
|
||||
print(sorted(s), '&', sorted(t), '=', sorted(s & t))
|
||||
print(sorted(s), '-', sorted(t), '=', sorted(s - t))
|
||||
u = s.copy()
|
||||
u |= t
|
||||
print(r(s), "|=", r(t), '-->', r(u))
|
||||
print(sorted(s), "|=", sorted(t), '-->', sorted(u))
|
||||
u = s.copy()
|
||||
u ^= t
|
||||
print(r(s), "^=", r(t), '-->', r(u))
|
||||
print(sorted(s), "^=", sorted(t), '-->', sorted(u))
|
||||
u = s.copy()
|
||||
u &= t
|
||||
print(r(s), "&=", r(t), "-->", r(u))
|
||||
print(sorted(s), "&=", sorted(t), "-->", sorted(u))
|
||||
u = s.copy()
|
||||
u -= t
|
||||
print(r(s), "-=", r(t), "-->", r(u))
|
||||
print(sorted(s), "-=", sorted(t), "-->", sorted(u))
|
||||
|
||||
print(r(s), '==', r(t), '=', s == t)
|
||||
print(r(s), '!=', r(t), '=', s != t)
|
||||
print(r(s), '>', r(t), '=', s > t)
|
||||
print(r(s), '>=', r(t), '=', s >= t)
|
||||
print(r(s), '<', r(t), '=', s < t)
|
||||
print(r(s), '<=', r(t), '=', s <= t)
|
||||
print(sorted(s), '==', sorted(t), '=', s == t)
|
||||
print(sorted(s), '!=', sorted(t), '=', s != t)
|
||||
print(sorted(s), '>', sorted(t), '=', s > t)
|
||||
print(sorted(s), '>=', sorted(t), '=', s >= t)
|
||||
print(sorted(s), '<', sorted(t), '=', s < t)
|
||||
print(sorted(s), '<=', sorted(t), '=', s <= t)
|
||||
|
@ -3,6 +3,4 @@ t = s.copy()
|
||||
s.add(5)
|
||||
t.add(7)
|
||||
for i in s, t:
|
||||
l = list(i)
|
||||
l.sort()
|
||||
print(l)
|
||||
print(sorted(i))
|
||||
|
@ -1,8 +1,3 @@
|
||||
def report(s):
|
||||
l = list(s)
|
||||
l.sort()
|
||||
print(l)
|
||||
|
||||
l = [1, 2, 3, 4]
|
||||
s = set(l)
|
||||
outs = [s.difference(),
|
||||
@ -10,12 +5,12 @@ outs = [s.difference(),
|
||||
s.difference({1}, [1, 2]),
|
||||
s.difference({1}, {1, 2}, {2, 3})]
|
||||
for out in outs:
|
||||
report(out)
|
||||
print(sorted(out))
|
||||
|
||||
s = set(l)
|
||||
print(s.difference_update())
|
||||
report(s)
|
||||
print(sorted(s))
|
||||
print(s.difference_update({1}))
|
||||
report(s)
|
||||
print(sorted(s))
|
||||
print(s.difference_update({1}, [2]))
|
||||
report(s)
|
||||
print(sorted(s))
|
||||
|
@ -1,12 +1,7 @@
|
||||
def report(s):
|
||||
l = list(s)
|
||||
l.sort()
|
||||
print(l)
|
||||
|
||||
s = {1, 2, 3, 4}
|
||||
report(s)
|
||||
report(s.intersection({1, 3}))
|
||||
report(s.intersection([3, 4]))
|
||||
print(sorted(s))
|
||||
print(sorted(s.intersection({1, 3})))
|
||||
print(sorted(s.intersection([3, 4])))
|
||||
|
||||
print(s.intersection_update([1]))
|
||||
report(s)
|
||||
print(sorted(s))
|
||||
|
@ -2,6 +2,4 @@ print(sorted({1,2}.symmetric_difference({2,3})))
|
||||
print(sorted({1,2}.symmetric_difference([2,3])))
|
||||
s = {1,2}
|
||||
print(s.symmetric_difference_update({2,3}))
|
||||
l = list(s)
|
||||
l.sort()
|
||||
print(l)
|
||||
print(sorted(s))
|
||||
|
@ -1,12 +1,7 @@
|
||||
def report(s):
|
||||
l = list(s)
|
||||
l.sort()
|
||||
print(l)
|
||||
|
||||
s = {1}
|
||||
s.update()
|
||||
report(s)
|
||||
print(s)
|
||||
s.update([2])
|
||||
report(s)
|
||||
print(sorted(s))
|
||||
s.update([1,3], [2,2,4])
|
||||
report(s)
|
||||
print(sorted(s))
|
||||
|
Loading…
Reference in New Issue
Block a user