【每日一题20220818】果汁的配比

:man_mage: 我们混合了一些美味的果汁。我们可以添加一定数量的一些成分。有时我们会倒一点果汁。然后我们想知道我们的果汁有哪些浓度。

例子:

  • 你拿一个空罐子装果汁
  • 每当罐子是空的,浓度总是 0
  • 现在你添加 200 单位的苹果汁
  • 然后你添加 200 单位的香蕉汁
  • 现在苹果汁的浓度是0.5(50%)
  • 然后你倒出 200 个单位
  • 苹果汁的浓度还是50%
  • 然后你再加入 200 单位的苹果汁
  • 现在苹果汁的浓度是0.75,而香蕉汁的浓度只有0.25(300单位苹果汁+100单位香蕉汁)

题目难度:一般
题目来源:https://www.codewars.com/kata/5264603df227072e6500006d

class Jar():
    def __init__(self):
        #your code here
        pass
    
    def add (self, amount, kind):
        #your code here
        pass
    
    def pour_out (self, amount):
        #your code here
        pass
    
    def get_total_amount(self):
        #your code here
        pass  
    
    def get_concentration(self, kind):
        #your code here
        pass

jar = Jar()
assert jar.get_total_amount() == 0
assert jar.get_concentration("apple") == 0
jar.add(100, "apple")
assert jar.get_total_amount() == 100
assert jar.get_concentration("apple") == 1
jar.add(100, "apple")
assert jar.get_total_amount() == 200
assert jar.get_concentration("apple") == 1
jar.add(200, "banana")
assert jar.get_total_amount() == 400
assert jar.get_concentration("apple") == 0.5
assert jar.get_concentration("banana") == 0.5
jar.pour_out(200)
assert jar.get_total_amount() == 200
assert jar.get_concentration("apple") == 0.5
assert jar.get_concentration("banana") == 0.5
jar.add(200, "apple")
assert jar.get_total_amount() == 400
assert jar.get_concentration("apple") == 0.75
assert jar.get_concentration("banana") == 0.25

class Jar():
def init(self):
# your code here
self.amount_all = 0
self.kind_dict = dict()

def add(self, amount, kind):
    """
    :param amount: 果汁浓度
    :param kind:  果汁种类
    """
    self.amount_all += amount
    if kind in self.kind_dict.keys():
        self.kind_dict[kind] += amount
    else:
        self.kind_dict[kind] = amount
    return f'你添加了{amount}浓度的{kind}果汁'

def pour_out(self, amount):
    # your code here
    for i in self.kind_dict.keys():
        self.kind_dict[i] -= amount / len(self.kind_dict)
    self.amount_all -= amount

def get_total_amount(self):
    return self.amount_all

def get_concentration(self, kind):
    """
    :param kind:
    :return: 浓度
    """
    return [self.kind_dict[kind] / self.amount_all
            if kind in self.kind_dict.keys() else 0][0]强调文本

手机…

解题思路
1、定义一个List<Map<<String,Interger>>
2、string对应kind int对应数量
3、总数量 = 所有的map的value 每次add相同kind直接增加数量即可
4、kind的数量 = 对应 key的value
5、对应种类的占比 = kind数量/总数量

直接定义一个map就行…

values 总数量 get kind对应数量

Add得时候修改相同数量的kind

class Jar():
    def __init__(self):
        self.kind_amount_dict={}

    def add(self, amount, kind):
        if self.kind_amount_dict.get(kind):
            self.kind_amount_dict[kind]+=amount
        else:
            self.kind_amount_dict[kind]=amount

    def pour_out(self, amount):
        self.kind_amount_dict={key_kind:(self.kind_amount_dict[key_kind]/sum(list(self.kind_amount_dict.values())))*(sum(list(self.kind_amount_dict.values()))-amount) for key_kind in self.kind_amount_dict.keys()}

    def get_total_amount(self):
        return sum(list(self.kind_amount_dict.values()))

    def get_concentration(self, kind):
        return self.kind_amount_dict[kind]/sum(list(self.kind_amount_dict.values())) if self.kind_amount_dict.get(kind) else 0


jar = Jar()
assert jar.get_total_amount() == 0
assert jar.get_concentration("apple") == 0
jar.add(100, "apple")
assert jar.get_total_amount() == 100
assert jar.get_concentration("apple") == 1
jar.add(100, "apple")
assert jar.get_total_amount() == 200
assert jar.get_concentration("apple") == 1
jar.add(200, "banana")
assert jar.get_total_amount() == 400
assert jar.get_concentration("apple") == 0.5
assert jar.get_concentration("banana") == 0.5
jar.pour_out(200)
assert jar.get_total_amount() == 200
assert jar.get_concentration("apple") == 0.5
assert jar.get_concentration("banana") == 0.5
jar.add(200, "apple")
assert jar.get_total_amount() == 400
assert jar.get_concentration("apple") == 0.75
assert jar.get_concentration("banana") == 0.25