본문 바로가기

Algorithm101

Sum of numbers from 0 to N Description: We want to generate a function that computes the series starting from 0 and ending until the given number. Example: Input: > 6 Output: 0+1+2+3+4+5+6 = 21 Input: > -15 Output: -15 0 Output: 0=0 Solution: 1. If n is 0, "0=0" is returned. 2. If n is negative, "n 2022. 5. 11.
Round up to the next multiple of 5 Given an integer as input, can you round it to the next (meaning, "higher") multiple of 5? Examples: input: output: 0 -> 0 2 -> 5 3 -> 5 12 -> 15 21 -> 25 30 -> 30 -2 -> 0 -5 -> -5 etc. Input may be any positive or negative integer (including 0). You can assume that all inputs are valid integers. Solution: 1. Divide n by 5 and round up. 2. Multiply the rounded value by 5. def round_to_next5(n): .. 2022. 5. 10.
Find the capitals Instructions Write a function that takes a single string (word) as argument. The function must return an ordered list containing the indexes of all capital letters in the string. Example Test.assertSimilar( capitals('CodEWaRs'), [0,3,4,6] ); Solution: 1. Find the uppercase character in the string. 2. It returns the index of the corresponding character in an array. def capitals(word): return [i f.. 2022. 5. 9.
Coding Meetup #5 - Higher-Order Functions Series - Prepare the count of languages You will be given an array of objects (associative arrays in PHP, table in COBOL) representing data about developers who have signed up to attend the next coding meetup that you are organising. Your task is to return an object (associative array in PHP, table in COBOL) which includes the count of each coding language represented at the meetup. For example, given the following input array: list1 .. 2022. 5. 8.
Initialize my name Some people just have a first name; some people have first and last names and some people have first, middle and last names. You task is to initialize the middle names (if there is any). Examples 'Jack Ryan' => 'Jack Ryan' 'Lois Mary Lane' => 'Lois M. Lane' 'Dimitri' => 'Dimitri' 'Alice Betty Catherine Davis' => 'Alice B. C. Davis' Solution: 1. If there are 3 or more elements of name, only the f.. 2022. 5. 7.
Even numbers in an array Given an array of numbers, return a new array of length number containing the last even numbers from the original array (in the same order). The original array will be not empty and will contain at least "number" even numbers. For example: ([1, 2, 3, 4, 5, 6, 7, 8, 9], 3) => [4, 6, 8] ([-22, 5, 3, 11, 26, -6, -7, -8, -9, -8, 26], 2) => [-8, 26] ([6, -25, 3, 7, 5, 5, 7, -3, 23], 1) => [6] Solutio.. 2022. 5. 6.