Daniel Chepenko
Daniel Chepenko

Reputation: 2268

Multiplication of two biggest elements of the array on Python

I'm trying to compare two algorithms, calculating multiplication of the two biggest elements of the array. I decided to implement such algorithm for that - going through every element of the array - finding the highest one, remembering it's index, then repeating the same for the rest of the elements.

import random


m = 2
n = random.randint(0+m,10+m)
b = [random.randint(0,12) for _ in range(n)]

def MaxPairwiseProductFast(a):
    for i in range(0,n):
        max_index1 = -1
        if (max_index1 == -1) or (a[i] > a[max_index1]):
            max_index1 = i

    for j in range(0,n):
        max_index2 = -1
        if (a[j] != a[max_index1]) and ((max_index2 == -1) or (a[j] > a[max_index2])):
            max_index2 = j

    resutl = a[max_index1]*a[max_index2]

    print(resutl)


MaxPairwiseProductFast(b)

This function multiples the highest element by itself. I can't figure out why it is happening so.

Upvotes: 0

Views: 221

Answers (3)

Paul Cornelius
Paul Cornelius

Reputation: 10916

You have two problems, one of them subtle. First you must move the initializers for max_index1 and max_index2 out of the loops, otherwise they get set to -1 on each step through the loop rather than retaining the value of the biggest element found so far.

In the second loop, you want to ignore the max_index1 element itself, rather than all elements that are numerically equal to a[max_index1]. So you need to modify the first term of the if statement. (If your list 'b' is, for example, [9,9,8] you will get the wrong answer otherwise).

import random

m = 2
n = random.randint(0 + m, 10 + m)
b = [random.randint(0, 12) for _ in range(n)]
print(b)

def MaxPairwiseProductFast(a):
    max_index1 = -1
    for i in range(0,n):
        if (max_index1 == -1) or (a[i] > a[max_index1]):
            max_index1 = i

    max_index2 = -1
    for j in range(0,n):
        if (j != max_index1) and ((max_index2 == -1) or (a[j] > a[max_index2])):
            max_index2 = j

    resutl = a[max_index1]*a[max_index2]

    print(resutl)

MaxPairwiseProductFast(b)

The really elegant, Pythonic solution is to import the standard module heapq, and then replace all of your code below the creation of the list 'b' with a single line:

print(sum(heapq.nlargest(2,b))

but this may not be the best learning experience.

Upvotes: 2

Sourav
Sourav

Reputation: 504

if you are using lists, then you can use list method 'max(list)' to find the highest value of a list. How about below code ? modify as per your logic

list1 = [1,2,3,4]
list2 = [5,7,3,9]

a, b = max(list1), max(list2)
result = a * b

the above code is just an example. Hope it helps.

Upvotes: 0

Saleem
Saleem

Reputation: 8978

How about reducing your code to

import random

m = 2
n = random.randint(0 + m, 10 + m)
b = [random.randint(0, 12) for _ in range(n)]

if len(b) >= 2:
    b.sort(reverse=True)

    n1 = b[0]
    n2 = b[1]

    result = n1 * n2

    print('List:   {0}'.format(b))
    print('Result: {0}'.format(result))

It will emit (on my machine, at time of execution but it could be different):

List:   [11, 7, 7, 6, 6, 6, 5, 4, 4, 2, 0]
Result: 77

As you can see two highest numbers in list are 11, 7 so their product is 77.

Upvotes: 3

Related Questions