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...

  • ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a...

    ON PYTHON: ''' Design the functions described below. RECALL: With functions that do not return a value and print a result to the console, to test you must call the function, run it and visually inspect the result for correctness. With functions that return a value, use the print_test function to provide feedback of the test results at the command line. The print_test function is implemented for you at the bottom of this file. RECALL: floating point arithmetic can lose...

  • 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,...

  • Design a function in python that sums all the numbers in a multi-dimensional list of numbers....

    Design a function in python that sums all the numbers in a multi-dimensional list of numbers. The function should accept the list as an argument. For example, give the following list [[3,5,6,8,9], [0,1,5,8,7], [2,5,9,2,4], [9,5,4,2,1]] The function should add all the numbers in the list and print the result. Analyze your solution and determine the time complexity of the function. Write the time complexity in the form of T(n) = ….

  • 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...

  • Language: Python Topic: APIs and JSON Function name: can_visit Parameters: country_code (str), langList (list of strings...

    Language: Python Topic: APIs and JSON Function name: can_visit Parameters: country_code (str), langList (list of strings ) Return: boolean Description: You are looking for a country to visit for SB2K19 and want to ensure that you know how to speak at least one of the languages spoken in that country. Given a country_code (str), a three - letter code representing the country you want to visit, and a langList (list), a list of the names of the languages you know...

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