[#5] KoreanCoding 101: binary addition, making B out of A
Table of contents
Another Friday, another chance to polish your Korean technical vocabulary skills!
Announcement: Due to some new exciting things happening in my private life, I will have to limit the number of challenges we will be going through from this day forward. I'll try to focus on choosing the ones that will introduce new vocabulary as much as possible.
Ready? Let's go!
Vocabulary
Nouns:
문자열 - string
이진수 - binary
소문자 - lowercase
대문자 - uppercase
합 - sum
순서 - order (of something)
두 이진수의 합 - the sum of two binary numbers
함수 - function
매개변수 - parameter
메소드 - method
길이 - length
제한사항 - constraints
입출력 예 - input-output examples
입출력 예 설명 - explanation of input-output examples
삼항 연산자 - ternary operator
Verbs:
의미하다 - to mean, to stand for
2에 1을 더하다 - to add 1 to 2
바꾸다 - to change
정렬하다 - to sort
Phrases:
[something]을/를 return 하도록 solution
함수를 완성해주세요.
Complete the solution
function to return [something].
문자열 bin1
과 bin2
가 주어질 때...
given two strings bin1
and bin2
...
Challenges
이진수 더하기
Description
이진수를 의미하는 두 개의 문자열
bin1
과bin2
가 매개변수로 주어질 때, 두 이진수의 합을 return하도록 solution 함수를 완성해주세요.제한사항
return 값은 이진수를 의미하는 문자열입니다.
1 ≤
bin1
,bin2
의 길이 ≤ 10
bin1
과bin2
는 0과 1로만 이루어져 있습니다.
bin1
과bin2
는 "0"을 제외하고 0으로 시작하지 않습니다.입출력 예
bin1 bin2 result "10" "11" "101" "1001" "1111" "11000" 입출력 예 설명
입출력 예 #1
- 10 + 11 = 101 이므로 "101" 을 return합니다.
입출력 예 #2
- 1001 + 1111 = 11000 이므로 "11000"을 return합니다.
Description:
이진수 더하기
Binary addition
이진수를 의미하는 두 개의 문자열 bin1
과 bin2
가 매개변수로 주어질 때,
Given two strings bin1
and bin2
which represent binary numbers,
두 이진수의 합을 return하도록 solution 함수를 완성해주세요.
complete the solution
function to return the sum of these two binary numbers.
제한사항 (constraints):
The return value should be a string representing a binary number.
The length of
bin1
andbin2
should be between 1 and 10 (inclusive).bin1
andbin2
are composed only of the digits 0 and 1.bin1
andbin2
should not start with "0" except when the string consists of a single "0".
입출력 예 (input-output examples):
#1: When bin1
= '10' and bin2
='11', the result should be '101'.
입출력 예 설명 (explanation of input-output examples):
Based on the example #1: Since 10 + 11 = 101, we return "101".
JavaScript Solution
In short, we want to perform binary addition on the two binary numbers provided and return the result as a binary string.
To do that, we can use parseInt()
to convert both bin1
and bin2
to their decimal equivalents and sum them. Then, we can use toString()
to convert the decimal sum back into a binary string by using it with a base argument of 2.
function solution(bin1, bin2) {
const sum = parseInt(bin1, 2) + parseInt(bin2, 2)
return sum.toString(2)
}
A로 B 만들기
Description
문자열
before
와after
가 매개변수로 주어질 때,before
의 순서를 바꾸어after
를 만들 수 있으면 1을, 만들 수 없으면 0을 return 하도록 solution 함수를 완성해보세요.제한사항
0 <
before
의 길이 ==after
의 길이 < 1,000
before
와after
는 모두 소문자로 이루어져 있습니다.입출력 예
before after result "olleh" "hello" 1 "allpe" "apple" 0 입출력 예 설명
입출력 예 #1
- "olleh"의 순서를 바꾸면 "hello"를 만들 수 있습니다.
입출력 예 #2
- "allpe"의 순서를 바꿔도 "apple"을 만들 수 없습니다.
Description:
A로 B 만들기
Making B out of A
문자열 before
와 after
가 매개변수로 주어질 때,
Given strings before
and after
as parameters,
before
의 순서를 바꾸어 after
를 만들 수 있으면 1을,
Complete the solution
function to return 1 if before
can be reordered to create after
만들 수 없으면 0을 return 하도록 solution 함수를 완성해보세요
and if it cannot be created, return 0.
제한사항 (constraints):
The length of the
before
andafter
string is greater than 0 and less than 1,000 and the length of thebefore
string is equal to the length of theafter
string.Both the
before
andafter
strings consist only of lowercase letters.
입출력 예 (input-output examples):
#1: When before
= "olleh" and after
= "hello" return 1.
#2: When before
= "allpe" and after
= "apple" return 0.
입출력 예 설명 (explanation of input-output examples):
Based on the example #1:
You can make "hello" by reversing the order of "olleh".
Based on the example #2:
Reversing the order of "allpe" does not make "apple".
JavaScript Solution
The easiest way to check if two strings consist of the same characters is to split the string into an array of individual characters because it allows us to manipulate and sort the characters easily. Then we sort the array of characters in ascending order and join the sorted array of characters back into a string for easy comparison.
Finally, we use a ternary operator (삼항 연산자) to return the correct result: if they match, we return 1 and if they do not, we return 0.
function solution(before, after) {
const bMod = before.split("").sort().join("")
const aMod = after.split("").sort().join("")
return aMod === bMod ? 1 : 0
}
Wrap-up
And that's it for today! Awesome job!
You can support this series and the work I put into it by buying me a coffee here:
Have an awesome weekend!
Subscribe to my newsletter
Read articles from Ha Anna directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Ha Anna
Ha Anna
I am a self-taught Front-end developer with a passion for web accessibility and design. I love puzzles, challenges, well-organized spaces, and lame jokes. When I am not coding, I am hanging out with the local stray cats, checking out new cool cafes, or playing golf.