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

PYTHON PROGRAMMING Start by defining a new Python function named buildIndex(). T

ID: 3762680 • Letter: P

Question

PYTHON PROGRAMMING

Start by defining a new Python function named buildIndex(). This function takes one argument: a string representing the text to be indexed. buildIndex() creates an empty dictionary to hold the final result, and uses split() to break the string into a list of individual words. The function then uses a loop to examine each word in turn; for each word, if the word is already present as a key in the dictionary, then the loop adds the current position/index of the word to the dictionary under that key. If the word is not already found in the dictionary, it is added as a new entry. When the loop is complete, buildIndex() returns the dictionary that it created. You may optionally convert the input string to all lowercase before processing it, and use replace() to remove punctuation like periods and commas. Hint 1: Use the list operation append() to easily add a new value to the end of a list. For example, myList.append(5) would add 5 as a new value at the very end of myList. Hint 2: To create a new list from a variable, add square brackets around the variable's name: anotherList = [myVariable] Pseudocode: procedure buildIndex (text) index empty dictionary words result of applying split() to text position 0 while position < length of words: nextWord position'th element of words if nextWord is in the set of keys for index: ref list of values associated with nextWord Add position to the end of ref Assign ref to the dictionary using nextWord as the key else: Assign a list containing position to the dictionary using nextWord as the key return the new dictionary (index) 2. Next, define a function named displayIndex() that takes a dictionary as its (only) input. displayIndex() retrieves the list of keys from the dictionary that was passed in, sorts that list alphabetically, and then prints out each key, along with its associated value, one at a time. Hint: To sort a list, use the built-in Python function called sorted(): s = sorted(myList) 3. Finally, define (and then call) a main() function that prompts the user to enter some text, passes that text to buildIndex(), and then prints the resulting index in sorted order using displayIndex(). Sample Program Execution (program output is in italics, and user input is bold) Please enter some text to index: Archimedes anticipated modern calculus and analysis by applying concepts of infinitesimals and the method of exhaustion to derive and rigorously prove a range of geometrical theorems including the area of a circle Index Contents: a: [21, 30] analysis: [5] and: [4, 11, 18] anticipated: [1] applying: [7] archimedes: [0] area: [28] by: [6] calculus: [3] circle: [31] concepts: [8] derive: [17] exhaustion: [15] geometrical: [24] including: [26] infinitesimals: [10] method: [13] modern: [2] of: [9, 14, 23,

HELP WITH MY CODE!

def Buildindex(text):
index = {}
words = text.split()
position = 0
while position < len(words):
nextWord = words[position]

if nextWord in index:
ref = [nextWord]
ref.append(position)
ref = index[nextWord]
  

else:
anotherlist = [x]
anotherlist.append(position)
anotherlist = index[nextWord]
return index
  

  
  
def displayindex(index):
key = index.keys()
key = sorted(k)
for i in key:
print( key + ":" + str(index[key]))
  

def main():
text = input("Enter text")
Buildindex(text)
displayindex(index)

Explanation / Answer

Change this in your code:

def displayIndex(index):
sortIndex = sorted(index.keys())
for key in sortIndex:
print(key + " " + sortIndex[key])

OR

def displayIndex(index):
sortIndex = sorted(index.keys())
print sortIndex
for key in sortIndex:
print(str(sortIndex.index(key)) + " " + key)
def main():
text = "Welcome to chegg "
displayIndex(buildIndex(text))
main()