Created
June 15, 2022 11:55
-
-
Save wetherc/e8a52af71afae6af4a14d8b9bb01a5b6 to your computer and use it in GitHub Desktop.
Drunk passenger logic problem
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
import java.io.*; | |
import java.util.*; | |
class Solution { | |
public static void main(String[] args) { | |
int keepsSeat = 0; | |
for (int i = 0; i < 10000; i++) { | |
List<Integer> availableSeats = new ArrayList<>(100); | |
for (int val = 0; val < 100; ++val) { | |
availableSeats.add(val); | |
} | |
//seat first passenger in random location | |
Random r = new Random(); | |
int drunkard = r.nextInt(availableSeats.size()); | |
availableSeats.remove(Integer.valueOf(drunkard)); | |
//seat all other passengers | |
for (int passenger = 1; passenger < 99; passenger++) { | |
if (availableSeats.contains(passenger)) { | |
availableSeats.remove(Integer.valueOf(passenger)); | |
} else { | |
availableSeats.remove(r.nextInt(availableSeats.size())); | |
} | |
} | |
if (availableSeats.contains(99)) { | |
keepsSeat = keepsSeat + 1; | |
} | |
} | |
System.out.println("Last passenger got her seat " + keepsSeat + " times!"); | |
System.out.println("Last passenger had to move " + (10000 - keepsSeat) + " times."); | |
} | |
} |
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
function generateRandomSeatIndex(availableSeats) { | |
min = 0; | |
max = availableSeats; | |
seatIndex = Math.floor(Math.random() * (max - min)) + min; | |
return seatIndex; | |
} | |
function assignSeats(passengers) { | |
let availableSeats = Array(passengers).fill().map((x, i)=>i); | |
let drunkPassenger = generateRandomSeatIndex(availableSeats.length); | |
availableSeats.splice(drunkPassenger, 1); | |
for (let passenger = 1; passenger < (passengers - 1); passenger++) { | |
let seatIndex = availableSeats.indexOf(passenger); | |
if (seatIndex == -1) { | |
let newSeatIndex = generateRandomSeatIndex(availableSeats.length); | |
availableSeats.splice(newSeatIndex, 1); | |
} else { | |
availableSeats.splice(seatIndex, 1); | |
} | |
} | |
if (availableSeats.indexOf(passengers - 1) > -1) { | |
return true; | |
} | |
return false; | |
} | |
let successes = []; | |
for (let trial = 0; trial < 10000; trial++) { | |
successes.push(assignSeats(100)); | |
} | |
console.log( | |
Math.round( | |
(successes.reduce((a, b) => a + b, 0) / successes.length) * 100 | |
)); |
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
import random | |
tally = [] | |
for _ in range(1000): | |
seats = [seat for seat in range(100)] | |
for passenger in range(99): | |
if passenger == 0: | |
seats.pop(seats.index(random.sample(seats, 1)[0])) | |
else: | |
if passenger in seats: | |
seats.pop(seats.index(passenger)) | |
else: | |
seats.pop(seats.index(random.sample(seats, 1)[0])) | |
tally.append(seats[0] == 99) | |
print(sum(tally) / len(tally)) |
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
import random | |
import multiprocessing | |
class Person: | |
def __init__(self, id: int) -> None: | |
self.seat: int = None | |
self.id: int = id | |
self.is_drunk: bool = False | |
def assign_seats(passengers: list, available_seats: list) -> bool: | |
for passenger in passengers[1:]: | |
if passenger.id in available_seats: | |
passenger.seat = passenger.id | |
available_seats.pop( | |
available_seats.index(passenger.seat)) | |
else: | |
_seat = available_seats[ | |
random.sample(range(len(available_seats)), 1)[0]] | |
passenger.seat = _seat | |
available_seats.pop(available_seats.index(passenger.seat)) | |
return passengers[-1].id == passengers[-1].seat | |
def simulate(trial: int) -> bool: | |
available_seats = [seat for seat in range(100)] | |
passengers = [Person(id) for id in range(100)] | |
passengers[0].is_drunk = True | |
passengers[0].seat = available_seats[random.sample(range(100), 1)[0]] | |
available_seats.pop(passengers[0].seat) | |
res = assign_seats(passengers, available_seats) | |
return res | |
if __name__ == '__main__': | |
_trials = 10000 | |
_results = [] | |
pool = multiprocessing.Pool(None) | |
for i in pool.imap(simulate, range(_trials)): | |
_results.append(i) | |
print( | |
f'The final passenger kept her assigned seat ' | |
f'{round(sum(_results) / len(_results), 2) * 100}% of the time ' | |
f'across {_trials} simulations' | |
) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment