Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

the first two photos are the instructions of the question and the last photo is the result of my submission. i cant figure out what

the first two photos are the instructions of the question and the last photo is the result of my submission. i cant figure out what is the reason of the errors. please help me to solve the problem or provide another way to answer the question. Thank you .

my code:

def solution(A): for i in range(len(A)-1,0,-1): if A[i]==A[i-1]: A.pop(i) else: pass

a=0 if len(A)==1: return 1

else:

for i in range(len(A)-1,0,-1): if A[i]A[1]: a+=1 if A[-1]>A[-2]:

a+=1 for i in range(len(A)-1,1,-1): if A[i]>A[i-1] and A[i-2]>A[i-1]: a+=1 else: pass if A[0]

if A[-1]

if __name__ == "__main__": q = [int(n) for n in input().split(',')] print(solution(q))

image text in transcribedimage text in transcribedimage text in transcribed

Castle Building Description Charlemagne, the King of Frankia, is considering building some castles on the border with Servia. The border is divided into N segments. The King knows the height of the terrain in each segment of the border. The height of each segment of the terrain is stored in array A, with A[P] den oting the height of the P-th segment of the border. The King has decided to build a castle on top of every hill and at the bottom of every valley. Let [P..Q] denote a group of consecutive segments from P to Q inclusive such that (0 s PsQs N-1). Segments [P..Q] form a hill or a valley if al I the following conditions are satisfied: The terrain height of each segment from P to Q is the same (A[P] = A[P+1] = ... = A[Q]); If P >0 then A[P-1] A[P] (for a valley); If Q A[Q] (for a valley); That is, a hill is higher than its surroundings and a valley is lower than its surroundings. Note that if the surroundings on either side of the hill or valley don't exist (i.e. at the edges of the area under consideration, where P = 0 or Q = N-1), then the condition is considered satisfied for that side of the hill/valley. The king is wondering how many castles is he going to build. Can you help him? For example, consider the following array A = [2, 2, 3, 4, 3, 3, 2, 2, 1, 1, 2, 5). There are two hills: [3..3] and [11..11]. There are also two valleys: [O..1] and [8.9). There are no other suitable places for castles. Write a function: def solution(A) that, given an array A consisting of N integers, as explained above, returns the total number of hills and valleys. For example, given array A as described above, the function should return 4. Given array A = (-3, -3] describing segments with a terrain height below 0, segment [0..1] forms both a hill and a valley, and only on Input Given an array A consisting of N integers N is an integer within the range (1..100,000); each element of array A is an integer Output Returns the total number of hills and valleys. Sample Input 1 Sample Output 1 2,2,3,4,3,3,2,2,1,1,2,5 4 ID 8MB 2 5 21ms 12 Wrong Answer 13 AMB<-1>

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Relational Theory Normal Forms And All That Jazz

Authors: Chris Date

1st Edition

1449328016, 978-1449328016

More Books

Students also viewed these Databases questions

Question

3. How would you address the problems that make up the situation?

Answered: 1 week ago

Question

2. What recommendations will you make to the city council?

Answered: 1 week ago