0

I am trying to write a function eliminate that takes a string and 2 optional arguments. The first optional argument (bad_characters) takes a letter and the third argument (case_sensitive) takes a Boolean value. The function should take a string s and remove all instances of bad_characters. If case_sensitive is true, then the function should act case sensitive. If false, then it does not need to. This is what I have so far.

def eliminate(s,bad_characters = [],case_sensitive = True or False):
    ''' Takes a string s and returns the string with all bad_characters
    removed. If case_sensitive is True, then the function will only
    remove uppercase letters if there are uppercase letters in
    bad_characters.
    String --> String'''
    while True:
        if case_sensitive = False:
            for character in s:
                if bad_characters == character:
                    newlist = s.replace(bad_characters,'')
                    return newlist
        break

I am having a hard time figuring out how to make the function remove upper case letters if needed. The function should also work if bad_characters is a list, tuple, or string.

Assem
  • 11,574
  • 5
  • 59
  • 97
Brett
  • 183
  • 3
  • 9
  • 1
    `True or False` == `True`. Is this your intention? – user Apr 06 '15 at 12:00
  • Why does `case_sensitive` remove only uppercase letters? this is odd. Perhaps name it `uppercase_only`? – Reut Sharabani Apr 06 '15 at 12:07
  • Also, try in the python console the following: `'A' == 'a'`. – user Apr 06 '15 at 12:09
  • 1
    Slightly off-topic, but never put an empty array `[]` as a default value in a python function or method. Instead use `bad_characters = None; ... if bad_characters is None: bad_characters=[]`. See: http://stackoverflow.com/questions/1132941/least-astonishment-in-python-the-mutable-default-argument – Dov Grobgeld Apr 06 '15 at 12:09
  • @DovGrobgeld you can do `bad_characters = bad_characters or []` since the arguments are not expected to be mutated anyway (so empty list passed in can be overrided by a different empty list). – Reut Sharabani Apr 06 '15 at 12:12

5 Answers5

2

Your problem is miss understanding about str.replace because it just replace one character so you need to loop over bad_characters and remove them one by one.

SO instead of using == you can just use in operand,for check the membership, and then remove character from your string :

def eliminate(s,bad_characters = '',case_sensitive = False):
    ''' Takes a string s and returns the string with all bad_characters
        removed. If case_sensitive is True, then the function will only
        remove uppercase letters if there are uppercase letters in
        bad_characters.
        String --> String'''
    if case_sensitive == False:
        for character in s:
            if  character in bad_characters:
                s = s.replace(character,'')
        return s

And as a more pythonic way for remove a special characters from string you can use str.translate method :

s.translate(None,bad_characters)
Mazdak
  • 105,000
  • 18
  • 159
  • 188
0

You can use a very handy str.swapcase function here. Please note that the below code overrides bad_characters for clarity, but you can easily modify it to keep it unchanged.

def eliminate(s, bad_characters, case_sensitive):    
    if not case_sensitive:
        bad_characters += map(str.swapcase, bad_characters)
    return ''.join([c for c in list(s) if c not in bad_characters])

print eliminate('AaBb', ['a'], False)    
print eliminate('AaBb', ['a'], True)    
print eliminate('AaBb', ['A'], True) 

Bb
ABb
aBb
pkacprzak
  • 5,537
  • 1
  • 17
  • 37
0
a = 'AbCdEfG'
print(''.join([x[0] for x in zip(a, a.upper()) if x[0] != x[1]]))
# bdf
Pavel Patrin
  • 1,630
  • 1
  • 19
  • 33
  • This is not readable, probably hard to maintain if changes will be needed, and it doesn't relate to the argument pass in (`case_sensitive`). – Reut Sharabani Apr 06 '15 at 12:15
0

You may use the string.translate(python2).

string.translate(s, table[, deletechars])

Delete all characters from s that are in deletechars (if present), and then translate the characters using table, which must be a 256-character string giving the translation for each character value, indexed by its ordinal. If table is None, then only the character deletion step is performed.

Other mistakes in the code:

  • Initialize case_sensitive only with True. True or False is True.
  • While True then break adds nothing. Remove it.
  • if case_sensitive = False: is an assignment while you need a comparison ==. case_sensitive is a boolean, so just use it is if case_sensitive or if not case_sensitive.
  • bad_characters == character is wrong. You cant compare a list to a character. use character in bad_characters instead.

Here is the whole solution with corrections:

def eliminate(s, bad_characters = [], case_sensitive = True):
    if case_sensitive: #True
        return s.translate(None, "".join(bad_characters))
    else: 
        s = s.translate(None, "".join(bad_characters).lower()) 
        s = s.translate(None, "".join(bad_characters).upper())
        return s
Community
  • 1
  • 1
Assem
  • 11,574
  • 5
  • 59
  • 97
-2
def eliminate(s, bad_characters = [], case_sensitive=True):
    for character in bad_characters:
        if not case_sensitive:
            s = s.replace(character.lower(),'')
            s = s.replace(character.upper(),'')
        else:
            s = s.replace(character,'')
    return s
Maks Skorokhod
  • 615
  • 1
  • 7
  • 14