Juni_Dev_log

(200124_TIL) 'Resistor Color', 'Resistor Color Duo' 본문

CodingTest/Exercism

(200124_TIL) 'Resistor Color', 'Resistor Color Duo'

Juni_K 2021. 1. 24. 14:27

▶ Resistor Color

Registor Color

(Problem)

Introduction

If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know two things about them:

  • Each resistor has a resistance value.
  • Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands.

These colors are encoded as follows:

  • Black: 0
  • Brown: 1
  • Red: 2
  • Orange: 3
  • Yellow: 4
  • Green: 5
  • Blue: 6
  • Violet: 7
  • Grey: 8
  • White: 9

The goal of this exercise is to create a way:

  • to look up the numerical value associated with a particular color band
  • to list the different band colors

Mnemonics map the colors to the numbers, that, when stored as an array, happen to map to their index in the array: Better Be Right Or Your Great Big Values Go Wrong.

More information on the color encoding of resistors can be found in the Electronic color code Wikipedia article

Although the color names are capitalised in the description, the function colorCode will always be called with the lowercase equivalent, e.g brown instead of Brown

 

- color 값으로 주어진 색깔에 해당하는 숫자가 나오도록 코드를 작성한다.

 

(Solution)

① if / else if 사용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
//
// This is only a SKELETON file for the 'Resistor Color' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
 
export const colorCode = (color) => {
    if(color=="black"){
        return 0;
    }else if(color=="brown"){
        return 1;
    }else if(color=="red"){
        return 2;
    }else if(color=="orange"){
        return 3;
    }else if(color=="yellow"){
        return 4;
    }else if(color=="green"){
        return 5;
    }else if(color=="blue"){
        return 6;
    }else if(color=="violet"){
        return 7;
    }else if(color=="grey"){
        return 8;
    }else if(color=="white"){
        return 9;
    }
};
 
export const COLORS = undefined;
 
cs

처음에는 단순히 if / else if 로 해야한다고 생각했는데, 생각해보니 indexOf 를 사용해서 배열의 순서값으로 매겨도 좋을 것 같다는 생각이 들었다.

 

② indexOf 를 활용

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// This is only a SKELETON file for the 'Resistor Color' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
 
export const colorCode = (color) => {
    return COLORS.indexOf(color)
};
 
export const COLORS = [
'black',
'brown',
'red',
'orange',
'yellow',
'green',
'blue',
'violet',
'grey',
'white'
];
 
cs

 

developer.mozilla.org/ko/docs/Web/JavaScript/Reference/Global_Objects/Array/indexOf

 

Array.prototype.indexOf() - JavaScript | MDN

indexOf() 메서드는 배열에서 지정된 요소를 찾을 수 있는 첫 번째 인덱스를 반환하고 존재하지 않으면 -1을 반환합니다. arr.indexOf(searchElement[, fromIndex]) searchElement 배열에서 찾을 요소입니다. fromInde

developer.mozilla.org

 

▶ Resistor Color Duo

(Problem)

Introduction

If you want to build something using a Raspberry Pi, you'll probably use resistors. For this exercise, you need to know two things about them:

  • Each resistor has a resistance value.
  • Resistors are small - so small in fact that if you printed the resistance value on them, it would be hard to read.

To get around this problem, manufacturers print color-coded bands onto the resistors to denote their resistance values. Each band has a position and a numeric value.

The first 2 bands of a resistor have a simple encoding scheme: each color maps to a single number.

For example, if they printed a brown band (value 1) followed by a green band (value 5), it would translate to the number 15.

In this exercise you are going to create a helpful program so that you don't have to remember the values of the bands. The program will take color names as input and output a two digit number, even if the input is more than two colors!

The band colors are encoded as follows:

  • Black: 0
  • Brown: 1
  • Red: 2
  • Orange: 3
  • Yellow: 4
  • Green: 5
  • Blue: 6
  • Violet: 7
  • Grey: 8
  • White: 9

From the example above: brown-green should return 15 brown-green-violet should return 15 too, ignoring the third color.

 

- 위에서 푼 문제와 유사한 문제이다. 두 가지의 컬러로 엮여있는 문장을 숫자로 바꾸는 문제이다.

- 예를들어, "Black and brown"이라면, 0 과 1을 합친 "01"이 되는 것이다.

 

(Solution)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
//
// This is only a SKELETON file for the 'Resistor Color Duo' exercise. It's been provided as a
// convenience to get you started writing code faster.
//
 
export const decodedValue = ([color1, color2]) => {
    const num_check = (color) => COLORS.indexOf(color);
    return num_check(color1)*10 + num_check(color2);
};
 
export const COLORS = [
'black',
'brown',
'red',
'orange',
'yellow',
'green',
'blue',
'violet',
'grey',
'white'
];
cs

 

위에서 indexOf 를 활용한 방법을 사용하고 싶어서

- 색깔에 따른 숫자값을 계산하는 num_check 라는 함수를 만들었다. (indexOf 를 사용해서 계산)

- 그리고 return 값으로 첫번째 배열값인 color1, 두번째 배열값인  color2 를 num_check 함수의 파라미터로 넣었다.

- color1에는 10을 곱하고, color2 는 그대로 더해서 문제를 해결했다.

 

 

 

'CodingTest > Exercism' 카테고리의 다른 글

(200128_TIL) 'Bank Account'  (0) 2021.01.28
(200127_TIL) 'Pangram'  (0) 2021.01.27
(200126_TIL) 'Space Age'  (0) 2021.01.26
(200125_TIL) 'Gigasecond', 'RNA Transcription'  (0) 2021.01.25
(210123_TIL) 'Hello World', 'Two Fer'  (0) 2021.01.23
Comments