Python Question Bank

Source: Internet
Author: User

date:2018-05-08

1. Given:an array containing hashes of names

RETURN:A string formatted as a list of names separated by commas except for the last and names, which should be separated by an ampersand.

Example:

namelist([ {‘name‘: ‘Bart‘}, {‘name‘: ‘Lisa‘}, {‘name‘: ‘Maggie‘} ])# returns ‘Bart, Lisa & Maggie‘namelist([ {‘name‘: ‘Bart‘}, {‘name‘: ‘Lisa‘} ])# returns ‘Bart & Lisa‘namelist([ {‘name‘: ‘Bart‘} ])# returns ‘Bart‘namelist([])# returns ‘‘

Best Practices:

def namelist(names):    if len(names) > 1:        return ‘{} & {}‘.format(‘, ‘.join(name[‘name‘] for name in names[:-1]),                                 names[-1][‘name‘])    elif names:        return names[0][‘name‘]    else:        return ‘‘

My Solutions:

def namelist(names):    #your code here    if len(names) > 1:        first_name = ‘, ‘.join(tmp_name[‘name‘] for tmp_name in names[:-1])        last_name = names[-1][‘name‘]        print(first_name)        print(last_name)        return first_name + ‘ & ‘ + last_name    elif names:        return names[0][‘name‘]    else:        return ‘‘

Python Question Bank

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.