Skip to content

Instantly share code, notes, and snippets.

@dandavison
Last active August 29, 2015 14:18
Show Gist options
  • Save dandavison/4780d40a866e0b942ff9 to your computer and use it in GitHub Desktop.
Save dandavison/4780d40a866e0b942ff9 to your computer and use it in GitHub Desktop.
One-to-one question

Two columns satisfy a "one-to-one" constraint if each value in column 1 is associated with exactly one value in column 2, and vice versa. E.g. in this data, the person column and the email column satisfy a one-to-one constraint:

>>> table = [('pablo', 'ppicasso@gmail.com'),
             ('georgia', 'gokeefe@hotmail.com'),
    	     ('mark', 'mrothko@aol.com')]
>>> is_one_to_one(table)
    True

But if mark gets a second email account, the columns no longer satisfy a one-to-one constraint:

>>> table = [('pablo', 'ppicasso@gmail.com'),
             ('georgia', 'gokeefe@hotmail.com'),
    	     ('mark', 'mrothko@aol.com'),
             ('mark', 'mark@gmail.com')]
>>> is_one_to_one(table)
    False

Similarly, if aol somehow give the same email address to two different people, the one-to-one constraint is violated:

>>> table = [('pablo', 'ppicasso@gmail.com'),
             ('georgia', 'gokeefe@hotmail.com'),
    	     ('mark', 'mrothko@aol.com'),
             ('moses', 'mrothko@aol.com')]
>>> is_one_to_one(table)
    False

A duplicate row doesn't violate one-to-one:

>>> table = [('pablo', 'ppicasso@gmail.com'),
             ('georgia', 'gokeefe@hotmail.com'),
    	     ('mark', 'mrothko@aol.com'),
             ('georgia', 'gokeefe@hotmail.com')]
>>> is_one_to_one(table)
    True

Implement the function is_one_to_one. It should take one argument, a list of tuples, as in the examples above.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment