본문 바로가기
알고리즘/Codewars

[Codewars] Printer Errors (7 kyu) / JavaScript

by fluss 2022. 12. 19.

https://www.codewars.com/kata/56541980fa08ab47a0000040

 

Codewars - Achieve mastery through coding practice and developer mentorship

A coding practice website for all programming levels – Join a community of over 3 million developers and improve your coding skills in over 55 programming languages!

www.codewars.com

 

DESCRIPTION:

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"

 

설명:

공장에서 프린터는 상자의 라벨을 인쇄합니다. 한 종류의 상자에 대해 프린터는 단순성을 위해 a부터 m까지의 문자로 이름이 붙은 색을 사용해야 한다.

프린터에서 사용되는 색상은 제어 문자열에 기록됩니다. 예를 들어 aaabbbbhaijjjm은 프린터가 색 a를 3번, 색 b를 4번, 색 h를 1번 그리고 색 a를 한번.... 을 사용한 "좋은" 제어 문자열입니다. 

 

색의 부족, 기술적 오작동 그리고 "나쁜" 제어 문자열 a부터 m까지의 문자가 아닌 단어들로 가끔 문제가 발생합니다. 예: aaaxbbbbyyhwawiwjjjwwm

 

문자열을 받아 분모가 오류의 수이고 분자가 제어 문자열의 길이인 프린터의 오류 비율을 문자열로 반환하는 printer_error 함수를 작성하세요. 이 분수를 더 단순한 표현으로 줄이지 마세요.

 

문자열의 길이는 1 이상으로 a부터 z까지의 문자만 포함합니다.

 

예시:

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

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

 

풀이

단어의 아스키코드 번호 범위가 a부터 m까지가 아닌 숫자를 세 그 수를 분자로, 문자열의 길이를 분모로 한 문자열을 반환하였다.

 

코드

function printerError(s) {
  let count = 0;
  for(let i = 0; i < s.length; i++){
    if(s.charCodeAt(i) >= 110 && s.charCodeAt(i) <= 122) count++;
  }
  return count + '/' + s.length;
}
 

댓글