Question

Q1 - Add This Many Write a function that takes in a value x, a value...

Q1 - Add This Many

Write a function that takes in a value x, a value el, and a list and adds as many el’s to the end of the list as there are x’s. Make sure to modify the original list using list mutation techniques.

def add_this_many(x, el, lst):

    """ Adds el to the end of lst the number of times x occurs in lst.

    >>> lst = [1, 2, 4, 2, 1]  

    >>> add_this_many(1, 5, lst)

    >>> lst [1, 2, 4, 2, 1, 5, 5]

    >>> add_this_many(2, 2, lst)

    >>> lst [1, 2, 4, 2, 1, 5, 5, 2, 2]

    """

use python

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

def add_this_many(x, el, lst):

  for i in range(x):

    lst.append(el)

lst = [1, 2, 4, 2, 1]

print(lst)

add_this_many(1, 5, lst)

print(lst)

add_this_many(2, 2, lst)

print(lst)

Note : Please comment below if you have concerns. I am here to help you

If you like my answer please rate and help me it is very Imp for me

Add a comment
Know the answer?
Add Answer to:
Q1 - Add This Many Write a function that takes in a value x, a value...
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: Create a function count_target_in_list that takes a list of integers and the target value then...

    Python: Create a function count_target_in_list that takes a list of integers and the target value then counts and returns the number of times the target value appears in the list. Write program in that uses get_int_list_from_user method to get a list of 10 numbers, then calls the count_target_list method to get the count then prints the number of times the target was found in the list. def get_int_list_from_user(): lst=[] y = int(input("Enter number of numbers")) for x in range(y): lst.append(int(input("Enter...

  • Python. Write a function that takes in a list and returns the first nonzero entry. def...

    Python. Write a function that takes in a list and returns the first nonzero entry. def nonzero(lst): """ Returns the first nonzero element of a list >>> nonzero([1, 2, 3]) 1 >>> nonzero([0, 1, 2]) 1 >>> nonzero([0, 0, 0, 0, 0, 0, 5, 0, 6]) 5 """ "*** YOUR CODE HERE ***"

  • Use the FDR to design and write a function, maxValTimes, that takes a list of integers...

    Use the FDR to design and write a function, maxValTimes, that takes a list of integers and returns a set containing the value(s) that occur the same number of times as the maximum value in the list. If the list is empty, return an empty set.   For example if the list was [2,1,1,2,3,3,1] the function would return {2,3} as the maximum value is 3 which occurs twice, and 2 also occurs twice (but 1 occurs 3 times). For full marks,...

  • Python 3: Write a LinkedList method named contains, that takes a value as a parameter and...

    Python 3: Write a LinkedList method named contains, that takes a value as a parameter and returns True if that value is in the linked list, but returns False otherwise. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list...

  • #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are...

    #We've written the function, sort_with_bubbles, below. It takes #in one list parameter, lst. However, there are two problems in #our current code: # - There's a missing line # - There's a semantic error (the code does not raise an # error message, but it does not perform correctly) # #Find and fix these problems! Note that you should only need #to change or add code where explicitly indicated. # #Hint: If you're stuck, use an example input list and...

  • write the following functions in Picat, Haskell, or python using recursion. 1. member(x, lst): this function...

    write the following functions in Picat, Haskell, or python using recursion. 1. member(x, lst): this function checks to see if x occurs in lst. 2. sorted_list( lst): this function checks if lst is sorted in ascending order 3. Cartesian(xs, ys): this function takes two sets represented as lists, xs and us,, and returns the Cartesian product of the set, I.e a set of all possible pairs (x, y), where x is an element of xs, and y is an element...

  • Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest...

    Python3: Write the function findLongestStreak(lst) which takes a list of numbers, lst, and returns the longest streak of numbers which occur in the list. A streak of numbers is a list of numbers where each number is one greater than the one that occurred before it- for example, [4, 5, 6, 7]. So findLongestStreak([3, 4, 8, 2, 3, 4, 5, 7, 8, 9]) would return [2, 3, 4, 5]. If there is more than one streak with the longest length,...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

  • Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display,...

    Python 3: Python 3: Write a LinkedList class that has recursive implementations of the add, display, remove methods. You may use default arguments and/or helper functions. class Node: """ Represents a node in a linked list """ def __init__(self, data): self.data = data self.next = None class LinkedList: """ A linked list implementation of the List ADT """ def __init__(self): self.head = None def add(self, val): """ Adds a node containing val to the linked list """ if self.head is...

  • Write a program in scheme using eopl language. Define a function "symbol-count" which takes a flat...

    Write a program in scheme using eopl language. Define a function "symbol-count" which takes a flat list of symbols and returns a list of lists in which each symbol is paired with the count of how many times it occurs in the original input. > (symbol-count '(b a)) '((b 1) (a 1))

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