I've been tasked to write a program in python to fit a given (and unchangeable) "driver" program which makes a Set class consisting of a single variable of a list called "members."
My problem is that output for the methods "has_subset()" and "intersect()" do not display properly. There are unnecessary parenthesis, commas, and apostrophes in the output.
Here is the set class:
class Set:
def __init__(self):
self.members = []
def add_element(self, integer):
if integer not in self.members:
self.members.append(integer)
def remove_element(self, integer):
while integer in self.members: self.members.remove(integer)
def remove_all(self):
self.members = []
def has_element(self, x):
while x in self.members: return True
return False
# probably doesnt work, __repr__
def __repr__(self):
if len(self.members) == 0:
return "{}"
return "{" + ", ".join(str(e) for e in self.members) + "}"
#Same as above, probably doesnt work
def __str__(self):
if len(self.members) == 0:
return "{}"
return "{" + ", ".join(str(e) for e in self.members) + "}"
def __add__(self, other):
counter = 0
while counter < len(other.members):
if other.members[counter] not in self.members:
self.members.append(other.members[counter])
counter = counter + 1
return self.members
def intersect(self, x):
counter = 0
answer = Set()
while counter < len(x.members):
if x.members[counter] in self.members: answer.members.append(x.members[counter])
counter = counter + 1
return answer
#No clue if this is what is intended
def has_subset(self, x):
counter = 0
while counter < len(x.members):
if x.members[counter] not in self.members: return False
counter = counter + 1
return True
Here is the driver file:
from Set import *
first = Set()
count = 0
while count < 10:
first.add_element(count)
count += 1
print(first)
second = Set()
count = 5
while count < 15:
second.add_element(count)
count += 1
print(second)
third = Set()
third.add_element(2)
third.add_element(1)
third.add_element(8)
third.add_element(5)
#Tests has_subset with a set that is a subset
if first.has_subset(third):
print(third, "is a subset of", first)
else:
print(third, "is not a subset of", first)
#Tests has_subset with a set that is not a subset
if second.has_subset(third):
print(third, "is a subset of", second)
else:
print(third, "is not a subset of", second)
#Tests overloaded +
fourth = first + second
print(first, "+", second, "=\n", fourth)
#Tests intersect
fifth = first.intersect(second)
print(fifth, "is the intersection of", first, "and\n", second)
And here is the output:
{0, 1, 2, 3, 4, 5, 6, 7, 8, 9}
{5, 6, 7, 8, 9, 10, 11, 12, 13, 14}
({2, 1, 8, 5}, 'is a subset of', {0, 1, 2, 3, 4, 5, 6, 7, 8, 9})
({2, 1, 8, 5}, 'is not a subset of', {5, 6, 7, 8, 9, 10, 11, 12, 13, 14})
({0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, '+', {5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, '=\n', [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14])
({5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 'is the intersection of', {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}, 'and\n', {5, 6, 7, 8, 9, 10, 11, 12, 13, 14})
Notice how the first two lines are properly formatted with no parenthesis, but once additional strings are combined, the unwanted punctuation comes into play. How do I create output by only editing the Set class to remove this unwanted output?
This has nothing to do with your Set class -- the problem is with how you're printing your content.
I suspect you're using Python 2.x?
If so, when you do print(third, "is a subset of", second)
, you're actually telling Python to print the tuple (which contains 3 elements). Python obliges, and prints the parens and commas since that's part of printing a tuple. print(...)
isn't a function in Python 2.
To fix this, you can either manually add together each piece:
print str(third) + ' is a subset of ' + str(second)
...or use string formatting:
print '{} is a subset of {}'.format(third, second)
...or add the line from __future__ import print_function
at the top of your driver file to include Python 3 printing semantics, which does turn print(...)
into a function and does what you want.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.