Designer PDF Viewer

Parag Naik
2 min readFeb 4, 2021

There is a list o 26 character heights aligned by index to their letters. For example, ‘a’ is at index 0 and ‘z’ is at index 25 . There will also be a string. Using the letter heights given, determine the area of the rectangle highlight in assuming all letters are 1 mm wide.
ex-h=[1, 3, 1, 3, 1, 4, 1, 3, 2, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5, 5]
word=’abc’.

Photo by Ricardo Gomez Angel on Unsplash

Solution-
A very intresting hackerrank problem,the only uphill task at hand was to find how to related the alphabets with the given weight, so I tried to use zip in-built function of python which binds two or more elements inside a tuple.so first we create a list of alphabets from A-Z and then zip this alphabet list with the list of height given from list ‘h’ inside a list named ‘packed’.

def designerPdfViewer(h, word):
alphabet=['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z']
packed=list(zip(alphabet,h))
lst=[]
for i in word:
for j in packed:
if i==j[0]:
lst.append(j[1])
area=max(lst)*len(word)
return area

The we create an empty list “lst” then we will iterate the variable word and also the zip list packed and then compare each character of string word with the first element of tuple in the list “packed”.if the character is found in the “packed” list then we will pick the associated height which we had zipped with the respective alphabet and then append it to empty list “lst”.Then we will select the highest value from the list ‘lst’ and then multiple it with the lenght of string word and return the value.

Conclusion-
As the phrase “Thinking is the hardest work there is, which is probably the reason so few engage in it” say keep learning and keep hustling…..(cheers:-PN)

--

--