0

I am trying to solve the google kickstart round b 2020 first question. but I a facing a RE- Runtime error. can anyone help me to tell? what am I dining wrong?

Question Problem

Li has planned a bike tour through the mountains of Switzerland. His tour consists of N checkpoints, numbered from 1 to N in the order he will visit them. The i-th checkpoint has a height of Hi.

A checkpoint is a peak if:

It is not the 1st checkpoint or the N-th checkpoint, and The height of the checkpoint is strictly greater than the checkpoint immediately before it and the checkpoint immediately after it.

Please help Li find out the number of peaks.

Input

The first line of the input gives the number of test cases, T. T test cases follow. Each test case begins with a line containing the integer N. The second line contains N integers. The i-th integer is Hi.

Output

For each test case, output one line containing Case #x: y, where x is the test case number (starting from 1) and y is the number of peaks in Li's bike tour.

Limits Time limit: 10 seconds per test set. Memory limit: 1GB. 1 ≤ T ≤ 100. 1 ≤ Hi ≤ 100.

Test set 1 3 ≤ N ≤ 5.

Test set 2 3 ≤ N ≤ 100.

Sample

Input

Output

4
3
10 20 14
4
7 7 7 7
5
10 90 20 90 10
3
10 3 10

Case #1: 1 Case #2: 0 Case #3: 2 Case #4: 0

In sample case #1, the 2nd checkpoint is a peak. In sample case #2, there are no peaks. In sample case #3, the 2nd and 4th checkpoint are peaks. In the sample case #4, there are no peaks.

my Code for that question

for z in range(int(input())):
    no_range,s = input(),list(map(int,input().split()))
    if s[0] and s[-1] < max(s[1:-1]):
        print(f'Case #{z+1}:',s.count(max(s[1:-1])))
    else:print(f'Case #{z+1}:',0)
Anshita Singh
  • 1,583
  • 1
  • 17
  • 40

1 Answers1

0

1. Wrong if-conditional:

if s[0] and s[-1] < max(s[1:-1]):

is True as soon as s[0] is different from 0. See How to test multiple variables against a value? :

You could fix the comparison to be

if s[0] < max(s[1:-1]) and s[-1] < max(s[1:-1]):

but that makes no sense in respect to what your task it: you do not have to check if the first and last number is less then the maximal height of the numbers in between.

2. Wrong logic:

Given data of

20, 10, 15, 10, 50, 70

your code reports the correct number - but the wrong thing - the peak that counts is 10,15,10. The 50 (the max value of [1:-1] ) is not a peak!

3. Wrong counting:

Furthermore you do not check triplets in with s.count(max(s[1:-1]))) but you count how often the max value is inside your list.

Fix:

for z in range(int(input())):
    _, s = input(), list(map(int,input().split())) 

    # create triplets of values - this will automagically get rid of the cornercases
    r = zip(s, s[1:], s[2:]) 

    # count how often the middle value is greater then the other 2 values
    print(f'Case #{z+1}: {sum(a<b>c for a,b,c in r)}') 
Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • @BB what RE error? Edit your question and add the exact error and stacktrace you get. – Patrick Artner May 16 '20 at 08:45
  • s[1:-1] gives me 2 to last -1 not last -2. – BB KI VLOGS May 16 '20 at 08:59
  • as you mention:your code reports the correct number - but the wrong thing - the peak that counts is 10,15,10. The 50 (the max value of [1:-1] ) is not a peak! but s[1:-1] give me - 10, 15, 10, 50 – BB KI VLOGS May 16 '20 at 09:00
  • @BB you need to look at triplets and report any peak thats stricly higher then the one directly before/after. in `20, 10, 15, 10, 50, 70`. for `50`: it is not higher then `70` after it so it is not a peak. `15` is because its bigger as the 10's before and after. – Patrick Artner May 16 '20 at 09:08