Find the largest contiguous element

0

Given an array, print the largest contiguous element (MEC) for each element. The largest contiguous element (MEC) for an element x is the first largest element on the right of x in an array. The elements for which the largest contiguous element does not exist, assign as a greater element the value -1.

Use these examples:

  • For any arrangement, the element on the far right always has the largest element with you at -1.
  • For any array ordered in descending order, all elements have as their largest element contiguous to -1.
  • For the input array [4, 5, 2, 25], the largest contiguous elements for each element are the following:

    Elemento       MEC
       4      -->   5;
       5      -->   25;
       2      -->   25;
       25     -->   -1;
    
  • For the input arrangement [13,7,6,12], the largest contiguous elements for each element are the following:

     Elemento        MEC
      13      -->    -1;
      7       -->     12;
      6       -->     12;
      12      -->     -1;
    
  • Your solution should be delivered using two methods:

  • Method # 1 (Using Arrangements)
  • Method # 2 (Using Batteries)
  • I have this:

    lista =  [4, 5, 2, 25]
    print "Los valores de la lista son: ",lista
    
    n=input("Busque el elemento contiguo de la lista: ")
    
    
    a=lista[0]
    b=lista[1]
    c=lista[2]
    d=lista[3]
    
    if n==a:
      if a<b:
        print"Contiguo de: ",n," es: ", b
    
    if n==b:
      if b<c:
        print"Contiguo de: ",n," es: ", c
      else: 
    
        
    asked by revilo zednem 22.05.2018 в 17:24
    source

    1 answer

    0

    Since this question looks like the task of a programming course, I am going to help you with a response that you may not be accepted "yet", although the only half way advanced is the use of filter.

    for i,value in enumerate(your_list):
        # Get all larger numbers to the right
        possible_numbers = list(filter(lambda x:x>val, your_list[i+1:]))
        if possible_numbers:
            # Show the closest number larger than value
            print(possible_numbers[0])
        else:
            print(-1)
    
        
    answered by 26.05.2018 в 18:55