0

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'

4
  • In your own words, what do you expect a[i] = x % 10 to do, if a is an empty list and i is equal to 2? Why? In your own words, what do you expect max(n) to do, if n is a list of lists? Why? Commented Feb 6, 2022 at 21:42
  • add returns a list, so n is a list of lists: [[1,2,3],[4,5,6],[7,8,9]]. You seem to want n = add(x1)+add(x2)+add(x3) to create a single list. Commented Feb 6, 2022 at 21:54
  • Note that x /= 10 is floating division. You really want x //= 10. Commented Feb 6, 2022 at 21:54
  • Next level: After this problem is solved one could think about a different algorithm. If I treat the numbers a strings I can write the full password function in two lines (and that includes the conversion of the integers to strings). Commented Feb 7, 2022 at 9:38

4 Answers 4

1

The problem is, that you can't change a value of an item at an index of a list in python, where there currently is no item in the list at that index. That is why

a[i] = x%10

won't work, if there are not i items in the list currently. Additionally, your while-loop in the add-function won't ever come to an end, because if you divide an integer in python it gives you an integer or a float, depending on the outcome. Therefore x/10 won't ever be zero or smaller.

Sign up to request clarification or add additional context in comments.

Comments

0
def password(N1, N2, N3):
#takes all three Ns ^ and makes them single digits
N1 = [int(char) for char in str(N1)]
N2 = [int(char) for char in str(N2)]
N3 = [int(char) for char in str(N3)]
#it goes into each variable and finds the max and mins in each section
#W is just the biggest digit out of all of them
W = (N1 + N2 + N3) 
W = max(W)
#X is the smallest digit from the first digits given in each N input
X = [N1[0], N2[0], N3[0]] 
X = min(X)
#same thing as X just for the second digits this time
Y = [N1[1], N2[1], N3[1]] 
Y = min(Y)
#same but for the third digits
Z = [N1[2], N2[2], N3[2]]
Z = min(Z)
#groups all the numbers together into a variable called "password"
password = [W, X, Y, Z]
#Done!
return password 

Comments

0

Your function could be implemented as follows:

def password(n1='123', n2='456', n3='789'):
    w = max(map(int, n1 + n2 + n3))
    x = min(map(int, n1[0] + n2[0] + n3[0]))
    y = min(map(int, n1[1] + n2[1] + n3[1]))
    z = min(map(int, n1[2] + n2[2] + n3[2]))
    return f"{w}{x}{y}{z}"


print(password())

Out:

9123

Comments

0

Here's an approach that might be considered more "Pythonic".

def digits(x):
    return list(map(int,str(x)))

def password(*args):
    n = list(map(digits,args))
    first = max([max(lst) for lst in n])
    lasts = [min(col) for col in zip(*n)]
    return str(first)+''.join(map(str,lasts))

Example application: password(123,456,789) yields '9123'.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.