2

I need to satisfy this test:

def test_capitalize_names():
    assert ["James"] == capitalize_names(["JAMES"])
    assert ["Harry", "Jack"] == capitalize_names(["HArry", "jack"])

I tried writing capitalize_names like so:

def capitalize_names(input):
    input = map(lambda s: s.capitalize(), input)
    li = map(lambda x: '"'+x+'"', input)
    return li

It is returning ['"James"'] but I want it to return ["James"] to satisfy the assert statements and I cannot use any control statements.

Karl Knechtel
  • 62,466
  • 11
  • 102
  • 153
  • 1
    Don't use Python built-in keywords and function names as your variable names. Change `input` to something else. Also, check out https://stackoverflow.com/questions/1801668/convert-a-python-list-with-strings-all-to-lowercase-or-uppercase and many other similar questions is SO – Mazdak Jun 23 '18 at 06:05
  • You can use `str.capitalize` instead of `lambda s: s.capitalize()`. – DYZ Jun 23 '18 at 06:07
  • The line `li = map(lambda x: '"'+x+'"', my_input)` adds quotation marks to your capitalized string. Why are you surprised that the string has the quotation marks? Remove that line. From Python's point of view, `"` and `'` serve the same purpose. – DYZ Jun 23 '18 at 06:08
  • i cannot use str.capitalize() since it is a list –  Jun 23 '18 at 06:12
  • But please read my comment. I said: use `str.capitalize` instead of `lambda s: s.capitalize()`. Just that: `my_input = map(str.capitalize, my_input)`. It makes your code 40% faster. Not using `map` (use list comprehension instead) makes it another 20% faster. – DYZ Jun 23 '18 at 06:14
  • in list of strings ["james"] is not equal to ['james'] –  Jun 23 '18 at 06:19
  • i removed that line and made it str.capitalize but now i want the code to satisfy assert the statement please help and thnk you for the suggestions –  Jun 23 '18 at 06:24
  • Did you try `["james"]==['james']`? The two lists (and strings) are exactly the same. – DYZ Jun 23 '18 at 06:26
  • i tried but it is still showing an error –  Jun 23 '18 at 06:33
  • thank you for your suggestions –  Jun 23 '18 at 06:36
  • @marshmallow, for input like `['KEn watson']`, what do you expect? `['Ken Watson']` or `['Ken watson']`. – hygull Jun 23 '18 at 07:36
  • The **original problem** here was **caused by a typo**. Questions should not be edited to ask a new question - the accepted answer was based on the code that I have restored in this edit. OP then edited to include the new working code and inappropriately ask a new question about getting a result `["James"]` rather than `['James']` - however, this **does not make sense**, because those **are the same thing** (and the `assert` would succeed in this case). Either way, there is not a reproducible problem in this question, and it should be closed and deleted. – Karl Knechtel Mar 07 '23 at 21:23
  • For the question described in the title - which is **not what OP was asking** - we have many superior duplicates, anyway. – Karl Knechtel Mar 07 '23 at 21:23

3 Answers3

2

Try this:

def capitalize_names(input):
    return list(map(lambda s: s.capitalize(), input))

this is the efficient way of doing this.

Mehrdad Pedramfar
  • 10,941
  • 4
  • 38
  • 59
  • 4
    It is bad to use variable name as **input**. The statements like `m = input;` works perfect and gives ``. – hygull Jun 23 '18 at 06:50
1

The fastest way is:

def capitalize_names(inp):
    return [i.capitalize() for i in inp]
def test_capitalize_names():
    assert ["James"] == capitalize_names(["JAMES"])
    assert ["Harry", "Jack"] == capitalize_names(["HArry", "jack"])
U13-Forward
  • 69,221
  • 14
  • 89
  • 114
1

A more succinct way: for example:

import string
def capitalize_names(lst_str):
    return map(string.capitalize, lst_str)

if __name__ == '__main__':
    print capitalize_names(["HArry", "jack", "JAMES"])
    # ['Harry', 'Jack', 'James']
Jayhello
  • 5,931
  • 3
  • 49
  • 56