这个算法有什么问题?

时间:2021-09-03 17:04:48

This is an abstracted form of the bug that led me into the code that formed the basis of my prior question. It's obvious once you see it, but several professional programmers familiar with the original problem and original language looked over the code and overlooked the bug before we caught it (admittedly, in its native environment it was closer to three pages long).

这是一种抽象形式的bug,它引导我进入了构成我先前问题的基础的代码。很明显,一旦你看到了它,但是一些熟悉原始问题和原始语言的专业程序员检查了代码,并在我们发现bug之前忽略了它(诚然,在它的原生环境中,它的长度接近3页)。

Please obfuscate your responses so latecomers can have some fun too.

请把你的回答弄得乱七八糟,这样迟到的人也会有一些乐趣。

most_bang_for_buck_score = 0.0
most_bang_for_buck_order = []
for appetizer in appetizers
    total_cost     = appetizer.cost
    total_calories = appetizer.calories
    for salad in salads
        total_cost     = total_cost     + salad.cost
        total_calories = total_calories + salad.calories
        for entree in entrees
            total_cost     = total_cost     + entree.cost
            total_calories = total_calories + entree.calories
            for desert in deserts
                total_cost     = total_cost      + desert.cost
                total_calories = total_calories + desert.calories
                if total_calories/total_cost > most_bang_for_buck_score
                    most_bang_for_buck_score = total_calories/total_cost
                    most_bang_for_buck_order = [appetizer,salad,entree,desert]
 print "You'll get the most food energy for your money ordering ",most_bang_for_buck_order,"\n"

6 个解决方案

#1


6  

ROT13 (or hover on this link to see the non-obfuscated answer as a tooltip)

ROT13(或将鼠标悬停在此链接上,以查看非混淆的答案作为工具提示)

V qba'g guvax lbh'er erfrggvat gbgny_pbfg/gbgny_pnybevrf ba rnpu cnff. Lbh bhtug gb whfg or fhzzvat rirelguvat hc bapr va gur vaarezbfg ybbc.

这是一种很好的方法。英国广播公司(英国广播公司)的英国广播公司。

Ol gur jnl lbh fnir gur beqre nf bar bs rnpu vgrz, V'z nffhzvat lbh'er bayl fhccbfrq gb or univat bar bs rnpu sbbq pngrtbel.

在此基础上,我们将对其进行分析,并对其进行分析,并对其进行分析。

#2


4  

Sbe rnpu vgrz (nccrgvmre, fnynq, ragerr, qrffreg) lbh'er nqqvat gur pbfg bs gur arj vgrz, ohg lbh'er abg fhogenpgvat bhg gur pbfg bs gur cerivbhf vgrz sebz gur fnzr pngrtbel. Guvf jvyy pnhfr lbh gb fhz gur gbgny pbfg bs nyy vgrzf va nyy pngrtbevrf, vafgrnq bs nyy pbzovangvbaf.

这是一个很好的例子,它是一个很好的例子,它是一个很好的例子,它是一个很好的例子。如果你想要你的产品,你就可以把它卖给你。

ROT13

ROT13

#3


2  

Vf vg ernyyl guvf rnfl? Lbh'er abg erfrggvat gbgny_pbfg be gbgny_pnybevrf va rnpu ybbc vgrengvba.

你想要什么?这是一个非常好的例子。

V pna frr ubj gur oht jbhyq or uneq gb fcbg ol ybbxvat ng gur bhgchg, gubhtu. Gur engvb bs gur gjb inyhrf zvtug abg punatr zhpu rira nf gurve inyhrf terj ovttre.

这是一个很好的例子。你可以在这里看到你的照片。

ROT13, hover to see non-obfuscated as tooltip

ROT13,鼠标悬停,以看到非模糊的工具提示。

#4


1  

So, the answer is clear from the other posts. I'm just going to use this space to do this in a functional language (Haskell), where this kind of bug doesn't happen.

因此,从其他帖子中可以清楚地看到答案。我将使用这个空间在函数式语言(Haskell)中这样做,这样的bug不会发生。

Just getting the maximum score is easy, assuming lists of (cost, calorie) tuples:

只要获得最大的分数就很容易,假设(成本,卡路里)元组的列表:

let bang (cost, cal) = cal / cost
 in maximum [bang c1 + bang c2 + bang c3 + bang c4 | 
                                 c1 <- appetizers,
                                 c2 <- salads,
                                 c3 <- entrees,
                                 c4 <- deserts]

If you represent your data as tuples of (name, (cost, calorie)), then it's more annoying. The best way would be to define a maximum function that uses a "key" parameter, like in Python (there might be one already, but I don't know about it). Then, you would just do this:

如果你将你的数据表示为元组(名称、成本、卡路里),那么它就更烦人了。最好的方法是定义一个使用“key”参数的最大函数,比如在Python中(可能已经有一个了,但是我不知道)。然后,你可以这样做:

maximumkey snd [([n1, n2, n3, n4], bang c1 + bang c2 + bang c3 + bang c4) | 
                      (n1, c1) <- appetizers,
                      (n2, c2) <- salads,
                      (n3, c3) <- entrees
                      (n4, c4) <- deserts]

To complete the solution. This is how (roughly) how maximum is defined:

完成解决方案。这就是如何定义最大值的方法:

maximum xs = foldl1 max xs

So maximumkey is:

所以maximumkey是:

maximumkey f xs = foldl1 (\(a,b) -> if (f a) > (f b) then a else b) xs

Note: This is a bit inefficient, since it calls 'f' more than once per element.

注意:这有点低效,因为它每一个元素调用“f”不止一次。

#5


0  

V thrff nyy gur cebqhpgf fubhyq or grfgrq gbtrgure :

五、二、二、四、四、三、四、四、四、三、四、四、四、四、三、四、四、四、四、三、四、四、四、四、四、四、四、四、四、四、四、四、

nccrgvmrefragerrffnynqf*qrfregf = gbgny pbzcnevfbaf

nccrgvmrefragerrffnynqf * qrfregf = gbgny pbzcnevfbaf

jurernf urer vg frrzf yvxr gurer'f nyjnlf gur fnzr cebqhpg bs gur svefg vgrzf pbzcnerq gb rnpu bs gur qrfregf (v.r. 7 pbzcnevfbaf sbe 7 qrfregf). vfa'g vg ?

在这一过程中,我们将会有更多的时间和时间来处理这些问题。vfa石头vg吗?

...znlor V whfg qba'g trg gur nofgenpgvba : )

…znlor V . whfg qba'g trg g nofgenpgvba:)

ogj, guvf ebg13 fvgr vf terng : )

ogj, guvf ebg13 fvgr vf terng:)

http://www.rot13.com/index.php

http://www.rot13.com/index.php

#6


0  

Not sure how one would calculate the number of calories in a desert, but I imagine it would be vanishingly small compared to its cost, particularly in today's economy, assuming an entire desert were for sale.

不知道如何计算沙漠中卡路里的数量,但我想这与它的成本相比是微不足道的,特别是在今天的经济中,假设整个沙漠都在出售。

Assuming total_calories and total_cost are some sort of integer data types, each most_bang_for_buck_score will be zero.

假设total_卡路里和total_cost是某种整数类型的数据类型,那么每个大多数的bang_for_buck_score将为零。

I'm guessing the algorithm always prints the first element from each course.

我猜这个算法总是会打印出每个课程的第一个元素。

#1


6  

ROT13 (or hover on this link to see the non-obfuscated answer as a tooltip)

ROT13(或将鼠标悬停在此链接上,以查看非混淆的答案作为工具提示)

V qba'g guvax lbh'er erfrggvat gbgny_pbfg/gbgny_pnybevrf ba rnpu cnff. Lbh bhtug gb whfg or fhzzvat rirelguvat hc bapr va gur vaarezbfg ybbc.

这是一种很好的方法。英国广播公司(英国广播公司)的英国广播公司。

Ol gur jnl lbh fnir gur beqre nf bar bs rnpu vgrz, V'z nffhzvat lbh'er bayl fhccbfrq gb or univat bar bs rnpu sbbq pngrtbel.

在此基础上,我们将对其进行分析,并对其进行分析,并对其进行分析。

#2


4  

Sbe rnpu vgrz (nccrgvmre, fnynq, ragerr, qrffreg) lbh'er nqqvat gur pbfg bs gur arj vgrz, ohg lbh'er abg fhogenpgvat bhg gur pbfg bs gur cerivbhf vgrz sebz gur fnzr pngrtbel. Guvf jvyy pnhfr lbh gb fhz gur gbgny pbfg bs nyy vgrzf va nyy pngrtbevrf, vafgrnq bs nyy pbzovangvbaf.

这是一个很好的例子,它是一个很好的例子,它是一个很好的例子,它是一个很好的例子。如果你想要你的产品,你就可以把它卖给你。

ROT13

ROT13

#3


2  

Vf vg ernyyl guvf rnfl? Lbh'er abg erfrggvat gbgny_pbfg be gbgny_pnybevrf va rnpu ybbc vgrengvba.

你想要什么?这是一个非常好的例子。

V pna frr ubj gur oht jbhyq or uneq gb fcbg ol ybbxvat ng gur bhgchg, gubhtu. Gur engvb bs gur gjb inyhrf zvtug abg punatr zhpu rira nf gurve inyhrf terj ovttre.

这是一个很好的例子。你可以在这里看到你的照片。

ROT13, hover to see non-obfuscated as tooltip

ROT13,鼠标悬停,以看到非模糊的工具提示。

#4


1  

So, the answer is clear from the other posts. I'm just going to use this space to do this in a functional language (Haskell), where this kind of bug doesn't happen.

因此,从其他帖子中可以清楚地看到答案。我将使用这个空间在函数式语言(Haskell)中这样做,这样的bug不会发生。

Just getting the maximum score is easy, assuming lists of (cost, calorie) tuples:

只要获得最大的分数就很容易,假设(成本,卡路里)元组的列表:

let bang (cost, cal) = cal / cost
 in maximum [bang c1 + bang c2 + bang c3 + bang c4 | 
                                 c1 <- appetizers,
                                 c2 <- salads,
                                 c3 <- entrees,
                                 c4 <- deserts]

If you represent your data as tuples of (name, (cost, calorie)), then it's more annoying. The best way would be to define a maximum function that uses a "key" parameter, like in Python (there might be one already, but I don't know about it). Then, you would just do this:

如果你将你的数据表示为元组(名称、成本、卡路里),那么它就更烦人了。最好的方法是定义一个使用“key”参数的最大函数,比如在Python中(可能已经有一个了,但是我不知道)。然后,你可以这样做:

maximumkey snd [([n1, n2, n3, n4], bang c1 + bang c2 + bang c3 + bang c4) | 
                      (n1, c1) <- appetizers,
                      (n2, c2) <- salads,
                      (n3, c3) <- entrees
                      (n4, c4) <- deserts]

To complete the solution. This is how (roughly) how maximum is defined:

完成解决方案。这就是如何定义最大值的方法:

maximum xs = foldl1 max xs

So maximumkey is:

所以maximumkey是:

maximumkey f xs = foldl1 (\(a,b) -> if (f a) > (f b) then a else b) xs

Note: This is a bit inefficient, since it calls 'f' more than once per element.

注意:这有点低效,因为它每一个元素调用“f”不止一次。

#5


0  

V thrff nyy gur cebqhpgf fubhyq or grfgrq gbtrgure :

五、二、二、四、四、三、四、四、四、三、四、四、四、四、三、四、四、四、四、三、四、四、四、四、四、四、四、四、四、四、四、四、

nccrgvmrefragerrffnynqf*qrfregf = gbgny pbzcnevfbaf

nccrgvmrefragerrffnynqf * qrfregf = gbgny pbzcnevfbaf

jurernf urer vg frrzf yvxr gurer'f nyjnlf gur fnzr cebqhpg bs gur svefg vgrzf pbzcnerq gb rnpu bs gur qrfregf (v.r. 7 pbzcnevfbaf sbe 7 qrfregf). vfa'g vg ?

在这一过程中,我们将会有更多的时间和时间来处理这些问题。vfa石头vg吗?

...znlor V whfg qba'g trg gur nofgenpgvba : )

…znlor V . whfg qba'g trg g nofgenpgvba:)

ogj, guvf ebg13 fvgr vf terng : )

ogj, guvf ebg13 fvgr vf terng:)

http://www.rot13.com/index.php

http://www.rot13.com/index.php

#6


0  

Not sure how one would calculate the number of calories in a desert, but I imagine it would be vanishingly small compared to its cost, particularly in today's economy, assuming an entire desert were for sale.

不知道如何计算沙漠中卡路里的数量,但我想这与它的成本相比是微不足道的,特别是在今天的经济中,假设整个沙漠都在出售。

Assuming total_calories and total_cost are some sort of integer data types, each most_bang_for_buck_score will be zero.

假设total_卡路里和total_cost是某种整数类型的数据类型,那么每个大多数的bang_for_buck_score将为零。

I'm guessing the algorithm always prints the first element from each course.

我猜这个算法总是会打印出每个课程的第一个元素。