#! /usr/bin/python



__author__ = 'Predrag Pejovic'
__license__ = 'GPLv3+'
__date__ = '2021-04-08'



################################################################################



from pylab import *
import sys
from engineeringnotation import *



# collect the data

print()
ok = False
while not ok:
    I0str = input('I_0 [A] = ')
    try:
        I0 = float(I0str)
        ok = True
    except EOFError:
        print
        sys.exit(1)
    except KeyboardInterrupt:
        print
        sys.exit(1)
    except:
        pass
        
ok = False
while not ok:
    R0str = input('R_0 [ohm] = ')
    try:
        R0 = float(R0str)
        ok = True
    except EOFError:
        print
        sys.exit(1)
    except KeyboardInterrupt:
        print
        sys.exit(1)
    except:
        pass
print()

I = [I0]
c = 0
out = False
while not out:
    c += 1
    ok = False
    while not ok:
        Ikstr = input('I_{:d} [A] (zero to finish) = '.format(c))
        try:
            Ik = float(Ikstr)
            if Ik == 0:
                out = True
                ok = True
            elif Ik > I[-1]:
                I.append(Ik)
                ok = True
            else:
                pass
        except EOFError:
            print
            sys.exit(1)
        except KeyboardInterrupt:
            print
            sys.exit(1)
        except:
            pass
print()
            

# compute the resistors

if c == 1:
    print()
    print('nothing to do!')
    print()

if c >= 2:

    Rs = R0 * I0 / (I[1] - I0)

    if c == 2:

        print()
        print('R_1 = {:6.4f} ohm'.format(Rs))
        print()

    else:

        a = empty(c)
        R = empty(c)
        a[0] = 0 # arbitrary
        R[0] = 0 # arbitrary
        ca = 0
        print()
        Rx = 0
        for k in range(c - 1, 0, -1):
            a[k] = I[1] / I[k]
            R[k] = a[k] * Rs - Rx
            Rx += R[k]
        for k in range(1, c):
            print('R_{:d} = {:s}ohm'.format(k, toengstring(R[k], fmtm = '6.2f')))
        print()
        
