Question: Task 4: Little and Long Lists <2.2.1 While-loops; 3.1 Strings; 2.1 Conditionals> Warm Up Let's start by learning about indexing. Indexing allows us to access

Task 4:

Little and Long Lists <2.2.1 While-loops; 3.1 Strings; 2.1 Conditionals> Warm Up Let's start by learning about indexing. Indexing allows us to access the ith element in a sequence.

For example, if we have the list lst = ['a', 'b', 'c', 'd'] and we want to access the 'b' we can ask for lst[1]. Notice that Python indexes from 0. This means if we want the first element in a sequence we have to ask for seq[0].

If the first element is at 0, in our list lst what index is the last element? How could we develop a loop that would look at every element?

Part A - Adding

Develop a function add(numbers) that returns the sum of every second number in a given list. (E.g. calling add([2,-3,5,8,10,1]) returns 6 because 3 + 8 + 1 = 6.)

Part B - Flipping

Develop a function flip(binary string) that takes a binary string (a string containing only 1s and 0s) and returns a new string where every bit has been flipped.

(E.g. calling flip('001011') returns '110100'.)

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Programming Questions!