Problem 13 Vrite a function that takes a list and computes the alternating sum of all its elements. An alternating sum alternates between dding and subtracting elements. The example returns -2 because 1 – 4+9 – 16 +9 – 7 +4 – 9+11. Your function should eturn the alternating sum of a list. xample IO: lt-sum ([1.4.9.16.9.7.4.9.11]) #returns
Q: Define a function named slice_and_swap(numbers, index) which takes a list of integers and an index…
A:
Q: Consider a (singly) linked list. Describe an algorithm to remove every second element from the list.…
A: (a) Write the steps to removeEverySecondElement from the linked list. This is a very simple task.…
Q: Given a Singly Linked List, write a function reverse() that reverses the list. Also determine the…
A: Read the inputs. Reverse the input. Print it. Exit.
Q: Consider the list l= [3,2,1]. We can use this list to construct a polynomial p by taking the…
A: Answer: Python Source Code: def poly_builder(l): x = 3 l.reverse() n = len(l) res = l[0]…
Q: 4 Cartesian map Use the built-in higher order functions? and write a non-recursive function val…
A: generating the Cartesian product of two arbitrary lists in your language. Demonstrate that your…
Q: anguage: C++ Solve the following problem using a Singly Linked List. Given a singly linked list of…
A: SUMMARY: -Hence, we discussed all the points.
Q: Write a function called mergeLists that takes two call-by-reference argu- ments that are pointer…
A: Write a function called mergeLists that takes two call-by-reference argu- ments that are pointer…
Q: Write a C++ program to reverse a single linked list. then calculate the time complexity of your…
A: Please find the solution in the below step
Q: 9. Consider the non-empty singly linked list with header node, containing the nodes in the order: […
A: Given list-[header-node,14,10,5] Initially p1 and p2 points to head
Q: A function dog_and_fox() has been analyzed to run with O(n²) complexity to process a list of size n.…
A: Time Complexity = O(n2) for size n 5 days to process a list of size 1000 for list of size 2000 would…
Q: Consider the matrix list x = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]. Write a list comprehension to…
A: For better understanding, the given criteria is written as a complete program below A Python program…
Q: Assume a generic recursive relation is defined as : A[i] = p*A[i-1] + q where p and q are…
A: The complete code is given below with output.
Q: The code of a sequential search function is shown on textbook page 60. In fact, if the list is…
A: code snippet:
Q: Given a singly linked list and a number k, write a function to find the (n/k)-th element, where n is…
A: Code: #include <iostream>#include<math.h>using namespace std; struct node{ int data;…
Q: Question #2 Consider there is a list i.e. listA = {20, 10, 5, 15, 3} Write python code that: a.…
A: def updateListA(listA): listA.insert(1,30) listA.append(40) return listA def…
Q: Write a program in C++ to insert a new node at the end of a Singly Linked List. Test Data and…
A:
Q: For each of these lists of integers, provide a simple formula that generates the terms of an integer…
A: a. 6, 9, 14, 21, 30, 41, … 9-6=3 14-9=5 21-14=7 30-21=9 41-30=11 54-41=13 69-54=15 86-69=17 b.…
Q: A single domino tile is represented as a two-tuple of its pip values, for example (2, 5) or (6, 6).…
A: Python used to answer this question
Q: Given two lines, determine whether or not they are parallel. Lines are represented by a list [a, b,…
A: CODE: print("To determine the lines are parallel or not \n") def lines_are_parallel(line1, line2):…
Q: 1. Make a linked list for 26 (a to z) english alphabets where each node consists a single alph…
A: According to the question we have to Implement the following sequence of operations one by one…
Q: Given the following function on lists: def foo : list A → list (list A) | [] := [[]] | (a :: as)…
A: Given: Which of the following properties hold? i) ∀ xs : list A, ∃ ys : list (list A), ys = foo…
Q: 10. Suppose you are given an implementation of a queue of integers. The operations tha can be…
A: 1. The initial value of Queue or Queue content is 1 2 3 4 2. now in the 1st iteration,it will…
Q: Consider the list 1=[3,2,1]. We can use this list to construct a polynomial p by taking the…
A: Program Approach:- 1. Create the user define function whose name is poly_builder. This function…
Q: A function owl_and_cat () has been analyzed to run with O(n) complexity to process a list of size n.…
A: Given Data : Time complexity of owl_and_cat() is O(n) where n = size of list. The Time taken by…
Q: SOB 22 – (Block 2) Write up what the properties of sets, lists, vectors and strings are and whether…
A: Set Lists Vector String It is an unordered collection of a list. It is an ordered collection of a…
Q: Consider a singly linked list where each node contains two fields – integer data and node type…
A: PROGRAM INTRODUCTION: Include the required header files. Create the structure for the node of the…
Q: Write a program that inserts the following numbers into two (2) empty doubly linked list. L1 50 30…
A: Actually, given following numbers into two(2) empty doubly linked list L1 50 30 25 75 82 28 77L2 50…
Q: A. Write a C++ code (function) to search an element in a queue? Also assume the situation that, you…
A: Codes are given below
Q: Problem-02: Reverse a Linked List in groups of given size. Given a singly linked list, reverse the…
A: Sample Response: //C++ program to reverse a linked list in groups of given size…
Q: Write a function sort_and_return_in_pairs_list for example: [12, 13, 11, 15] return =>(11.12) and…
A: Write a function sort_and_return_in_pairs_list for example:[12, 13, 11, 15] return =>(11.12) and…
Q: Write a program in C to delete first node of Singly Linked List. Test Data : Input the number of…
A: Step-1: StartStep-2: Create a struct node which has two attributes data and nextStep-3: Declare a…
Q: Computer Science please solve this problem as soon as possible Write a function to insert the…
A: First, create a linked list and insert values in the linked list. Accept the value of x from the…
Q: 2) Consider an ordered linked list with n entries in ascending order. Each entry has 2 components: a…
A: Ordered linked list is a singly sorted linked list.
Q: Write an inductive definition for A = {0, 2, 4, 6, 8, ...) U {1, 4, 9, 16, 25, ...). 7. The function…
A: Notice that A can be written as a union of simpler sets: A = {0,2,4,6,8,…} ∪ {1,4,9,16,25,…} Basis:…
Q: Consider there is a list i.e. listA = {20, 10, 5, 15, 3} Write python code that: a. Creates a…
A: a) def updateListA(listA): listA.insert(1,30) listA.insert(len(listA),40) return listA…
Q: One of the linked list variants we saw was a singly-linked list with head and tail pointers. Let's…
A: 1) The presence of a tail pointer is not enough to allow us to remove the last node in Θ(1) because…
Q: 2 3 [[1,2, 3], [4, 5, 6] ] \4 5 Complete the Python function transpose () below that takes as input…
A: def transpose(M): # function to transpose a matrix T = [] #…
Q: By using linked list, add a binary search tree function for your program that is able to search for…
A: // CPP code to implement binary search// on Singly Linked…
Q: Let's assume a list A that contains non-repeating and unsorted list of numbers. A number A[i] is…
A:
Q: Provide the output of the programming segments below 1) A Python function that takes a list and…
A: Answer 1: We have defined a function called uniqueFromList which takes a list as an input and return…
Q: 1. A function polyEval x p that evaluates the polynomial (represented by list) p at x; for ex-…
A: Note: Answering the question in python as no language is mentioned. Input : Value of x Value of…
Q: A. Write a C++ code (function) to search an element in a queue? Also assume the situation that, you…
A: Here is the required function :
Q: 20. Consider an algorithm intending to store all prime numbers between 2 and 20 (inclusive) in a…
A: (a) if is_prime(2*i): prime_list.append(2*i) i=i+1increment of i is being done inside the if…
Q: Exercise B Implement the Prelude function init (which returns a list with the last element renoved)…
A: Given:- init : : [a] -> [a]init xs = Perlude function:- There are different kind of functions in…
Q: def f(x,v): return -x**3+5*x**2-8*x+4-(v**2)*(2-x) Define v_list = np.linspace(0, 0.5, 6)…
A: Given program def f(x,v): return -x**3+5*x**2-8*x+4-(v**2)*(2-x) v_list = np.linspace(0, 0.5,…
python problem
PROGRAM CODING:
#include<iostream.h>
using namespace std;
int alternate_sum(int ar[], int size)
{
int sign = 1, sum = 0;
for (int a = 0; a < size; ++a)
{
sum += sign*arr[a];
sign *= -1;
}
return sum;
}
Step by step
Solved in 2 steps
- The function list_combination takes two lists as arguments, list_one and list_two. Return from the function the combination of both lists - that is, a single list such that it alternately takes elements from both lists starting from list_one. You can assume that both list_one and list_two will be of equal lengths. As an example if list_one = [1,3,5] and if list_two = [2,4,6]. The function must return [1,2,3,4,5,6].emacs/lisp function Write a function that takes one parameter that is a list of numbers and returns true (t) if all the elements of the list are multiples of the first one and nil otherwise. This said, a list generated by the function make-multiples could be passed to this new function and the result should be true. On the other hand, show an example of a list that could not have been generated by the first function for which the result is still true.Exercise 2 Write a function called cumsum that takes a list of numbers and returns the cumulative sum; that is, a new list where the ith element is the sum of the first i+1 elements from the original list. For example: >>> t = [1, 2, 3] >>> cumsum(t) [1, 3, 6]
- 2. Write a function called chop that takes a list, modifies it by removing the first and last elements, and returns None. For example: >>> t = [1, 2, 3, 4] >>> chop (t) >>> t [2, 3]1. Write a function called chop that takes a list, modifies it by removing the first and last elements, and returns None. For example: >>> t = [1, 2, 3, 4, 5, 6] >>> chop(t) >>> t [2, 3, 4, 5] 2. Write a function called list_median that takes a list and returns a new list. If the list has even numbers of elements, the new list will have two elements. If the list has odd numbers of elements, the new list will include only one element. For example: >>> t1 = [5, 1, 2, 3, 4] >>> list_median(t1) [3] >>> t2 = [8, 3, 10, 6] >>> list_median(t2) [6, 8] 3. Write a function called has_duplicates that takes a list and returns True if there is any element that appears more than once. It should not modify the original list. For example: >>> t1 = [5, 1, 2, 3, 4] >>> has_duplicates(t1) False >>> t2 = [5, 1, 2, 3, 2, 8, 10] >>> has_duplicates(t2) TrueWrite a function reverse_iter_for that takes a list and returns a new list that is the reverse of the original using a for loop. You should not need any indexing notation. def reverse_iter_for(lst): """Returns the reverse of the given list. >>> reverse_iter_for([1, 2, 3, 4]) [4, 3, 2, 1] TELEEL "*** YOUR CODE HERE ***" Complete the function reverse_iter_while that behaves identically to reverse_iter_for but is implemented as using a while loop. You may use indexing or slicing notation. Do not use 1st [::-1] ! def reverse_iter_while(lst): """Returns the reverse of the given list. >>> reverse_iter_while([1, 2, 3, 4]) [4, 3, 2, 1] rev_lst, i = [], 0 while i < len(lst): "*** YOUR CODE HERE ***"
- 16-Write a function called find_integer_with_most_divisors that receives a list of integers and returns the integer from the list with the most divisors .In the event of a tie, the first case with the most divisors returns .For example: If the list looks like this: [8, 12, 18, 6] In this list, the number 8 has four divisors, which are ;[1,2,4,8] :The number 12 has six divisors, which are ;[1,2,3,4,6,12] :The number 18 has six divisors, which are ;[1,2,3,6,9,18] :And the number 6 has four divisors, which are : [1,2,3,6]Note that both items 12 and 18 have the maximum number of divisors and are equal in number of divisors (both have 6 divisors) .Your function must return the first item with the maximum number of divisors ;Therefore it must return the following value: 12Exercise 5 Write a function called is_sorted that takes a list as a parameter and returns True if the list is sorted in ascending order and False otherwise. For example: >>> is_sorted([1, 2, 2]) True >>> is_sorted(['b', 'a']) FalseThe mapped list pattern Our second pattern is the mapped list pattern, described in video 4 3 mapped list pattern. Often we need to write a function that takes a list as a parameter and returns a new list in which each item in the original list is "mapped" to a new item in the result list. For example, the following function takes a list of numbers as a parameter and returns a list of all the numbers squared, e.g. squares ( [1, 3, 7]) returns [1, 9, 49]. def squares (nums): "Returns the squares of the given numbers""" result = [] for num in nums: result.append (num * num) return result Although this is just a special case of the accumulator pattern, it is so common that we give it its own name: the mapped list pattern. Consider the following function: def squares(nums): ""Returns the squares of the given numbers""" result = [] for num in nums: result.append (num * num) return result If the main program calls print(squares ( [5, -3, 2, 7]) what is the state table for the function…
- Exercise 4 Write a function called chop that takes a list, modifies it by removing the first and last elements, and returns None. For example: >>> t = [1, 2, 3, 4] >>> chop(t) >>> t [2, 3]Write a function called chop_list that takes a list, modifies it by removing the first and last elements, and return a list. For example: list1=[1,2,3,4,5] -> chop_list( list1) =[2,3,4] list2=["a", "b", "e", "f") -> chop_list( list2) =["b","e"]Code in python Given a list, L, the function front_two creates a new list of length 2 out of the first two items in L as follows: In the new list, the second item becomes the first item, and the first item becomes the second. If L contains only one item, the first item in the new list should be the value None. If L is empty, both items in the new list should be value None. The function returns the new list. For example: Test Result L = ['a', 'b', 'c', 'd'] print(front_two(L)) ['b', 'a'] L = [] print(front_two(L)) [None, None] L = [5, 2] print(front_two(L)) [2, 5] L = [7] print(front_two(L)) [None, 7]