Skip to content

Commit

Permalink
0-lockboxes.py
Browse files Browse the repository at this point in the history
  • Loading branch information
Ambesawi committed Oct 7, 2023
1 parent b7edb97 commit 23ac11b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
Binary file added 0x01-lockboxes/.0-lockboxes.py.swp
Binary file not shown.
27 changes: 27 additions & 0 deletions 0x01-lockboxes/0-lockboxes.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
#!/usr/bin/python3
"""
Defines a function canUnlockAll that determines if a box can be unlock
"""


def canUnlockAll(boxes):
"""
A method that determines if all the boxes can be opened.
Args:
boxes (list of boxes)
Returns:
bool: True if all boxes can be unlock, else False
"""
keys = set([0] + boxes[0])
locked = set()
for box in boxes:
ibox = boxes.index(box)
if ibox not in keys:
if max(keys) > ibox:
locked.add(ibox)
continue
keys |= set(box)
for key in locked:
if key in keys:
keys |= set(boxes[key])
return not bool(locked - keys)

0 comments on commit 23ac11b

Please sign in to comment.