checkio (How much gold)

本文探讨了一种方法,用于计算包含多种金属如金、铁、铜和锡的神秘金属条中黄金的比例。通过给定的合金比例,我们能够计算出整个金属条中黄金所占的份额。

Our Favorite Trio has found a mysterious metal bar. This bar appears to be made of various metal including gold, iron, copper and tin. The bar does not contain any other metal except these. We do not know the quantity of each metal in the bar, but we do know the proportions of the various alloys. For example: the gold and tin proportion is 1/2, gold and iron -- 1/3, gold and copper -- 1/4. "the gold and tin proportion id 1/2" means that gold and tin together (their sum, not their ratio!) are the 1/2 of the whole bar (the sum of them all). You should calculate the proportion of gold in the entire bar.

The proportions are given as a dictionary where keys are alloy names and values are proportions. The alloy names are presented as strings, which are composed of two metal names separated by a dash (Ex: "gold-tin", "iron-copper", "iron-gold"). The proportions are presented as fractions (fractions.Fraction date type. Read about this module).

alloys

You should return the proportion of gold in the bar as a fraction (fractions.Fraction).

Precondition: All tests are solvable.

Input: Alloys names and proportions. A dictionary.

Output: Proportion of gold. A fractions.Fraction.

Example:

?
1
2
3
4
5
6
7
8
9
10
checkio({
    'gold-tin': Fraction(1, 2),
    'gold-iron': Fraction(1, 3),
    'gold-copper': Fraction(1, 4),
    }) == Fraction(1, 24)
checkio({
    'tin-iron': Fraction(1, 2),
    'iron-copper': Fraction(1, 2),
    'copper-tin': Fraction(1, 2),
    }) == Fraction(1, 4)

How it is used: This teaches you how to work with Fraction data type.

我的想法是根据gold数的不同分类讨论

from fractions import Fraction

def cal(target, condition, alloys):
    num = 0
    total = sum(i for i in alloys.values())
    for c in condition:
        for i in c:
            if i == target:
                num += 1
    if num == 0:
        return  1-total/2
    elif num == 2:
        for i in alloys:
            if 'gold' not in i:
                return (total-2*alloys[i])/2
    elif num == 3:
        return (total-1)/2
    else:
        return None

def find(alloys):
    for i in alloys:
        if 'gold' in i:
            s = i.split('-')
            if s[0] == 'gold':
                return s[1], alloys[i]
            else:
                return s[0], alloys[i]

def checkio(alloys):
    condition = [c.split('-') for c in alloys]
    tem = cal('gold', condition, alloys)
    if tem is None:
        another,value = find(alloys)
        return value - cal(another, condition, alloys)
    else:
         return tem

print checkio({"iron-gold":Fraction(1, 2),"iron-copper":Fraction(1, 4),"iron-tin":Fraction(2, 3)})
print checkio({"copper-tin":Fraction(5, 9),"tin-gold":Fraction(1, 2),"gold-copper":Fraction(2, 7)})
#These "asserts" using only for self-checking and not necessary for auto-testing
if __name__ == '__main__':
    assert checkio({
        'gold-tin': Fraction(1, 2),
        'gold-iron': Fraction(1, 3),
        'gold-copper': Fraction(1, 4),
        }) == Fraction(1, 24), "1/24 of gold"
    assert checkio({
        'tin-iron': Fraction(1, 2),
        'iron-copper': Fraction(1, 2),
        'copper-tin': Fraction(1, 2),
        }) == Fraction(1, 4), "quarter"

然后看了第一名的代码,惊呆了
def checkio(alloys):
    """
        Find proportion of gold, assume exist unique solution.
    """
    return 1 - sum([1 - v if 'gold' in k else v for (k, v) in alloys.iteritems()]) / 2


<think>嗯,用户之前问过同义词的问题,现在又问“how much”,看起来是想了解这个词组的含义和用法。不过“how much”本身是一个常见的疑问词组,通常用来询问数量或价格,比如“How much does this cost?”。但用户可能想知道更多的同义词或者不同的表达方式。 首先,我需要确定用户的具体需求。他们可能是在学习英语,想要扩展词汇量,或者需要在不同场合下使用不同的表达方式。比如在商务环境中询问价格,或者在日常对话中询问数量。此外,用户可能还想知道“how much”在不同语境下的替换词,比如正式和非正式场合的区别。 接下来,我需要考虑用户可能没有明确提到的深层需求。也许他们在写作或口语中遇到了重复使用“how much”的问题,想要更丰富的表达方式。或者他们需要了解在不同英语变体(如英式英语和美式英语)中的不同用法。 然后,我要整理相关的同义词和替代表达,按场景分类,比如日常购物、商务谈判、学术讨论等。同时,注意语法结构的变化,比如有些替代词可能需要不同的介词或动词搭配。例如,“What's the price of...”和“What does this cost?”虽然意思相近,但结构不同。 还要提醒用户注意使用时的常见错误,比如混淆“how much”和“how many”的用法,或者在正式场合使用过于口语化的表达。此外,提供一些例句会帮助用户更好地理解和应用这些替代表达。 最后,确认是否需要进一步提供特定场景的详细对照表,比如商务邮件中的正式询问方式,或者朋友之间的随意询问。这样可以确保回答全面且实用,满足用户的实际需求。</think>以下是对疑问词组 "how much" 的深度解析及使用指南: 一、核心语义分类 1. 数量询问(不可数) $$ Q = \frac{\text{询问量}}{\text{标准单位}} $$ 例:How much sugar do we need? $\Rightarrow$ 需用计量单位回答 2. 价格询问 $$ \text{总价} = \sum_{i=1}^{n} (单价_i \times 数量_i) $$ 例:How much is this painting including tax? 3. 程度量化 $$ \lim_{x\to\infty} \frac{\text{情感强度}}{\text{时间}} $$ 例:How much do you regret that decision? 二、场景化替代表达 1. 商务谈判 - "Could you specify the unit price breakdown?" - "What would be the total expenditure allocation?" 2. 学术研究 $$ \exists\ \epsilon > 0,\ \text{such that}\ |x - a| < \delta \Rightarrow |f(x) - L| < \epsilon\ ? $$ (对应:需精确到数学量级的询问) 3. 日常对话 - "What's the damage?"(俚语,指消费金额) - "Ballpark figure?"(请求估算值) 三、语法变体结构 1. 嵌入式疑问: $$ I_{need} = \int_{a}^{b} \text{"how much time"} \, dt $$ 例:We're calculating how much energy the system requires 2. 省略式: "Much?"(升调,口语确认数量) 3. 强调式: $$ \frac{\text{Just how much}}{\text{exactly}} \geq \text{threshold value} $$ 例:Just how much data was compromised? 四、常见错误预警 1. 单位混淆: $$ \frac{\text{how much}}{\text{how many}} = \begin{cases} 1 & \text{不可数名词} \\ \infty & \text{可数名词} \end{cases} $$ 2. 时态陷阱: "How much did it cost?"(过去时) vs. "How much does it cost?"(现在时) 3. 介词缺失: × "How much you paid?" √ "How much did you pay?" 五、跨文化差异 1. 亚洲市场:常需区分 $$ \text{报价} = \begin{cases} \text{税前价} & \text{西方} \\ \text{税込价} & \text{日本} \\ \text{批发价} & \text{中国} \end{cases} $$ 2. 中东地区:需预谈判空间 $$ \text{初始报价} = \text{预期价} \times 1.5 $$ 需要制作具体场景的数学建模公式吗?例如:国际贸易中的复合成本计算、实验室试剂用量换算表等专业领域的特殊表达方式。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值