Question
use python to write the code Q1 Write a function sum_5_consecutive that takes a list of numbers as input and returns True if there are
use python to write the code
Q1
Write a function sum_5_consecutive that takes a list of numbers as input and
returns True if there are 5 consecutive numbers in the list that sum to zero.
Otherwise it returns False. The function should also return False if the list has
less than 5 elements
Solve this in two ways:
1.for loop (over indices of the list)
2.while loop (over indices of the list)
In both cases you need to think about stopping condition in order to avoid
IndexError: list index out of range
Test your function with at least the examples below
>>> sum_5_consecutive([2, 3, -3, 2, 4,-6])
True
>>> sum_5_consecutive ([-10, 1, 1, 4, 2, 10, 13])
False
>>> sum_5_consecutive([2, 1, -3, -3, -3, 2, 7, 4, -6])
True
>>> sum_5_consecutive ([])
False
>>> sum_5_consecutive ([1, -1,0])
False
Q2
Recall, for example, that
a=[2] creates a list (refer to by variable a) with one element, a number
2 in this case.
b=[None] creates a list with one element. That element is an object of
type None. That is sometimes useful when we are not ready yet to assign a
value to an element of a list.
c=[] creates an empty list, i.e. a list of length zero
Recall further that multiplying a list by an integer n, creates a new
list that repeats the given list n times. Or that applying + operator
to two lists creates a new lists that concatenates the given two lists.
For example:
[1,2]+[10,20,0] creates a list [1,2,10,20,0]
[7,2]*3 creates a list [7,2,7,2,7,2]
Finally, recall the slicing. For example, if a=[2,3,4,1], a[:] returns a
new list that is a copy of list a.
Open the file called creating_various_lists.py. The first line is given to you. It asks the user to enter a positive
even integer n. For each green programing exercise below, try to find at least two solutions (e.g. one by
using a loop with accumulator pattern and another by using int*list).
1.Create a list a (i.e a list referred to by variable a) of length n filled with zeros
2.Create a list b of length n filled with random integers between 1 and 100
3.Create a variable c that is an alias of b
4.Set first half of the elements of c to zero, and then print both b and c
5.Copy list b into a list d
6.Create a new list e that has every 2nd element of b
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started