0
Write a function called remove_duplicates which will take one argument called string
This string input will only have characters between a-z The function should remove all repeated characters in the string and return a tuple with two values: Can some one please tell me what is wrong with my code? def remove_duplicates(string): string = string.lower() k = set(string) x = list(string) value = len(x) - len(k) if value == 0: z =list(k) z.sort() result = "".join(z),value print(tuple(result)) else: z = list(k) z.sort() result = "".join(z),value print(tuple(result))
7 Answers
+ 7
You can remove the 'if' and it will still work:
def remove_duplicates(string):
string = string.lower()
k = set(string)
x = list(string)
value = len(x) - len(k)
z =list(k)
z.sort()
result = "".join(z),value
print(tuple(result))
+ 7
I think the problem is that the version of remove_duplicates you had before,
doesn''t return a value, but you are expecting it to return a tuple.
You was only printing the tuple. Instead of a print statement use:
return result
in the end of the function.
compare to my version:
https://code.sololearn.com/cET9m1IODOg0/?ref=app
+ 6
question: should the string returned in the tuple be sorted?
If yes. Then the function works. But I see no need for using 'if' in the function,
because it does the same thing if value is equal to zero or not.
0
yes the string returned is the one without the duplicates
if no use for using "if" what should i use then
0
gfgh
0
thank you Cruz it works though i get a result with an error i cant read if you could help:
{"finished": true, "success": [], "passed": false, "started": true, "failures": [{"failedSpecNumber": 1, "fullName": "test_output_1", "failedExpectations": [{"message": "Failure in line 15, in test_output_1\n self.assertIsInstance(self.result1, tuple, msg='Incorrect output type')\nAssertionError: Incorrect output type\n"}]}, {"failedSpecNumber": 2, "fullName": "test_output_2", "failedExpectations": [{"message": "Failure in line 19, in test_output_2\n self.assertIsInstance(self.result2, tuple, msg='Incorrect output type')\nAssertionError: Incorrect output type\n"}]}, {"failedSpecNumber": 3, "fullName": "test_output_3", "failedExpectations": [{"message": "Failure in line 23, in test_output_3\n self.assertIsInstance(self.result3, tuple, msg='Incorrect output type')\nAssertionError: Incorrect output type\n"}]}], "specs": {"count": 3, "pendingCount": 0, "time": "0.000117"}}
('abc', 5)
('a', 0)
('aehlstx', 2)
('abc', 5)
('a', 0)
('aehlstx', 2)
('abc', 5)
('a', 0)
('aehlstx', 2)
after the test on
import unittest
class RemoveDuplicatesTestCases(unittest.TestCase):
def setUp(self):
self.result1 = remove_duplicates('aaabbbac')
self.result2 = remove_duplicates('a')
self.result3 = remove_duplicates('thelexash')
def test_output_1(self):
self.assertIsInstance(self.result1, tuple, msg='Incorrect output type')
self.assertEqual(self.result1, ('abc', 5), msg='Incorrect output')
def test_output_2(self):
self.assertIsInstance(self.result2, tuple, msg='Incorrect output type')
self.assertEqual(self.result2, ('a', 0), msg='Incorrect output')
def test_output_3(self):
self.assertIsInstance(self.result3, tuple, msg='Incorrect output type')
self.assertEqual(self.result3, ('aehlstx', 2), msg='Incorrect output')
0
Cruz your the man :)