0

I just watched a veritaseum video which gave me an idea for a project, (https://www.youtube.com/watch?v=iSNsgj1OCLA). Basically, there are 100 prisioners, 100 boxes and a paper slip with a random number on each box. The prisioners' job is to find their own number (1-100) while looking at just half the boxes. The strategy involves each prisioner starting from their own number (prisioner1 goes to box1). I downscaled my project so there's just 10 of each since I'm still kinda new to this stuff, however, I plan on upscaling it for fun. Because of this, I want to automate as much of this process as I can.

Now for the code. Prisioner 1 has to go looking for Box 1 (b1). The problem is that Prisioners are in an array whereas Boxes are in a class. Since they have the number 1 in common, is there a way to automate this process? Maybe something like box[n] (??). I don't truly know what I'm talking about, here's my code, have fun.

import random

prisioner = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
boxes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
papers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
attempts = 5

class Box: 
    def __init__(self, box_n, paper):
        self.box_n = box_n
        self.paper = paper

    def print_boxes(self):
        print(f'{self.box_n}({self.paper})  ')

b1 = Box(boxes[0], random.choice(papers))
boxes.remove(b1.box_n)
papers.remove(b1.paper)

b2 = Box(boxes[0], random.choice(papers))
boxes.remove(b2.box_n)
papers.remove(b2.paper)

b3 = Box(boxes[0], random.choice(papers))
boxes.remove(b3.box_n)
papers.remove(b3.paper)

b4 = Box(boxes[0], random.choice(papers))
boxes.remove(b4.box_n)
papers.remove(b4.paper)

b5 = Box(boxes[0], random.choice(papers))
boxes.remove(b5.box_n)
papers.remove(b5.paper)

b6 = Box(boxes[0], random.choice(papers))
boxes.remove(b6.box_n)
papers.remove(b6.paper)

b7 = Box(boxes[0], random.choice(papers))
boxes.remove(b7.box_n)
papers.remove(b7.paper)

b8 = Box(boxes[0], random.choice(papers))
boxes.remove(b8.box_n)
papers.remove(b8.paper)

b9 = Box(boxes[0], random.choice(papers))
boxes.remove(b9.box_n)
papers.remove(b9.paper)

b10 = Box(boxes[0], random.choice(papers))
boxes.remove(b10.box_n)
papers.remove(b10.paper)


b1.print_boxes()
b2.print_boxes()
b3.print_boxes()
b4.print_boxes()
b5.print_boxes()
b6.print_boxes()
b7.print_boxes()
b8.print_boxes()
b9.print_boxes()
b10.print_boxes()

I know it's wordy and there's probably many ways to have made my code more efficient or faster running, but all I really care about is just getting this project to work. Thanks in advance, nerds.

Nerd
  • 19
  • 2
  • You just need one shuffled list of numbers, nothing more. The list index is the box number, the value is the number in it. Now go through the list 100 times (i.e. just go through each index), and follow the value at the index to the next index etc. – deceze Aug 03 '22 at 05:40

1 Answers1

0

I would insert the boxes into an array, it makes the code much shorter,cleaner and ,much easier to scale

import random

prisioner = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
boxes = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
papers = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
attempts = 5

class Box:
    def __init__(self, box_n, paper):
        self.box_n = box_n
        self.paper = paper

    def print_boxes(self):
        print(f'{self.box_n}({self.paper})  ')

boxes_objs = []
n=len(boxes)
for i in range(n):
    boxes_objs.append(Box(boxes[0], random.choice(papers)))
    boxes.remove(boxes_objs[-1].box_n)
    papers.remove(boxes_objs[-1].paper)

for box in boxes_objs:
    box.print_boxes()

if you have any questions abut this solution feel free to ask me in the comments :)

Ohad Sharet
  • 1,097
  • 9
  • 15