PYTHON:
MUST NOT USE PYTHONS BUILT IN FUNCTIONS SUCH AS result.append MUST BE SOLVED OUT WITHOUT SUCH FUNCTIONS
>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
1. Write a function interleave(vals1, vals2) that takes as inputs
two lists vals1 and vals2 and uses
recursion to construct and
return a new string that is formed by
interleaving the elements in the lists vals1 and vals2 to create a
single list. In other words, the new list should alternate elements
from the two input lists: the first element from vals1, followed by
the first element from vals2, followed by the second element from
vals1, followed by the second element from vals2, etc. If one of
the lists is longer than the other, its “extra” elements – the ones
with no counterparts in the shorter list – should appear
immediately after the interleaved elements (if any) in the new
list. For example:
>>> interleave([1, 1, 1], [2, 2, 2]) result: [1, 2, 1, 2, 1, 2] >>> interleave([3, 4, 5, 6], [7, 8, 9, 0]) result: [3, 7, 4, 8, 5, 9, 6, 0] >>> interleave([0, 0, 0, 0], [1, 1]) # two extra elements in first list result: [0, 1, 0, 1, 0, 0] >>> interleave([2, 1, 0], [3, 4, 5, 6]) # one extra element in second list result: [2, 3, 1, 4, 0, 5, 6]) >>> interleave([1, 2], []) # all of the first list's values are extra! result: [1, 2] >>> interleave([], [3, 4, 5]) # all of the second list's values are extra! result: [3, 4, 5] >>> interleave([], []) result: []
Hint: You will need more than one base case.


CODE
def interleave(a, b):
if len(a) == 0: # base case
return b
if len(b) == 0: # base case
return a
if not(a[-1] == "True" or a[-1] == "False"): # first case for non
empty lists
a.append("True")
b.append("False")
if a[0] == "True": # base case
return b[:-1]
if b[0] == "True": # base case
return a[:-1]
if a[-1] == "True": # taking element from a
a[-1] = "False"
b[-1] = "True"
return [a[0]] + interleave(a[1:], b)
if b[-1] == "True": # taking element from b
b[-1] = "False"
a[-1] = "True"
return [b[0]] + interleave(a, b[1:])
# sample runs
print(interleave([1, 1, 1], [2, 2, 2]))
print(interleave([3, 4, 5, 6], [7, 8, 9, 0]))
print(interleave([0, 0, 0, 0], [1, 1]))
print(interleave([2, 1, 0], [3, 4, 5, 6]))
print(interleave([1, 2], []))
print(interleave([], [3, 4, 5]))
#Please up vote or comment if you have any doubts.
PYTHON: MUST NOT USE PYTHONS BUILT IN FUNCTIONS SUCH AS result.append MUST BE SOLVED OUT WITHOUT SUCH...
Help with solution using Python
test using
def riffle_generator(seed):
random.seed(seed)
for i in range(1000):
n = random.randint(0, 100)
items = [random.randint(0, 10**6) for j in range(2 * n)]
yield (items, True)
yield (items, False)
Riffle def riffle(items, out True): Given a list of items that is guaranteed to contain an even number of elements (note that the integer zero is an even number), create and return a list produced by performing a perfect riffle to the items by interleaving the...
Write a method called alternate that accepts two Lists as its parameters and returns a new List containing alternating elements from the two lists, in the following order: • First element from first list • First element from second list • Second element from first list • Second element from second list • Third element from first list • Third element from second list If the lists do not contain the same number of elements, the remaining elements from the...
In PYTHON!
Exercise 3: Lists and Functions In this exercise we will explore the use of lists and functions with multiple inputs and multiple outputs. Your task is to implement the separate_and_sort function. This function takes two input parameters: 1. A list containing strings and integers in any order. 2. An optional integer parameter size which controls how many items in the list the function will process. If the length of the list is smaller than the value of size,...
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...
I
need help with this problem this is what I put and these are the
errors I got
Given the lists list1 and list2 that are of the same length, create a new list consisting of the last element of listi followed by the last element of list2, followed by the second to last element of listi, followed by the second to last element of list2, and so on (in other words the new list should consist of alternating elements...
c++
programming please. thx.
Define a function template named "merge" to merge two lists of items into one list including all the items of the first list followed by the items of the second list. Even though, the data type of the items of the lists is a generic type, both input lists include items of the same type. To merge the two lists, the function first dynamically allocates enough memory to accommodate the items of both lists. The allocated...
Create a Scheme function for the following:
Thank you!
2. combine which has 3 arguments that are all lists. The result should be equivalent to the concatenation of the first list, the reverse of the second list, followed by the third list. Use the built-in functions append and reverse. For example, (combine '(1 2) (3 4 5) '(6 7)) should return the list '(1 2 5 4 3 6 7).
IN PYTHON Develop the program that will allow you to obtain an ordered list of whole numbers from least to greatest from two lists of whole numbers. The individual values of the input lists are captured by the user one by one and then both lists will be joined and then sorted in ascending order (from lowest to highest value). Your program should allow the capture of the entire values of the lists without making use of messages (legends) to...
Use python!!! need to match the execution result that is provided. Part One – Keyword Arguments and Default Values Write an invoice function. The function will generate a simple invoice and will have two required arguments and two keyword arguments. The two required arguments are unitPrice and quantity. The first keyword argument is shipping, and it has a default value of 10. The second keyword argument is handling, and it has a default value of 5. Test it twice from...