Skip to content

Instantly share code, notes, and snippets.



Created May 6, 2013
What would you like to do?
JGC's "one-eyed robot" problem, implemented in Python. See
import random
class ball:
def __init__(self, color):
self.color = color
self.examined = 0
def look(self):
self.examined += 1
return self.color
class buckets:
def __init__(self, size=15):
self.line = [ball(random.choice(['r', 'g', 'b'])) for _ in range(size)]
self.i = 0
self.r = 0
self.b = size-1
def swap(self, i, j):
self.line[i], self.line[j] = self.line[j], self.line[i]
def check(self):
c = self.line[self.i].look()
if c == 'r':
self.swap(self.i, self.r)
self.r += 1
self.i += 1
elif c == 'b':
self.swap(self.i, self.b)
self.b -= 1
self.i += 1
def solve(self):
while self.i <= self.b:
b = buckets()
print([x.color for x in b.line])
print([x.color for x in b.line])
print([x.examined for x in b.line])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.