Last active
December 16, 2019 14:49
-
-
Save toksdotdev/94204ea50f64ba8c0e7a1fd1f2d06337 to your computer and use it in GitHub Desktop.
Give gifts to a group of people such that no family members (with same surname) should give gifts to each other.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class Person: | |
def __init__(self, firstname, lastname): | |
self._firstname = firstname | |
self._lastname = lastname | |
@property | |
def firstname(self): | |
return self._firstname | |
@property | |
def lastname(self): | |
return self._lastname | |
def __str__(self): | |
return f"{self.firstname} {self.lastname}" | |
def __repr__(self): | |
return f"{self.firstname} {self.lastname}" | |
peoples = [ | |
Person("tola", "ade"), | |
Person("jimi", "ades"), | |
Person("sola", "awo"), | |
Person("toks", "nkem"), | |
] | |
def assignGiftPartner(persons, prev, path, result): | |
if len(persons) == 1: | |
path.append(persons[0]) | |
result.append(path) | |
return | |
for person in persons: | |
if prev != None and prev.lastname == person.lastname: | |
continue | |
old = path.copy() | |
old.append(person) | |
assignGiftPartner(persons[1:], person, old, result) | |
result = [] | |
assignGiftPartner(peoples, None, [], result) | |
print(result) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment