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:

ip_addresses = ["192.168.0.1","192.168.0.0","255.255.255.255","193.144.222.889"]
registered_list = ["192.168.0.1","123.123.123.123"]
banned_list = ["255.255.255.255"]

# Return answer[] as below
# 0 - Initial
# 1 - Registered
# 2 - Banned
# 3 - Wrong format
# 4 - Correct format but not registered

answer = []
def solution(ip_addresses, registered_list, banned_list):

    for ip_ID in ip_addresses: 

        # check ip_ID in register list
        for reg_ID in registered_list:
            if ip_ID==reg_ID:
                check=1     # if ip_ID in register list
                answer.append(check)

        # check ip_ID in banned list        
        for ban_ID in banned_list:
            if ip_ID==ban_ID:
                check=2     # if ip_ID in banned list
                answer.append(check)

        # if ip_ID not in being processed in registered and banned list        
        if check == 0:
            split=ip.split(".") # split ip into 4 elements             

            # check numbering for each element
            for spl_ID in split:
                if (
                    int(spl_ID)<= 255 and len(spl_ID)==3 or  
                    int(spl_ID)<= 100 and len(spl_ID)==2 or
                    int(spl_ID)<= 10 and len(spl_ID)==1
                    ):
                    check=4     # if all element correct
                    answer.append(check)
                else:
                    check=3     # either one of the element incorrect
                    answer.append(check)
    return answer 

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:

for ip_ID in ip_addresses: 
    check = 0
    ...

btw you will get a error, because here

if check == 0:
    split=ip.split(".") # split ip into 4 elements 

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

def solution(ip_addresses, registered_list, banned_list):
    from re import fullmatch
    answer = []
    for ip in ip_addresses:
        if ip in registered_list:
            answer.append(1)
        elif ip in banned_list:
            answer.append(2)
        elif not fullmatch(r'^((25[0-5]|(2[0-4]|1d|[1-9]|)d).?b){4}$', ip):
            answer.append(3)
        elif fullmatch(r'^((25[0-5]|(2[0-4]|1d|[1-9]|)d).?b){4}$', ip):
            answer.append(4)
    return answer

stdout:

[1, 4, 2, 3]
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement