Create a lisp function(s) that can find and print out the solution for a row of
ID: 3823230 • Letter: C
Question
Create a lisp function(s) that can find and print out the solution for a row of colored balls.
· You have eight colored balls: 1 black, 2 white, 2 red and 3 green.
· The balls in positions 2 and 3 are not green.
· The balls in positions 4 and 8 are the same color.
· The balls in positions 1 and 7 are of different colors.
· There is a green ball to the left of every red ball.
· A white ball is neither first nor last.
· The balls in positions 6 and 7 are not red.
Explanation / Answer
#file arraybag.py
from arrays import Array
class ArrayBag(object):
DEFAULT_CAPACITY=10
# Constructor
def __init__(self, sourceCollection = None):
self._items = Array(ArrayBag.DEFAULT_CAPACITY)
self._size = 0
if sourceCollection:
for item in sourceCollection:
self.add(item)
# Accessor methods
def isEmpty(self):
return len(self) == 0
def __len__(self):
return self._size
def __str__(self):
return "{" + ", ".join(map(str, self)) + "}"
# Mutator methods
def clear(self):
self._size = 0
self._items = Array(ArrayBag.DEFAULT_CAPACITY)
def add(self, item):
self._items[len(self)] = item
self._size += 1
def remove(self, item):
self._size -= 1
#file ball.py
# Ball class
class Ball:
# Class variable
DEFAULT_RADIUS = 1
# Constructor:
def __init__(self, color, radius = 1):
self.color = color
self.radius = radius
# Special methods:
def __eq__(self, other):
if self is other:
return True
if (type(self) != type(other)) or
(self.color != other.color) or
(self.radius != other.radius):
return False
return True
def __str__(self):
return (self.color + " ball, radius " + str(self.radius))
# Accessor methods:
def getColor(self):
return self.color
def getRadius(self):
return self.radius
# Mutator methods:
def setColor(self, color):
self.color = color
def setRadius(self, radius):
self.radius = radius
def distributeBag(bag):
redBag = ArrayBag()
blueBag = ArrayBag()
for x in bag:
if(x.getColor == "red"):
redBag.add(x)
elif(x.getColor=="blue"):
blueBag.add(x)
bag.remove(x)
return (redBag, blueBag)
def ArrayBag(mylist = [], *args):
bag = []
for i in mylist:
bag[i]=mylist
return bag
def printBag(bag):
for x in bag:
str(x)
# Test 1:
print("Test1:")
bag1 = ArrayBag([Ball("red", 1),Ball("red", 2),Ball("red", 3),Ball("blue", 1),
Ball("blue"),Ball("blue", 3)])
print("Original mixed bag:")
printBag(bag1)
redBag, blueBag = distributeBag(bag1)
print("Red bag:")
printBag(redBag)
print("Blue bag:")
printBag(blueBag)
print("Final mixed bag:")
printBag(bag1)
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.