0

Here I am trying to solve sequence problem. I am doing 1:1 model mix sequence from the input. Example:

[sw,cr,dw,sw]

Here I have attached my code. I have a bug. I can't find any.

sw = ['1','4','5','8']
dw = ['3','7']
cr = ['2','6']

#sample output = ['sw 1','dw 1','c 1']
max_item = [len(sw),len(dw),len(cr)]
a=0
b=0
c=0
flag_sw = True
flag_dw = True
flag_cr = True
for i in range(max(max_item)):
  if (flag_sw)|(flag_dw)|(flag_cr)== False:
      break
  # Single Weld
  try:
      print(i,"i--swI #",sw[i+a])
      a +=1
  except:
      flag_sw = False
      print("--empty---swI #")
      try:
          print(i,"i---crI #",cr[i+c])
          c +=1
      except:
          try:
              print(i,"i---dwI #",dw[i+b])
              b +=1
          except:
              flag_dw = False
              print("---dw array Empty dwI #")
   # Ceramic
  try:
      print(i,"i---crII #",cr[i+c])
      c +=1
  except:
      flag_cr = False
      print("---empty crII #")
      try:
          print(i,"i---swII #",sw[i+a])
          a +=1
      except:
          print("--- empty SWII---")
          try:
              print(i,"i---dwII #",dw[i+b])
              b +=1
          except:
              flag_dw = False
              print("--- empty dwII---")
   # Double Weld
  try:
      print(i,"i--dwI3 #",dw[i+b])
      b +=1
  except:
      print(i,"i--empty---dwI3 #")
      b +=1
      try:
          print(i,"i---crI3 #",cr[i+c])
          c +=1
      except:
          try:
              print(i,"i---saI3 #", sw[i+a])
              a +=1              
          except:
              print("--empty-swI3 #")

  # Again Single Weld
  try:
      #if flag_sw & flag_dw == False:
      #  a +=1
      print(i,"i-- repeat swI4 #",sw[i+a])
  except:
      try:
        #if flag_sw & flag_dw == False:
        #  c +=1
        print(i,"i--repeat crI4 #",cr[i+c])
      except:
          try:
            #if flag_sw & flag_c == False:
            #  b +=1
              print(i,"i--repeat----dwI4 #",dw[i+b])
          except:
              print("---empty repeat dwI4 #")
  print(" -- cycle ending {}--".format(i))
  print("")

I'm trying to get output like

1,2,3,4
5,6,7,8

But sequence 6 and 7 are missing.

My current output is 1,2,3,4 5,8

Can anyone tell me where I went wrong?

4
  • How should the program behave when any of the lists is exhausted? Or it it guaranteed that there are always enough elements? Commented Dec 18, 2018 at 11:10
  • nope. there is no guaranteed that it has enough elements Commented Dec 18, 2018 at 11:45
  • Note that in Python, | is the bitwise "or", not the logical "or". Not that it makes much difference here but... Commented Dec 18, 2018 at 12:08
  • 1
    Also, your use of exception is wrong. You should NEVER use bare except clauses (always specify the exact exception(s) you except) nor assume anything about the exception you got. Your current code will silent ALL exceptions, preventing you from detecting unexpected ones. Commented Dec 18, 2018 at 12:11

1 Answer 1

1

Not sure if I understand the question correctly. To be honest, your code does not make too much sense.

  • the outer loop iterates max(max_items) times, although it can clearly only iterate at most min(max_items) times, assuming each group is used at least once
  • the condition if flag_sw | flag_dw | flag_cr == False: break will break the loop only if all those flags are False
  • in each of those blocks of nested try/except structures, you take three items from the different groups instead of just the one the comment above suggests 1)
  • by taking sw[i+a], dw[i+b] and cr[i+c] instead of just sw[a], dw[b] and cr[c] you are bound to get index-errors as i increases

Instead, you could just create iterators for the different lists using iter and then get the next elements from those iterators according to the desired pattern.

>>> isw, idw, icr = map(iter, (sw, dw, cr))
>>> num = len(sw) // 2
>>> [[next(isw), next(icr), next(idw), next(isw)] for _ in range(num)]
[['1', '2', '3', '4'], ['5', '6', '7', '8']]

This is assuming that there are enough elements in sw, dw and cr and that none of the iterators will be exhausted too early. If this could be the case, you could use e.g. next(icr, None) and then filter out the None elements from the result, but it's not really clear how the code should behave in this case.


1) If I understand those nested try/except correctly, you want to get the next item from cr if sw is empty, and similar for the others. In this case, you could make your code a lot more compact by defining a helper function, trying to get the next element from a number of iter objects.

def get_next(*iterators):
    for it in iterators:
        try:
            return next(it)
        except StopIteration:
            pass

isw, idw, icr = map(iter, (sw, dw, cr))
while True:
  a = get_next(isw, icr, idw)
  b = get_next(icr, isw, idw)
  c = get_next(idw, isw, icr)
  d = get_next(isw, icr, idw)
  if not any([a, b, c, d]):
      break
  print([x for x in (a, b, c, d) if x is not None])

This will also work if the numbers of items in the lists don't match up.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.