Hello! I’m a student working on a project. I have searched for this solution everywhere. I’ve read every Python documentation regarding lists, re-read the chapters in my textbook that are supposed to cover this, searched online and watched more videos than I can count. I’m wondering if I can’t reach a solution because I’m approaching it from the entirely wrong direction.
My code:
import random
def list_eggs(chkns, eggs):
''' pen = [&s * eggs, 0s * randomly assigned number of indices w/out eggs],
shuffle 0s and &s, index 0s, alter 0s to a value (0/1/2) depending
on number of adjacent &s '''
chkns = chkns - eggs
pen = [0] * chkns
egg_list = ["&"] * eggs
new_pen = pen + egg_list
index = new_pen.index(0)
random.shuffle(new_pen)
print(new_pen)
def main():
chkns = 5
eggs = 4
list_eggs(chkns, eggs)
main()
Ouput:
[0, '&', '&', '&', '&']
Variables chkns and eggs are actually user inputs taken and validated in their own functions. &s represent eggs and 0s, currently, represent chickens (chkns will range from 2 to 100; eggs will range from 1 to (chkns - 1)). I need the 0s to accumulate, update, increment, or some other method such that the position remains 0 if no &s are in adjacent indices, 0 = 1 if there is one adjacent &, or 0 = 2 if both adjacent indices contain an &.
i.e. I need the above output to read:
[1, '&', '&', '&', '&']
I’ve tried various slices, join() attempts, insert attempts, I feel like the solution is closest to this for loop:
for index in index.new_pen[0]:
if index[-1] == "&":
index += 1
elif index[1] == "&":
index += 1
However, this raises an AttributeError:
AttributeError: 'int' object has no attribute 'new_pen'
Finding resources online and collaborating/reaching out to the programming community is encouraged. I want to do everything I can to find a solution before emailing my professor for hints/help. Thank you!