Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Help to find a command
#5
I still feel that description of the problem and your idea how to solve it are mixed.

How I understand the problem: you have list which consists lists as elements. List represents chain and element lists represent links. Links are represented with integer pair where first number indicates strength and second location. It can be expressed something like that (this ain't code, this is visualisation of data with descriptive names):

chain = link_1: (strength, location), link_2: (strength, location)...

You apply tension (which is integer) to chain and need to know which links will break. Link will break only when tension is larger than strength i.e if they are equal chain stays in one piece.

Sidenote: it puzzles me that if first link of chain breaks then the chain is broken and it can't break in another link (but I don't know the whole picture)

I am wondering whether it makes point to find weakest link? There is no point to compare tension and strength if we know that tension is smaller. We can find strengths of all links (it assumes that strength is first integer, in you first post strength seemed to be second integer):

In [1]: chain = [[10, 20], [30, 40], [50, 60]]

In [2]: [link[0] for link in chain]        # give me first element of every link in chain
Out[2]: [10, 30, 50]

In [3]: min([link[0] for link in chain])   # find the smallest value i.e. strength of weakest link
Out[3]: 10
If I know this value (strength of weakest link) I know that it is kind of pointless to compare with tension values <= 10.

Now to the tension. What do you actually need to know? At which tension link will not hold anymore and chain will be broken? You can just min + 1 and have your answer. If you have fixed tension then one should compare strength to tension to find out whether it will break or not.

If you need the location of weakest link:

In [4]: min([(link[0], i) for i, link in enumerate(chain)])
Out[4]: (10, 0)
Weakest link in chain is at index 0 (first element) with value 10.

If you need compare all links to some arbitrary tension, let's say 35:

In [5]: tension = 35

In [6]: [(link[0], i) for i, link in enumerate(chain) if tension > link[0]]
Out[6]: [(10, 0), (30, 1)]
Links at index position 0 and 1 will break.

I personally think that appending some value to link is not good idea. If you know location of the 'broken' links you can access them by their location. However, if it is needed:

In [7]: [link +  [0] if tension > link[0] else link for link in chain]
Out[7]: [[10, 20, 0], [30, 40, 0], [50, 60]]
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
Reply


Messages In This Thread
Help to find a command - by andre_fermart - Feb-02-2019, 08:06 AM
RE: Help to find a command - by stullis - Feb-02-2019, 04:15 PM
RE: Help to find a command - by perfringo - Feb-02-2019, 11:47 PM
RE: Help to find a command - by perfringo - Feb-03-2019, 12:51 PM
RE: Help to find a command - by andre_fermart - Feb-04-2019, 08:01 AM
RE: Help to find a command - by perfringo - Feb-04-2019, 09:49 AM
RE: Help to find a command - by andre_fermart - Feb-05-2019, 05:47 AM
RE: Help to find a command - by perfringo - Feb-05-2019, 09:01 AM
RE: Help to find a command - by perfringo - Feb-05-2019, 12:15 PM
RE: Help to find a command - by andre_fermart - Feb-12-2019, 02:34 AM

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020