diff --git a/codewars/Adding Big Numbers/AddingBigNumbers.txt b/codewars/Adding Big Numbers/AddingBigNumbers.txt new file mode 100644 index 0000000..7beca64 --- /dev/null +++ b/codewars/Adding Big Numbers/AddingBigNumbers.txt @@ -0,0 +1,54 @@ +#include +#include +#include + +int ssize(const std::string& s) +{ + return static_cast(s.size()); +} + +std::string sum(const std::string& a, const std::string& b) +{ + std::string result; + + int offset{ssize(a) - ssize(b)}; + int rememberedDigit{0}; + char sum{}; + + for (int i{ ssize(a)-1 }; i >= 0; --i) + { + if (i - offset >= 0) + sum = a[i] + b[i-offset] - 48*2; + else + sum = a[i] - 48; + + if (rememberedDigit > 0) + sum += rememberedDigit; + + if (sum > 9) + { + rememberedDigit = sum / 10; + sum %= 10; + } + else + { + rememberedDigit = 0; + } + + result += sum+48; + } + + if (rememberedDigit > 0) + result += rememberedDigit+48; + + std::reverse(result.begin(), result.end()); + + return result; +} + +std::string add(const std::string& a, const std::string& b) { + if (a.size() > b.size()) + return sum(a, b); + else + return sum(b, a); +} \ No newline at end of file diff --git a/codewars/Anagram difference/Difference.txt b/codewars/Anagram difference/Difference.txt new file mode 100644 index 0000000..b767236 --- /dev/null +++ b/codewars/Anagram difference/Difference.txt @@ -0,0 +1,28 @@ +def countLettersInWord(setOfLetters, word): + lettersCount = {} + + for letter in word: + if letter not in lettersCount.keys(): + lettersCount[letter] = 1 + setOfLetters.add(letter) + else: + lettersCount[letter] += 1 + + return lettersCount + + +def anagram_difference(w1, w2): + setOfLetters = set() + + lettersCountW1 = countLettersInWord(setOfLetters, w1) + lettersCountW2 = countLettersInWord(setOfLetters, w2) + + lettersToRemove = 0 + + for elem in setOfLetters: + if (elem in lettersCountW1) and (elem in lettersCountW2): + lettersToRemove += abs(lettersCountW1[elem] - lettersCountW2[elem]) + else: + lettersToRemove += lettersCountW1[elem] if elem in lettersCountW1 else lettersCountW2[elem] + + return lettersToRemove \ No newline at end of file diff --git a/codewars/Array Deep Count/DeepCount.txt b/codewars/Array Deep Count/DeepCount.txt new file mode 100644 index 0000000..9503287 --- /dev/null +++ b/codewars/Array Deep Count/DeepCount.txt @@ -0,0 +1,8 @@ +def deep_count(a): + count = 0 + for elem in a: + count += 1 + if type(elem) is list: + count += deep_count(elem) + return count + \ No newline at end of file diff --git a/codewars/Build Tower/BuildTower.txt b/codewars/Build Tower/BuildTower.txt new file mode 100644 index 0000000..a8b02ab --- /dev/null +++ b/codewars/Build Tower/BuildTower.txt @@ -0,0 +1,41 @@ +#include +#include + +constexpr unsigned int arithmeticProgressionDiff{ 2 }; + +unsigned int arithmeticProgression(unsigned int startValue, unsigned int memberNumber) +{ + return startValue + (memberNumber - 1) * arithmeticProgressionDiff; +} + +std::string generateFloor(const unsigned int lengthOfEachLine, + unsigned int firstSymbolPosition, unsigned int amountOfSymbols) +{ + std::string floor(lengthOfEachLine,' '); + + for (size_t i{ firstSymbolPosition }; amountOfSymbols > 0; ++i, --amountOfSymbols) + { + floor[i] = '*'; + } + + return floor; +} + +std::vector towerBuilder(unsigned nFloors) +{ + const unsigned int symbolsOnLastFloor{ arithmeticProgression(1, nFloors) }; + unsigned int firstSymbolPosition{ symbolsOnLastFloor / 2 }; + + std::vector tower(nFloors); + + for (size_t i{0}; i < nFloors; ++i) + { + std::string floor{ generateFloor(symbolsOnLastFloor, firstSymbolPosition, arithmeticProgression(1, i+1)) }; + + --firstSymbolPosition; + + tower[i] = floor; + } + + return tower; +} \ No newline at end of file diff --git a/codewars/Convert string to camel case/ConvertStringToCamelCase.txt b/codewars/Convert string to camel case/ConvertStringToCamelCase.txt new file mode 100644 index 0000000..d2d1a1a --- /dev/null +++ b/codewars/Convert string to camel case/ConvertStringToCamelCase.txt @@ -0,0 +1,29 @@ +#include + +char to_upper(char c) +{ + if (c >= 'a' && c <= 'z') + c -= 32; + + return c; +} + +std::string to_camel_case(std::string text) { + + std::string result{}; + + for (size_t i{0}; i < text.size(); i++) + { + switch (text[i]) + { + case '_': + case '-': + result += to_upper(text[++i]); + break; + default: + result += text[i]; + } + } + + return result; +} \ No newline at end of file diff --git a/codewars/Duplicate Encoder/DuplicateEncoder.txt b/codewars/Duplicate Encoder/DuplicateEncoder.txt new file mode 100644 index 0000000..3d49b43 --- /dev/null +++ b/codewars/Duplicate Encoder/DuplicateEncoder.txt @@ -0,0 +1,32 @@ +#include +#include +#include + +char to_lower(char c) +{ + if (c >= 'A' && c <= 'Z') + c += 32; + + return c; +} + +std::string duplicate_encoder(const std::string& word){ + std::map letterCount{}; + + for (char c: word) + ++letterCount[to_lower(c)]; + + std::string output{}; + + for (char c: word) + { + int count{letterCount[to_lower(c)]}; + + if (count > 1) + output += ")"; + else + output += "("; + } + + return output; +} \ No newline at end of file diff --git a/codewars/Find the missing letter/FindTheMissingLetter.txt b/codewars/Find the missing letter/FindTheMissingLetter.txt new file mode 100644 index 0000000..bd98b38 --- /dev/null +++ b/codewars/Find the missing letter/FindTheMissingLetter.txt @@ -0,0 +1,15 @@ +#include + +char findMissingLetter(const std::vector& chars) +{ + // TODO: Find the missing char in the consecutive letter sequence and return it. + + char lastChar{ chars[0] }; + for (char c: chars) + { + if (c - lastChar > 1) + return ++lastChar; + + lastChar = c; + } +} \ No newline at end of file diff --git a/codewars/Flatten a Nested Map/FlattenANestedMap.txt b/codewars/Flatten a Nested Map/FlattenANestedMap.txt new file mode 100644 index 0000000..7c0af51 --- /dev/null +++ b/codewars/Flatten a Nested Map/FlattenANestedMap.txt @@ -0,0 +1,28 @@ +function flattenMap(map) { + const res = {}; + + for (let i in map) { + if ( + typeof map[i] === "object" && + Object.keys(i).length > 0 && + !Array.isArray(map[i]) + ) { + const arr = flattenMap(map[i]); + + if (Object.keys(arr).length === 0) { + console.log(i, arr); + + res[i] = null; + } else { + for (let j in arr) { + res[i + "/" + j] = arr[j]; + } + } + + } else { + res[i] = map[i]; + } + } + + return res; +} \ No newline at end of file diff --git a/codewars/Fun with tree - max sum/FunWithTrees.txt b/codewars/Fun with tree - max sum/FunWithTrees.txt new file mode 100644 index 0000000..5f1c088 --- /dev/null +++ b/codewars/Fun with tree - max sum/FunWithTrees.txt @@ -0,0 +1,13 @@ +#include + +class Solution +{ + public: + static int maxSum(TreeNode* root) + { + if (root == NULL) + return 0; + else + return root->value + (std::max(maxSum(root->left), maxSum(root->right))); + } +}; \ No newline at end of file diff --git a/codewars/Linked Lists - Sorted Insert/SortedInsert.txt b/codewars/Linked Lists - Sorted Insert/SortedInsert.txt new file mode 100644 index 0000000..2ecc63b --- /dev/null +++ b/codewars/Linked Lists - Sorted Insert/SortedInsert.txt @@ -0,0 +1,17 @@ +class Node(object): + def __init__(self, data): + self.data = data + self.next = None + + +def sorted_insert(head, data): + if head is None: + return Node(data) + + elem=Node(data) + if elem.data < head.data: + elem.next=head + return elem + else: + head.next=sorted_insert(head.next,data) + return head \ No newline at end of file diff --git a/codewars/Merge two arrays/MergeTwoArrays.txt b/codewars/Merge two arrays/MergeTwoArrays.txt new file mode 100644 index 0000000..020286b --- /dev/null +++ b/codewars/Merge two arrays/MergeTwoArrays.txt @@ -0,0 +1,14 @@ +function mergeArrays(a, b) { + let answer = []; + + for (let i = 0; a[i] || b[i]; i++) { + if (a[i] != null) { + answer.push(a[i]); + } + if (b[i] != null) { + answer.push(b[i]); + } + } + + return answer; +} \ No newline at end of file diff --git a/codewars/Moving Zeros To The End/MovingZerosToTheEnd.txt b/codewars/Moving Zeros To The End/MovingZerosToTheEnd.txt new file mode 100644 index 0000000..2f4ba09 --- /dev/null +++ b/codewars/Moving Zeros To The End/MovingZerosToTheEnd.txt @@ -0,0 +1,18 @@ +#include + +std::vector move_zeroes(const std::vector& input) { + std::vector arr(input.size()); + + int lastIndex{}; + + for (int num: input) + { + if (num != 0) + { + arr[lastIndex] = num; + ++lastIndex; + } + } + + return arr; +} \ No newline at end of file diff --git a/codewars/Permutations/Permutations.txt b/codewars/Permutations/Permutations.txt new file mode 100644 index 0000000..4714a28 --- /dev/null +++ b/codewars/Permutations/Permutations.txt @@ -0,0 +1,24 @@ +#include +#include + +unsigned int factorial(int n) +{ + unsigned int result{ 1 }; + + for (int i{1}; i <= n; ++i) + result *= i; + + return result; +} + +std::vector permutations(std::string s) { + std::vector permutations_res{}; + + std::sort(s.begin(), s.end()); + + do { + permutations_res.push_back(s); + } while(std::next_permutation(s.begin(), s.end())); + + return permutations_res; +} \ No newline at end of file diff --git a/codewars/Product of consecutive Fib numbers/ProductFib.txt b/codewars/Product of consecutive Fib numbers/ProductFib.txt new file mode 100644 index 0000000..983d46f --- /dev/null +++ b/codewars/Product of consecutive Fib numbers/ProductFib.txt @@ -0,0 +1,20 @@ +def fibNumCalculate(fibNum, n): + if n < 2: + return fibNum[n-1] + if len(fibNum)-1 >= n: + return fibNum[n-1] + fibNum.append(fibNum[-1]+fibNum[-2]) + return fibNum[-1] + +def productFib(prod): + fibNum = [0, 1] + + n = 2 + + while fibNum[-1] * fibNum[-2] <= prod: + if fibNum[-1] * fibNum[-2] == prod: + return [fibNum[-2], fibNum[-1], True] + fibNumCalculate(fibNum, n) + n += 1 + + return [fibNum[-2], fibNum[-1], False] \ No newline at end of file diff --git a/codewars/Simple Pig Latin/SimplePigLatin.txt b/codewars/Simple Pig Latin/SimplePigLatin.txt new file mode 100644 index 0000000..d7d809e --- /dev/null +++ b/codewars/Simple Pig Latin/SimplePigLatin.txt @@ -0,0 +1,47 @@ +bool isLetterF(char c) +{ + if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z')) + return true; + return false; +} + +std::string pig_it(std::string str) +{ + char firstLetter{}; + bool isFirstLetter{true}; + + std::string result; + + for (size_t i{0}; i < str.size(); ++i) + { + char s{str[i]}; + bool isLetter{isLetterF(s)}; + + if (str[i] == ' ') + { + isFirstLetter = true; + if (isLetterF(str[i-1])) + { + result += firstLetter; + result += "ay"; + } + } + + if (isFirstLetter && isLetter) + { + firstLetter = str[i]; + isFirstLetter = false; + } + else + { + result += str[i]; + } + } + + if (isLetterF(str[str.size()-1])) { + result += firstLetter; + result += "ay"; + } + + return result; +} \ No newline at end of file diff --git a/codewars/Snail/Snail.txt b/codewars/Snail/Snail.txt new file mode 100644 index 0000000..26be617 --- /dev/null +++ b/codewars/Snail/Snail.txt @@ -0,0 +1,44 @@ +#include + +std::vector snail(const std::vector> &snail_map) { + const int border{static_cast(snail_map[0].size())}; + + int leftBorder{0}, upBorder{0}; + int rightBorder{border-1}, downBorder{border-1}; + + std::vector ans; + + int times{(border + 2)/2}; + while (times > 0) { + for (int j{leftBorder}, i{upBorder}; j <= rightBorder; ++j) + { + ans.push_back(snail_map[i][j]); + } + + ++upBorder; + + for (int i{upBorder}, j{rightBorder}; i <= downBorder; ++i) + { + ans.push_back(snail_map[i][j]); + } + + --rightBorder; + + for (int j{rightBorder}, i{downBorder}; j >= leftBorder; --j) + { + ans.push_back(snail_map[i][j]); + } + + --downBorder; + + for (int i{downBorder}, j{leftBorder}; i >= upBorder; --i) + { + ans.push_back(snail_map[i][j]); + } + + ++leftBorder; + + --times; + } + return ans; +} \ No newline at end of file diff --git a/codewars/Sum of Digits - Digital Root/DigitalRoot.txt b/codewars/Sum of Digits - Digital Root/DigitalRoot.txt new file mode 100644 index 0000000..37ef9ed --- /dev/null +++ b/codewars/Sum of Digits - Digital Root/DigitalRoot.txt @@ -0,0 +1,21 @@ +int sum_of_digits(int n) +{ + int sum{0}; + while (n > 0) + { + sum += n % 10; + n /= 10; + } + + return sum; +} + +int digital_root(int n) +{ + while (n - 9 > 0) + { + n = sum_of_digits(n); + } + + return n; +} \ No newline at end of file diff --git a/codewars/Sum of Intervals/SumOfIntervals.txt b/codewars/Sum of Intervals/SumOfIntervals.txt new file mode 100644 index 0000000..0f89519 --- /dev/null +++ b/codewars/Sum of Intervals/SumOfIntervals.txt @@ -0,0 +1,15 @@ +def sort_first(val): + return val[0] + +def sum_of_intervals(intervals): + intervals.sort(key=sort_first) + + sum = 0 + + for i in range(len(intervals)-1): + if intervals[i][1] >= intervals[i+1][0]: + intervals[i+1] = (intervals[i][0], max(intervals[i+1][1], intervals[i][1])) + else: + sum += intervals[i][1] - intervals[i][0] + sum += intervals[len(intervals)-1][1] - intervals[len(intervals)-1][0] + return sum \ No newline at end of file diff --git a/codewars/Sum of pairs/SumOfPairs.txt b/codewars/Sum of pairs/SumOfPairs.txt new file mode 100644 index 0000000..9a065af --- /dev/null +++ b/codewars/Sum of pairs/SumOfPairs.txt @@ -0,0 +1,9 @@ +import sys + +def sum_pairs(ints, s): + values = set() + for elem in ints: + if elem in values: + return [s-elem, elem] + values.add(s-elem) + \ No newline at end of file diff --git a/codewars/Tic-Tac-Toe Checker/TicTacToeChecker.txt b/codewars/Tic-Tac-Toe Checker/TicTacToeChecker.txt new file mode 100644 index 0000000..ffd335c --- /dev/null +++ b/codewars/Tic-Tac-Toe Checker/TicTacToeChecker.txt @@ -0,0 +1,29 @@ +def is_solved(board): + grid = [ + [a1, a2, a3], + [b1, b2, b3], + [c1, c2, c3] + ] = board + + if a1 == a2 == a3 == 1: return 1 + if b1 == b2 == b3 == 1: return 1 + if c1 == c2 == c3 == 1: return 1 + if a1 == b2 == c3 == 1: return 1 + if a3 == b2 == c1 == 1: return 1 + if a1 == b1 == c1 == 1: return 1 + if a2 == b2 == c2 == 1: return 1 + if a3 == b3 == c3 == 1: return 1 + + if a1 == a2 == a3 == 2: return 2 + if b1 == b2 == b3 == 2: return 2 + if c1 == c2 == c3 == 2: return 2 + if a1 == b2 == c3 == 2: return 2 + if a3 == b2 == c1 == 2: return 2 + if a1 == b1 == c1 == 2: return 2 + if a2 == b2 == c2 == 2: return 2 + if a3 == b3 == c3 == 2: return 2 + + if 0 in [a1, a2, a3, b1, b2, b3, c1, c2, c3]: + return -1 + else: + return 0 \ No newline at end of file diff --git a/codewars/Valid Parentheses/ValidParenthesis.txt b/codewars/Valid Parentheses/ValidParenthesis.txt new file mode 100644 index 0000000..a9cb391 --- /dev/null +++ b/codewars/Valid Parentheses/ValidParenthesis.txt @@ -0,0 +1,13 @@ +def valid_parentheses(string): + stack = [] + + for elem in string: + if elem == ')': + if len(stack) > 0 and stack[-1] == '(': + stack.pop() + else: + stack.append(elem) + elif elem == '(': + stack.append(elem) + + return len(stack) == 0 \ No newline at end of file diff --git a/codewars/Where my anagrams at/AnagramsAt.txt b/codewars/Where my anagrams at/AnagramsAt.txt new file mode 100644 index 0000000..347b923 --- /dev/null +++ b/codewars/Where my anagrams at/AnagramsAt.txt @@ -0,0 +1,11 @@ +def anagrams(word, words): + ans = [] + + word = sorted(word) + + for w in words: + if len(w) == len(word): + if word == sorted(w): + ans.append(w) + + return ans \ No newline at end of file