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:

A new string with only unique, sorted characters.

The total number of duplicates dropped.

For example:

remove_duplicates('aaabbbac') => ('abc', 5)

remove_duplicates('a') => ('a', 0)

remove_duplicates('thelexash') => ('aehlstx', 2)

Here's my solution, and I'm new to python:

string = raw_input("Please enter a string...")

def remove_duplicates(string):
  string = set(string)
  if only_letters(string):
    return (string, string.length)
  else:
    print "Please provide only alphabets"

remove_duplicates(string)

What might I be doing wrongly? This is the error I get below

THERE IS AN ERROR/BUG IN YOUR CODE Results: /bin/sh: 1: python/nose2/bin/nose2: not found

Thanks.

EdChum
  • 376,765
  • 198
  • 813
  • 562
Daniel Oyama
  • 1
  • 1
  • 1
  • 2
    That sounds like an error in the verification scaffold, not your code. – Ignacio Vazquez-Abrams Mar 16 '17 at 16:27
  • 1
    I imagine the testing part can fail in obscure ways if your code is not valid Python : it's `len(string)` not `string.length`. You should test locally before sending a submission to see this kind of errors. – polku Mar 16 '17 at 16:33
  • Refer here http://stackoverflow.com/questions/9841303/removing-duplicate-characters-from-a-string . In your code you did not define "only_letters" – manvi77 Mar 16 '17 at 16:38

3 Answers3

2

This works just fine. The output should be sorted.

def remove_duplicates(string):
   new_string = "".join(sorted(set(string)))
   if new_string:
     return (new_string, len(string)-len(new_string))
   else:
     print "Please provide only alphabets"

No need to include this:

string = raw_input("Please enter a string...")

remove_duplicates(string)
0

As the order is not important, you can use

string = raw_input("Please enter a string...")

def remove_duplicates(string):
   new_string = "".join(set(string))
   if new_string:
     return (new_string, len(string)-len(new_string))
   else:
     print "Please provide only alphabets"

 remove_duplicates(string)

Please enter a string...aaabbbac
Out[27]: ('acb', 5)

set() will create a set of unique letters in the string, and "".join() will join the letters back to a string in arbitrary order.

manvi77
  • 536
  • 1
  • 5
  • 15
0

was receiving the same error from a test I am working on, I feel the error is not from your end but the tester's end

Uche
  • 1