I have recently been given quite a complex piece of work to do by my computer science supervisor. The idea is that the function:- [def shape_resistivity(shape,R):] allows the user to input a shape and resistance value like so:

[shape=[\
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']

'R' is an arbitrary value in ohms.

Here, the given shape is a pictorial representation of an 'I' shaped circuit where the positive terminals are at the top, each 'x' represents a resistor cell and are given a resistance value 'R', and the negative cells are at the bottom.

What i am trying to do is take the list of strings, which must be of equal length, and use a nested for loop to go over each character and its neighbours for creation of the relevant equations.

So far, I have constructed the following code which initially indexes the list of strings so i can then recall them to place them into the relevant resistor and node equations.

def index_chars(shape):
    coords=[]
    neg_list=[]
    node_eqn=[]
    rownum=0
    for row in shape:
        rownum+=1
        charnum=0
        for char in row:
            if char==' ':
                rownum=0
                charnum=0
                continue
            else:
                charnum+=1
            if char=='+':
                char='pos term'
                rownum=0
                charnum=0
                coords.append((char,rownum,charnum)) 
            if char=='x':
                char='RC'
                coords.append((char,rownum,charnum))
            if char=='-':
                char='RC'
                neg_list.append((char,rownum-1,charnum))
    return coords,neg_list

I have also tried to use numpy to manipulate arrays in a similar fashion:

from numpy import *
def create_array(shape,R):
    import numpy
    coords=[]
    row_length=0
    for row in shape:
        row_length+=1
        char_length=0
        for char in row:
            char_length+=1
            if char==' ':
                char=0
            else:
                if char=='+':
                    char=1
                if char=='x':
                    char=R
                if char=='-':
                    char=1
            coords.append(char)
            coordinates=numpy.array(coords)
            coordinates.resize([row_length,char_length])
    return coordinates

def index_array(shape,R):
    import numpy
    coordinates=create_array(shape,R)
    index_coord=ndenumerate(coordinates)
    for position,value in index_coord: print position,value

I believe, by using a nested for loop, that i can somehow go through the shape/list of strings and somehow look at each 'x' and look at its neighbours. In pseudo code "Look at x, append x to a list and append the x above it, below it and left and right of it if there should be x's in these positions. This equals one node equation." etc.

Despite being able to index the shape, I cannot think how to write the condition statements necessary to create the above equations.

I hope this makes sense.

Recommended Answers

All 31 Replies

I have recently been given quite a complex piece of work to do by my computer science supervisor. The idea is that the function:- [def shape_resistivity(shape,R):] allows the user to input a shape and resistance value like so:

And what is the expected output?

The required output is a single floating point integer that gives the overall resistance of the shape/circuit.

What i am trying to do is take the list of strings, which must be of equal length, and use a nested for loop to go over each character and its neighbours for creation of the relevant equations.

This solution is similar to a tic-tac-toe game, except the board can be any size. One way to do it is to index a dictionary using a tuple containing the row & column number. The neighbors then would be a row, column tuple for row+1, row-1, col+1, and col-1.

shape=[
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']

shape_dict = {}
for row, rec in enumerate(shape):
    for col, ch in enumerate(rec):
        shape_dict[(row, col)] = ch

print shape_dict[(2,2)]   ## print one as an example

That is a pretty elegant peice of code. Thanks.

I did have something a bit bulkier:

def assign_coords(positions):
    (ref_dict,dict_ref)=({},{})
    for position in positions:
        if not(ref_dict.has_key(position)):
            series=len(ref_dict)
            ref_dict[position]=series
            dict_ref[series]=position
    return (ref_dict,dict_ref)

However, my next objective is to populate a node equation.

Pseudo code - "For x in row 2, col 1, append to empty list[]. Now look to see if there is an x above it....No? well now is there an x to the left.....No? well now look to right....Yes? right, append it to the list[]. Is there now an x underneath this x.....Tes? right well append it to the list[]. This completes the list for the first resistor cell (x). Now move on and do the same for the next x."

This is what I have been battling with. I cant quite get this code to do the job.

Any suggestions?

For x in row 2, col 1, append to empty list[]. Now look to see if there is an x above it....No? well now is there an x to the left.....No? well now look to right....Yes? right, append it to the list[]. Is there now an x underneath this x.....Tes? right well append it to the list[].

Now look to see if there is an x above it (row-1)
well now is there an x to the left (col-1)
well now look to right (col+1)
Is there now an x underneath this x (row+1)

Why not:

shape=[
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']

r=0.123
print "".join(shape).count('x')*r

Plus maybe format and connectivity checking?

Thanks woooee, my apologies. missed the last part of your advice. Yes, I see how that should work.

Well, so far I have constructed my resistor equations. I am yet to do my node equations as previously dicussed.

I am sure my code is grossely inefficient but I am learning.

The code below effectively states in pseudo code - "For each cell in the dictionary, if the cell is a resistor cell then we append that to a list and its neighbour on the right/the next one in order. This pair are then their own list within the larger list. Now go on and do the same for the next resistor in the next column."

The next list states in psuedo code - "For each cell in the dictionary, if the cell is a resistor cell then we append that to a list and its neighbour below. This pair are then their own list in the larger list. Go on and do the same for the next resistor in the next column."

Basically, I want a series of lists from the dictionary where each one contains the first resistor we come along followed by its neighbour on the right. Then the same but coupled with the one below it.

Is there a way I can now split/isolate each mini list on its own and call it individually?

Then I will effectively do the same with the node equations where each resistor will then be in a mini list comprising of all its neighbours up, down, left,and right.

These lists will then go into a peice of support code we have been supplied with which will then return a single floating point integer giving the total resistance of the shape/circuit.

My supervisor has told me that this same script can then be used to analyse laminar flow over an airfoil. God help me.

def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    shape_dict = {}
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            shape_dict[(row, col)] = ch
    for key in shape_dict:
        if shape_dict[key]=='x':
          shape_dict[key]='RC'
          Resist_plus_neigh_on_right.append([(shape_dict[key],key),
                                             (shape_dict[key],key[0],key[1]+1)])
    for key in shape_dict:
        if shape_dict[key]=='RC':
            Resist_plus_neigh_below.append([(shape_dict[key],key),
                                            (shape_dict[key],key[0]+1,key[1])])
    print sorted(Resist_plus_neigh_on_right)
    print sorted(Resist_plus_neigh_below)

Additionaly,

Are there some more elegant and efficient ways of constructing this code so far? Woooee seemed to have it but I could only muster the code above.

That is assuming that my code really is doing what i think it is doing so far.

Additionaly,

Are there some more elegant and efficient ways of constructing this code so far? Woooee seemed to have it but I could only muster the code above.

That is assuming that my code really is doing what i think it is doing so far.

shape=[
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']

def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    shape_dict = {}
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            if ch =='x': ch = 'RC'
            shape_dict[(row, col)] = ch

    for key in shape_dict:
        if shape_dict[key]=='RC':
            ## neighbours must be already in dic so can not put in first loop
          Resist_plus_neigh_on_right.append([
              (shape_dict[key],key),
              (shape_dict[key],key[0],key[1]+1)
              ])
          Resist_plus_neigh_below.append([
              (shape_dict[key],key),
              (shape_dict[key],key[0]+1,key[1])
              ])

    print sorted(Resist_plus_neigh_on_right)
    print sorted(Resist_plus_neigh_below)

shape_index(shape)

This with the input from before seemed at least from the test run to function same as yours.

Lovely. Thanks for that tonyjv.

I have now constructed both my resistor equations and my node equations. However, because of the way i have appended the various values to each of my lists i have some problems.

For list a, I only need 3 pairs per row. I have 4. this is because i am iterating over the fourth cell in the row when carrying out the operation. I only need to count, if you like, [cell 1, cell 2],[cell 2, cell 3],[cell 3, cell 4]. Obviously python is then counting [cell 4, cell 5]. This last one cannot be.

Similarly, for list b, on my last row I am counting one more, again this cannot be.

I cant see where or how to include the conditions to solve this.

shape=[\
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']
       
  
def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    Central_node_plus_neighs=[]
    shape_dict = {}
    
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            if ch =='x': ch = 'RC'
            shape_dict[(row,col)] = ch
            
    for key in shape_dict:
        if shape_dict[key]=='RC':
            ## neighbours must be already in dic so can not put in first loop

            Resist_plus_neigh_on_right.append([
            shape_dict[key],key,
            shape_dict[key],(key[0],key[1]+1)
            ])
            
            a=sorted(Resist_plus_neigh_on_right)
            
            Resist_plus_neigh_below.append([
            shape_dict[key],key,
            shape_dict[key],(key[0]+1,key[1])
            ])
            
            b=sorted(Resist_plus_neigh_below)
            
            Central_node_plus_neighs.append([
            shape_dict[key],key,
            shape_dict[key],(key[0],key[1]+1),
            shape_dict[key],(key[0]+1,key[1]),
            shape_dict[key],(key[0],key[1]-1),
            shape_dict[key],(key[0]-1,key[1])
            ])
            
            c=sorted(Central_node_plus_neighs)
            
    return a,b,c

Any advice?

Just to be clear,

I need 3 equations per row, therefore nine mini lists within list a.

I need 4 equations per row, therefore eight mini lists within list b.

I greatly appreciate anybody who bothers to spend time on this.

Just to be clear,

I need 3 equations per row, therefore nine mini lists within list a.

I need 4 equations per row, therefore eight mini lists within list b.

I greatly appreciate anybody who bothers to spend time on this.

I seem to remember from my student days long ago that this kind of neighbour-formulas can often be generalized in all positions if we put padding of extra row in beginning and end and column left and right. Then the values can take out extra equations in sides. So if we have multiplication with neighbour and want to take out those extra equations we can put the padding of zeroes around the matrix.
Zeroes nullify the equations and we can optimize them out by recognizing them and not putting in the list.

Something like this:

0   0  0  0  0  0
 0  rc rc rc rc  0
 0  rc rc rc rc  0
 0  rc rc rc rc  0
 0  rc rc rc rc  0
 0  0   0  0  0  0

Yeah I see. I am trying to restructure my lists as i believe that the dictionary seems to be ok.

I think the message i just sent should explain the idea in a generalised form:)

Yeah I see. I am trying to restructure my lists as i believe that the dictionary seems to be ok.

I think the message i just sent should explain the idea in a generalised form:)

One alternative came to my mind after is the instead of safe padding with zeroes could use

try:
use the index in equation for the dict
it was OK we are in safe area
construct the equation
except:
got index error, no problem, continue to next equation
this equation was faulty

or it is possible to use dicts property

(x,y) in shapedict

if the tupple in faulty equations is not in dict we do not do nothing. If the referenced pairs are in dict, we put the pairs in list. I am not so clear what are the keys and what are the values in your dicts.

This code with test of in with keys produced 9 lists for first part (a?)

shape=[\
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']
       
  
def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    Central_node_plus_neighs=[]
    shape_dict = {}
    
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            if ch =='x': ch = 'RC'
            shape_dict[(row,col)] = ch
            
    for key in shape_dict:
        if shape_dict[key]=='RC': 
            ## neighbours must be already in the dict so can not put in first loop

            if (key[0],key[1]+1) in shape_dict:
                Resist_plus_neigh_on_right.append([
                shape_dict[key],key,
                shape_dict[key],(key[0],key[1]+1)
                ])
            
            a=sorted(Resist_plus_neigh_on_right)
            
            Resist_plus_neigh_below.append([
            shape_dict[key],key,
            shape_dict[key],(key[0]+1,key[1])
            ])
            
            b=sorted(Resist_plus_neigh_below)
            
            Central_node_plus_neighs.append([
            shape_dict[key],key,
            shape_dict[key],(key[0],key[1]+1),
            shape_dict[key],(key[0]+1,key[1]),
            shape_dict[key],(key[0],key[1]-1),
            shape_dict[key],(key[0]-1,key[1])
            ])
            
            c=sorted(Central_node_plus_neighs)
    print (2,2), (2,2) in shape_dict  ## test
    print (4,5), (4,5) in shape_dict  ## test
            
    return a,b,c

from pprint import pprint as pp
for i in  shape_index(shape): pp(i)

b becomes 12 long with continuing with this style of if statements:

shape=[\
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']
       
  
def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    Central_node_plus_neighs=[]
    shape_dict = {}
    
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            if ch =='x': ch = 'RC'
            shape_dict[(row,col)] = ch
            
    for key in shape_dict:
        if shape_dict[key]=='RC': 
            ## neighbours must be already in the dict so can not put in first loop

            if (key[0],key[1]+1) in shape_dict:
                Resist_plus_neigh_on_right.append([
                shape_dict[key],key,
                shape_dict[key],(key[0],key[1]+1)
                ])
            
            a=sorted(Resist_plus_neigh_on_right)
            
            if (key[0]+1,key[1]) in shape_dict:
                Resist_plus_neigh_below.append([
                shape_dict[key], key,
                shape_dict[key],(key[0]+1,key[1])
                ])
            
            b=sorted(Resist_plus_neigh_below)
            
            if ((key[0],   key[1]+1) in shape_dict and
                (key[0]+1, key[1])   in shape_dict and
                (key[0],   key[1]-1) in shape_dict and
                (key[0]-1, key[1])   in shape_dict):
                Central_node_plus_neighs.append([
                shape_dict[key], key,
                shape_dict[key],(key[0],   key[1]+1),
                shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0],   key[1]-1),
                shape_dict[key],(key[0]-1, key[1])
                ])
            
            c=sorted(Central_node_plus_neighs)
    return a,b,c

from pprint import pprint as pp

for i in  shape_index(shape):
    print "Length",len(i)
    pp(i)
""" Output:
Length 9
[['RC', (2, 0), 'RC', (2, 1)],
 ['RC', (2, 1), 'RC', (2, 2)],
 ['RC', (2, 2), 'RC', (2, 3)],
 ['RC', (3, 0), 'RC', (3, 1)],
 ['RC', (3, 1), 'RC', (3, 2)],
 ['RC', (3, 2), 'RC', (3, 3)],
 ['RC', (4, 0), 'RC', (4, 1)],
 ['RC', (4, 1), 'RC', (4, 2)],
 ['RC', (4, 2), 'RC', (4, 3)]]
Length 12
[['RC', (2, 0), 'RC',  (3, 0)],
 ['RC', (2, 1), 'RC', (3, 1)],
 ['RC', (2, 2), 'RC', (3, 2)],
 ['RC', (2, 3), 'RC', (3, 3)],
 ['RC', (3, 0), 'RC', (4, 0)],
 ['RC', (3, 1), 'RC', (4, 1)],
 ['RC', (3, 2), 'RC', (4, 2)],
 ['RC', (3, 3), 'RC', (4, 3)],
 ['RC', (4, 0), 'RC', (5, 0)],
 ['RC', (4, 1), 'RC', (5, 1)],
 ['RC', (4, 2), 'RC', (5, 2)],
 ['RC', (4, 3), 'RC', (5, 3)]]
Length 6
[['RC', (2, 1), 'RC', (2, 2), 'RC', (3, 1), 'RC', (2, 0), 'RC', (1, 1)],
 ['RC', (2, 2), 'RC', (2, 3), 'RC', (3, 2), 'RC', (2, 1), 'RC', (1, 2)],
 ['RC', (3, 1), 'RC', (3, 2), 'RC', (4, 1), 'RC', (3, 0), 'RC', (2, 1)],
 ['RC', (3, 2), 'RC', (3, 3), 'RC', (4, 2), 'RC', (3, 1), 'RC', (2, 2)],
 ['RC', (4, 1), 'RC', (4, 2), 'RC', (5, 1), 'RC', (4, 0), 'RC', (3, 1)],
 ['RC', (4, 2), 'RC', (4, 3), 'RC', (5, 2), 'RC', (4, 1), 'RC', (3, 2)]]
>>> 
"""

Just to be clear,

I need 3 equations per row, therefore nine mini lists within list a.

I need 4 equations per row, therefore eight mini lists within list b.

I greatly appreciate anybody who bothers to spend time on this.

shape=[\
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']
       
  
def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    Central_node_plus_neighs=[]
    shape_dict = {}
    
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            if ch =='x': ch = 'RC'
            shape_dict[(row,col)] = ch
            
    for key in shape_dict:
        if shape_dict[key]=='RC': 
            ## neighbours must be already in the dict so can not put in first loop

            if (key[0],key[1]+1) in shape_dict:
                Resist_plus_neigh_on_right.append([
                shape_dict[key],key,
                shape_dict[key],(key[0],key[1]+1)
                ])
            
            a=sorted(Resist_plus_neigh_on_right)
            
            if (key[0]+1,key[1]) in shape_dict and shape_dict[(key[0]+1,key[1])]!='-':
                Resist_plus_neigh_below.append([
                shape_dict[key], key,
                shape_dict[key],(key[0]+1,key[1])
                ])
            
            b=sorted(Resist_plus_neigh_below)
            
            if ((key[0],   key[1]+1) in shape_dict and
                (key[0]+1, key[1])   in shape_dict and
                (key[0],   key[1]-1) in shape_dict and
                (key[0]-1, key[1])   in shape_dict):
                    
                Central_node_plus_neighs.append([
                shape_dict[key], key,
                shape_dict[key],(key[0],   key[1]+1),
                shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0],   key[1]-1),
                shape_dict[key],(key[0]-1, key[1])
                ])
            
            c=sorted(Central_node_plus_neighs)
    return a,b,c

from pprint import pprint as pp

for i in  shape_index(shape):
    print "Length",len(i)
    pp(i)
""" Output:
Length 9
[['RC', (2, 0), 'RC', (2, 1)],
 ['RC', (2, 1), 'RC', (2, 2)],
 ['RC', (2, 2), 'RC', (2, 3)],
 ['RC', (3, 0), 'RC', (3, 1)],
 ['RC', (3, 1), 'RC', (3, 2)],
 ['RC', (3, 2), 'RC', (3, 3)],
 ['RC', (4, 0), 'RC', (4, 1)],
 ['RC', (4, 1), 'RC', (4, 2)],
 ['RC', (4, 2), 'RC', (4, 3)]]
Length 8
[['RC', (2, 0), 'RC', (3, 0)],
 ['RC', (2, 1), 'RC', (3, 1)],
 ['RC', (2, 2), 'RC', (3, 2)],
 ['RC', (2, 3), 'RC', (3, 3)],
 ['RC', (3, 0), 'RC', (4, 0)],
 ['RC', (3, 1), 'RC', (4, 1)],
 ['RC', (3, 2), 'RC', (4, 2)],
 ['RC', (3, 3), 'RC', (4, 3)]]
Length 6
[['RC', (2, 1), 'RC', (2, 2), 'RC', (3, 1), 'RC', (2, 0), 'RC', (1, 1)],
 ['RC', (2, 2), 'RC', (2, 3), 'RC', (3, 2), 'RC', (2, 1), 'RC', (1, 2)],
 ['RC', (3, 1), 'RC', (3, 2), 'RC', (4, 1), 'RC', (3, 0), 'RC', (2, 1)],
 ['RC', (3, 2), 'RC', (3, 3), 'RC', (4, 2), 'RC', (3, 1), 'RC', (2, 2)],
 ['RC', (4, 1), 'RC', (4, 2), 'RC', (5, 1), 'RC', (4, 0), 'RC', (3, 1)],
 ['RC', (4, 2), 'RC', (4, 3), 'RC', (5, 2), 'RC', (4, 1), 'RC', (3, 2)]]
"""

Oh dear, ive got myself into a mess with this now.

Basically, the narrative in the code says what i,m trying to do but i am not getting the desired output which is the following:

'''Length 9
[['RC', (2, 0), 'RC', (2, 1)],
 ['RC', (2, 1), 'RC', (2, 2)],
 ['RC', (2, 2), 'RC', (2, 3)],
 ['RC', (3, 0), 'RC', (3, 1)],
 ['RC', (3, 1), 'RC', (3, 2)],
 ['RC', (3, 2), 'RC', (3, 3)],
 ['RC', (4, 0), 'RC', (4, 1)],
 ['RC', (4, 1), 'RC', (4, 2)],
 ['RC', (4, 2), 'RC', (4, 3)]]
Length 8
[['RC', (2, 0), 'RC', (3, 0)],
 ['RC', (2, 1), 'RC', (3, 1)],
 ['RC', (2, 2), 'RC', (3, 2)],
 ['RC', (2, 3), 'RC', (3, 3)],
 ['RC', (3, 0), 'RC', (4, 0)],
 ['RC', (3, 1), 'RC', (4, 1)],
 ['RC', (3, 2), 'RC', (4, 2)],
 ['RC', (3, 3), 'RC', (4, 3)]]
Length 12
[[['RC', (2, 0)], ['RC', (2, 1), 'RC', (3, 0)]],
 [['RC', (2, 1)], ['RC', (2, 2), 'RC', (3, 1)'RC', (2, 0)]],
 [['RC', (2, 2)], ['RC', (2, 3), 'RC', (3, 2)'RC', (2, 1)]],
 [['RC', (2, 3)], ['RC', (3, 3), 'RC', (2, 2)]],
 [['RC', (3, 0)], ['RC', (3, 1), 'RC', (4, 0), 'RC', (2, 0)]],
 [['RC', (3, 1)], ['RC', (3, 2), 'RC', (4, 1), 'RC', (2, 1)'RC', (3, 0)]],
 [['RC', (3, 2)], ['RC', (3, 3), 'RC', (4, 2), 'RC', (2, 2)'RC', (3, 1)]],
 [['RC', (3, 3)], ['RC', (3, 2), 'RC', (4, 3), 'RC', (2, 3)]],
 [['RC', (4, 0)], ['RC', (3, 0), 'RC', (4, 1)]],
 [['RC', (4, 1)], ['RC', (3, 1), 'RC', (4, 2)'RC', (4, 0)]],
 [['RC', (4, 2)], ['RC', (3, 2), 'RC', (4, 3)'RC', (4, 1)]],
 [['RC', (4, 3)], ['RC', (3, 3), 'RC', (4, 2)]]]'''

I think you can see the algorithm I am trying to achieve but obviously I am getting tangled up in my conditional code which seems far too inefficient.

The code at present is:

shape=[\
    '    ',
    '++++',
    'xxxx',
    'xxxx',
    'xxxx',
    '----',
    '    ']
 
def shape_index(shape):
    Resist_plus_neigh_on_right=[]
    Resist_plus_neigh_below=[]
    Central_node_plus_neighs=[]
    listed1=[]
    listed2=[]
    shape_dict = {}
 
    for row, rec in enumerate(shape):
        for col, ch in enumerate(rec):
            if ch =='x': ch = 'RC'
            elif ch==' ': continue
            shape_dict[(row,col)] = ch
    print shape_dict
    for key in shape_dict:
        if shape_dict[key]=='RC':
 
            if (key[0],key[1]+1) in shape_dict:
                Resist_plus_neigh_on_right.append([
                shape_dict[key],key,
                shape_dict[key],(key[0],key[1]+1)
                ])
 
            a=sorted(Resist_plus_neigh_on_right)

            if (key[0]+1,key[1]) in shape_dict and shape_dict[(key[0]+1,key[1])]!='-':
                Resist_plus_neigh_below.append([
                shape_dict[key], key,
                shape_dict[key],(key[0]+1,key[1])
                ])
 
            b=sorted(Resist_plus_neigh_below)
            '''Here, take the first RC cell and its neighbours'''
            if ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]-1,key[1])]=='+' and
                (key[0]==max(key))):
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0], key[1]+1),
                shape_dict[key],(key[0]+1, key[1])]
                ])
                '''Here, take the next two RC cells and their respective neighbours'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]-1,key[1])]=='+' and
                (key[0]+1, key[1]) in shape_dict and
                (key[0], key[1]+1) in shape_dict and
                shape_dict[(key[0],key[1]+1)]=='RC' and
                (key[0], key[1]-1) in shape_dict and
                shape_dict[(key[0],key[1]-1)]=='RC'):
                    
               Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0], key[1]+1),
                shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0], key[1]-1)]
                ])
               
            ##'''Here, take the last RC cell in the top row and its respective neighbours'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]-1,key[1])]=='+' and
                (key[1]==max(key))):
                    
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0], key[1]-1)]
                ])

                
                '''Here, take the first RC cell in the middle rows.....'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]-1,key[1])]=='RC' and
                (key[0]+1, key[1]) in shape_dict and
                shape_dict[(key[0]+1,key[1])]!='-' and
                (key[1]==min(key))):
                    
                Central_node_plus_neighs.append([
               [shape_dict[key], key],
                [shape_dict[key],(key[0], key[1]+1),
                shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0]-1, key[1])]
                ]) 
                '''Here, take the middle RCs in the middle rows...'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]-1,key[1])]=='RC' and
                (key[0]+1, key[1]) in shape_dict and
                shape_dict[(key[0]+1,key[1])]!='-' and
                (key[0], key[1]+1) in shape_dict and
                shape_dict[(key[0],key[1]+1)]=='RC' and
                (key[0], key[1]-1) in shape_dict and
                shape_dict[(key[0],key[1]-1)]=='RC'):
                    
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0], key[1]+1),
                shape_dict[key],(key[0], key[1]-1),
                shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0]-1, key[1])]
                ])
                '''Here, take the last RC in the middle row...'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]-1,key[1])]=='RC' and
                (key[0]+1, key[1]) in shape_dict and
                shape_dict[(key[0]+1,key[1])]=='RC' and
                (key[1]==max(key))):
                    
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0]+1, key[1]),
                shape_dict[key],(key[0], key[1]-1),
                shape_dict[key],(key[0]-1, key[1])]
                ])
                '''Here, take the first RC on the bottom row...'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]+1,key[1])]=='-' and
                (key[0]-1, key[1]) in shape_dict):
                    
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0]-1, key[1]),
                shape_dict[key],(key[0], key[1]+1)]
                ])
                '''Here, take the middle RCs on ther bottom row...'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]+1,key[1])]=='-' and
                (key[0]-1, key[1]) in shape_dict and
                (key[0], key[1]+1) in shape_dict and
                shape_dict[(key[0],key[1]+1)]=='RC' and
                (key[0], key[1]-1) in shape_dict and
                shape_dict[(key[0],key[1]-1)]=='RC'):
                    
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0], key[1]+1),
                shape_dict[key],(key[0]-1, key[1]),
                shape_dict[key],(key[0], key[1]-1)]
                ])
                '''Here, take the last RC on the bottom row...'''
            elif ((key[0], key[1]) in shape_dict and
                shape_dict[(key[0]+1,key[1])]=='-' and
                (key[0]-1, key[1]) in shape_dict and
                (key[0]==max(key))):
                Central_node_plus_neighs.append([
                [shape_dict[key], key],
                [shape_dict[key],(key[0]-1, key[1]),
                shape_dict[key],(key[0], key[1]-1)]
                ])
                
            c=sorted(Central_node_plus_neighs)
                
        
    return a,b,c


    
from pprint import pprint as pp
 
for i in shape_index(shape):
    print "Length",len(i)
    pp(i)

Any advice?

Evidently, it is my third list, for the node equations that is incorrect. Lists a and b are working correctly.

Correction, the required output is the following:

Length 12
[[['RC', (2, 0)], ['RC', (2, 1), 'RC', (3, 0),'RC', (4, 0)]],
[['RC', (2, 1)], ['RC', (2, 2), 'RC', (3, 1)'RC', (2, 0),'RC', (4, 1)]],
[['RC', (2, 2)], ['RC', (2, 3), 'RC', (3, 2)'RC', (2, 1),'RC', (4, 2)]],
[['RC', (2, 3)], ['RC', (3, 3), 'RC', (2, 2),'RC', (4, 3)]],
[['RC', (3, 0)], ['RC', (3, 1), 'RC', (4, 0), 'RC', (2, 0)]],
[['RC', (3, 1)], ['RC', (3, 2), 'RC', (4, 1), 'RC', (2, 1)'RC', (3, 0)]],
[['RC', (3, 2)], ['RC', (3, 3), 'RC', (4, 2), 'RC', (2, 2)'RC', (3, 1)]],
[['RC', (3, 3)], ['RC', (3, 2), 'RC', (4, 3), 'RC', (2, 3)]],
[['RC', (4, 0)], ['RC', (3, 0), 'RC', (4, 1),'RC', (2, 0)]],
[['RC', (4, 1)], ['RC', (3, 1), 'RC', (4, 2)'RC', (4, 0),'RC', (2, 1)]],
[['RC', (4, 2)], ['RC', (3, 2), 'RC', (4, 3)'RC', (4, 1),'RC', (2, 2)]],
[['RC', (4, 3)], ['RC', (3, 3), 'RC', (4, 2),'RC', (2, 3)]]]'''

Do you mean

[[['RC', (2, 0)], ['RC', (2, 1), 'RC', (3, 0), 'RC', (4, 0)]],
 [['RC', (2, 1)], ['RC', (2, 2), 'RC', (3, 1), 'RC', (2, 0), 'RC', (4, 1)]],
 [['RC', (2, 2)], ['RC', (2, 3), 'RC', (3, 2), 'RC', (2, 1), 'RC', (4, 2)]],
 [['RC', (2, 3)], ['RC', (3, 3), 'RC', (2, 2), 'RC', (4, 3)]],
 [['RC', (3, 0)], ['RC', (3, 1), 'RC', (4, 0), 'RC', (2, 0)]],
 [['RC', (3, 1)], ['RC', (3, 2), 'RC', (4, 1), 'RC', (2, 1), 'RC', (3, 0)]],
 [['RC', (3, 2)], ['RC', (3, 3), 'RC', (4, 2), 'RC', (2, 2), 'RC', (3, 1)]],
 [['RC', (3, 3)], ['RC', (3, 2), 'RC', (4, 3), 'RC', (2, 3)]],
 [['RC', (4, 0)], ['RC', (3, 0), 'RC', (4, 1), 'RC', (2, 0)]],
 [['RC', (4, 1)], ['RC', (3, 1), 'RC', (4, 2), 'RC', (4, 0), 'RC', (2, 1)]],
 [['RC', (4, 2)], ['RC', (3, 2), 'RC', (4, 3), 'RC', (4, 1), 'RC', (2, 2)]],
 [['RC', (4, 3)], ['RC', (3, 3), 'RC', (4, 2), 'RC', (2, 3)]]]

Your required output has points without comma before 'RC'.

The comma is not crucial providing I can then manipulate these lists in the usual way.

If constructing these list is easier without the comma then fine. I tried to build the dictionary slightly different to then enable me to use a nested forloop to try and append the neg and pos terminals to their respective equations i.e:

[[['RC', (2, 0)], ['RC', (2, 1), 'RC', (3, 0), 'RC', (4, 0)]],

Just to be clear,

I have omitted spaces being counted as characters in order allow any shape/circuit to be used by the code and this so far i have been unsuccessful in. The problem is just the node equations in the last list it would seem.

Maybe it would work better to do iteration of the dictionary and do some kind of recursive connection routine instead.

Or do

#Pseudocode
for first_line:
for middle_line:
    for first_item:
    for middle:
    for last item:
for last_line:

middle items are slice(1,-1) of the list.

Ok, well, I have gone back to basics on this now and would like to try another approach. Suppose I wanted to directly append a particular cell to a list whilst first going through the list of strings.

pseudo code:

"for the shape shown below, go through each row and column in turn. when you get to a positive cell, append it to a cell and any neighbours it may have directly to its left, right, above and below. not diagonally. If a neighbouring site is vacant, i.e. has a space or is the first or last cell in the string; append only the available neighbours."

Therefore for the first positive cell, it would be in a list with just the positive cell to its right and the x cell below it.

whereas in the second row of x's, the third x along would be appended to a list with the x's either side of it, below it and the positive cell above it. therefore that list would be five cells in length.

If coordinates for each cell can be applied during this process then that would be ideal.

shape=[\
    '      ',
    ' ++++ ',
    ' xxxx ',
    ' xxxx ',
    ' xxxx ',
    ' ---- ']

Is this any help to you, I made this program that lists valid neighbours based on the shape.

"""for the shape shown below, go through each row and column in turn.
when you get to a positive cell, append it to a cell and any neighbours
it may have directly to its left, right, above and below. not diagonally.
If a neighbouring site is vacant, i.e. has a space or is the first or
last cell in the string; append only the available neighbours."""

shape=[\
    '      ',
    ' ++++ ',
    ' xxxx ',
    ' xxxx ',
    ' xxxx ',
    ' ---- ']

up=(-1,0)
down=(1,0)
left=(0,-1)
right=(0,1)


directions=(right,down,left,up)

def getneigh(sh,i,j,d=directions):
    res=[]
    for a,b in d:
        x,y=a+i,b+j
        if (shape[i][j] in 'x+-' and
            0<=x<len(shape[0]) and
            0<=y<len(shape)):
            if shape[x][y]=='x':
                res.extend(['RC', (x,y)])
            elif shape[x][y] in '+-':
                res.extend([shape[x][y], (x,y)])
    return res

neighbours=dict()

for i,line in enumerate(shape):
    for j, column in enumerate(line):
        neighbours[(i,j)]=getneigh(shape,i,j)

for i,j in sorted(neighbours.keys()):
    if neighbours[(i,j)]: print (i,j),neighbours[(i,j)]
>>> 
(1, 1) ['+', (1, 2), 'RC', (2, 1)]
(1, 2) ['+', (1, 3), 'RC', (2, 2), '+', (1, 1)]
(1, 3) ['+', (1, 4), 'RC', (2, 3), '+', (1, 2)]
(1, 4) ['RC', (2, 4), '+', (1, 3)]
(2, 1) ['RC', (2, 2), 'RC', (3, 1), '+', (1, 1)]
(2, 2) ['RC', (2, 3), 'RC', (3, 2), 'RC', (2, 1), '+', (1, 2)]
(2, 3) ['RC', (2, 4), 'RC', (3, 3), 'RC', (2, 2), '+', (1, 3)]
(2, 4) ['RC', (3, 4), 'RC', (2, 3), '+', (1, 4)]
(3, 1) ['RC', (3, 2), 'RC', (4, 1), 'RC', (2, 1)]
(3, 2) ['RC', (3, 3), 'RC', (4, 2), 'RC', (3, 1), 'RC', (2, 2)]
(3, 3) ['RC', (3, 4), 'RC', (4, 3), 'RC', (3, 2), 'RC', (2, 3)]
(3, 4) ['RC', (4, 4), 'RC', (3, 3), 'RC', (2, 4)]
(4, 1) ['RC', (4, 2), '-', (5, 1), 'RC', (3, 1)]
(4, 2) ['RC', (4, 3), '-', (5, 2), 'RC', (4, 1), 'RC', (3, 2)]
(4, 3) ['RC', (4, 4), '-', (5, 3), 'RC', (4, 2), 'RC', (3, 3)]
(4, 4) ['-', (5, 4), 'RC', (4, 3), 'RC', (3, 4)]
(5, 1) ['-', (5, 2), 'RC', (4, 1)]
(5, 2) ['-', (5, 3), '-', (5, 1), 'RC', (4, 2)]
(5, 3) ['-', (5, 4), '-', (5, 2), 'RC', (4, 3)]
(5, 4) ['-', (5, 3), 'RC', (4, 4)]
>>>

maybe i exchanged left/right and up/down?

Excellent!!! I should be able to now transform these lists into the exact format, i.e. "%06d"%n etc and then start to see what the support code will do. Hopefully, it will only offer a few possible solutions and then from there i can chisle it down to the right one.

This is quite different from the tactic i was trying with my tutor so hopefully i can use this code to overcome my current snags. Talk about a steep learning curve!!!

Thanks again, will keep you posted and hopefully have the finished article by next week.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.