jeudi 7 juillet 2016

How to generate all injective functions with specified domain and range?


I am a (very) beginner programmer, and especially new at Python. I want to take a specified domain and a specific range and generate injective maps. For example, I could have the domain be A = [1, 2] and the range be B = [1, 3] and I would like the output to look something like "(1 -> 1, 2 ->3 ), (1 -> 3, 2 -> 1)". Or something like ((1,1),(2,3)) and ((1,3),(2,1)) would be fine. As another example, I could take A = [2, 3] and B = [1, 2] and I would want "(2 -> 1, 3 -> 2), (2 -> 2, 3 -> 1)". My best attempt is A = [1, 2] B = [1, 3] C = [] for x in A: for y in B: C.append((x, y)) which produces the list [(1, 1), (1, 3), (2, 1), (2, 3)]. I understand why it produces that, but I can't think of any way to actually produce what I want. I would prefer a solution using basic Python tools, like loops, lists, sets, etc.

Aucun commentaire:

Enregistrer un commentaire