Search code examples
pythonhashtable

Get all keys in a hash-table that satisfy certain arithmetic property


Let's say I have a Hash-table, each key is defined as a tuple with 4 integers (A, B, C, D). where are integers represent a quantity of a certain attribute, and its corresponding value is a tuple of gears that satisfy (A, B, C, D).

I wanted to write a program that do the following: with any given attribute tuple (x, y, z, w), I want to find all the keys satisfying (|A - x| + |B - y| + |C - z| + |D - w|) / 4 <= i where i is a user defined threshold; return the value of these keys if exist and do some further calculation. (|A - x| means the absolute value of A - x)

To my experience, this kind of thing can be better done with Answer set programming, Haskell, Prolog and all this kind of logical programming languages, but I'm forced to use python for this is a python project...

I can hard code for a particular "i" but I really have no idea how to do this for arbitrary integers. please tell me how I can do this in pure python, Thank you very much!!!!


Solution

  • Just write a function that loops over all values in the table and checks them one by one. The function will take the table and i as arguments.