更改字典中除一个键外的值

Change values in dictionary except for one key

我花了将近一个小时来解决这个小问题。

Python is an MIT student who loves fruits. He carries different types of fruits (represented by capital letters) daily from his house to the MIT campus to eat on the way. But the way he eats fruits is unique. After each fruit he eats (except the last one which he eats just on reaching the campus), he takes a 30 second break in which he buys 1 fruit of each type other than the one he just had. Cobra, his close friend, one day decided to keep a check on Python. He followed him on his way to MIT campus and noted down the type of fruit he ate in the form of a string pattern (e.g. AABBBBCA). Can you help Cobra determine the maximum quantity out of the different types of fruits that is present with Python when he has reached the campus?

Write a function nfruits that takes two arguments:

  • A non-empty dictionary containing type of fruit and its quantity initially with Python when he leaves home (length < 10)

  • A string pattern of the fruits eaten by Python on his journey as observed by Cobra.

This function should return the maximum quantity out of the different types of fruits that is available with Python when he has reached the campus.

For example, if the initial quantities are {'A': 1, 'B': 2, 'C': 3} and the string pattern is AC then:

  1. A is consumed, updated values are {'A': 0, 'B': 2, 'C': 3}
  2. Python buys B and C, updated values are {'A': 0, 'B': 3, 'C': 4}
  3. ´C´ is consumed, updated values are {'A': 0, 'B': 3, 'C': 3}

Now Python has reached the campus. So the function will return 3 that is maximum of the quantities of the three fruits.

这是 MOOC 的可选练习,因此没有评分:我解决了更难的问题(更难),但我无法解决它。

我的尝试:

def nfruits(dictionary, string):
    i = 0
    string = sorted(string)

    for char in string:
        dictionary[char] -= 1
        # print dictionary
        i += 1
        for char in string[i:]:
            dictionary[char] += 1
            # print dictionary

     return dictionary[max(dictionary, key = dictionary.get)]

如何在所有地方添加 1 然后为特定键减去 2

类似于

def nfruits(dictionary, string):
    i = 0
    string = sorted(string)

    for idx, char in enumerate(string):
        # We should update other fruits on all steps except the
        # last one
        if idx < len(string) - 1:
            for key in dictionary:
                dictionary[key] += 1
            dictionary[char] -= 2
        else:
            # for the last step - only decrement the fruit
            # Python ate
            dictionary[char] -= 1
        print dictionary
    return dictionary[max(dictionary, key = dictionary.get)]

if __name__ == "__main__":
    dd = {'A': 1, 'B': 2, 'C': 3}
    print nfruits(dd, 'AC')

更新:另一种选择是在我们检查字典时跳过 char

def nfruits2(dictionary, string):
    i = 0
    string = sorted(string)

    for idx, char in enumerate(string):
        # Update the fruit Python ate
        dictionary[char] -= 1
        # update others he bought, skip this on the last step
        if idx < len(string) - 1:
            for key in dictionary:
                if key != char:
                    dictionary[key] += 1
        print dictionary
    return dictionary[max(dictionary, key = dictionary.get)]