Question

python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]]...

python

List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]]

1 def function(Letter,List),example, function("c",List) return 3

2 def function(word,List),example, function("a",List) return 1, function("be",List) return 4, function("bee",[b,5],[e,20]) return 45

Dictionary = ["a","bee","ad","ae"]

3 ["a","b","y","e"] return[["a",1],["ae",2],["bee",5]]

4 input a list, find largest value words can spell, return words and value

dont using loop,

using recursion for those code

0 0
Add a comment Improve this question Transcribed image text
Answer #1

SOURCE CODE:

*Please follow the comments to better understand the code.

**Please look at the Screenshot below and use this code to copy-paste.

***The code in the below screenshot is neatly indented for better understanding.

# This is the recursive function that calculates the value for a given word or lette
def helper_recursive(s,itemDict):
# It is a letter
if len(s)==1:
return itemDict.get(s[0])
else:
# It is a word.
# So, call it recursively and add the value of first letter
return itemDict.get(s[0]) + helper_recursive(s[1:],itemDict)
  

def function(input_list,List):
max_word = ''
max_value = 0
  
# Convert it to dict for easier access
itemDict = {item[0]: item[1] for item in List}
# print(itemDict)
  
# For each item in the input list, call the recursive function and check for the max values
for item in input_list:
# Get the value for that word/letter
h_value = helper_recursive(item,itemDict)
  
print(item,"has value=>",h_value,'from recursive "helper_recursive" function')
  
# Update the max value
if h_value > max_value:
max_word = item
max_value = h_value
  
# return max word and the value
return max_word, max_value
  
# Test the function
List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2] ]
sol = function(["a","bee","ad","ae"],List1)
print('The Maximum word and value are: ',sol)

======================

SCREENSHOT:

Add a comment
Know the answer?
Add Answer to:
python List1=[ ["a", 1], ["b", 3], ["c", 3], ["d", 2], ["e", 1], ["f", 4], ["g", 2]]...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • PYTHON Stuck with this problem with these implementation requirements. PLEASE HELP! THANK YOU! PYTHON Q7 16...

    PYTHON Stuck with this problem with these implementation requirements. PLEASE HELP! THANK YOU! PYTHON Q7 16 Points Give a python implementation of the following function: def without_Vowels(listi): The above function gets a list of positive integers and English letters, list1. When called, it should remove all the vowels from list1, and keep only the consonants and integers. The Order of the remaining consonants and integers does not matter. For example, if list1 = ['a', 'b', 5, 'c', 'o', 2, 'e',...

  • write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named...

    write the following code in python 3.2+. Recursive function prefered. Thank you! Define a function named q3() that accepts a List of characters as a parameter and returns a Dictionary. The Dictionary values will be determined by counting the unique 3 letter combinations created by combining the values at index 0, 1, 2 in the List, then the values at index 1, 2, 3 and so on. The q3() function should continue analyzing each sequential 3 letter combination and using...

  • Must be written in python 3: # # Complete the 'SmallestDictionaryOrderedArray' function below. # # The...

    Must be written in python 3: # # Complete the 'SmallestDictionaryOrderedArray' function below. # # The function is expected to return an INTEGER_ARRAY. # The function accepts following parameters: # 1. INTEGER_ARRAY a # 2. INTEGER_ARRAY b # def SmallestDictionaryOrderedArray(a, b): # Write your code here ☆ Dictionary Ordered Array Given two arrays of numbers, we can use "dictionary ordering" to identify which array is "smaller" (similar to which array would come first in a dictionary). Just like the order...

  • python please 11 def add_item(items, word): 14 15 16 F # check if the word is...

    python please 11 def add_item(items, word): 14 15 16 F # check if the word is in the dictionary (keys of dictionary) if word in items: items [word] = items (word] + 1 # update the count else: # word is not in dictionary items[word] = 1 # add the word with count 1 return items [word] # return the current count of word after updation Create a function named build_dictionary that takes a list of words (as a parameter)...

  • python

    Complete the below function that takes a file name and a list as arguments and reads the text within the corresponding file, then creates a dictionary whose keys are the characters of the provided list, and the values are the counts of these characters within the text. If a character in the list is not included in  the text, "0" should be written in the dictionary as the value of this  character. The function returns the created dictionary as a result. If the file  does not exist, then the function returns an empty dictionary. Here is an example case, assuming a file named "sample.txt" exists with the  following content: ----- sample.txt ----- This is an example sentence. This is yet another sentence. ------------------------- >>> text2dict("sample.txt", ['a', 'b', 'c', 't']) {'a':3, 'b':0, 'c':2, 't':4} """ def text2dict(filename, characters):     return # Remove this line to answer this question

  • 6 (4 points): 4 3 2 1 0 Use Kruskal's algorithm to find the minimum spanning tree for the graph G...

    6 (4 points): 4 3 2 1 0 Use Kruskal's algorithm to find the minimum spanning tree for the graph G defined by V(G) E(G) a, b, c, d, e ac, ad, ae, be, bd, be Vo(ad) = (a, d) (ae) a, e (be) b,e) using the weight function f : E(G)Rgiven by f(ac)-(ad)-3 f(ae)-2 f(be) =4 f(bd) = 5 f(be) = 3 6 (4 points): 4 3 2 1 0 Use Kruskal's algorithm to find the minimum spanning tree...

  • PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as...

    PYTHON 3: Write a function removeRand() that takes a dictionary d and an integer n as parameters. The function randomly chooses n key-value pairs and removes them from the dictionary d. If n is greater than the number of elements in the dictionary, the function prints a message but does not make any changes to d. If n is equal to the number of elements in d, the function clears the dictionary. Otherwise the function goes through n rounds in...

  • Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as o...

    Solve above using prolog!! 3. 2 marks] Write the rule remdup (List1, List2) that takes as input List1 and produces as output List2. List2 contains all items that appear in List1, except that repeated or duplicate items are not present in List1. For example: ?- remdup([1,2,3] ,x) ?- remdup([3,1,2,31,x) X=[1, 2, 3]. Note that the order of items in List2 is unspecified: for instance, [3,1,2 is an acceptable result for the second example above. 4. 2 marks Consider a course...

  • Python 3.6 I JUST NEED HELP WITH PROBLEM 2 (CONTINUATION OF PROBLEM 1)!!! THIS IS MY...

    Python 3.6 I JUST NEED HELP WITH PROBLEM 2 (CONTINUATION OF PROBLEM 1)!!! THIS IS MY WORK FOR PROBLEM 1 (I NEED HELP WITH PROBLEM 2): Python 3.6 Problem 1 This problem provides practice using a while Irue loop Write a function named twoWords that gets and returns two words from a user. The first word is of a specified length, and the second word begins with a specified letter. The function twoWords takes two parameters: 1. an integer, length,...

  • 1. use python List to Dictionary Write a function that has three parameters: a list of...

    1. use python List to Dictionary Write a function that has three parameters: a list of unsorted numbers with no duplicates, a start number, and an end number. This function should return a dictionary with all integers between the start and end number (inclusive) as the keys and their respective indices in the list as the value. If the integer is not in the list, the corresponding value would be None. Example unsorted list: [2,1,10,0,4,3] two numbers: 3, 10 returned...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT