Modify set tests to print sorted sets directly

instead of creating temporary lists and sorting those in-place
This commit is contained in:
Andrew Scheller 2014-04-07 05:00:03 +01:00
parent 2bfd2dc770
commit 1ff6011abc
7 changed files with 28 additions and 53 deletions

View File

@ -1,5 +1,3 @@
s = {1, 2, 3, 4} s = {1, 2, 3, 4}
print(s.add(5)) print(s.add(5))
l = list(s) print(sorted(s))
l.sort()
print(l)

View File

@ -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}] sets = [set(), {1}, {1, 2}, {1, 2, 3}, {2, 3}, {2, 3, 5}, {5}, {7}]
for s in sets: for s in sets:
for t in sets: for t in sets:
print(r(s), '|', r(t), '=', r(s | t)) print(sorted(s), '|', sorted(t), '=', sorted(s | t))
print(r(s), '^', r(t), '=', r(s ^ t)) print(sorted(s), '^', sorted(t), '=', sorted(s ^ t))
print(r(s), '&', r(t), '=', r(s & t)) print(sorted(s), '&', sorted(t), '=', sorted(s & t))
print(r(s), '-', r(t), '=', r(s - t)) print(sorted(s), '-', sorted(t), '=', sorted(s - t))
u = s.copy() u = s.copy()
u |= t u |= t
print(r(s), "|=", r(t), '-->', r(u)) print(sorted(s), "|=", sorted(t), '-->', sorted(u))
u = s.copy() u = s.copy()
u ^= t u ^= t
print(r(s), "^=", r(t), '-->', r(u)) print(sorted(s), "^=", sorted(t), '-->', sorted(u))
u = s.copy() u = s.copy()
u &= t u &= t
print(r(s), "&=", r(t), "-->", r(u)) print(sorted(s), "&=", sorted(t), "-->", sorted(u))
u = s.copy() u = s.copy()
u -= t u -= t
print(r(s), "-=", r(t), "-->", r(u)) print(sorted(s), "-=", sorted(t), "-->", sorted(u))
print(r(s), '==', r(t), '=', s == t) print(sorted(s), '==', sorted(t), '=', s == t)
print(r(s), '!=', r(t), '=', s != t) print(sorted(s), '!=', sorted(t), '=', s != t)
print(r(s), '>', r(t), '=', s > t) print(sorted(s), '>', sorted(t), '=', s > t)
print(r(s), '>=', r(t), '=', s >= t) print(sorted(s), '>=', sorted(t), '=', s >= t)
print(r(s), '<', r(t), '=', s < t) print(sorted(s), '<', sorted(t), '=', s < t)
print(r(s), '<=', r(t), '=', s <= t) print(sorted(s), '<=', sorted(t), '=', s <= t)

View File

@ -3,6 +3,4 @@ t = s.copy()
s.add(5) s.add(5)
t.add(7) t.add(7)
for i in s, t: for i in s, t:
l = list(i) print(sorted(i))
l.sort()
print(l)

View File

@ -1,8 +1,3 @@
def report(s):
l = list(s)
l.sort()
print(l)
l = [1, 2, 3, 4] l = [1, 2, 3, 4]
s = set(l) s = set(l)
outs = [s.difference(), outs = [s.difference(),
@ -10,12 +5,12 @@ outs = [s.difference(),
s.difference({1}, [1, 2]), s.difference({1}, [1, 2]),
s.difference({1}, {1, 2}, {2, 3})] s.difference({1}, {1, 2}, {2, 3})]
for out in outs: for out in outs:
report(out) print(sorted(out))
s = set(l) s = set(l)
print(s.difference_update()) print(s.difference_update())
report(s) print(sorted(s))
print(s.difference_update({1})) print(s.difference_update({1}))
report(s) print(sorted(s))
print(s.difference_update({1}, [2])) print(s.difference_update({1}, [2]))
report(s) print(sorted(s))

View File

@ -1,12 +1,7 @@
def report(s):
l = list(s)
l.sort()
print(l)
s = {1, 2, 3, 4} s = {1, 2, 3, 4}
report(s) print(sorted(s))
report(s.intersection({1, 3})) print(sorted(s.intersection({1, 3})))
report(s.intersection([3, 4])) print(sorted(s.intersection([3, 4])))
print(s.intersection_update([1])) print(s.intersection_update([1]))
report(s) print(sorted(s))

View File

@ -2,6 +2,4 @@ print(sorted({1,2}.symmetric_difference({2,3})))
print(sorted({1,2}.symmetric_difference([2,3]))) print(sorted({1,2}.symmetric_difference([2,3])))
s = {1,2} s = {1,2}
print(s.symmetric_difference_update({2,3})) print(s.symmetric_difference_update({2,3}))
l = list(s) print(sorted(s))
l.sort()
print(l)

View File

@ -1,12 +1,7 @@
def report(s):
l = list(s)
l.sort()
print(l)
s = {1} s = {1}
s.update() s.update()
report(s) print(s)
s.update([2]) s.update([2])
report(s) print(sorted(s))
s.update([1,3], [2,2,4]) s.update([1,3], [2,2,4])
report(s) print(sorted(s))