본문 바로가기

Algorithm101

Write Number in Expanded Form 문제 설명 You will be given a number and you will need to return it as a string in Expanded Form. For example: expanded_form(12) # Should return '10 + 2' expanded_form(42) # Should return '40 + 2' expanded_form(70304) # Should return '70000 + 300 + 4' NOTE: All numbers will be whole numbers greater than 0. 해결 방법 1. 숫자를 문자열로 바꾼다. 2. 문자열 중에 '0'이 아닌 수를 찾는다. 3. 찾은 수에 현재 남은 자리수 만큼 '0'을 붙인다. 4. 리스트에 담는다. .. 2022. 2. 18.
Split String 문제 설명 Complete the solution so that it splits the string into pairs of two characters. If the string contains an odd number of characters then it should replace the missing second character of the final pair with an underscore ('_'). Examples: solution('abc') # should return ['ab', 'c_'] solution('abcdef') # should return ['ab', 'cd', 'ef'] 해결 방법 1. s의 문자열 수가 만약 홀수라면 뒤에 '_'를 붙인다. 2. 문자열을 두개씩 잘라서.. 2022. 2. 17.
Bit Counting 문제 설명 Write a function that takes an integer as input, and returns the number of bits that are equal to one in the binary representation of that number. You can guarantee that input is non-negative. Example: The binary representation of 1234 is 10011010010, so the function should return 5 in this case 해결 방법 1. 들어온 수를 2진수로 바꾼다. 2. 바꾼 수에서 '1'의 개수를 세어 반환한다. def count_bits(n): return bin(n).count('1.. 2022. 2. 16.
Create Phone Number 문제 설명 Write a function that accepts an array of 10 integers (between 0 and 9), that returns a string of those numbers in the form of a phone number. Example create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) # => returns "(123) 456-7890" The returned format must be correct in order to complete this challenge. Don't forget the space after the closing parentheses! 해결 방법 1. n에 담긴 숫자 요소들을 문자로 바꾼다. .. 2022. 2. 14.
Array.diff 문제 설명 Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result. It should remove all values from list a, which are present in list b keeping their order. array_diff([1,2],[1]) == [2] If a value is present in b, all of its occurrences must be removed from the other: array_diff([1,2,2,2,3],[2]) == [1,3] 해결 방법 1. b리스트 안의 요소가 a리스트 안에 .. 2022. 2. 13.
Ones and Zeros 문제 설명 Given an array of ones and zeroes, convert the equivalent binary value to an integer. Eg: [0, 0, 0, 1] is treated as 0001 which is the binary representation of 1. Examples: Testing: [0, 0, 0, 1] ==> 1 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 0, 1] ==> 5 Testing: [1, 0, 0, 1] ==> 9 Testing: [0, 0, 1, 0] ==> 2 Testing: [0, 1, 1, 0] ==> 6 Testing: [1, 1, 1, 1] ==> 15 Testing: [1, 0, 1, 1] .. 2022. 2. 12.