Created
July 17, 2023 15:27
-
-
Save igormcsouza/0c131e43a9a2778b0e70c4c367257270 to your computer and use it in GitHub Desktop.
The number challenge!
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
from random import shuffle | |
class Ball: | |
consecutives = None | |
number = None | |
def __repr__(self): | |
return f"<number: {self.number}>" | |
def set_cons(self, consecutives=None): | |
self.consecutives = consecutives | |
def set_number(self, number=None): | |
if self._check_availability(number): | |
self.number = number | |
else: | |
raise Exception("Error, it can be set.") | |
def _check_availability(self, number): | |
if number is None: | |
return False | |
if self.consecutives is None: | |
return False | |
if self.number is not None: | |
return False | |
for each in self.consecutives: | |
if each.number in (number - 1, number + 1, number): | |
return False | |
return True | |
def reset(self): | |
self.number = None | |
if __name__ == "__main__": | |
A = Ball() | |
B = Ball() | |
C = Ball() | |
D = Ball() | |
E = Ball() | |
F = Ball() | |
G = Ball() | |
H = Ball() | |
A.set_cons([B, C, D]) | |
B.set_cons([A, C, F, E]) | |
C.set_cons([A, D, G, F, E, B]) | |
D.set_cons([A, C, F, G]) | |
E.set_cons([B, C, F, H]) | |
F.set_cons([C, D, G, H, E, B]) | |
G.set_cons([D, H, F, C]) | |
H.set_cons([G, E, F]) | |
possible_numbers = [1, 2, 3, 4, 5, 6, 7, 8] | |
pn_size = len(possible_numbers) | |
breakpoint() | |
while True: | |
shuffle(possible_numbers) | |
n = next(iter(possible_numbers)) | |
for b in [A, B, C, D, E, F, G, H]: | |
try: | |
b.set_number(n) | |
except: | |
continue | |
else: | |
break | |
for b in [A, B, C, D, E, F, G, H]: | |
if b.number in possible_numbers: | |
possible_numbers.remove(b.number) | |
if pn_size == len(possible_numbers): | |
for b in [A, B, C, D, E, F, G, H]: | |
b.reset() | |
possible_numbers = [1, 2, 3, 4, 5, 6, 7, 8] | |
elif len(possible_numbers) == 0: | |
break | |
pn_size = len(possible_numbers) | |
print( | |
[ | |
A.number, | |
B.number, | |
C.number, | |
D.number, | |
E.number, | |
F.number, | |
G.number, | |
H.number, | |
] | |
) | |
print("--- Final Result:") | |
print([A, B, C, D, E, F, G, H]) | |
for b in [A, B, C, D, E, F, G, H]: | |
for c in A.consecutives: | |
if c.number in (A.number, A.number + 1, A.number - 1): | |
raise Exception("Wrong Answer") |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
This is the video that explains the challenge: https://youtube.com/shorts/Rh8dZt0KgTE?feature=share
The main idea is to put the number on the correct order.
^ Solution!