Skip to content

Instantly share code, notes, and snippets.

@les-peters
Created July 10, 2022 11:48
Show Gist options
  • Save les-peters/082b2b245b51f78f8b1645a61d2c55bc to your computer and use it in GitHub Desktop.
Save les-peters/082b2b245b51f78f8b1645a61d2c55bc to your computer and use it in GitHub Desktop.
Rumikub simulation
question = """
The game Rummikub has 106 tiles: 8 sets numbered 1-13, colored
red, blue, black, and yellow, and two (2) “wildcard” tiles.
Write two functions: one that creates a new player's tray of
14 tiles (repetitions allowed), and one that returns the valid
sets from a given tray. A set can be either 3 or 4 tiles of the
same number (but all different colors), or it can be a “run”
(which is three or more consecutive numbers all in the same color).
The rules for Rummikub are here if you need more clarification!
"""
from random import random
from pprint import pprint
colors = [ 'red', 'blue', 'black', 'yellow']
def createTray(rum_set):
tray = []
for pick in range(0,14):
i = int(random() * len(rum_set))
tile = rum_set[i]
tray.append(tile)
rum_set.remove(tile)
return tray
def validSets(tray):
wild_count = 0
for tile in tray:
if tile['color'] == 'wild':
wild_count += 1
set_wild = wild_count
sets = []
value_analysis = []
for value in range(1, 14):
count = len(list(filter(lambda x: x['value'] == value, tray)))
if count >= 2 and set_wild > 0 and value * (count + 1) >= 30:
sets.append({'value': value, 'length': count, 'has_wild': True})
set_wild -= 1
elif count >= 3 and value * count >= 30:
sets.append({'value': value, 'length': count})
return sets
# create set of tiles
rum_set = []
for j in range(0,2):
for i in range(1,14):
for color in colors:
tile = { 'color': color, 'value': i }
rum_set.append(tile)
tile = { 'color': 'wild', 'value': j }
rum_set.append(tile)
tray = createTray(rum_set)
sets = validSets(tray)
print(sets)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment