🐍Python/Python_알고리즘

[알고리즘] 30. Designer PDF Viewer

728x90
반응형

When you select a contiguous block of text in a PDF viewer, the selection is highlighted with a blue rectangle. In this PDF viewer, each word is highlighted independently. For example:

PDF-highighting.png

In this challenge, you will be given a list of letter heights in the alphabet and a string. Using the letter heights given, determine the area of the rectangle highlight in  assuming all letters are  wide.

For example, the highlighted . Assume the heights of the letters are  and . The tallest letter is  high and there are  letters. The hightlighted area will be  so the answer is .

Function Description

Complete the designerPdfViewer function in the editor below. It should return an integer representing the size of the highlighted area.

designerPdfViewer has the following parameter(s):

  • h: an array of integers representing the heights of each letter
  • word: a string

Input Format

The first line contains  space-separated integers describing the respective heights of each consecutive lowercase English letter, ascii[a-z].
The second line contains a single word, consisting of lowercase English alphabetic letters.

Constraints

  • , where  is an English lowercase letter.
  •  contains no more than  letters.

Output Format

Print a single integer denoting the area in  of highlighted rectangle when the given word is selected. Do not print units of measure.

Sample Input 0

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
abc

Sample Output 0

9

Explanation 0

We are highlighting the word abc:

Letter heights are  and . The tallest letter, b, is  high. The selection area for this word is .

Note: Recall that the width of each character is .

Sample Input 1

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 7
zaba

Sample Output 1

28

Explanation 1

The tallest letter in  is  at . The selection area for this word is .



답 : 


#!/bin/python3

import math
import os
import random
import re
import sys

# Complete the designerPdfViewer function below.
def designerPdfViewer(h, word):
val = list()
for words in list(word):
val.append(h[ord(words) - 97])
return len(word)*max(val)



if __name__ == '__main__':
fptr = open(os.environ['OUTPUT_PATH'], 'w')

h = list(map(int, input().rstrip().split()))

word = input()

result = designerPdfViewer(h, word)

fptr.write(str(result) + '\n')

fptr.close()


ord()를 이용해서 알파벳의 순서대로 h값을 추출해냈다. ord(a)가 97이기에 0으로 맞춰주기 위해 -97을 계산했다. 그리고 최댓값을 뽑아 단어의 길이와 곱해주면 끝!


728x90
반응형