9
\$\begingroup\$

Write a program or function that takes input: all resistors available and a resistance value and outputs a truthy value of whether or not it is possible to get the resistance by using those resistors.

Rules:

Any format for input will do.

There will be at least 1 available resistor and your program should output for at least 10 available resistors.

Resistance of all available resistors and the required resistance will be positive integers.

For available resistors if a fractional value is also possible, the required resistance may be the approximated value.(See example)

Output should be any 2 unique values for Possible and Not possible.

The resistors may be connected in any manner.

Series Resistance: For n resistances in series: Result=R1+R2+R3+....Rn

Parallel Resistance: For n resistances in parallel: Result=1/(1/R1+1/R2+1/R3+....+1/Rn)

The circuit may not require all the resistors to get the required resistance (output True if that is the case).

Winner:

This is code-golf so shortest-code wins.

Examples:

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

Explanation for the last two examples: https://physics.stackexchange.com/questions/22252/resistor-circuit-that-isnt-parallel-or-series

\$\endgroup\$
  • \$\begingroup\$ What bout 3 3 1, 3 3 2? \$\endgroup\$ – l4m2 Oct 29 '18 at 8:05
  • \$\begingroup\$ 1.5 is rounded to 2, required resistance will not be given 0(added it to the question), 3 3 will be true \$\endgroup\$ – Vedant Kandoi Oct 29 '18 at 8:08
  • \$\begingroup\$ Nice challenge, but I deal enough with EE as it is... \$\endgroup\$ – user77406 Oct 29 '18 at 21:10
  • \$\begingroup\$ I guess Machematica win? \$\endgroup\$ – l4m2 Oct 31 '18 at 10:13
  • 2
    \$\begingroup\$ This problem is harder than the description makes it sound because general resistor circuits can't be broken down recursively into series and parallel parts, in more complicated ways than the last two test cases. 10 resistors should be easily enough to make such examples. I suspect that none of the currently posted answers work in general, and a correct answer needs to have matrix inversion in some form. \$\endgroup\$ – xnor Oct 31 '18 at 17:04
1
\$\begingroup\$

Python 3, 253 bytes

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])]])

I take the powerset of all resistor values and then calculate the sums for series and 1/sum(1/values) for paralel and then I take a powerset of these two sets. When you take the sum of all the subsets and put them into a set then this set eiter contains the value or not. -> return True/False

@stephen thanks :)

| improve this answer | |
\$\endgroup\$
  • 2
    \$\begingroup\$ Welcome to PPCG! If you need any imports, they need to be included in your code. Additionally, you need to take inputs yourself, rather than assuming the input is in a variable. Also, b != 0 -> b!=0. \$\endgroup\$ – Stephen Oct 31 '18 at 13:19
  • \$\begingroup\$ As Stephen saod, you can't take input through a pre-defined variable, otherwise this is a snippet, which is not allowed. You should change it to either a function, or a full program \$\endgroup\$ – Jo King Oct 31 '18 at 13:28
  • 1
    \$\begingroup\$ It doesn't work for the third test case (also, golfed a bit and with proper input. if you're worried I broke something, your original code doesn't work either) \$\endgroup\$ – Jo King Oct 31 '18 at 13:36
  • \$\begingroup\$ dammit. Ill have a look :( \$\endgroup\$ – SimonSchuler Oct 31 '18 at 13:38
1
\$\begingroup\$

Japt, 52 bytes

à má

ÈÊ<2?X:X¯1 ïXÅgW @[X+Y1/(1/XÄ/Y]Ãc
mmW c mr øV

Try it!

This was a tough one, and I had to do a couple of strange things to make it work. I can't mathematically prove that this works for everything, but it works for all the test cases as well as my extra proposed test case. Specifically, I know that the function I define called W gives different results depending on the order of the resistors in its input so I run it on each possible ordering of each possible combination of resistors. I also know that it will produce a list of resistances that are all possible to create using the input resistors. I don't know with 100% certainty that those two things together end up with every possible resistance.

Explanation:

à       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 otherwise
| improve this answer | |
\$\endgroup\$
0
\$\begingroup\$

Ruby, 153 bytes

f=->v,r{[r]-v==[]||r[1]&&[*2..v.size].any?{|n|v.permutation.any?{|l|[l[0,n].sum,(1.0/l[0,n].reduce(0){|s,x|s+1.0/x}).round].any?{|b|f[l[n..-1]+[b],r]}}}}

Try it online!

Brute force. I mean it.

| improve this answer | |
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.