-1

This is a school problem I encounter. I have written my code and it returns correct result. However, the test program shows checksum does not match. Below is the question and my code.

An element of items is said to be a "dominator" if every element to its right is strictly smaller than it. This function should count how many elements in the given list of items are dominators, and return that count. For example, in the list [42, 7, 12, 9, 2, 5], the elements 42, 12, 9 and 5 are dominators. By this definition, the last item of the list is automatically a dominator.

Click here: The expected result

def count_dominators(items):
    if len(items) ==0:
        return len(items)
    else:
        k = 1
        for i in range(1,len(items)):
            if items[i]<items[i-1]:
                k = k+1
            else:
                k = k+0        
        return k
Trenton McKinney
  • 43,885
  • 25
  • 111
  • 113
  • I think the word you are missing is `every` – njzk2 Oct 02 '19 at 04:24
  • Add this within your for loop `print('comparing {} and {}'.format(items[i], items[i-1]) )` and you will see that your function is not checking for dominators. – cronoik Oct 02 '19 at 04:26
  • Taken from https://stackoverflow.com/a/5389599/977593. You may use `for for i in range(0, len(items), 2):`, Just put `if items[i] > items[i+1]:` inside the for loop. The for loop is basically jumping i by 2. – slackmart Oct 02 '19 at 04:37
  • 1
    I *think* you're asking a debugging question, but I'm not sure (i.e. it's unclear what you're asking). If you are asking a debugging question, please [edit] your question to be on-topic: include a [mcve] that duplicates the problem. Questions seeking debugging help ("why isn't this code working the way I want?") must include: (1) the desired behavior, (2) a specific problem or error and (3) the shortest code necessary to reproduce it *in the question itself*. Please also see: [What topics can I ask about here?](/help/on-topic), and [ask]. – Makyen Oct 02 '19 at 05:16
  • Thank you guys. I think I understand why my code is failing on test program. The reason is I am comparing the first and second element, second and third element. I ignored the word "Every". – James Wang Oct 02 '19 at 14:20

1 Answers1

1

Like already mentioned in the comments you aren't comparing opration isn't correct. Currently you are comparing:

  • comparing 7 < 42
  • comparing 12 < 7
  • comparing 9 < 12
  • comparing 2 < 9
  • comparing 5 < 2

and increase k if the condition is true. According to your task a dominator is defined as "every element to its right is strictly smaller than it". That means you have to compare each item with every element to its right (i.e. you have to check if 42 > 7, 42 > 12, 42 > 9, 42 > 2 and 42 > 5 to determine if 42 is a dominator).

Have a look at the function below:

def count_dominators(items):
    k = 0
    for idx,item in enumerate(items):
        dominator = True
        for ritem in items[idx+1:]:
            print('comparing {} and {}'.format(item, ritem))
            #If True not a dominator
            if item<=ritem:
                dominator = False
                break
        if dominator:
            print('{} is a dominator'.format(item)) 
            k = k+1        
    return k

count_dominators([42, 7, 12, 9, 2, 5])

Output:

comparing 42 and 7
comparing 42 and 12
comparing 42 and 9
comparing 42 and 2
comparing 42 and 5
42 is a dominator
comparing 7 and 12
comparing 12 and 9
comparing 12 and 2
comparing 12 and 5
12 is a dominator
comparing 9 and 2
comparing 9 and 5
9 is a dominator
comparing 2 and 5
5 is a dominator
John Kugelman
  • 330,190
  • 66
  • 504
  • 555
cronoik
  • 11,101
  • 2
  • 32
  • 61