Egor Punanov
Egor Punanov

Reputation: 1

How to make my code more efficient (IMEI validator in python)

is it possible for someone to rate the efficiency of this code?

It's supposed to imitate an IMEI validator for phone numbers just as a fun project.

If you don't know what that uits supposed to multiply every second digit by 2 and then add all the digits together, except the last one which it will always add on, for example:

123456789

1 + (4) + 3 + (8) + 5 + (1 + 2) + 7 + (1 + 6) = 38

38 + 9 = 47

then it would modulo by 10 and if it had no remainder it would be a valid number, if there is a remainder its invalid.

I have some print statements still there I used for testing ignoring those, how can I possibly make my code in maybe a fewer line or make it as efficient as possible.

I've been doing a bit of programming but struggle to figure out the efficiency of programs.

Any help would be greatly appreciated!!

n = input("Enter number: ")
og = n.split()
new = []
sum_new = 0

def create_new():
  global n
  global new
  temp_n = 0
  for i in range (len(n)):
    #test_bool =  i+1 % 2 == 0 #i < len(n) - 1 and
    #print(test_bool)
   # print(i+1)
    if i < len(n) - 1 and (i+1) % 2 == 0:
      #print("test")
      temp_n = str(int(n[i]) * 2)
      if len(temp_n) > 1:
        new.append(int(temp_n[0]))
        new.append(int(temp_n[1]))
        #print("doubled double! " + str(new[i]) + " " + str(new[i+1]))
      else:
        new.append(int(temp_n))
        #print("doubled! " + str(new[i]))
    else:
      new.append(int(n[i]))
     # print("not doubled " + str(new[i]))

create_new()
      
def find_sum():
  global sum_new
  for i in range (len(new)-1):
    sum_new += new[i]
    
  sum_new += new[len(new)-1]  

find_sum()

#print(new)
#print (sum_new)

if sum_new % 10 == 0:
  print("Valid.")
else:
  print("Invalid.")

Upvotes: 0

Views: 788

Answers (2)

user23136197
user23136197

Reputation: 1

One approach is to make two lists, one containing digits with odd indices and the other containing digits with even indices. You can accomplish this by using slicing. For example, if you've saved the digits of the IMEI to a list called IMEI, then IMEI[::2] and IMEI[1::2] will give you two lists containing digits with even and odd indices respectively. This can be implemented as such:

summed = 0
imei = list(input("Enter number: "))[::-1]
for digit in imei[1::2]:
    digit = str(int(digit) * 2)
summed += sum(int(x) for x in digit)
for digit in imei[::2]:
    summed += int(digit)
print("Valid." if summed % 10 == 0 else "Invalid.")

I've used sum(int(x) for x in digit) here to solve the problem of adding together the digits of a two-double number together. It's essentially the same code used to calculate a digital root, except it only runs once as there will never be a number with more than two digits.

Personally, this is how I would do it:

double = False
summed = 0
for digit in input("Enter number: ")[::-1]:
    if double is True:
        digit = str(int(digit) * 2)
summed += sum(int(x) for x in digit)
double = not double
print("Valid." if summed % 10 == 0 else "Invalid.")

This way, you only need to iterate through the IMEI once. In the previous example, we created and then iterated through two separate lists, which isn't very efficient.

Upvotes: 0

Seyi Daniel
Seyi Daniel

Reputation: 2379

Why not use a stepped for to iterate through n? With this, you won't need the to check if the element has an even index, the stepped for skips a number of elements (as specified) in your iterable. Just like this

for i in range (0, len(n)-1, 2):

This will select only elements with even index

Upvotes: 0

Related Questions