Take three integers as input.
N1=123
N2=456
N3=789
Generate 4 digit password i.e. WXYZ.
Where
W is maximum digit of all the input numbers
X is the minimum of 100th position of all input numbers
Y is the minimum of 10th position of all input numbers
Z is the minimum of 1st position of all input numbers
Example:
N1=123
N2=456
N3=789
Password=WXYZ
W=maximum of 1,2,3,4,5,6,7,8,9 is 9
X= minimum of 1,4,7 is 1
Y=minimum of 2,5,8 is 2
Z=minimum of 3,6,9 is 3
4 digit password generated from given numbers is 9123
def add(x):
a = []
i = 2
while x > 0:
a[i] = x % 10
x /= 10
i -= 1
return a
def password(x1, x2, x3):
i = 0
p = 0
n = []
n.append(add(x1))
n.append(add(x2))
n.append(add(x3))
p = max(n) * 1000
m = []
for j in range(0, 9, 3):
m.append(n[j])
p += min(m) * 100
m = []
for j in range(1, 9, 3):
m.append(n[j])
p += min(m) * 10
m = []
for j in range(2, 9, 3):
m.append(n[j])
p += min(m)
return p
print("Enter three numbers: ")
n1 = int(input())
n2 = int(input())
n3 = int(input())
print("Generated password: ", password(n1, n2, n3))
I initially wrote this code in Java and it worked perfectly. I am trying to write it in Python and am getting this error. I'm new to Pythons so I'm not sure what I'm doing wrong. Please advice on how to correct my code.
a[i] = x % 10
IndexError: list assignment index out of range
p = max(n) * 1000
'>' not supported between instances of 'list' and 'int'
a[i] = x % 10to do, ifais an empty list andiis equal to 2? Why? In your own words, what do you expectmax(n)to do, if n is a list of lists? Why?addreturns a list, sonis a list of lists:[[1,2,3],[4,5,6],[7,8,9]]. You seem to wantn = add(x1)+add(x2)+add(x3)to create a single list.x /= 10is floating division. You really wantx //= 10.passwordfunction in two lines (and that includes the conversion of the integers to strings).