Skip to content

Commit

Permalink
Solved all tasks from codewars
Browse files Browse the repository at this point in the history
  • Loading branch information
EugeneWWolf committed Sep 26, 2023
1 parent c8965a3 commit 23a7c8c
Show file tree
Hide file tree
Showing 22 changed files with 530 additions and 0 deletions.
54 changes: 54 additions & 0 deletions codewars/Adding Big Numbers/AddingBigNumbers.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
#include <algorithm>
#include <iostream>
#include <string>

int ssize(const std::string& s)
{
return static_cast<int>(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);
}
28 changes: 28 additions & 0 deletions codewars/Anagram difference/Difference.txt
Original file line number Diff line number Diff line change
@@ -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
8 changes: 8 additions & 0 deletions codewars/Array Deep Count/DeepCount.txt
Original file line number Diff line number Diff line change
@@ -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

41 changes: 41 additions & 0 deletions codewars/Build Tower/BuildTower.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
#include <string>
#include <vector>

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<std::string> towerBuilder(unsigned nFloors)
{
const unsigned int symbolsOnLastFloor{ arithmeticProgression(1, nFloors) };
unsigned int firstSymbolPosition{ symbolsOnLastFloor / 2 };

std::vector<std::string> 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;
}
29 changes: 29 additions & 0 deletions codewars/Convert string to camel case/ConvertStringToCamelCase.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <string>

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;
}
32 changes: 32 additions & 0 deletions codewars/Duplicate Encoder/DuplicateEncoder.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include <map>
#include <string>
#include <string_view>

char to_lower(char c)
{
if (c >= 'A' && c <= 'Z')
c += 32;

return c;
}

std::string duplicate_encoder(const std::string& word){
std::map<char, int> 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;
}
15 changes: 15 additions & 0 deletions codewars/Find the missing letter/FindTheMissingLetter.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <vector>

char findMissingLetter(const std::vector<char>& 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;
}
}
28 changes: 28 additions & 0 deletions codewars/Flatten a Nested Map/FlattenANestedMap.txt
Original file line number Diff line number Diff line change
@@ -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;
}
13 changes: 13 additions & 0 deletions codewars/Fun with tree - max sum/FunWithTrees.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
#include <algorithm>

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)));
}
};
17 changes: 17 additions & 0 deletions codewars/Linked Lists - Sorted Insert/SortedInsert.txt
Original file line number Diff line number Diff line change
@@ -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
14 changes: 14 additions & 0 deletions codewars/Merge two arrays/MergeTwoArrays.txt
Original file line number Diff line number Diff line change
@@ -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;
}
18 changes: 18 additions & 0 deletions codewars/Moving Zeros To The End/MovingZerosToTheEnd.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#include <vector>

std::vector<int> move_zeroes(const std::vector<int>& input) {
std::vector<int> arr(input.size());

int lastIndex{};

for (int num: input)
{
if (num != 0)
{
arr[lastIndex] = num;
++lastIndex;
}
}

return arr;
}
24 changes: 24 additions & 0 deletions codewars/Permutations/Permutations.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#include <string>
#include <vector>

unsigned int factorial(int n)
{
unsigned int result{ 1 };

for (int i{1}; i <= n; ++i)
result *= i;

return result;
}

std::vector<std::string> permutations(std::string s) {
std::vector<std::string> 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;
}
20 changes: 20 additions & 0 deletions codewars/Product of consecutive Fib numbers/ProductFib.txt
Original file line number Diff line number Diff line change
@@ -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]
Loading

0 comments on commit 23a7c8c

Please sign in to comment.