Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Q5. Dictionaries An index into a string is a data structure that tells you where

ID: 3847868 • Letter: Q

Question

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 While is Welsh the data structure would store the fact that occurs at indices 7 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) fa": [0, 3, 5, 'b': 11, 21, "g I7, 91, "o': [8, 10), def makeindex(s): for k in range (len(s) return d

Explanation / Answer

Q5.

#first iteration of loop:

#d[s[k]] = d[s[0]] = 'a'
#d.get(s[k],[]) + [k] => checks the dictionary for 'a' if match found, it will add index of 'a' into dictionary key value
#now 'a' found in the indexes of [0,3,5].

#second iteration of loop:

#d[s[k]] = d[s[1]] = 'b'
#d.get(s[k],[]) + [k] => checks the dictionary for 'b' if match found, it will add index of 'b' into dictionary key value
#now 'b' found in the indexes of [1,2]
# and so on for loop executes till it reaches the end of input.

#it wont populate dictionary if key and item already found in dictionary.

#if the output is {'a': [0, 3, 5], 'b': [1, 2], ' ': [4], '-': [6], 'g': [7, 9], 'o': [8, 10]}
#item values are 'a','b',' ','-','g','o' and key values are [0,3,5], [1,2] and so on

Q7.

def substrings(xs):
d = {}                   #empty dictionary
for i in range(len(xs)):           #for loop from beginning till the length of input
for j in range(i+1, len(xs) + 1):   #nested for loop
d[xs[i:j]] = d.get(xs[i:j], 0) + 1  
print(d)

#first loop value 'a'

#second loop value 'a' same as first loop
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "a's" found in the string and stores it into dictionary

#second loop value 'b'
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "ab's" found in the string and stores it into dictionary

#second loop value 'b'
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "abb's" found in the string and stores it into dictionary

#second loop value 'a'
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "abba's" found in the string and stores it into dictionary


#first loop value 'b'

#second loop value 'b' same as first loop
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "b's" found in the string and stores it into dictionary

#second loop value 'b'
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "bb's" found in the string and stores it into dictionary

#second loop value 'a'
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "bba's" found in the string and stores it into dictionary


#first loop value 'b'

#second loop value 'a'
#d[xs[i:j]] = d.get(xs[i:j], 0) + 1 => gets the count of "ba's" found in the string and stores it into dictionary


substrings('abba')           #calling the function substrings