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

I need help implementing this python function based on the RME and the direction

ID: 3780268 • Letter: I

Question

I need help implementing this python function based on the RME and the directions. I made an attempt but I know it is wrong. Help me fix my code please. So first off I attempt to make the two lists but I'm not sure I did that right. Then I made a list for the index because the key and value don't include the index. I don't know if making an index list is neccessary or not. Then is my attempt to loop through all the values and make a list for each of the cumulative values as it loops. Then I tried to make a new dictionary and I don't know if it is neccessary or not to do that. Then I assigned the random value we are supposed to get to x. Then I tried to make a loop to loop through all the cumulative values and if the cumulative value is greater than the random value it returns the corresponding token. I am pretty sure my code will not return the corresponding token and I'm not sure how to return the corresponding token.

Directions:

weightedChoice

This function takes a dictionary as input and chooses a key in that dictionary to return, using the dictionary's values to compute probabilities of each key being chosen. It will be used in getNextToken as a helper function in choosing a next word for a randomly generated sentence.

Suppose your input dictionary was this:

{'north': 4, 'south': 1, 'east': 3, 'west': 2}

Here's how to choose a key to return from the above dictionary. First, make two lists: one to represent all the keys in the dictionary, and one to represent all the values in the dictionary. Here's a table where the "token" column is our list of keys, and the "count" column is our list of values.

index token count
0 north 4
1 south 1
2 east 3
3 west 2

We can now create a third list which is a cumulative count list, shown in the fourth column:

index token count cumulative
0 north 4 4
1 south 1 5
2 east 3 8
3 west 2 10

Now that we have a cumulative count list that corresponds directly to our list of tokens, we generate a random integer in the interval [0, 10) (where 10 is the maximum value in the cumulative list) using Python’s random.randrange(min, max) function. This will return an integer x such that min <= x < max. We then walk through the token list and return the first token whose corresponding cumulative count is larger than our random number.

For example, using the table above, say we generated 7 as our random number. Then we see the word at index 2, "east", is the first word with a cumulative count greater than 7, since the word's cumulative value is 8. Therefore, we return the token at index 2.

def weightedChoice (self, candidates Requires candidates is a dictionary the keys of candidates are items you want to choose from and the values are integers Modifies: nothing Effects returns a candidate item (a key in the candidates dictionary) based on the algorithm described in the spec import random token keys candidates token keys count values candidates. count values index 0, 1, 2, 3] cumulative for num in count values counting 3 counting num cumulative extend Counting dict (zip (index, token keys random. rand range (min max) for y in cumulative if: cumulative x return token keys

Explanation / Answer

def weightedChoice(self, candidates):
  
token = []
count = []
# make a list of keys and a list of values'''
for keyname in candidates:
token.append(keyname)
count.append(candidates[keyname])
# creat a third list
cumulative = []
if count != []:
cumulative.append(count[0])
for i in range(1,len(count)):
cumulative.append(cumulative[i - 1] + count[i])
else:
return
random_number = random.randrange(0, cumulative[- 1])
# find the index of the value and return the coresponding token
for i in cumulative:
if i > random_number:
return token[cumulative.index(i)]
return token[-1]

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote