+ 1
Hey guys I have question ,how to specify the even positions in a list in python
I was trying to solve a code coach and it asked me to check if the credit card number is valid so one of the steps is multiplying the even positions in it Sample input: 1234 Output: 1438
14 Answers
+ 3
ZIZO Abd alkawy
That what you want to do can be done in right way as follows:
a_list = [4, 55, 64, 25, 16, 32]
x = a_list.index(16)
but is not needed.
You can do that in this way:
for i in range(len(list)):
if i % 2!= 0:
list[i] *= 2
print(list)
+ 8
You can check the position with modulo division:
position % 2 == 0
+ 8
ZIZO Abd alkawy ,
can you share your current code, so that we may give you some hints.
+ 6
dear all,
the task that ZIZO Abd alkawy is trying to solve is a code coach exercise, that should verify if a credit card number is valid or not.
> *one step* in this evaluation is to multiply every second digit by 2 (please see the explanations given by sandra). the total steps for evaluating a credit card number are: (the total processing of the number is called the *luhn formula* )
Here is the Luhn formula:
1. Reverse the number.
2. Multiple every second digit by 2.
3. Subtract 9 from all numbers higher than 9.
4. Add all the digits together.
5. Modulo 10 of that sum should be equal to 0.
+ 4
ZIZO Abd alkawy modulo „%“ division results in a remainder of the division. For example:
5 % 2 equals to 1, because 5 / 2 equals to 2 (2*2 =4) and stay a remainder of (5-4= 1) 1.
3 % 2 = 1
6 % 2 = 0 because 6 / 2 = 3 fulfill the division and the remainder equals to 0 (zero).
In this way you can differentiate between even and odd numbers.
+ 3
What is modulo?JaScript
+ 2
jascript, ZIZO Abd alkawy, Be careful with this `formula` (which is generally correct).
The task description is NOT: ... how to specify the even positions in a list
BUT is says: We have to multiply every second digit by 2.
>>> This is something different.
Let us use the sample number 1234:
> The 1. digit is 1 and has the index 0, this value should keep as it is => 1, (it has an even index number)
> The 2. digit is 2 and has the index 1, and has to be multiplied by 2 => 4, (it has an odd index number)
> ...
So 1234 is getting => 1438.
This is a part of the task description:
Sample from the task description in code coach:
Input : 4091131560563988
Reverse: 8893650651311904
Multiplying the even positions by 2: 8 16 9 6 6 10 0 12 5 2 3 2 1 18 0 8
^ ^ ^ ^ ^ ^ ^ ^
...
+ 2
sandra I see what you mean but iam still stuck at this
+ 2
ZIZO Abd alkawy
This is test of the code:
https://sololearn.com/compiler-playground/cdXYG2FDgxa1/?ref=app
Easier to manage indexes is with the for loop over indexes:
for index in range(len(list)):
…
list[index] = …
etc.
+ 1
JaScript
I can't describe how was this was helpful,thanks
And I made this and it didn't work and I don't know why
https://code.sololearn.com/c6Z2NuQ698Vs/?ref=app
+ 1
ZIZO Abd alkawy
This is test result of your code:
Number CN = 4091131560563988
list = [4, 0, 9, 1, 1, 3, 1, 5, 6, 0, 5, 6, 3, 9, 8, 8]
Reversed list = [8, 8, 9, 3, 6, 5, 0, 6, 5, 1, 3, 1, 1, 9, 0, 4]
item i = 8 => found index x(i,list) = 0
item i = 8 => found index x(i,list) = 0
item i = 9 => found index x(i,list) = 2
item i = 3 => found index x(i,list) = 3
item i = 6 => found index x(i,list) = 4
item i = 5 => found index x(i,list) = 5
item i = 0 => found index x(i,list) = 6
item i = 6 => found index x(i,list) = 4
item i = 5 => found index x(i,list) = 5
item i = 1 => found index x(i,list) = 9
item i = 3 => found index x(i,list) = 3
item i = 1 => found index x(i,list) = 9
item i = 1 => found index x(i,list) = 9
item i = 9 => found index x(i,list) = 2
item i = 0 => found index x(i,list) = 6
item i = 4 => found index x(i,list) = 15
Results list_A2 = [8, 8, 9, 6, 6, 10, 0, 6, 10, 2, 6, 2, 2, 9, 0, 8]
The found index is always first from left with equals value.
0
Lothar
Here it is
I don't know why the index func doesn't work in my func or even when I did this
For I in list:
D= Index(i)
If d %2 != 0
I=i*2
Also didn't work
https://code.sololearn.com/ckF6Lcx34e9o/?ref=app
My code⬆️⬆️