Question

PLEASE DO THIS WITH PYTHON!

Assume the example of a name list for all problems below (use it as a line in your code) e.g. nameist -llJulia Truong.Chen WuvJeb Castro,Ron Kennedy, X YI 1. Write a recursive function convertNames0 to accept the initial nameList and return a new list containing only first names. Provide function call and print the resulting list after the function had returned the results. (solution similar to problem on the slide 13 for Chapter 11) Result should be: lJulia.Chen,Jeb,Ron,Xl 2. Write a recursive function chooseNames0 to accept nameList and return a new list only with names starting with J Provide function call and print the resulting list after the function had returned the results. (solution similar problem on slide 15 for Chapter 11) Result should be: [Julia Truong Jeb Castro] 3. Write a recursive function countNames0 to accept nameList and return a number (count) of first names starting with J. Provide function call and print the result after the function had returned the result. (solution similar to problem on slide 17 for Chapter 11) Result should be: 2Please do this with PYTHON!

0 0
Add a comment Improve this question Transcribed image text
Answer #1

Part 1)

Code:

def convertNames(l):
    if l==[]:
        return []
    else:
        first=l[0].split(' ')[0]
        return [first]+convertNames(l[1:])


nameList=['Julia Truong','Chen Wo','Jeb Castro','Ron Kennedy','X Y']
print(convertNames(nameList))

Output:

[Julia, Chen, Jeb , Ron , X]

Part 2)

Code:

def chooseNames(l):
    if l==[]:
        return []
    else:
        start=l[0][0] # Initialising first character of first name of list
        if start=='J':
            return [l[0]]+chooseNames(l[1:]) # when name start with 'J'
        return chooseNames(l[1:]) # when name does not start with 'J'

nameList=['Julia Truong','Chen Wo','Jeb Castro','Ron Kennedy','X Y']
print(chooseNames(nameList))

Output:

Part 3)

Code:

def countNames(l):
    if l==[]:
        return 0
    else:
        start=l[0][0]
        if start=='J':
            return 1+countNames(l[1:])
        return countNames(l[1:])

nameList=['Julia Truong','Chen Wo','Jeb Castro','Ron Kennedy','X Y']
print(countNames(nameList))

Output:

Add a comment
Know the answer?
Add Answer to:
PLEASE DO THIS WITH PYTHON! Please do this with PYTHON! Assume the example of a name...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP!...

    PYTHON this implementation is really hard, Im stuck especially with the requirements they give. PLEASE HELP! THANK YOU! RECURSIVE! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define...

  • PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give...

    PYTHON please help! im stuck on this homework question, THANK YOU! please follow the rules! Give a recursive python implementation of the following function: def check_Decreasing_Order(lissy): Given lissy, a python list of integers, the above function will return True if lissy is sorted in decreasing order, otherwise it will return false. For example, print(check_Decreasing Order([100,50,8, -2])) True print(check_Decreasing_Order([108,50,8,2,35])) False Implementation Requirements: 1. Your implementation must be recursive. 2. If you need more parameters, you may define new functions or helper...

  • ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and...

    ****python**** Q1. Write a recursive function that returns the sum of all numbers up to and including the given value. This function has to be recursive; you may not use loops! For example: Test Result print('%d : %d' % (5, sum_up_to(5))) 5 : 15 Q2, Write a recursive function that counts the number of odd integers in a given list. This function has to be recursive; you may not use loops! For example: Test Result print('%s : %d' % ([2,...

  • use python 2a. Union (1 points) Make a union function which takes in 2 lists as...

    use python 2a. Union (1 points) Make a union function which takes in 2 lists as parameters and returns a list. It should return a list that contain all elements that are in either list. Your results list should contain no duplicates. For example, if the two lists are [1,5,6,5, 2] and [3,5, 1,9] then the result list is (1,5,6,2,3,9). Note that the list sizes don't have to be equal. Note that the input lists could have duplicates, but your...

  • #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function...

    #Starting Out With Python, 4th Edition #Chapter 7 #Exercise 6 #in a program, write a function named roll that #accepts an integer argument number_of_throws. The #function should generate and return a sorted #list of number_of_throws random numbers between #1 and 6. The program should prompt the user to #enter a positive integer that is sent to the function, #and then print the returned list. How would you do this?

  • THIS IS A PROGRAM FOR PYTHON THIS IS A PROGRAM FOR PYTHON THIS IS A PROGRAM...

    THIS IS A PROGRAM FOR PYTHON THIS IS A PROGRAM FOR PYTHON THIS IS A PROGRAM FOR PYTHON This time, we will write a program that uses functions to parse through all those names in a List object (see below for the "us_counties" list) to count the number of occurrences each vowel (a, e, i, o, u) has in all the names. We want 5 separate answers, one for each vowel. Not looking for output here. All my solution will...

  • PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one...

    PLEASE USE WHILE LOOP (NOT FOR LOOPS). Python programming. In this problem, you should write one function named multiply. This function should accept one parameter, which you can assume will be a list with one or more integers in it. (If you don’t know what I mean when I say “list of integers”, go do the reading!) The function should loop through all of the numbers in the list, and multiply all of the numbers together. It should return the...

  • python Days of the Week Function Name: days_of_the_week() Parameters: list of birthdays ( list ), year...

    python Days of the Week Function Name: days_of_the_week() Parameters: list of birthdays ( list ), year (int ) Returns: list of names ( list ) Description: You and your friends all want to celebrate your birthdays together, but you don't want to stay up late on a school night. Write a function that takes in a list of tuples formatted as (day ( int ), month ( int ), name (string)), and a year ( int ). Use Python's calendar...

  • PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK...

    PYTHON I need help with this Python problem, please follow all the rules! Please help! THANK YOU! Bonus Question Implement an efficient algorithm (Python code) for finding the 11th largest element in a list of size n. Assume that n will be greater than 11. det find 11th largest numberlissy Given, lissy, a list of n integers, the above function will return the 11th largest integer from lissy You can assume that length of lissy will be greater than 11....

  • project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary,...

    project-8c Write a class named Employee that has data members for an employee's name, ID_number, salary, and email_address (you must use those names - don't make them private). Write a function named make_employee_dict that takes as parameters a list of names, a list of ID numbers, a list of salaries and a list of email addresses (which are all the same length). The function should take the first value of each list and use them to create an Employee object....

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT