2

Example:

I have a solution list a:

a = [1, 1, 0, 0, 0]

and input lists bs:

b1 = [1, 1, 0, 0, 0]
b2 = [0, 1, 1, 0, 0]
b3 = [0, 0, 1, 1, 0]
...
bn = [1, 0, 0, 0, 1]

If I compare a to either b1, b2, ..., bn, I expected to get True value from the comparisons. For sure, this simple expression will not work:

if a == b:
    ...

because in Python only identical lists can be equal.

Is there any beautiful math that I can easily implement it in programming languages? Now I am thinking about building some hash function but I'm still not sure how?

Note 1) it can be easily implemented by just using for loop but I need some thing more robust. 2) this is maybe also related to problem of this post Cyclic group

fronthem
  • 4,011
  • 8
  • 34
  • 55
  • Stackoverflow is far better for such questions than MSE – Alex Apr 16 '18 at 16:19
  • To my knowledge, Python does not have any built-in (or any common package) to easily do this sort of comparison of equivalence under some group action. Your best bet is to try to cyclically shift everything to some canonical form before comparing. Or to build a hash function. – xxxxxxxxx Apr 16 '18 at 16:20
  • I think it's a math problem. I don't need a code or bunch of python functions. I just need something that can tell me that `a` and `b`s can be matched e.g. some hash function that produce same value for specific pattern, in this case `a` and `b`s. – fronthem Apr 16 '18 at 16:22
  • 4
    This question is a perfect duplicate of [How to check whether two lists are circularly identical in Python](https://stackoverflow.com/questions/26924836/how-to-check-whether-two-lists-are-circularly-identical-in-python) – xxxxxxxxx Apr 16 '18 at 16:30
  • Possible duplicate of [Is there a Python equivalent for C++ "multiset"?](https://stackoverflow.com/questions/17346905/is-there-a-python-equivalent-for-c-multisetint) – wnoise Apr 23 '18 at 01:10

1 Answers1

0

A simple solution could be to adjust the a and b values:

a_original = [5, 2, 3, 1, 4]
a_formatted = sorted(a_original)

Then, you can just use the formatted variables. A simple "for" loop can be used to format all of your variables.

Hope this helps!

Allan Cao
  • 11
  • 6
  • So this would SORT the entries? But wouldn't this give the same result for [1,1,0,0,0] as for [1,0,1,0,0] (which are not cyclic shifts so you do not want to consider them equal here)? – xxxxxxxxx Apr 16 '18 at 16:43
  • This answer is based on my understanding of the question. This function sorts the entries from smallest to largest. I'm not sure if that's what the original poster wants. – Allan Cao Apr 16 '18 at 20:52