0

I am taking a DS class using Python where they asked the me to fix the next function. Since I am learning programming in Python parallel to take this class, I am kind of lost any help. Will be appreciated it!

split_title_and_name

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):
    return person.split()[0] + ' ' + person.split()[-1]

#option 1
for person in people:
    print(split_title_and_name(person) == (lambda person:???))

#option 2
#list(map(split_title_and_name, people)) == list(map(???))
4
  • 1
    why do you think you need a lambda here? what exactly is being asked? what is the topic of the class? Commented Oct 27, 2016 at 17:45
  • 3
    What should be the result when it is fixed? Commented Oct 27, 2016 at 17:46
  • 2
    Wait so what's the problem/question? Commented Oct 27, 2016 at 17:47
  • 1
    Fix? You haven't explained what's wrong about it! Commented Oct 27, 2016 at 18:03

3 Answers 3

1

Based on the name of the function, I think you want this:

>>> people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']
>>> def split_title_and_name(people_list):
...     return [p.split('. ') for p in people_list]
...                    # ^ Assuming title will always be followed by dot '.',
                       # There will be only one '.' dot in the sample string

>>> split_title_and_name(people)
[['Dr', 'Christopher Brooks'],
#  ^     ^
# Title  Name 
 ['Dr', 'Kevyn Collins-Thompson'], 
 ['Dr', 'VG Vinod Vydiswaran'], 
 ['Dr', 'Daniel Romero']]

Note: And definitely you do not need lambda over here. It is not needed here in any context.

Sign up to request clarification or add additional context in comments.

8 Comments

Nice answer but what if the person's name contains a . like Dr. Bob J. Lee?
You are right. It would be a minor change to fix that. But this is entirely dependent on the data user intends to pass. What if there is no title? There will always be what ifs. Sample examples from user will be helpful here. I am assuming the list he mentioned is the entire sample set :)
Splitting at the first instance of a space should do that. If you want to verify a title, there are a few things you can do. The easiest seems to be checking if the instance of a space is preceded by a period (which is what you're doing). That would be a good indicator. However, this is of course not reliable. Another way would be to have a whitelist of possible values for titles and check against those.
Isn't this a moot discussion if we don't know what is intended? Maybe they wanted only the last name intentionally?
@Goodies: I liked the idea of making the whitelist of all know titles. And it is not about fixing, the comment. You can fix anything as far as you know what to fix. As I said, there will always be possibility of what if. Until and unless user mentions otherwise, I am keeping this answer as per the use case (personally I do not believe in complicating logic based on ifs)
|
0

list(map(split_title_and_name, people)) == list(map(lambda person: person.split()[0] + ' ' + person.split()[-1], people))

1 Comment

give some explanation and place your code in right coding format
0

THIS WORKS(:

people = ['Dr. Christopher Brooks', 'Dr. Kevyn Collins-Thompson', 'Dr. VG Vinod Vydiswaran', 'Dr. Daniel Romero']

def split_title_and_name(person):
    title = person.split()[0]
    lastname = person.split()[-1]
    return '{} {}'.format(title, lastname)

list(map(split_title_and_name, people))

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.