Hello! Please tell me how in Python, by mind, do the finding whether ranges overlap. For example, a person is looking for people aged “from 9 to 14” and “from 15 to 21”, while he himself indicates that he is from “18 to 21”. The second person indicates that he is "from 8 to 11" and is looking for "from 21 to 40". It is necessary that everyone was happy. That is, firstly, the desired person must correspond to the desired intervals of age, secondly, if the one who searches does not satisfy the search parameters of the desired one, then the search must return false.
Clearly such arrays:

"search_users"{ "user_1" => { "my_age_from" => "18", "my_age_to" => "21", "wish_user_ages" => { {"from"=>"9", "to"=>"14"}, {"from"=>"15", "to"=>"21"} } }, "user_2" => { "my_age_from" => "8", "my_age_to" => "11", "wish_user_ages" => { {"from"=>"21", "to"=>"40"} } }, .... 

How to find only overlapping users. So that they were to each other. There is a cycle option to go through all the conditions, but it is too difficult and somehow difficult to fit in my head, if my age would have been more than diapozon, there would be no problems. And it seems to me that there is some generally simpler method than conditional cycles. Please help, guru!))

  • 1- your input is similar to the php array, which most closely matches OrderedDict in Python (lists in Python do not have named keys, dictionaries in Python do not maintain order). If you intentionally used the python label for the question, then convert the input data into a format understood by Python (for example, json text). Label arrays most likely out of place 2- Specify what your question. Do you want at least some algorithm that finds for all users a list of suitable users by age? Or do you need an algorithm with a certain Big O behavior? - jfs
  • 1 - this should be nothing like it, I just gave an array for clarity, wrote it with my hands. The question is exactly in the python algorithm, How to find the user that suits you and, moreover, that you also fit this user. I only got 2 cycles, first checking for all the desires of the user who is looking for, and then if such a user was caught in the cycle already on his desires for the user who is looking. But all this seems to me not the most rational solution, maybe some kind of recursive function can be done. Also stupid and I can not write whether the condition corresponds to - iproger
  • one range to another. there would be something like this range (8,14) is in range (14,18) - true. It would be convenient) - iproger
  • 1- the word "array" means not what you think (see, for example, a description of the label). 2- Do not put the information you need to answer in the comments, edit your question instead. Click edit . - jfs

1 answer 1

Well, once started minus and edit all the formalities of the type of why you used the word "array". I wrote the algorithm myself, and I hope someone will find someone who can improve it :)

 def isComplyUsers(user1, user2, exchange=true): for wish_age in user1.wish_user_ages: # user2 не удовлетворяет желаниям user1 if(user2.my_age_from > wish_age.to or (user2.my_age_from < wish_age.from and user2.my_age_to < wish_age.to)): continue else: if exchange: # Меняем местами и чтобы всё это не зациклилось говорим, что местами менять больше не требуется return isComplyUsers(user2, user1, false) else: return true break; return false