나는 이렇게 학습한다/Algorithm & SQL

Printer Errors

daco2020 2022. 7. 31. 00:46
반응형

In a factory a printer prints labels for boxes. For one kind of boxes the printer has to use colors which, for the sake of simplicity, are named with letters from a to m.

The colors used by the printer are recorded in a control string. For example a "good" control string would be aaabbbbhaijjjm meaning that the printer used three times color a, four times color b, one time color h then one time color a...

Sometimes there are problems: lack of colors, technical malfunction and a "bad" control string is produced e.g. aaaxbbbbyyhwawiwjjjwwm with letters not from a to m.

You have to write a function printer_error which given a string will return the error rate of the printer as a string representing a rational whose numerator is the number of errors and the denominator the length of the control string. Don't reduce this fraction to a simpler expression.

The string has a length greater or equal to one and contains only letters from ato z.

Examples:

s="aaabbbbhaijjjm"
printer_error(s) => "0/14"

s="aaaxbbbbyyhwawiwjjjwwm"
printer_error(s) => "8/22"

 

 

Solution:

def printer_error(s):
    err = [i for i in s if ord(i) not in range(ord("a"),ord("m")+1)]
    return f"{len(err)}/{len(s)}"

 

 

Other Solution:

from re import sub
def printer_error(s):
    return "{}/{}".format(len(sub("[a-m]",'',s)),len(s))

 

반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

Form The Minimum  (0) 2022.08.02
Make a function that does arithmetic!  (0) 2022.07.31
Build Tower  (0) 2022.07.29
Highest Scoring Word  (0) 2022.07.28
Simple Fun #176: Reverse Letter  (0) 2022.07.27