[Challenge] Superior Character | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

[Challenge] Superior Character

Given a string, a character is said to be superior if it has two neighboring letters that are strictly smaller than itself. We compare characters by their location in the alphabet. More formally, we say that the character at the ith position is superior if a character exists at the (i+1)th position and (i-1)th position, and the character at the ith position is strictly greater than the character at both (i+1)thand (i-1)th positions. Given the frequencies of the lowercase English letters, form a string using all these characters, such that the resultant string has the maximum number of superior characters. You need to print the maximum number of superior characters. Test : 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 output is 1 1 2 2 3 4 0 3 4 4 1 3 1 4 4 1 0 0 0 0 0 4 2 3 2 2 1 output is 25 1 1 3 3 1 1 4 4 3 1 3 3 3 0 1 2 0 4 2 1 3 0 3 1 1 1 output 24 3 3 0 2 2 2 4 1 2 1 1 1 3 3 0 0 3 2 2 4 1 4 4 1 2 1 output 25 2 1 4 1 0 2 0 3 1 2 0 3 1 1 2 0 1 4 2 3 2 3 2 0 2 1 output 21

18th Apr 2018, 11:13 AM
Momo Belia
Momo Belia - avatar
5 Answers
+ 3
Test 1: 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 0 1 0 0 0 0 0 0 0 output is 1 Test 2: 1 2 2 3 4 0 3 4 4 1 3 1 4 4 1 0 0 0 0 0 4 2 3 2 2 1 output is 25 Test 3: 1 1 3 3 1 1 4 4 3 1 3 3 3 0 1 2 0 4 2 1 3 0 3 1 1 1 output 24 etc.. According to me the outputs of the test cases, based on the definition should be Test 1: 3 Test 2: 4 Test 3: 4 etc... ?????
18th Apr 2018, 11:56 AM
Louis
Louis - avatar
+ 1
testCase=[[0,0,0,0,2,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0,0],[1,2,2,3,4,0,3,4,4,1,3,1,4,4,1,0,0,0,0,0,4,2,3,2,2,1], [1,1,3,3,1,1,4,4,3,1,3,3,3,0,1,2,0,4,2,1,3,0,3,1,1,1], [3,3,0,2,2,2,4,1,2,1,1,1,3,3,0,0,3,2,2,4,1,4,4,1,2,1], [2,1,4,1,0,2,0,3,1,2,0,3,1,1,2,0,1,4,2,3,2,3,2,0,2,1]] print([len([inp[i+1] for i in range(len(inp)-2) if inp[i]<inp[i+1]>inp[i+2]]) for inp in testCase ])
18th Apr 2018, 12:20 PM
Louis
Louis - avatar
+ 1
"i want to solve the problem on hackerrank so i'll have other people do it for me hurr durr"
18th Apr 2018, 12:38 PM
hinanawi
hinanawi - avatar
0
The question is from the ongoing contest at hacker rank - https://www.hackerrank.com/contests/w37/challenges/superior-characters/problem Please refrain from answering it.
18th Apr 2018, 12:10 PM
Mohit Rathore (markroxor)
Mohit Rathore (markroxor) - avatar
0
def maximumSuperiorCharacters(freq): return [len([freq[i+1] for i in range(len(freq)-2) if freq[i]<freq[i+1]>freq[i+2]]) for inp in freq ] why this doesnt work?
18th Apr 2018, 12:37 PM
Momo Belia
Momo Belia - avatar