Question

Write a function that takes two lists that are in ascending order as arguments. The function...

Write a function that takes two lists that are in ascending order as arguments. The function should return a new list that has all the values from the two argument lists and is also in ascending order (e.g., [1,3,5] and [2,4,6] become [1,2,3,4,5,6]). Please use python

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

merge lists.py - D:lCodingPYTHONImerge lists.py (3.6.2) File Edit Format Run Options Window Help def merge (listA, listB): result = [] indexA 0 indexB-0 #We should iterate while both the indices are in the ranges while indexA<len (1istA) and indexB<ien (listB) if listA[indexA]<listB [indexB]: result.append (listA[indexA]) indexA index·A+ 1 #When element from listB is lower, we need to append this to the result else: result.append (listB [indexB]) indexBindexB+1 #If listB is processed, we need to append all the elements of list A to result if indexA<len (listA) result.extend (listA[indexA: ]) #If listA is processed, we need to append all the elements from listB to teh ans if indexB<len (listB) result.extend (listB[indexB:]) return result if _name , main b= [2,4,6,8] print( a= , a, b,b, merge :.merge (a,b)) a[3, 5,7] b[2,6,8,10] print (a,a, b-,b, merge: ,merge (a,b))

OUTPUT :

CODE :

def merge(listA,listB):
    result = []
    indexA=0
    indexB=0
    #We should iterate while both the indices are in the ranges
    while indexA<len(listA) and indexB<len(listB):
        if listA[indexA]<listB[indexB]:
            result.append(listA[indexA])
            indexA = indexA+1
        #When element from listB is lower, we need to append this to the result
        else:
            result.append(listB[indexB])
            indexB = indexB+1

    #If listB is processed, we need to append all the elements of list A to result
    if indexA<len(listA):
        result.extend(listA[indexA:])

    #If listA is processed, we need to append all the elements from listB to teh ans
    if indexB<len(listB):
        result.extend(listB[indexB:])

    return result


if __name__ == '__main__':
    a = [1,3,5,7]
    b = [2,4,6,8]
    print('a=',a,' b=',b,' merge:',merge(a,b))

    a = [3,5,7]
    b = [2,6,8,10]
    print('a=',a,' b=',b,' merge:',merge(a,b))

Add a comment
Know the answer?
Add Answer to:
Write a function that takes two lists that are in ascending order as arguments. The function...
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
  • IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a...

    IN PYTHON: Write a function that takes, as arguments, two lists, one of which is a character list and the other is the corresponding frequencies, and sorts the characters in ascending order of frequencies. The function should return a list consististing of two lists: the first list is the list of characters and the second is the list of corresponding frequencies, consistent with the rearranged characters. Name this function sortCharacters(characterList, frequencyList). For example, >>>sortCharacters(["a", "b", "c", "d"], [5, 8, 3,...

  • Using Python: write a function that takes two arguments: (1) a target item to find, and...

    Using Python: write a function that takes two arguments: (1) a target item to find, and (2) a list. Your function should return the index (offset from 0) of the last occurrence of the target item or None if the target is not found. E.g. the last occurrence of 33 is at offset 3 in the list [ 42, 33, 21, 33 ] because 42 is offset 0, the first 33 is at offset 1, 21 is offset 2, and...

  • ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns...

    ANSWER USING PYTHON Write a recursive function that takes 2 sorted lists as arguments and returns a single, merged sorted list as a result. For example, if the two input lists are [0, 2, 4, 6, 8] and [1, 3, 5, 7, 9], the returned result should be [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]. Remember that you can add lists in Python using the + operator ([0] + [1,2,3] = [0,1,2,3]) and can take slices of...

  • Write a Python function, called counting, that takes two arguments (a string and an integer), and...

    Write a Python function, called counting, that takes two arguments (a string and an integer), and returns the number of digits in the string argument that are not the same as the integer argument. Include a main function that inputs the two values (string and integer) and outputs the result, with appropriate labelling. You are not permitted to use the Python string methods (such as count(), etc.). Sample input/output: Please enter a string of digits: 34598205 Please enter a 1-digit...

  • PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments:...

    PYTHON: Using Lists and Arrays! 5. In a program, write a function that accepts two arguments: a list, and a number n. The program should generate 20 random numbers, in the range of 1 through 100, and assign each number to the list. The program should also ask the user to pick a number from 1 through a 100 and assign that number to n. The function should display all of the number in the list that are greater than...

  • Write a python function called "get_sma", that takes two input arguments, similar to the example provided....

    Write a python function called "get_sma", that takes two input arguments, similar to the example provided. First argument is the original prices of type list, and the 2nd input argument being the number of days we want to calculate SMA, like a 8 day sma or 35 day sma. This function should return a new list of sma price, based on the input day range, and should be able to handle any SMA calculation, like: sma200 for 200 days moving...

  • Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount,...

    Using PYTHON (LOOPS, IF STATEMENTS ) I should write a function buckets that  takes two arguments: (bucketCount, an integer specifying the number of “buckets” to return and numberLs, a list of numbers ) buckets should split the range of values in numberLs into bucketCount sub-ranges. Specifically buckets should return a list-of-lists of length bucketCount. Each list in the returned list-of-lists represents a “bucket”. Each bucket should contain the numbers from numberLs whose values are greater than or equal to min(numberLs) +...

  • In Python beginner code: Write a function named even_div, that takes two arguments (you can safely...

    In Python beginner code: Write a function named even_div, that takes two arguments (you can safely assume that both arguments will always be non-zero integers). When this function is called, it should return how many times the first argument can be divided by the second argument, if it is evenly divisible (no remainder). If it is not evenly divisible, the function should return 0. Examples: even_div(5, 2) will return 0 (5 divided by 2 is 2.5, not evenly divisible) even_div(10,...

  • Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys)...

    Python 3 Define a function below, get_subset, which takes two arguments: a dictionary of strings (keys) to integers (values) and a list of strings. All of the strings in the list are keys to the dictionary. Complete the function such that it returns the subset of the dictionary defined by the keys in the list of strings. For example, with the dictionary ("puffin": 5, "corgi": 2, "three": 3) and the list ("three", "corgi"), your function should return {"corgi": 2, "three"...

  • in python Write a function, named max_of_two, that takes two numbers as arguments and returns the...

    in python Write a function, named max_of_two, that takes two numbers as arguments and returns the largest of the two values. (Note, this function is actually already provided in Python as max. Do not use the provided function; reason through the logic yourself).

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