Skip to content
Advertisement

check element in list and return a value and store in another list

I am trying to identify few IP accordingly, the requirement as below:

  • There is a list of IP called ip_addresses.
  • There is a list of registered IP called registered_list.
  • There is a list of banned IP called banned_list.
  1. If the element in ip_addresses in registered_list, return 1 and store in another list.
  2. If the element in ip_addresses in banned_list, return 2 and store in another list.
  3. If the element in ip_addresses in not in list and incorrect format, return 3 and store in another list.
  4. If the element in ip_addresses in not in list and correct format, return 4 and store in another list.

My code as below:

JavaScript

it only return the value [1,2] instead of [1,4,2,3]

enter image description here

May i know why my code stop flowing ?

Advertisement

Answer

That’s happening because you are using check variable in loop and don’t reset it. So after first iteration your check variable already have value equal 1, because of logic of your code and first from ip_addresses list in registered_list list. To fix this, set check to 0 in the beginning of every iteration:

JavaScript

btw you will get a error, because here

JavaScript

you are using ip variable which is not defined. I think you should rename it as ip_ID.

Anyway this code won’t pass this task. If you want, i can try provide my solution.

Strongly recommended to use debug mode to understand what is really happening in your code. Also read PEP-8

p.s. is it from codewars?

My solution

JavaScript

stdout:

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement