![]() |
Intersection - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Homework (https://python-forum.io/forum-9.html) +--- Thread: Intersection (/thread-30167.html) |
Intersection - Kimberley100 - Oct-10-2020 Task: you are given the number of channels, the number(always even)of logs into each channel, the log itself For example: 1 #the number of channels 2 #the number of logs 1 4 7 8#log itself, where the number on uneven positions is the time when the channel became busy ,the number on the even position-when the channel became free ( so 1 is when somebody logged into the channel,4-when smb logged out,7-logged in,8-logged out) You need to write a program which will return the time when all the channel were busy. Example: Input: 2 4 1 3 6 9 4 1 2 8 9 Output: 1 2 8 9 Input: 3 4 1 5 7 10 2 3 6 6 1 2 4 6 8 11 Output: 4 5 My idea is to create a list of channels, and insert in that list lists of logs of every channel For the first example it would be [[1,3,6,9],[1,2,8,9]]. Then write all the numbers between the difference of logged in time ,and logged out time,and place them into the sets. It would become [[(1,2,3),(6,7,8,9)],[(1,2),(8,9)]]. Then intersect every set with others. Maybe there is an easier solution or more optimal? RE: Intersection - buran - Oct-10-2020 I would have one set per channel RE: Intersection - Kimberley100 - Oct-10-2020 (Oct-10-2020, 09:48 AM)buran Wrote: I would have one set per channel But then you wouldn’t know when the channel was busy til what time, sessions would be mixed up,wouldn’t they? (Maybe I misunderstood you) RE: Intersection - buran - Oct-10-2020 So, let's assume the input is as follows: 2 4 1 3 6 9 4 1 2 7 9 I made the change so that you see the full picture channel1 is busy at 1,2,3,6,7,8,9 and channel2 is busy at 1, 2,7,8,9 what I suggest: channels_busy = sorted(list(set([1,2,3,6,7,8,9]).intersection(set([1,2,7,8,9])))) print(channels_busy)the output will be [1, 2, 7, 8, 9] and you only have to transform it to [1, 2, 7, 9]
RE: Intersection - Kimberley100 - Oct-10-2020 (Oct-10-2020, 03:12 PM)buran Wrote: So, let's assume the input is as follows: I just wrote a code based on your logic,I can send it if you want, but it breaks on the 4th example there the output == 0(because at the log-in-time the channel becomes busy,and at the log-out time already free) Can’t quite comprehend what to do. I am so not eager to re-write the whole code😭 RE: Intersection - buran - Oct-10-2020 you can publish minimal reproducible example here, with the input, expected and real output. I am not sure I understand what 4th example is and how the output will be 0 RE: Intersection - Kimberley100 - Oct-10-2020 (Oct-10-2020, 08:21 PM)buran Wrote: you can publish minimal reproducible example here, with the input, expected and real output. I am not sure I understand what 4th example is and how the output will be 0 Actually,I just modified code a little bit , and it works (at least on this examples) correctly. Nevertheless,thanks a lot for your help and your time😍😍😍 |