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 rst element in a sequence we have to ask for seq[0]. If the rst element is at 0, in our list lst what index is the last element? How could we write a loop that would look at every element?
Part A - Adding
Write 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
Write 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 ipped. (E.g. calling flip('001011') returns '110100'.
# Task 4
def add(numbers):
pass
def flip(binary_string):
pass
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