0

How can I search for the three maximum elements of a list and replace it at same index with its result when divided by 2.

Please what am I doing wrong:

input is: 2 5 8 19 1 15 7 20 11
output should be : 2 5 8 9.5 1 7.5 7 10 11

Index out of range is the result displayed

def numberInput(line):
  lineInput = [int(i) for i in line.split()]

  min1 = min(lineInput)

  for j in range(len(lineInput)):

     if lineInput[j]>min1 and lineInput[j] > lineInput[j+1]:

           max1 = lineInput[j]/float(2)
     else:
           max1 = lineInput[j]/float(2)
           lineInput[j] = max1

     lineInput[j] = max1
  return(lineInput)

number = '2 5 8 19 1 15 7 20 11' 
print(numberInput(number))
11
  • Why is average you expect always just number/2? Commented Feb 13, 2020 at 1:54
  • divided by 2 not average (half of the number) Commented Feb 13, 2020 at 1:56
  • 1
    When you get to the last element of the list, lineInput[j+1] is outside the list. You need to use range(len(lineInput)-1) Commented Feb 13, 2020 at 1:57
  • I tried this ( range(len(lineInput)-1)) and it did not work Commented Feb 13, 2020 at 1:59
  • 2
    @MateenUlhaq: heapq.nlargest should be somewhat faster; it's O(n log k) (where k is 3 in this case) rather than the O(n log n) you pay on a complete sort (whether it's faster in practice would require testing; sorted is implemented in C, nlargest is Python with C-accelerated helpers, so it's slower than big-O might lead you to believe). Commented Feb 13, 2020 at 2:10

2 Answers 2

1

If the order of list isn't important, you can simply sort the list in descending order and then replace the first 3 elements as

a = [2, 5, 8, 19, 1, 15, 7, 20, 11]
a.sort(reverse = True)
a[0] = a[0] / 2
a[1] = a[1] / 2
a[2] = a[2] / 2

Output

[10.0, 9.5, 7.5, 11, 8, 7, 5, 2, 1]

If the order is important,

import heapq
largest = heapq.nlargest(3, a)
for i in range(len(a)):
    if a[i] in largest:
        a[i] = a[i] / 2    

Output

[2, 5, 8, 9.5, 1, 7.5, 7, 10.0, 11]

heapq.nlargest() is a function of heapq which can give you n largest numbers from a list. Since lists in Python do not have a replace function, the list had to be traversed once, and then replaced manually. Hope this resolves your issue.

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

Comments

0

Why wouldn't you just walk through the list once (it maybe a bit longer code, but definitely efficient)

def numberInput(line):
    lineInput = [int(i) for i in line.split()]
    n = len(lineInput)
    if n <= 3:
        return [i / 2 for i in lineInput]
    max_pairs = [(lineInput[i], i) for i in range(3)] # keep track of 3 max's and their indices
    max_pairs.sort(key = lambda x: -x) # sort in descending order
    for i in range(3, n):
        if lineInput[i] >= max_pairs[0][0]: # greater than the largest element
            max_pairs = [(lineInput[i], i)] + max_pairs[:2]
        elif lineInput[i] >= max_pairs[1][0]: # greater than second element
            max_pairs = [max_pairs[0], (lineInput[i], i), max_pairs[1]]
        elif lineInput[i] >= max_pairs[2][0]: # greater than third element
            max_pairs = max_pairs[:2] + [(lineInput[i], i)]
     for pair in max_pairs:
         lineInput[pair[1]] = lineInput[pair[0]] / 2
     return lineInput

Explanation: max_pairs is a set of three tuples, containing the maximum three elements and their indices

Note: I think the above is easiest to understand, but you can do it in a loop if you don't like all those ifs

1 Comment

Also, as @AMC commented above, python convention is to use underscores rather than camelCase so you usually see lineInput and numberInput as line_input and number_input respectively

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.