Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Financial management theory and practice

Authors: Eugene F. Brigham and Michael C. Ehrhardt

12th Edition

978-0030243998, 30243998, 324422695, 978-0324422696