-
Notifications
You must be signed in to change notification settings - Fork 34
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c8965a3
commit 23a7c8c
Showing
22 changed files
with
530 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
29
codewars/Convert string to camel case/ConvertStringToCamelCase.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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))); | ||
} | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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
20
codewars/Product of consecutive Fib numbers/ProductFib.txt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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] |
Oops, something went wrong.