编写一个接受输入的程序或函数:所有可用的电阻和电阻值,并输出一个真实的值,无论是否有可能通过使用这些电阻器获得电阻。
任何格式的输入都可以。
至少有一个可用电阻器,你的程序应该输出至少10个可用电阻器。
所有可用电阻的电阻和所需的电阻将是正整数。
对于可用的电阻,如果也可能有分数值,则所需的电阻可以是近似值。
输出应该是可能的和不可能的任何两个唯一值。
电阻器可以以任何方式连接。
串联电阻:n个串联电阻: Result=R1+R2+R3+....Rn
并联电阻:n个并联电阻:结果=1/(1/R1+1/R2+1/R3+.+1/Rn)
电路可能不需要所有的电阻器来获得所需的电阻(如果是这样的话,输出为真)。
这是密码高尔夫所以最短代码赢了。
R List
110 220,220 -> True
440 220,220 -> True
550 400,300 -> False
3000 1000,3000 -> True
750 1000,3000 -> True
333 1000,1000,1000 -> True (1000||1000||1000=333.333)
667 1000,1000,1000 -> True ((1000+1000)||1000=666.6666)
8000 1000,1000,7000 -> True
190 100,200,333,344,221 -> True
193 105,200,333,344,221 -> True
400 200,100 -> False对最后两个示例的解释:https://physics.stackexchange.com/questions/22252/resistor-circuit-that-isnt-parallel-or-series
发布于 2018-10-31 13:10:10
import functools
def p(l): return functools.reduce(lambda z,x:z+[y+[x]for y in z],l,[[]])
print(l[-1:][0]in[round(a)for a in[sum(a)for a in p([sum(a)for a in p(l)]+[1/sum(c)for c in[[1/b for b in a if b!=0]for a in p(l).copy()]if sum(c)!=0])]])我取所有电阻值的powerset,然后计算级数和1/和(1/值)的并行值,然后取这两组的powerset。当您取所有子集的和并将它们放入一个集合时,则此集合eiter包含值或不包含值。->返回真假
@stephen致谢:)
发布于 2018-10-31 16:42:26
à má
ÈÊ<2?X:X¯1 ïXÅgW @[X+Y1/(1/XÄ/Y]Ãc
mmW c mr øV这是一个艰难的过程,我不得不做一些奇怪的事情来让它发挥作用。我无法从数学上证明这适用于一切,但它适用于所有测试用例以及我的额外拟议测试用例。具体来说,我知道我定义的函数W根据其输入中电阻的顺序给出不同的结果,所以我对每个可能的电阻组合的每个可能的顺序运行它。我也知道,它将产生一个电阻列表,所有的可能创建使用输入电阻器。我不能百分之百确定这两件事结合在一起会产生每一种可能的阻力。
解释:
à Get each combination e.g. [1,2,3] -> [[1,2,3],[1,2],[1,3],[2,3],[1],[2],[3]]
m For each combination...
á Get each order e.g. [1,2,3] -> [[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]
Skip a line to avoid overwriting the target resistance
È Define a function W taking an array input X:
Ê<2?X If X is only 1 resistor, return it as-is
XÅgW Get the resistances from applying W to the tail of X
X¯1 ï Pair the first resistor with each possible tail resistance
@[ ]Ã For each pair get these two values:
X+Y Those resistances in series
1/(1/XÄ/Y Those resistances in parallel
c Collapse all those resistances into a single flat list
mmW For each combination and order, get the resistances using W
c Collapse to a single list of resistances
mr Round each one
øV Return true if the target resistance is in the list, false otherwisehttps://codegolf.stackexchange.com/questions/174875
复制相似问题