Python Forum
simple task with lists...
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
simple task with lists...
#1
Write the function equal(lst1, lst2), which for two given integer lists will give the result True if these lists are equal after compression and False otherwise. By compression in this task, we mean a replacement on the list
adjacent non-negative (negative) numbers by their sum.

Examples:
• equal([], []) == True
• equal([1], []) == False
• equal([], [1]) == False
• equal([1, -2, 3, -1], [1, -2, 3, -1]) == True

I have no idea how to do it, i have written this, but it seems bad...

def equal(lst1, lst2):
 j = 0
 for elt1 in lst1:
     for k in range(j, len(lst2)):
         if elt1 == lst2[k]:
            j = k+1
            break
     else: 
         return False
 return True
Reply


Messages In This Thread
simple task with lists... - by Maxwell123 - Jun-27-2020, 11:12 AM
RE: simple task with lists... - by Yoriz - Jun-27-2020, 11:28 AM
RE: simple task with lists... - by Maxwell123 - Jun-27-2020, 11:31 AM
RE: simple task with lists... - by GOTO10 - Jun-27-2020, 01:00 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Stuck with using lists to solve task gery576 9 822 Jan-16-2024, 06:29 PM
Last Post: DPaul
  simple task in python Rapito 4 2,162 Nov-22-2021, 10:09 PM
Last Post: jefsummers

Forum Jump:

User Panel Messages

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