008: Set

Serie	treffen:problemsolving
Von	nopx
Datum	15.Dez 2021
Download	Aufgabenbeschreibung [pdf]
Schwierigkeit

Lösungen

nopx

Stupide Brute Force Loesung in Python 3.8:

import sys import numpy as np import itertools options = 'abcdefghijklmnopqrstuvwxyz0123456789' num_properties = 4 setsize = 3 def solve_set(instr : str) -> list: assert(len(instr) % num_properties == 0) num_cards = len(instr) // num_properties cards = np.zeros( ( num_cards, num_properties), dtype=int) for i in range(0,len(instr),num_properties): for j in range(num_properties): cards[i//num_properties,j] = options.index(instr[i+j]) idxs_cards = list(range(len(cards))) solutions = [] for perm in itertools.permutations(idxs_cards,setsize): for prop_id in range(num_properties): l = cards[perm,prop_id] unique = np.unique(l) if len(unique) != 1 and len(unique) < setsize: break else: if prop_id == num_properties-1: solutions.append(perm) return solutions, cards if __name__ == "__main__": instr = str(sys.argv[1]) if len(instr) < num_properties: exit(1) solutions, cards = solve_set(instr) if len(solutions) > 0: exit(0) else: exit(1)

Ausfuehrlich Code kann hier runtergeladen werden.

~~DISCUSSION~~