├── C++ ├── 5 kyu. Number of trailing zeros of N!.md ├── 6 kyu. Create Phone Number.md ├── 6 kyu. Duplicate Encoder.md ├── 6 kyu. Sum of Digits Digital Root.md ├── 7 kyu. Incrementer.md ├── 7 kyu. Sum of odd numbers.md ├── 8 kyu. All Star Code Challenge #18.md ├── 8 kyu. Even or Odd.md ├── 8 kyu. Find the smallest integer in the array.md ├── 8 kyu. Is it a palindrome.md ├── 8 kyu. Is this a triangle.md ├── 8 kyu. Jenny's secret message.md └── 8 kyu. Twice as old.md ├── Python ├── 4 kyu. Next smaller number with the same digits.md ├── 4 kyu. Range Extraction.md ├── 4 kyu. Strip Comments.md ├── 4 kyu. Twice linear.md ├── 5 kyu. Factorial decomposition.md ├── 5 kyu. Maximum subarray sum.md ├── 5 kyu. Ninety Nine Thousand Nine Hundred Ninety Nine.md ├── 5 kyu. Number of trailing zeros of N!.md ├── 5 kyu. Paginating a huge book.md ├── 5 kyu. Rotate an array matrix.md ├── 5 kyu. Simple Pig Latin.md ├── 5 kyu. Write out numbers.md ├── 5 kyu. int32 to IPv4.md ├── 6 kyu. +1 Array.md ├── 6 kyu. A Rule of Divisibility by 13.md ├── 6 kyu. Are they the same.md ├── 6 kyu. Array.diff.md ├── 6 kyu. Ascend, Descend, Repeat.md ├── 6 kyu. Build Tower.md ├── 6 kyu. Consecutive strings.md ├── 6 kyu. Count words.md ├── 6 kyu. Create Phone Number.md ├── 6 kyu. Delete occurrences of an element if it occurs more than n times.md ├── 6 kyu. Does my number look big in this.md ├── 6 kyu. Duplicate Encoder.md ├── 6 kyu. Encrypt this!.md ├── 6 kyu. Highest Rank Number in an Array.md ├── 6 kyu. IP Validation.md ├── 6 kyu. Sum of Digits Digital Root.md ├── 6 kyu. World Bits War.md ├── 7 kyu. Incrementer.md ├── 7 kyu. Is this a triangle.md ├── 7 kyu. Single digit.md ├── 7 kyu. Sort rectangles and circles by area II.md ├── 7 kyu. Sum of odd numbers.md ├── 7 kyu. Test Your Knowledge Of Function Scope.md ├── 7 kyu. The Poet And The Pendulum .md ├── 7 kyu. Which string is worth more.md └── 8 kyu. Even or Odd.md └── Shell ├── 8 kyu. Century From Year.md ├── 8 kyu. Expressions Matter.md ├── 8 kyu. Remove First and Last Character.md └── 8 kyu. Third Angle of a Triangle.md /C++/5 kyu. Number of trailing zeros of N!.md: -------------------------------------------------------------------------------- 1 | # Number of trailing zeros of N! 2 | Write a program that will calculate the number of trailing zeros in a factorial of a given number. 3 | 4 | N! = 1 * 2 * 3 * ... * N 5 | 6 | Be careful 1000! has 2568 digits... 7 | 8 | For more info, see: http://mathworld.wolfram.com/Factorial.html 9 | 10 | # Examples 11 | ``` 12 | zeros(6) = 1 13 | # 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero 14 | 15 | zeros(12) = 2 16 | # 12! = 479001600 --> 2 trailing zeros 17 | ``` 18 | Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros. 19 | # Solution 20 | ``` 21 | long zeros(long n) { 22 | int sum = 0; 23 | while (n>0){ 24 | n/=5; 25 | sum+=n; 26 | } 27 | return sum; 28 | } 29 | ``` -------------------------------------------------------------------------------- /C++/6 kyu. Create Phone Number.md: -------------------------------------------------------------------------------- 1 | # Create Phone Number 2 | 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. 3 | # Example 4 | ``` 5 | create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) # => returns "(123) 456-7890" 6 | ``` 7 | The returned format must be correct in order to complete this challenge. 8 | Don't forget the space after the closing parentheses! 9 | # Solution 1 10 | ``` 11 | #include 12 | 13 | std::string createPhoneNumber(const int arr [10]){ 14 | int arr1[10]; 15 | for (int i=0;i<10;i++) 16 | arr1[i] = arr[i]; 17 | std::string str; 18 | for (int i: arr1) { 19 | str += std::to_string(i); 20 | } 21 | return "("+str.substr(0,3)+") "+str.substr(3,3)+"-"+str.substr(6); 22 | } 23 | ``` 24 | -------------------------------------------------------------------------------- /C++/6 kyu. Duplicate Encoder.md: -------------------------------------------------------------------------------- 1 | # Duplicate Encoder 2 | The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears 3 | only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining 4 | if a character is a duplicate. 5 | 6 | # Examples 7 | ``` 8 | "din" => "(((" 9 | "recede" => "()()()" 10 | "Success" => ")())())" 11 | "(( @" => "))((" 12 | ``` 13 | # Notes 14 | Assertion messages may be unclear about what they display in some languages. If you read "...It Should encode XXX", the "XXX" is the expected result, not the input! 15 | # Solution 16 | ``` 17 | #include 18 | #include 19 | std::string str_tolower(std::string s) { 20 | std::transform(s.begin(), s.end(), s.begin(), 21 | [](unsigned char c){ return std::tolower(c); } 22 | ); 23 | return s; 24 | } 25 | std::string duplicate_encoder(const std::string& word){ 26 | std::string res = ""; 27 | std::string word_new = str_tolower(word); 28 | for (auto i:word_new){ 29 | if (std::count(word_new.begin(), word_new.end(), i) > 1) 30 | res+=")"; 31 | else 32 | res+="("; 33 | } 34 | return res; 35 | } 36 | ``` -------------------------------------------------------------------------------- /C++/6 kyu. Sum of Digits Digital Root.md: -------------------------------------------------------------------------------- 1 | # Sum of Digits / Digital Root 2 | Digital root is the recursive sum of all the digits in a number. 3 | 4 | Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer. 5 | 6 | **Examples** 7 | ``` 8 | 16 --> 1 + 6 = 7 9 | 942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6 10 | 132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6 11 | 493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2 12 | ``` 13 | # Solution 1 14 | ``` 15 | int digital_root(int n) 16 | { 17 | int sum = 0; 18 | int res = 0; 19 | int res1 = 0; 20 | while (n!=0){ 21 | sum += n%10; 22 | n/=10; 23 | } 24 | while (sum!=0){ 25 | res += sum%10; 26 | sum/=10; 27 | } 28 | while (res!=0){ 29 | res1 += res%10; 30 | res/=10; 31 | } 32 | return res1; 33 | } 34 | ``` 35 | # Solution 2 36 | ``` 37 | int digital_root(int n) 38 | { 39 | int sum = 0; 40 | while (n!=0){ 41 | sum += n%10; 42 | n/=10; 43 | } 44 | return (sum<=9)? sum:digital_root(sum); 45 | } 46 | ``` -------------------------------------------------------------------------------- /C++/7 kyu. Incrementer.md: -------------------------------------------------------------------------------- 1 | # Incrementer 2 | Given an input of an array of digits, return the array with each digit incremented by its position in the array: the first digit will be incremented by 1, the second digit by 2, etc. Make sure to start counting your positions from 1 (and not 0). 3 | 4 | Your result can only contain single digit numbers, so if adding a digit with it's position gives you a multiple-digit number, only the last digit of the number should be returned. 5 | 6 | Notes: 7 | 1. return an empty array if your array is empty 8 | 2. arrays will only contain numbers so don't worry about checking that 9 | Examples 10 | ``` 11 | [1, 2, 3] --> [2, 4, 6] # [1+1, 2+2, 3+3] 12 | 13 | [4, 6, 9, 1, 3] --> [5, 8, 2, 5, 8] # [4+1, 6+2, 9+3, 1+4, 3+5] 14 | # 9+3 = 12 --> 2 15 | ``` 16 | # Solution 17 | ``` 18 | #include 19 | std::vector incrementer(std::vector nums){ 20 | std::vector res = {}; 21 | int k = 1; 22 | for (int i=0; i=10) 24 | res.push_back((nums[i]+k)%10); 25 | else 26 | res.push_back(nums[i]+k); 27 | k++; 28 | } 29 | return res; 30 | } 31 | ``` -------------------------------------------------------------------------------- /C++/7 kyu. Sum of odd numbers.md: -------------------------------------------------------------------------------- 1 | # Sum of odd numbers 2 | **Description**: 3 | 4 | Given the triangle of consecutive odd numbers: 5 | ``` 6 | 1 7 | 3 5 8 | 7 9 11 9 | 13 15 17 19 10 | 21 23 25 27 29 11 | ... 12 | ``` 13 | # Solution 14 | ``` 15 | long long rowSumOddNumbers(unsigned n){ 16 | return n*n*n; 17 | } 18 | ``` -------------------------------------------------------------------------------- /C++/8 kyu. All Star Code Challenge #18.md: -------------------------------------------------------------------------------- 1 | # All Star Code Challenge #18 2 | **This Kata is intended as a small challenge for my students** 3 | 4 | All Star Code Challenge #18 5 | 6 | Create a function that accepts 2 string arguments and returns an integer of the count of occurrences the 2nd argument is found in the first one. 7 | 8 | If no occurrences can be found, a count of 0 should be returned. 9 | ``` 10 | ("Hello", "o") ==> 1 11 | ("Hello", "l") ==> 2 12 | ("", "z") ==> 0 13 | ``` 14 | **Notes**: 15 | 16 | 1. The first argument can be an empty string 17 | 2. The second string argument will always be of length 1 18 | # Solution 19 | ``` 20 | #include 21 | 22 | unsigned int strCount(std::string word, char letter){ 23 | int sum = 0; 24 | for (int i=0;i 14 | 15 | using namespace std; 16 | 17 | int findSmallest(vector list) 18 | { 19 | int min = list[0]; 20 | for (int i =0;i 6 | #include 7 | #include 8 | std::string str_tolower(std::string s) { 9 | std::transform(s.begin(), s.end(), s.begin(), 10 | [](unsigned char c){ return std::tolower(c); } 11 | ); 12 | return s; 13 | } 14 | 15 | bool isPalindrom (const std::string& str) 16 | { 17 | std::string str1 = str; 18 | std::string str2 = str; 19 | std::reverse(str1.begin(), str1.end()); 20 | return (str_tolower(str1)==str_tolower(str2)); 21 | } 22 | ``` -------------------------------------------------------------------------------- /C++/8 kyu. Is this a triangle.md: -------------------------------------------------------------------------------- 1 | # Is this a triangle? 2 | Implement a function that accepts 3 integer values a, b, c. The function should return true if a triangle can be built with the sides of given length and false in any other case. 3 | 4 | (In this case, all triangles must have surface greater than 0 to be accepted). 5 | # Solution 6 | ``` 7 | namespace Triangle 8 | { 9 | bool isTriangle(long int a, long int b, long int c) 10 | { 11 | bool isTrue = (a+b>c) && (a+c>b) && (b+c>a) && (a>0) && (b>0) && (c>0); 12 | return isTrue; 13 | } 14 | }; 15 | ``` -------------------------------------------------------------------------------- /C++/8 kyu. Jenny's secret message.md: -------------------------------------------------------------------------------- 1 | # Jenny's secret message 2 | Jenny has written a function that returns a greeting for a user. However, she's in love with Johnny, and would like to greet him slightly different. She added a special case to her function, but she made a mistake. 3 | 4 | Can you help her? 5 | # Solution 6 | ``` 7 | std::string greet(std::string name) 8 | { 9 | if(name == "Johnny") { 10 | return "Hello, my love!"; 11 | } 12 | return "Hello, " + name + "!"; 13 | } 14 | ``` -------------------------------------------------------------------------------- /C++/8 kyu. Twice as old.md: -------------------------------------------------------------------------------- 1 | # Twice as old 2 | Your function takes two arguments: 3 | 4 | 1. current father's age (years) 5 | 2. current age of his son (years) 6 | Сalculate how many years ago the father was twice as old as his son (or in how many years he will be twice as old). 7 | # Solution 8 | ``` 9 | int twice_as_old(int dad, int son) { 10 | return abs(dad-son*2); 11 | } 12 | ``` -------------------------------------------------------------------------------- /Python/4 kyu. Next smaller number with the same digits.md: -------------------------------------------------------------------------------- 1 | # Next smaller number with the same digits 2 | Write a function that takes a positive integer and returns the next smaller positive integer containing the same digits. 3 | 4 | For example: 5 | ``` 6 | next_smaller(21) == 12 7 | next_smaller(531) == 513 8 | next_smaller(2071) == 2017 9 | ``` 10 | Return -1 (for Haskell: return Nothing, for Rust: return None), when there is no smaller number that contains the same digits. Also return -1 when the next smaller number with the same digits would require the leading digit to be zero. 11 | ``` 12 | next_smaller(9) == -1 13 | next_smaller(135) == -1 14 | next_smaller(1027) == -1 # 0721 is out since we don't write numbers with leading zeros 15 | ``` 16 | * some tests will include very large numbers. 17 | * test data only employs positive integers. 18 | # Solution 19 | ``` 20 | def swap(string, index1, index2): 21 | str = list(string) 22 | str[index1], str[index2] = str[index2], str[index1] 23 | str = sorted(str[:index1]) + str[index1:] 24 | return ''.join(str) 25 | 26 | def next_smaller(n): 27 | number = str(n) 28 | if list(number)==sorted(number): 29 | return -1 30 | reverse_n = number[::-1] 31 | for i, digit in enumerate(reverse_n,0): 32 | if any(n for n in reverse_n[:i] if n 1: 24 | tmp = str(args[i]) + "-" + str(args[j]) 25 | i = j+1 26 | else: 27 | i = (j if j > i else i+1) 28 | res.append(tmp) 29 | return ",".join(str(x) for x in res) 30 | ``` -------------------------------------------------------------------------------- /Python/4 kyu. Strip Comments.md: -------------------------------------------------------------------------------- 1 | # Strip Comments 2 | Complete the solution so that it strips all text that follows any of a set of comment markers passed in. Any whitespace at the end of the line should also be stripped out. 3 | 4 | # Example: 5 | 6 | Given an input string of: 7 | ``` 8 | apples, pears # and bananas 9 | grapes 10 | bananas !apples 11 | ``` 12 | The output expected would be: 13 | ``` 14 | apples, pears 15 | grapes 16 | bananas 17 | ``` 18 | The code would be called like so: 19 | ``` 20 | result = solution("apples, pears # and bananas\ngrapes\nbananas !apples", ["#", "!"]) 21 | # result should == "apples, pears\ngrapes\nbananas" 22 | ``` 23 | # Solution 24 | ``` 25 | def solution(string,markers): 26 | string = string.split('\n') 27 | for i in range(len(string)): 28 | s = string[i] 29 | for marker in markers: 30 | index = s.find(marker) 31 | if index != -1: 32 | s = s[:index].rstrip() 33 | string[i] = s 34 | return '\n'.join(string) 35 | ``` -------------------------------------------------------------------------------- /Python/4 kyu. Twice linear.md: -------------------------------------------------------------------------------- 1 | # Twice linear 2 | Consider a sequence u where u is defined as follows: 3 | 4 | 1. The number u(0) = 1 is the first one in u. 5 | 2. For each x in u, then y = 2 * x + 1 and z = 3 * x + 1 must be in u too. 6 | 3. There are no other numbers in u. 7 | # Example: 8 | ``` 9 | u = [1, 3, 4, 7, 9, 10, 13, 15, 19, 21, 22, 27, ...] 10 | ``` 11 | 1 gives 3 and 4, then 3 gives 7 and 10, 4 gives 9 and 13, then 7 gives 15 and 22 and so on... 12 | 13 | # Task: 14 | Given parameter n the function dbl_linear (or dblLinear...) returns the element u(n) of the ordered sequence u (ordered with < so there are no duplicates) . 15 | 16 | # Example: 17 | dbl_linear(10) should return 22 18 | 19 | # Note: 20 | Focus attention on efficiency 21 | 22 | # Solution 23 | ``` 24 | def dbl_linear(n): 25 | res,x,y = [1],0,0 26 | while(len(res)<=n): 27 | a = 2*res[x] + 1 28 | b = 3*res[y] + 1 29 | if a>b: 30 | res.append(b) 31 | y+= 1 32 | elif a "2^10 * 3^5 * 5^2 * 7 * 11" 7 | since 12! is divisible by 2 ten times, by 3 five times, by 5 two times and by 7 and 11 only once. 8 | 9 | n = 22; decomp(22) -> "2^19 * 3^9 * 5^4 * 7^3 * 11^2 * 13 * 17 * 19" 10 | 11 | n = 25; decomp(25) -> 2^22 * 3^10 * 5^6 * 7^3 * 11^2 * 13 * 17 * 19 * 23 12 | ``` 13 | Prime numbers should be in increasing order. When the exponent of a prime is 1 don't put the exponent. 14 | 15 | # Notes 16 | 17 | 1. the function is decomp(n) and should return the decomposition of n! into its prime factors in increasing order of the primes, as a string. 18 | 2. factorial can be a very big number (4000! has 12674 digits, n can go from 300 to 4000). 19 | 3. In Fortran - as in any other language - the returned string is not permitted to contain any redundant trailing whitespace: you can use dynamically allocated character strings. 20 | # Solution 21 | ``` 22 | def primes(n): 23 | n += 1 24 | a = [True] * n 25 | for i in range(3,int(n**0.5)+1,2): 26 | if a[i]: 27 | a[i*i::2*i]=[False]*((n-i*i-1)//(2*i)+1) 28 | return [2] + [i for i in range(3,n,2) if a[i]] 29 | 30 | 31 | def decomp(n): 32 | res = [] 33 | for i in primes(n): 34 | rest,s = n,0 35 | while rest >0: 36 | j = rest // i 37 | s += j 38 | rest = j 39 | if s > 1: 40 | res.append(f'{i}^{s}') 41 | else: 42 | res.append(str(i)) 43 | return ' * '.join(res) 44 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Maximum subarray sum.md: -------------------------------------------------------------------------------- 1 | # Maximum subarray sum 2 | The maximum sum subarray problem consists in finding the maximum sum of a contiguous subsequence in an array or list of integers: 3 | ``` 4 | max_sequence([-2, 1, -3, 4, -1, 2, 1, -5, 4]) 5 | # should be 6: [4, -1, 2, 1] 6 | ``` 7 | Easy case is when the list is made up of only positive numbers and the maximum sum is the sum of the whole array. If the list is made up of only negative numbers, return 0 instead. 8 | 9 | Empty list is considered to have zero greatest sum. Note that the empty list or array is also a valid sublist/subarray. 10 | # Solution 11 | ``` 12 | def max_sequence(arr): 13 | if not arr or max(arr) < 0: 14 | return 0 15 | 16 | max_value_till_now = arr[0] 17 | max_end = 0 18 | for i in arr: 19 | max_end = max_end + i 20 | if max_end < 0: 21 | max_end = 0 22 | elif max_value_till_now < max_end: 23 | max_value_till_now = max_end 24 | return max_value_till_now 25 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Ninety Nine Thousand Nine Hundred Ninety Nine.md: -------------------------------------------------------------------------------- 1 | # Ninety Nine Thousand Nine Hundred Ninety Nine 2 | Write a method that takes a number and returns a string of that number in English. 3 | 4 | Your method should be able to handle any number between 0 and 99999. If the given number is outside of that range or not an integer, the method should return an empty string. 5 | 6 | #Examples 7 | ``` 8 | 0 --> "zero" 9 | 27 --> "twenty seven" 10 | 100 --> "one hundred" 11 | 7012 --> "seven thousand twelve" 12 | 99205 --> "ninety nine thousand two hundred five" 13 | ``` 14 | # Solution 15 | ``` 16 | def number_to_english(n): 17 | d = { 0 : 'zero', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 18 | 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten', 19 | 11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen', 20 | 15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen', 21 | 19 : 'nineteen', 20 : 'twenty', 22 | 30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty', 23 | 70 : 'seventy', 80 : 'eighty', 90 : 'ninety' } 24 | k = 1000 25 | m = k * 1000 26 | b = m * 1000 27 | 28 | if n<0 or int(n)!=n or n>=100000: 29 | return '' 30 | 31 | if (n < 20): 32 | return d[n] 33 | 34 | if (n < 100): 35 | if n % 10 == 0: return d[n] 36 | else: return d[n // 10 * 10] + ' ' + d[n % 10] 37 | 38 | if (n < k): 39 | if n % 100 == 0: return d[n // 100] + ' hundred' 40 | else: return d[n // 100] + ' hundred ' + number_to_english(n % 100) 41 | 42 | if (n < m): 43 | if n % k == 0: return number_to_english(n // k) + ' thousand' 44 | else: return number_to_english(n // k) + ' thousand ' + number_to_english(n % k) 45 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Number of trailing zeros of N!.md: -------------------------------------------------------------------------------- 1 | # Number of trailing zeros of N! 2 | Write a program that will calculate the number of trailing zeros in a factorial of a given number. 3 | 4 | N! = 1 * 2 * 3 * ... * N 5 | 6 | Be careful 1000! has 2568 digits... 7 | 8 | For more info, see: http://mathworld.wolfram.com/Factorial.html 9 | 10 | # Examples 11 | ``` 12 | zeros(6) = 1 13 | # 6! = 1 * 2 * 3 * 4 * 5 * 6 = 720 --> 1 trailing zero 14 | 15 | zeros(12) = 2 16 | # 12! = 479001600 --> 2 trailing zeros 17 | ``` 18 | Hint: You're not meant to calculate the factorial. Find another way to find the number of zeros. 19 | # Solution 20 | ``` 21 | def zeros(n): 22 | sum = 0 23 | while n>0: 24 | n//=5 25 | sum+=n 26 | return sum 27 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Paginating a huge book.md: -------------------------------------------------------------------------------- 1 | # Paginating a huge book 2 | Johnny is working as an intern for a publishing company, and was tasked with cleaning up old code. He is working on a program that determines how many digits are in all of the page numbers in a book with pages from 1 to n (inclusive). 3 | 4 | For example, a book with 4 pages has 4 digits (one for each page) while a 12-page book has 15 (9 for 1-9, plus 2 each for 10, 11, 12). 5 | 6 | Johnny's boss, looking to futureproof, demanded that the new program be able to handle books up to 400,000,000,000,000,000 pages. 7 | # Solution 8 | ``` 9 | def page_digits(pages): 10 | if pages<10: 11 | return pages 12 | tmp,rank=pages,1 13 | while tmp>9: 14 | tmp//=10 15 | rank*=10 16 | return len(str(pages))*(pages+1-rank)+page_digits(rank-1) 17 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Rotate an array matrix.md: -------------------------------------------------------------------------------- 1 | # Rotate an array matrix 2 | Write a rotate function that rotates a two-dimensional array (a matrix) either clockwise or anti-clockwise by 90 degrees, and returns the rotated array. 3 | 4 | The function accepts two parameters: an array, and a string specifying the direction or rotation. The direction will be either `"clockwise"` or `"counter-clockwise"`. 5 | 6 | Here is an example of how your function will be used: 7 | ``` 8 | matrix = [[1, 2, 3], 9 | [4, 5, 6], 10 | [7, 8, 9]] 11 | 12 | rotate(matrix, "clockwise") # Would return [[7, 4, 1], [8, 5, 2], [9, 6, 3]] 13 | ``` 14 | To help you visualize the rotated matrix, here it is formatted as a grid: 15 | ``` 16 | [[7, 4, 1], 17 | [8, 5, 2], 18 | [9, 6, 3]] 19 | ``` 20 | Rotated counter-clockwise it would looks like this: 21 | ``` 22 | [[3, 6, 9], 23 | [2, 5, 8], 24 | [1, 4, 7]] 25 | ``` 26 | # Solution 1 27 | ``` 28 | def rotate(matrix, direction): 29 | def clockwise_rotate(matrix): 30 | for i in range(3): 31 | matrix = counter_clockwise_rotate(matrix) 32 | return matrix 33 | def counter_clockwise_rotate(matrix): 34 | matrix_tmp = [[0]*len(matrix) for _ in range(len(matrix[0]))] 35 | for i in range(len(matrix)): 36 | for j in range(len(matrix[i])): 37 | matrix_tmp[j][i] = matrix[i][j] 38 | return matrix_tmp[::-1] 39 | if direction == 'clockwise': 40 | return clockwise_rotate(matrix) 41 | elif direction == 'counter-clockwise': 42 | return counter_clockwise_rotate(matrix) 43 | 44 | ``` 45 | # Solution 2 46 | ``` 47 | import numpy as np 48 | d = {"clockwise":3, "counter-clockwise":1} 49 | 50 | def rotate(matrix, direction): 51 | return np.rot90(matrix, d[direction]).tolist() 52 | 53 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Simple Pig Latin.md: -------------------------------------------------------------------------------- 1 | # Simple Pig Latin 2 | Move the first letter of each word to the end of it, then add "ay" to the end of the word. Leave punctuation marks untouched. 3 | 4 | # Examples 5 | ``` 6 | pig_it('Pig latin is cool') # igPay atinlay siay oolcay 7 | pig_it('Hello world !') # elloHay orldway ! 8 | ``` 9 | # Solution 10 | ``` 11 | def pig_it(text): 12 | a,res = text.split(),[] 13 | for i in a: 14 | if i.isalpha(): 15 | b = i[1:]+i[0]+'ay' 16 | res.append(b) 17 | else: 18 | res.append(i) 19 | return ' '.join(res) 20 | ``` -------------------------------------------------------------------------------- /Python/5 kyu. Write out numbers.md: -------------------------------------------------------------------------------- 1 | # Write out numbers 2 | Create a function that transforms any positive number to a string representing the number in words. The function should work for all numbers between 0 and 999999. 3 | 4 | # Examples 5 | ``` 6 | number2words(0) ==> "zero" 7 | number2words(1) ==> "one" 8 | number2words(9) ==> "nine" 9 | number2words(10) ==> "ten" 10 | number2words(17) ==> "seventeen" 11 | number2words(20) ==> "twenty" 12 | number2words(21) ==> "twenty-one" 13 | number2words(45) ==> "forty-five" 14 | number2words(80) ==> "eighty" 15 | number2words(99) ==> "ninety-nine" 16 | number2words(100) ==> "one hundred" 17 | number2words(301) ==> "three hundred one" 18 | number2words(799) ==> "seven hundred ninety-nine" 19 | number2words(800) ==> "eight hundred" 20 | number2words(950) ==> "nine hundred fifty" 21 | number2words(1000) ==> "one thousand" 22 | number2words(1002) ==> "one thousand two" 23 | number2words(3051) ==> "three thousand fifty-one" 24 | number2words(7200) ==> "seven thousand two hundred" 25 | number2words(7219) ==> "seven thousand two hundred nineteen" 26 | number2words(8330) ==> "eight thousand three hundred thirty" 27 | number2words(99999) ==> "ninety-nine thousand nine hundred ninety-nine" 28 | number2words(888888) ==> "eight hundred eighty-eight thousand eight hundred eighty-eight" 29 | ``` 30 | # Solution 31 | ``` 32 | def number2words(n): 33 | d = { 0 : 'zero', 1 : 'one', 2 : 'two', 3 : 'three', 4 : 'four', 5 : 'five', 34 | 6 : 'six', 7 : 'seven', 8 : 'eight', 9 : 'nine', 10 : 'ten', 35 | 11 : 'eleven', 12 : 'twelve', 13 : 'thirteen', 14 : 'fourteen', 36 | 15 : 'fifteen', 16 : 'sixteen', 17 : 'seventeen', 18 : 'eighteen', 37 | 19 : 'nineteen', 20 : 'twenty', 38 | 30 : 'thirty', 40 : 'forty', 50 : 'fifty', 60 : 'sixty', 39 | 70 : 'seventy', 80 : 'eighty', 90 : 'ninety' } 40 | k = 1000 41 | m = k * 1000 42 | b = m * 1000 43 | 44 | if (n < 20): 45 | return d[n] 46 | 47 | if (n < 100): 48 | if n % 10 == 0: return d[n] 49 | else: return d[n // 10 * 10] + '-' + d[n % 10] 50 | 51 | if (n < k): 52 | if n % 100 == 0: return d[n // 100] + ' hundred' 53 | else: return d[n // 100] + ' hundred ' + number2words(n % 100) 54 | 55 | if (n < m): 56 | if n % k == 0: return number2words(n // k) + ' thousand' 57 | else: return number2words(n // k) + ' thousand ' + number2words(n % k) 58 | 59 | if (n < b): 60 | if (n % m) == 0: return number2words(n // m) + ' million' 61 | else: return number2words(n // m) + ' million ' + number2words(n % m) 62 | ``` 63 | -------------------------------------------------------------------------------- /Python/5 kyu. int32 to IPv4.md: -------------------------------------------------------------------------------- 1 | # int32 to IPv4 2 | Take the following IPv4 address: 128.32.10.1 3 | 4 | This address has 4 octets where each octet is a single byte (or 8 bits). 5 | 6 | 1st octet 128 has the binary representation: 10000000 7 | 2nd octet 32 has the binary representation: 00100000 8 | 3rd octet 10 has the binary representation: 00001010 9 | 4th octet 1 has the binary representation: 00000001 10 | So 128.32.10.1 == 10000000.00100000.00001010.00000001 11 | 12 | Because the above IP address has 32 bits, we can represent it as the unsigned 32 bit number: 2149583361 13 | 14 | Complete the function that takes an unsigned 32 bit number and returns a string representation of its IPv4 address. 15 | 16 | # Examples 17 | ``` 18 | 2149583361 ==> "128.32.10.1" 19 | 32 ==> "0.0.0.32" 20 | 0 ==> "0.0.0.0" 21 | ``` 22 | # Solution 1 23 | ``` 24 | def int32_to_ip(int32): 25 | res = bin(int32) 26 | res = res[2:] 27 | if res == "0": 28 | return "0.0.0.0" 29 | else: 30 | n = len(res)-24 31 | return str(int(res[:n],2))+"."+str(int(res[n:n+8],2))+"."+str(int(res[n+8:n+16],2))+"."+str(int(res[n+16:],2)) 32 | 33 | ``` 34 | # Solution 2 35 | ``` 36 | from ipaddress import IPv4Address 37 | def int32_to_ip(int32): 38 | return str(IPv4Address(int32)) 39 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. +1 Array.md: -------------------------------------------------------------------------------- 1 | # +1 Array 2 | Given an array of integers of any length, return an array that has 1 added to the value represented by the array. 3 | * the array can't be empty 4 | * only non-negative, single digit integers are allowed 5 | Return `nil` (or your language's equivalent) for invalid inputs. 6 | 7 | ## Examples 8 | For example the array `[2, 3, 9]` equals `239`, adding one would return the array `[2, 4, 0]`. 9 | 10 | `[4, 3, 2, 5]` would return `[4, 3, 2, 6]` 11 | 12 | # Solution 1 13 | ``` 14 | def up_array(arr): 15 | try: 16 | for num in arr: 17 | if len(str(num)) > 1: 18 | return None 19 | return list(map(int,str((int(''.join(map(str, arr))) + 1)))) 20 | except: 21 | return None 22 | ``` 23 | # Solution 2 24 | ``` 25 | def up_array(arr): 26 | return None if not arr or min(arr) < 0 or max(arr) > 9 else list(map(int,str((int(''.join(map(str, arr))) + 1)))) 27 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. A Rule of Divisibility by 13.md: -------------------------------------------------------------------------------- 1 | # A Rule of Divisibility by 13 2 | From Wikipedia: 3 | 4 | "A divisibility rule is a shorthand way of determining whether a given integer is divisible by a fixed divisor without performing the division, usually by examining its digits." 5 | 6 | When you divide the successive powers of `10` by `13` you get the following remainders of the integer divisions: 7 | 8 | `1, 10, 9, 12, 3, 4` because: 9 | 10 | ``` 11 | 10 ^ 0 -> 1 (mod 13) 12 | 10 ^ 1 -> 10 (mod 13) 13 | 10 ^ 2 -> 9 (mod 13) 14 | 10 ^ 3 -> 12 (mod 13) 15 | 10 ^ 4 -> 3 (mod 13) 16 | 10 ^ 5 -> 4 (mod 13) 17 | ``` 18 | 19 | Then the whole pattern repeats. Hence the following method: 20 | 21 | Multiply 22 | 23 | * the right most digit of the number with the left most number in the sequence shown above, 24 | * the second right most digit with the second left most digit of the number in the sequence. 25 | The cycle goes on and you sum all these products. Repeat this process until the sequence of sums is stationary. 26 | 27 | ## Example: 28 | What is the remainder when `1234567` is divided by `13`? 29 | ``` 30 | 7 6 5 4 3 2 1 (digits of 1234567 from the right) 31 | × × × × × × × (multiplication) 32 | 1 10 9 12 3 4 1 (the repeating sequence) 33 | ``` 34 | 35 | Therefore following the method we get: 36 | ``` 37 | 7×1 + 6×10 + 5×9 + 4×12 + 3×3 + 2×4 + 1×1 = 178 38 | ``` 39 | We repeat the process with the number 178: 40 | ``` 41 | 8x1 + 7x10 + 1x9 = 87 42 | ``` 43 | and again with 87: 44 | ``` 45 | 7x1 + 8x10 = 87 46 | ``` 47 | From now on the sequence is stationary (we always get `87`) and the remainder of `1234567` by `13` is the same as the remainder of `87` by `13` ( i.e `9`). 48 | 49 | ## Task: 50 | Call `thirt` the function which processes this sequence of operations on an integer `n (>=0)`. `thirt` will return the stationary number. 51 | 52 | `thirt(1234567) calculates `178`, then `87`, then 87 and returns `87`. 53 | 54 | `thirt(321)` calculates 48, 48 and returns `48` 55 | 56 | # Solution 57 | ``` 58 | def thirt(n): 59 | m = [int(i) for i in list(str(n)[::-1])] 60 | mod = [1, 10, 9, 12, 3, 4] * (len(m) // 6 + min(1, len(m) % 6)) 61 | res = sum(a*b for a, b in zip(m, mod)) 62 | if n != res: 63 | return thirt(res) 64 | return res 65 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Are they the same.md: -------------------------------------------------------------------------------- 1 | # Are they the same 2 | Given two arrays a and b write a function comp(a, b) (orcompSame(a, b)) that checks whether the two arrays have the "same" elements, with the same multiplicities. "Same" means, here, that the elements in b are the elements in a squared, regardless of the order. 3 | 4 | # Examples 5 | Valid arrays 6 | ``` 7 | a = [121, 144, 19, 161, 19, 144, 19, 11] 8 | b = [121, 14641, 20736, 361, 25921, 361, 20736, 361] 9 | ``` 10 | comp(a, b) returns true because in b 121 is the square of 11, 14641 is the square of 121, 20736 the square of 144, 361 the square of 19, 25921 the square of 161, and so on. It gets obvious if we write b's elements in terms of squares: 11 | ``` 12 | a = [121, 144, 19, 161, 19, 144, 19, 11] 13 | b = [11*11, 121*121, 144*144, 19*19, 161*161, 19*19, 144*144, 19*19] 14 | ``` 15 | Invalid arrays 16 | If, for example, we change the first number to something else, comp may not return true anymore: 17 | ``` 18 | a = [121, 144, 19, 161, 19, 144, 19, 11] 19 | b = [132, 14641, 20736, 361, 25921, 361, 20736, 361] 20 | ``` 21 | comp(a,b) returns false because in b 132 is not the square of any number of a. 22 | ``` 23 | a = [121, 144, 19, 161, 19, 144, 19, 11] 24 | b = [121, 14641, 20736, 36100, 25921, 361, 20736, 361] 25 | ``` 26 | comp(a,b) returns false because in b 36100 is not the square of any number of a. 27 | 28 | **Remarks** 29 | a or b might be [] or {} (all languages except R, Shell). 30 | a or b might be nil or null or None or nothing (except in C++, Elixir, Haskell, PureScript, Pascal, Perl, R, Rust, Shell). 31 | If a or b are nil (or null or None, depending on the language), the problem doesn't make sense so return false. 32 | 33 | **Note for C** 34 | The two arrays have the same size (> 0) given as parameter in function comp. 35 | # Solution 36 | ``` 37 | from math import sqrt 38 | def comp(array1, array2): 39 | if array1 is None or array2 is None: 40 | return False 41 | for i in range(len(array1)): 42 | if array1[i]<0: 43 | array1[i]*=-1 44 | for i in array2: 45 | if array2.count(i) != array1.count(sqrt(i)): 46 | return False 47 | return True 48 | ``` 49 | -------------------------------------------------------------------------------- /Python/6 kyu. Array.diff.md: -------------------------------------------------------------------------------- 1 | # Array.diff 2 | Your goal in this kata is to implement a difference function, which subtracts one list from another and returns the result. 3 | 4 | It should remove all values from list a, which are present in list b keeping their order. 5 | ``` 6 | array_diff([1,2],[1]) == [2] 7 | ``` 8 | If a value is present in b, all of its occurrences must be removed from the other: 9 | ``` 10 | array_diff([1,2,2,2,3],[2]) == [1,3] 11 | ``` 12 | # Solution 13 | ``` 14 | def array_diff(a, b): 15 | for i in b: 16 | if i in a: 17 | while i in a: 18 | a.remove(i) 19 | return a 20 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Ascend, Descend, Repeat.md: -------------------------------------------------------------------------------- 1 | # Ascend, Descend, Repeat 2 | You are given three integer inputs: length, minimum, and maximum. 3 | 4 | ## Return a string that: 5 | 6 | 1. Starts at minimum 7 | 2. Ascends one at a time until reaching the maximum, then 8 | 3. Descends one at a time until reaching the minimum 9 | 4. repeat until the string is the appropriate length 10 | ## Examples: 11 | ``` 12 | length: 5, minimum: 1, maximum: 3 ==> "12321" 13 | length: 14, minimum: 0, maximum: 2 ==> "01210121012101" 14 | length: 11, minimum: 5, maximum: 9 ==> "56789876567" 15 | ``` 16 | ## Notes: 17 | 18 | * length will always be non-negative 19 | * negative numbers can appear for minimum and maximum values 20 | * hyphens/dashes ("-") for negative numbers do count towards the length 21 | * the resulting string must be truncated to the exact length provided 22 | * return an empty string if maximum < minimum or length == 0 23 | * minimum and maximum can equal one another and result in a single number repeated for the length of the string 24 | 25 | # Solution 26 | ``` 27 | def ascend_descend(length, minimum, maximum): 28 | try: 29 | s = [str(i) for i in range(minimum, maximum+1)] 30 | res = ''.join(s) + ''.join(s[1:-1][::-1]) 31 | return (res * (length // len(res) + 1))[:length] if length // len(res) > 0 else res[:length] 32 | except: 33 | return '' 34 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Build Tower.md: -------------------------------------------------------------------------------- 1 | # Build Tower 2 | Build Tower by the following given argument: 3 | number of floors (integer and always greater than 0). 4 | 5 | Tower block is represented as * 6 | Python: return a list; 7 | 8 | for example, a tower of 3 floors looks like below 9 | ``` 10 | [ 11 | ' * ', 12 | ' *** ', 13 | '*****' 14 | ] 15 | ``` 16 | and a tower of 6 floors looks like below 17 | ``` 18 | [ 19 | ' * ', 20 | ' *** ', 21 | ' ***** ', 22 | ' ******* ', 23 | ' ********* ', 24 | '***********' 25 | ] 26 | ``` 27 | 28 | # Solution 29 | ``` 30 | def tower_builder(n_floors): 31 | a = [] 32 | for i in range(n_floors): 33 | a.append(' ' * (n_floors - i-1) + '*' * (i + 1) + '*' * i+ ' ' * (n_floors-i-1)) 34 | return a 35 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Consecutive strings.md: -------------------------------------------------------------------------------- 1 | # Consecutive strings 2 | 3 | You are given an array(list) strarr of strings and an integer k. Your task is to return the first longest string consisting of k consecutive strings taken in the array. 4 | 5 | ## Examples: 6 | ``` 7 | strarr = ["tree", "foling", "trashy", "blue", "abcdef", "uvwxyz"], k = 2 8 | 9 | Concatenate the consecutive strings of strarr by 2, we get: 10 | 11 | treefoling (length 10) concatenation of strarr[0] and strarr[1] 12 | folingtrashy (" 12) concatenation of strarr[1] and strarr[2] 13 | trashyblue (" 10) concatenation of strarr[2] and strarr[3] 14 | blueabcdef (" 10) concatenation of strarr[3] and strarr[4] 15 | abcdefuvwxyz (" 12) concatenation of strarr[4] and strarr[5] 16 | 17 | Two strings are the longest: "folingtrashy" and "abcdefuvwxyz". 18 | The first that came is "folingtrashy" so 19 | longest_consec(strarr, 2) should return "folingtrashy". 20 | 21 | In the same way: 22 | longest_consec(["zone", "abigail", "theta", "form", "libe", "zas", "theta", "abigail"], 2) --> "abigailtheta" 23 | ``` 24 | n being the length of the string array, if `n = 0` or `k > n` or `k <= 0` return "" (return Nothing in Elm, "nothing" in Erlang). 25 | 26 | ## Note 27 | consecutive strings : follow one after another without an interruption 28 | 29 | # Solution ` 30 | ``` 31 | def longest_consec(strarr, k): 32 | res = '' 33 | if len(strarr) > k and k >= 0: 34 | for i in range(len(strarr)-k+1): 35 | word = ''.join(strarr[i:i+k]) 36 | res = max([res, word], key=len) 37 | return res 38 | ``` 39 | 40 | # Solution 2 41 | ``` 42 | def longest_consec(strarr, k): 43 | res = '' 44 | if len(strarr) >= k and k >= 0: 45 | for i in range(len(strarr) - k + 1): 46 | res = max([res, ''.join(strarr[i:i+k])], key=len) 47 | return res 48 | ``` 49 | 50 | # Solution 3 51 | ``` 52 | def longest_consec(strarr, k): 53 | return max([''.join(strarr[i:i+k]) for i in range(len(strarr) - k + 1)], key=len) if len(strarr) >= k and k >= 0 else '' 54 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Count words.md: -------------------------------------------------------------------------------- 1 | # Count words 2 | Kate likes to count words in text blocks. By words she means continuous sequences of English alphabetic characters (from a to z ). Here are examples: 3 | ``` 4 | Hello there, little user5453 374 ())$. I’d been using my sphere as a stool. Slow-moving target 839342 was hit by OMGd-63 or K4mp. contains "words" ['Hello', 'there', 'little', 'user', 'I', 'd', 'been', 'using', 'my','sphere', 'as', 'a', 'stool', 'Slow', 'moving', 'target', 'was', 'hit', 'by', 'OMGd', 'or', 'K', 'mp'] 5 | ``` 6 | Kate doesn't like some of words and doesn't count them. Words to be excluded are "a", "the", "on", "at", "of", "upon", "in" and "as", case-insensitive. 7 | 8 | Today Kate's too lazy and have decided to teach her computer to count "words" for her. 9 | 10 | # Example Input 1 11 | ``` 12 | Hello there, little user5453 374 ())$. 13 | ``` 14 | # Example Output 1 15 | ``` 16 | 4 17 | ``` 18 | # Example Input 2 19 | ``` 20 | I’d been using my sphere as a stool. I traced counterclockwise circles on it with my fingertips and it shrank until I could palm it. My bolt had shifted while I’d been sitting. I pulled it up and yanked the pleats straight as I careered around tables, chairs, globes, and slow-moving fraas. I passed under a stone arch into the Scriptorium. The place smelled richly of ink. Maybe it was because an ancient fraa and his two fids were copying out books there. But I wondered how long it would take to stop smelling that way if no one ever used it at all; a lot of ink had been spent there, and the wet smell of it must be deep into everything. 21 | ``` 22 | # Example Output 2 23 | ``` 24 | 112 25 | ``` 26 | # Solution 27 | ``` 28 | import re 29 | def word_count(s): 30 | exec,count = ["a", "the", "on", "at", "of", "upon", "in", "as"],0 31 | s = re.sub(r'[^A-Za-z]', ' ', s) 32 | s = s.split() 33 | for i in s: 34 | if i.lower() not in exec: 35 | count+=1 36 | return count 37 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Create Phone Number.md: -------------------------------------------------------------------------------- 1 | # Create Phone Number 2 | 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. 3 | # Example 4 | ``` 5 | create_phone_number([1, 2, 3, 4, 5, 6, 7, 8, 9, 0]) # => returns "(123) 456-7890" 6 | ``` 7 | The returned format must be correct in order to complete this challenge. 8 | Don't forget the space after the closing parentheses! 9 | # Solution 1 10 | ``` 11 | def create_phone_number(n): 12 | a = ''.join(str(e) for e in n) 13 | return "("+a[:3]+") "+a[3:6] + "-" + a[6:] 14 | ``` 15 | # Solution 2 16 | ``` 17 | def create_phone_number(n): 18 | return "({}{}{}) {}{}{}-{}{}{}{}".format(*n) 19 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Delete occurrences of an element if it occurs more than n times.md: -------------------------------------------------------------------------------- 1 | # Delete occurrences of an element if it occurs more than n times 2 | ## Enough is enough! 3 | Alice and Bob were on a holiday. Both of them took many pictures of the places they've been, and now they want to show Charlie their entire collection. However, Charlie doesn't like these sessions, since the motif usually repeats. He isn't fond of seeing the Eiffel tower 40 times. 4 | He tells them that he will only sit for the session if they show the same motif at most N times. Luckily, Alice and Bob are able to encode the motif as a number. Can you help them to remove numbers such that their list contains each number only up to N times, without changing the order? 5 | 6 | ## Task 7 | Given a list and a number, create a new list that contains each number of `list` at most `N` times, without reordering. 8 | For example if the input number is `2`, and the input list is `[1,2,3,1,2,1,2,3]`, you take `[1,2,3,1,2]`, drop the next `[1,2]` since this would lead to `1` and `2` being in the result `3` times, and then take `3`, which leads to `[1,2,3,1,2,3]`. 9 | With list `[20,37,20,21]` and number `1`, the result would be `[20,37,21]`. 10 | 11 | # Solution 12 | ``` 13 | def delete_nth(order,max_e): 14 | res = [] 15 | for num in order: 16 | if res.count(num) < max_e: 17 | res.append(num) 18 | return res 19 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Does my number look big in this.md: -------------------------------------------------------------------------------- 1 | # Does my number look big in this? 2 | A Narcissistic Number is a positive number which is the sum of its own digits, each raised to the power of the number of digits in a given base. In this Kata, we will restrict ourselves to decimal (base 10). 3 | 4 | For example, take 153 (3 digits), which is narcisstic: 5 | ``` 6 | 1^3 + 5^3 + 3^3 = 1 + 125 + 27 = 153 7 | ``` 8 | and 1652 (4 digits), which isn't: 9 | ``` 10 | 1^4 + 6^4 + 5^4 + 2^4 = 1 + 1296 + 625 + 16 = 1938 11 | ``` 12 | The Challenge: 13 | 14 | Your code must return true or false (not 'true' and 'false') depending upon whether the given number is a Narcissistic number in base 10. This may be True and False in your language, e.g. PHP. 15 | 16 | Error checking for text strings or other invalid inputs is not required, only valid positive non-zero integers will be passed into the function. 17 | # Solution 18 | ``` 19 | import math 20 | def narcissistic( value ): 21 | if value<10: 22 | return True 23 | else: 24 | num,sum = str(value),0 25 | for i in num: 26 | sum += pow(int(i),len(num)) 27 | return True if sum==value else False 28 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Duplicate Encoder.md: -------------------------------------------------------------------------------- 1 | # Duplicate Encoder 2 | The goal of this exercise is to convert a string to a new string where each character in the new string is "(" if that character appears 3 | only once in the original string, or ")" if that character appears more than once in the original string. Ignore capitalization when determining 4 | if a character is a duplicate. 5 | 6 | # Examples 7 | ``` 8 | "din" => "(((" 9 | "recede" => "()()()" 10 | "Success" => ")())())" 11 | "(( @" => "))((" 12 | ``` 13 | # Notes 14 | Assertion messages may be unclear about what they display in some languages. If you read "...It Should encode XXX", the "XXX" is the expected result, not the input! 15 | # Solution 16 | ``` 17 | def duplicate_encode(word): 18 | a,word = "",word.lower() 19 | for i in word: 20 | if word.count(i)>1: 21 | a+=")" 22 | else: 23 | a+="(" 24 | return a 25 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Encrypt this!.md: -------------------------------------------------------------------------------- 1 | # Encrypt this! 2 | # Acknowledgments: 3 | I thank yvonne-liu for the idea and for the example tests :) 4 | 5 | # Description: 6 | Encrypt this! 7 | 8 | You want to create secret messages which can be deciphered by the Decipher this! kata. Here are the conditions: 9 | 10 | 1. Your message is a string containing space separated words. 11 | 2. You need to encrypt each word in the message using the following rules: 12 | *The first letter must be converted to its ASCII code. 13 | *The second letter must be switched with the last letter 14 | 3. Keepin' it simple: There are no special characters in the input. 15 | # Examples: 16 | ``` 17 | encrypt_this("Hello") == "72olle" 18 | encrypt_this("good") == "103doo" 19 | encrypt_this("hello world") == "104olle 119drlo" 20 | ``` 21 | # Solution 22 | ``` 23 | def encrypt_this(text): 24 | text, res = text.split(), '' 25 | for i in text: 26 | if len(i)==1: 27 | res += str(ord(i)) 28 | elif len(i)==2: 29 | res+=str(ord(i[0]))+i[1] 30 | else: 31 | res+=str(ord(i[0]))+i[-1]+i[2:-1]+i[1] 32 | res+=" " 33 | return res[:-1] 34 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Highest Rank Number in an Array.md: -------------------------------------------------------------------------------- 1 | # Highest Rank Number in an Array 2 | Complete the method which returns the number which is most frequent in the given input array. If there is a tie for most frequent number, return the largest number among them. 3 | 4 | Note: no empty arrays will be given. 5 | 6 | # Examples 7 | ``` 8 | [12, 10, 8, 12, 7, 6, 4, 10, 12] --> 12 9 | [12, 10, 8, 12, 7, 6, 4, 10, 12, 10] --> 12 10 | [12, 10, 8, 8, 3, 3, 3, 3, 2, 4, 10, 12, 10] --> 3 11 | ``` 12 | # Solution 1 13 | ``` 14 | def highest_rank(arr): 15 | res = max(arr, key=arr.count) 16 | cnt = arr.count(res) 17 | for i in arr: 18 | if arr.count(i) == cnt and i != res: 19 | if i > res: 20 | res = i 21 | return res 22 | ``` 23 | # Solution 2 24 | ``` 25 | def highest_rank(arr): 26 | return max(sorted(arr)[::-1], key=arr.count) 27 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. IP Validation.md: -------------------------------------------------------------------------------- 1 | # IP Validation 2 | Write an algorithm that will identify valid IPv4 addresses in dot-decimal format. IPs should be considered valid if they consist of four octets, with values between 0 and 255, inclusive. 3 | 4 | Valid inputs examples: 5 | 6 | ## Examples of valid inputs: 7 | ``` 8 | 1.2.3.4 9 | 123.45.67.89 10 | ``` 11 | ## Invalid input examples: 12 | ``` 13 | 1.2.3 14 | 1.2.3.4.5 15 | 123.456.78.90 16 | 123.045.067.089 17 | ``` 18 | ## Notes: 19 | * Leading zeros (e.g. `01.02.03.04`) are considered invalid 20 | * Inputs are guaranteed to be a single string 21 | 22 | 23 | # Solution 1 24 | ``` 25 | def is_valid_IP(strng): 26 | return strng.count('.') == 3 and all(octet.isdigit() and 0 <= int(octet) <= 255 and str(int(octet)) == octet for octet in strng.split('.')) 27 | ``` 28 | # Solution 2 29 | ``` 30 | def is_valid_IP(strng):) 31 | try: 32 | ip = strng.split('.') 33 | if len(ip) != 4: 34 | return False 35 | for octet in ip: 36 | if isinstance(int(octet), int): 37 | if len(octet) > 1 and octet[0] == '0' or int(octet) < 0 or int(octet) > 255 or ' ' in octet or '\n' in octet: 38 | return False 39 | return True 40 | except: 41 | return False 42 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. Sum of Digits Digital Root.md: -------------------------------------------------------------------------------- 1 | # Sum of Digits / Digital Root 2 | Digital root is the recursive sum of all the digits in a number. 3 | 4 | Given n, take the sum of the digits of n. If that value has more than one digit, continue reducing in this way until a single-digit number is produced. The input will be a non-negative integer. 5 | 6 | **Examples** 7 | ``` 8 | 16 --> 1 + 6 = 7 9 | 942 --> 9 + 4 + 2 = 15 --> 1 + 5 = 6 10 | 132189 --> 1 + 3 + 2 + 1 + 8 + 9 = 24 --> 2 + 4 = 6 11 | 493193 --> 4 + 9 + 3 + 1 + 9 + 3 = 29 --> 2 + 9 = 11 --> 1 + 1 = 2 12 | ``` 13 | # Solution 14 | ``` 15 | def digital_root(n): 16 | sum = 0 17 | while n!=0: 18 | sum += n%10 19 | n //= 10 20 | return sum if sum<=9 else digital_root(sum) 21 | ``` -------------------------------------------------------------------------------- /Python/6 kyu. World Bits War.md: -------------------------------------------------------------------------------- 1 | # World Bits War 2 | Variation of this nice kata, the war has expanded and become dirtier and meaner; both even and odd numbers will fight with their pointy 1s. And negative integers are coming into play as well, with, ça va sans dire, a negative contribution (think of them as spies or saboteurs). 3 | 4 | A number's strength is determined by the number of set bits (1s) in its binary representation. Negative integers work against their own side so their strength is negative. For example -5 = -101 has strength -2 and +5 = +101 has strength +2. 5 | 6 | The side with the largest cumulated strength wins. 7 | 8 | Again, three possible outcomes: odds win, evens win and tie. 9 | 10 | # Examples: 11 | ``` 12 | bits_war([1,5,12]) => "odds win" #1+101 vs 1100, 3 vs 2 13 | bits_war([7,-3,20]) => "evens win" #111-11 vs 10100, 3-2 vs 2 14 | bits_war([7,-3,-2,6]) => "tie" #111-11 vs -1+110, 3-2 vs -1+2 15 | ``` 16 | # Solution 1 17 | ``` 18 | def bits_war(numbers): 19 | sum_even, sum_odd = 0,0 20 | for i in numbers: 21 | if i<0: 22 | if i%2!=0: 23 | a = bin(i) 24 | a = a[3:] 25 | b = a.count("1") 26 | sum_odd -= b 27 | elif i%2==0: 28 | a = bin(i) 29 | a = a[3:] 30 | b = a.count("1") 31 | sum_even -= b 32 | else: 33 | if i%2!=0: 34 | a = bin(i) 35 | a = a[2:] 36 | b = a.count("1") 37 | sum_odd += b 38 | elif i%2==0: 39 | a = bin(i) 40 | a = a[2:] 41 | b = a.count("1") 42 | sum_even += b 43 | if sum_even>sum_odd: 44 | return "evens win" 45 | elif sum_odd>sum_even: 46 | return "odds win" 47 | else: 48 | return "tie" 49 | ``` 50 | # Solution 2 51 | ``` 52 | def bits_war(numbers): 53 | odd, even = 0, 0 54 | for number in numbers: 55 | if number % 2 == 0: 56 | if number > 0: 57 | even += bin(number).count('1') 58 | else: 59 | even -= bin(number).count('1') 60 | else: 61 | if number > 0: 62 | odd += bin(number).count('1') 63 | else: 64 | odd -= bin(number).count('1') 65 | return 'odds win' if odd > even else 'evens win' if even > odd else 'tie' 66 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Incrementer.md: -------------------------------------------------------------------------------- 1 | # Incrementer 2 | Given an input of an array of digits, return the array with each digit incremented by its position in the array: the first digit will be incremented by 1, the second digit by 2, etc. Make sure to start counting your positions from 1 (and not 0). 3 | 4 | Your result can only contain single digit numbers, so if adding a digit with it's position gives you a multiple-digit number, only the last digit of the number should be returned. 5 | 6 | Notes: 7 | 1. return an empty array if your array is empty 8 | 2. arrays will only contain numbers so don't worry about checking that 9 | Examples 10 | ``` 11 | [1, 2, 3] --> [2, 4, 6] # [1+1, 2+2, 3+3] 12 | 13 | [4, 6, 9, 1, 3] --> [5, 8, 2, 5, 8] # [4+1, 6+2, 9+3, 1+4, 3+5] 14 | # 9+3 = 12 --> 2 15 | ``` 16 | # Solution 17 | ``` 18 | def incrementer(nums): 19 | a, k = [], 1 20 | for i in nums: 21 | if i+k >=10: 22 | a.append((i+k)%10) 23 | else: 24 | a.append(i+k) 25 | k += 1 26 | return a 27 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Is this a triangle.md: -------------------------------------------------------------------------------- 1 | # Is this a triangle? 2 | Implement a function that accepts 3 integer values a, b, c. The function should return true if a triangle can be built with the sides of given length and false in any other case. 3 | 4 | (In this case, all triangles must have surface greater than 0 to be accepted). 5 | # Solution 6 | ``` 7 | def is_triangle(a, b, c): 8 | return (a+b>c) and (a+c>b) and (b+c>a) and (a>0) and (b>0) and (c>0) 9 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Single digit.md: -------------------------------------------------------------------------------- 1 | # Single digit 2 | The goal of this Kata is to reduce the passed integer to a single digit (if not already) by converting the number to binary, taking the sum of the binary digits, and if that sum is not a single digit then repeat the process. 3 | 4 | 1. If the passed integer is already a single digit there is no need to reduce 5 | 2. n will be an integer such that 0 < n < 10^20 6 | For example given 5665 the function should return 5: 7 | ``` 8 | 5665 --> (binary) 1011000100001 9 | 1011000100001 --> (sum of binary digits) 5 10 | ``` 11 | Given 123456789 the function should return 1: 12 | ``` 13 | 123456789 --> (binary) 111010110111100110100010101 14 | 111010110111100110100010101 --> (sum of binary digits) 16 15 | 16 --> (binary) 10000 16 | 10000 --> (sum of binary digits) 1 17 | ``` 18 | # Solution 19 | ``` 20 | def single_digit(n): 21 | if n<=9: 22 | return n 23 | a, sum = bin(n)[2:], 0 24 | for i in str(a): 25 | if i=="1": 26 | sum+=1 27 | return sum if sum<=9 else single_digit(sum) 28 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Sort rectangles and circles by area II.md: -------------------------------------------------------------------------------- 1 | # Sort rectangles and circles by area II 2 | 3 | In this kata you will be given a sequence of the dimensions of rectangles ( sequence with width and length ) and circles ( radius - just a number ). 4 | Your task is to return a new sequence of dimensions, sorted ascending by area. 5 | 6 | For example, 7 | ``` 8 | seq = [ (4.23, 6.43), 1.23, 3.444, (1.342, 3.212) ] # [ rectangle, circle, circle, rectangle ] 9 | sort_by_area(seq) => [ ( 1.342, 3.212 ), 1.23, ( 4.23, 6.43 ), 3.444 ] 10 | ``` 11 | # Solution 1 12 | ``` 13 | from math import pi 14 | 15 | def get_seq(dim): 16 | if isinstance(dim, tuple): 17 | return dim[0] * dim[1] 18 | else: 19 | return pi * dim ** 2 20 | 21 | def sort_by_area(seq): 22 | res = {} 23 | for i in seq: 24 | res[i] = get_seq(i) 25 | sorted_res = sorted(res, key=res.get) 26 | return sorted_res 27 | ``` 28 | # Solution 2 29 | ``` 30 | from math import pi 31 | def sort_by_area(seq): 32 | return sorted(seq, key=lambda x: x[0] * x[1] if isinstance(x, tuple) else x * x * pi) 33 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Sum of odd numbers.md: -------------------------------------------------------------------------------- 1 | # Sum of odd numbers 2 | **Description**: 3 | 4 | Given the triangle of consecutive odd numbers: 5 | ``` 6 | 1 7 | 3 5 8 | 7 9 11 9 | 13 15 17 19 10 | 21 23 25 27 29 11 | ... 12 | ``` 13 | # Solution 14 | ``` 15 | def row_sum_odd_numbers(n): 16 | return n*n*n 17 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Test Your Knowledge Of Function Scope.md: -------------------------------------------------------------------------------- 1 | # Test Your Knowledge Of Function Scope 2 | Write a function that adds from two invocations. 3 | 4 | All inputs will be integers. 5 | 6 | ``` 7 | add(3)(4) // 7 8 | add(12)(20) // 32 9 | ``` 10 | 11 | # Solution 12 | ``` 13 | def add(a): 14 | def addone(b): 15 | return a+b 16 | return addone 17 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. The Poet And The Pendulum .md: -------------------------------------------------------------------------------- 1 | # The Poet And The Pendulum 2 | 3 | ## Scenario 4 | the rhythm of beautiful musical notes is drawing a Pendulum 5 | 6 | Beautiful musical notes are the Numbers 7 | 8 | ## Task 9 | Given an array/list [] of n integers , Arrange them in a way similar to the to-and-fro movement of a Pendulum 10 | 11 | The Smallest element of the list of integers , must come in center position of array/list. 12 | 13 | The Higher than smallest , goes to the right . 14 | The Next higher number goes to the left of minimum number and So on , in a to-and-fro manner similar to that of a Pendulum. 15 | 16 | ## Notes 17 | * Array/list size is at least 3 . 18 | 19 | * In Even array size , The minimum element should be moved to (n-1)/2 index (considering that indexes start from 0) 20 | 21 | * Repetition of numbers in the array/list could occur , So (duplications are included when Arranging). 22 | 23 | ### Input >> Output Examples: 24 | #### 1 25 | ``` 26 | pendulum ([6, 6, 8 ,5 ,10]) ==> [10, 6, 5, 6, 8] 27 | ``` 28 | 29 | ### Explanation: 30 | * Since , 5 is the The Smallest element of the list of integers , came in The center position of array/list 31 | 32 | * The Higher than smallest is 6 goes to the right of 5 . 33 | 34 | * The Next higher number goes to the left of minimum number and So on . 35 | 36 | * Remember , Duplications are included when Arranging , Don't Delete Them . 37 | 38 | #### 2 39 | ``` 40 | pendulum ([-9, -2, -10, -6]) ==> [-6, -10, -9, -2] 41 | ``` 42 | 43 | ### Explanation: 44 | * Since , -10 is the The Smallest element of the list of integers , came in The center position of array/list 45 | 46 | * The Higher than smallest is -9 goes to the right of it . 47 | 48 | * The Next higher number goes to the left of -10 , and So on . 49 | 50 | * Remeber , In Even array size , The minimum element moved to (n-1)/2 index (considering that indexes start from 0) . 51 | 52 | 53 | #### 3 54 | ``` 55 | pendulum ([11, -16, -18, 13, -11, -12, 3, 18]) ==> [13, 3, -12, -18, -16, -11, 11, 18] 56 | ``` 57 | 58 | ### Explanation: 59 | * Since , -18 is the The Smallest element of the list of integers , came in The center position of array/list 60 | 61 | * The Higher than smallest is -16 goes to the right of it . 62 | 63 | * The Next higher number goes to the left of -18 , and So on . 64 | 65 | * Remember , In Even array size , The minimum element moved to (n-1)/2 index (considering that indexes start from 0) . 66 | 67 | # Solution 1 68 | ``` 69 | def pendulum(values): 70 | values = sorted(values) 71 | return values[::2][::-1] + values[1::2] 72 | ``` 73 | 74 | # Solution 2 75 | ``` 76 | def pendulum(values): 77 | res, left, right, minim = [], True, True, min(values) 78 | res.append(minim) 79 | values.remove(minim) 80 | values.sort() 81 | for value in values: 82 | if value < minim: 83 | if left: 84 | res.insert(0, value) 85 | left = False 86 | else: 87 | res.append(value) 88 | left = True 89 | else: 90 | if right: 91 | res.append(value) 92 | right = False 93 | else: 94 | res.insert(0, value) 95 | right = True 96 | return res 97 | ``` -------------------------------------------------------------------------------- /Python/7 kyu. Which string is worth more.md: -------------------------------------------------------------------------------- 1 | # Which string is worth more? 2 | You will be given two ASCII strings, a and b. Your task is write a function to determine which one of these strings is "worth" more, and return it. 3 | 4 | A string's worth is determined by the sum of its ASCII codepoint indexes. So, for example, the string HELLO has a value of 372: H is codepoint 72, E 69, L 76, and O is 79. The sum of these values is 372. 5 | 6 | In the event of a tie, you should return the first string, i.e. a. 7 | # Solution 8 | ``` 9 | def highest_value(a, b): 10 | sum1,sum2 = 0,0 11 | for i in a: 12 | sum1+=ord(i) 13 | for i in b: 14 | sum2+=ord(i) 15 | return a if sum1>=sum2 else b 16 | ``` -------------------------------------------------------------------------------- /Python/8 kyu. Even or Odd.md: -------------------------------------------------------------------------------- 1 | # Even or Odd 2 | Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers. 3 | # Solution 4 | ``` 5 | def even_or_odd(number): 6 | return "Even" if (number%2==0) else "Odd" 7 | ``` -------------------------------------------------------------------------------- /Shell/8 kyu. Century From Year.md: -------------------------------------------------------------------------------- 1 | # Century From Year 2 | The first century spans from the year 1 up to and including the year 100, the second century - from the year 101 up to and including the year 200, etc. 3 | 4 | # Task 5 | Given a year, return the century it is in. 6 | 7 | # Examples 8 | ``` 9 | 1705 --> 18 10 | 1900 --> 19 11 | 1601 --> 17 12 | 2000 --> 20 13 | ``` 14 | # Solution 15 | ``` 16 | #!/bin/bash 17 | year=$1 18 | res=$(((year-1)/100+1)) 19 | echo "$res" 20 | ``` -------------------------------------------------------------------------------- /Shell/8 kyu. Expressions Matter.md: -------------------------------------------------------------------------------- 1 | # Expressions Matter 2 | # Task 3 | * Given three integers a ,b ,c, return the largest number obtained after inserting the following operators and brackets: +, *, () 4 | * In other words , try every combination of a,b,c with [*+()] , and return the Maximum Obtained 5 | # Consider an Example : 6 | With the numbers are 1, 2 and 3 , here are some ways of placing signs and brackets: 7 | ``` 8 | 1 * (2 + 3) = 5 9 | 1 * 2 * 3 = 6 10 | 1 + 2 * 3 = 7 11 | (1 + 2) * 3 = 9 12 | ``` 13 | So the maximum value that you can obtain is 9. 14 | 15 | # Notes 16 | * The numbers are always positive. 17 | * The numbers are in the range (1  ≤  a, b, c  ≤  10). 18 | * You can use the same operation more than once. 19 | * It's not necessary to place all the signs and brackets. 20 | * Repetition in numbers may occur . 21 | * You cannot swap the operands. For instance, in the given example you cannot get expression (1 + 3) * 2 = 8. 22 | # Input >> Output Examples: 23 | ``` 24 | expressionsMatter(1,2,3) ==> return 9 25 | ``` 26 | # Explanation: 27 | After placing signs and brackets, the Maximum value obtained from the expression (1+2) * 3 = 9. 28 | ``` 29 | expressionsMatter(1,1,1) ==> return 3 30 | ``` 31 | # Explanation: 32 | After placing signs, the Maximum value obtained from the expression is 1 + 1 + 1 = 3. 33 | ``` 34 | expressionsMatter(9,1,1) ==> return 18 35 | ``` 36 | # Explanation: 37 | After placing signs and brackets, the Maximum value obtained from the expression is 9 * (1+1) = 18. 38 | # Solution 39 | ``` 40 | a=$1 41 | b=$2 42 | c=$3 43 | declare -A results 44 | results[0]=$(( a * ( b + c ))) 45 | results[1]=$(( a * b * c )) 46 | results[2]=$(( a * b + c )) 47 | results[3]=$(( a + b * c )) 48 | results[4]=$((( a + b ) * c )) 49 | results[5]=$(( a + b + c )) 50 | sorted=($(tr ' ' '\n' <<<"${results[@]}" | sort -n)) 51 | echo "${sorted[-1]}" 52 | ``` -------------------------------------------------------------------------------- /Shell/8 kyu. Remove First and Last Character.md: -------------------------------------------------------------------------------- 1 | # Remove First and Last Character 2 | It's pretty straightforward. Your goal is to create a function that removes the first and last characters of a string. You're given one parameter, the original string. You don't have to worry with strings with less than two characters. 3 | # Solution 4 | ``` 5 | function removeChar() { 6 | echo "${1:1}" | perl -ple 'chop' 7 | } 8 | removeChar $1 9 | ``` -------------------------------------------------------------------------------- /Shell/8 kyu. Third Angle of a Triangle.md: -------------------------------------------------------------------------------- 1 | # Third Angle of a Triangle 2 | You are given two interior angles (in degrees) of a triangle. 3 | Write a function to return the 3rd. 4 | Note: only positive integers will be tested. 5 | https://en.wikipedia.org/wiki/Triangle 6 | # Solution 1 7 | ``` 8 | a=$1 9 | b=$2 10 | let res=180-$a-$b 11 | echo "$res" 12 | ``` 13 | # Solution 2 14 | ``` 15 | a=$1 16 | b=$2 17 | echo $((180-a-b)) 18 | ``` --------------------------------------------------------------------------------