Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Hi can someone explain the answers provided below for each questions with relevant comments :) i am a beginner programmer. Thank you! Q5. Dictionaries An

Hi can someone explain the answers provided below for each questions with relevant comments :) i am a beginner programmer. Thank you!

image text in transcribedimage text in transcribed
Q5. Dictionaries An index into a string is a data structure that tells you where each character in the alphabet occurs in the string. So given the string "Lyndon While is Welsh", the data structure would store the fact that "W" occurs at indices 7 and 16, "h" occurs at indices 8 and 20, etc. Any character which doesn't occur in the string doesn't get an entry in the data structure. Use the dictionary method get to define the following function. 10 marks def makeindex(s): #makeindex returns a dictionary that holds an index for s e.g. makeindex("abba a-gogo") = {"a": [0, 3, 5], "b": [1, 2], "g": [7, 9], "o": [8, 10], "-": [6], " ": [4]]. def makeindex(s): d =f for k in range(len(s)): d[s[k]] = d.get(s[k], ) + [k] return dQ5. List iteration A simple lossless algorithm for compressing a list of Booleans replaces each sequence of consecutive values with the length of that sequence. All lists are assumed to start with (0 or more) Trues. So e.g. [True, True, False, True, False, False, False] is compressed to [2, 1, 1, 3], and [False, False, True] is compressed to [0, 2, 1]. Use iteration over lists to define the following function. 10 marks def simple(xs): #simple returns the compressed version of the list of Booleans xs def simple(xs): if xs == : return b = False ZS = [ while b in xs: k = xs.index(b) zs.append(k) Xs = XS[K:] b = not b zs. append(len(xs)) return zs

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

Modern Dental Assisting

Authors: Doni Bird, Debbie Robinson

13th Edition

978-0323624855, 0323624855

Students also viewed these Programming questions