Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Lab. 1, Lab. 2 _and_ Lab. 3 is done. #17

Merged
merged 8 commits into from
Dec 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
92 changes: 92 additions & 0 deletions codewars/Adding Big Numbers/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>

// Some essentials.
const char fallbackNum = '0';

static inline int toInt(char c) {
return c - '0';
}

static inline char toChar(int i) {
return i + '0';
}

static inline bool digitBack(const char* digitRoot, const char** position) {
if(*position != &fallbackNum) {
if(--(*position) < digitRoot) {
*position = &fallbackNum;
return true;
}

return false;
}

return true;
}

char *add(const char *a, const char *b) {

// Crap... We don't know lengths of strings....
// Let get it.
size_t aLen = strlen(a);
size_t bLen = strlen(b);

// We need to allocate result buffer.
// Let assume that max number length is sum of strings + 1 (for carry).
size_t digits = aLen + bLen + 1;

// Assume calloc will set memory piece by 0's.
char* number = calloc(digits + 2, sizeof(char)); // +1 for \0
// +1 for temporary carry

char* numberStart = number + digits;
*numberStart = '\0';

// Preparing for sum
const char* aStart = a + aLen - 1; // To cut \0
const char* bStart = b + bLen - 1; // To cut \0

// Where rocket starts...
bool digitsEnds = false;
while(1) {
numberStart--;

// Sum digits. Don't forget cast chars to integers.
int sum = toInt(*aStart) + toInt(*bStart);

// Apply carry.
sum += *numberStart;

if(!sum & digitsEnds) {
numberStart++;
break;
}

int digit = sum % 10;
int carry = sum / 10;

// Let write carry.
// We no need to convert it to char 'cause it's temoprary value.
*(numberStart - 1) = carry;

// Let write our digit. Convert it to char before.
*numberStart = toChar(digit);

digitsEnds = true;
digitsEnds &= digitBack(a, &aStart);
digitsEnds &= digitBack(b, &bStart);
}

// Well, we have sum written by ptr. Anyway, it can't be freed properly.
// We need to create accurate copy of integer.
size_t resultSize = sizeof(char) * (number + digits - numberStart + 1);
char* result = malloc(resultSize);
strcpy(result, numberStart);

free(number);

return result;
}

24 changes: 24 additions & 0 deletions codewars/Anagram difference/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
function anagramDifference(w1,w2){
let w1S = w1.split('').sort()
let w2S = w2.split('').sort()
console.log(w1S)
console.log(w2S)
let i = 0;
let j = 0;
let diff = 0;
while(i < w1S.length && j < w2S.length) {
if(w1S[i] == w2S[j]) {
i++;
j++;
}
else {
diff++;
if(w1S[i] < w2S[j])
i++;
else
j++;
}
}
diff += w1S.length - i + w2S.length - j
return diff
}
16 changes: 16 additions & 0 deletions codewars/Array Deep Count/Kata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
using System;

public class Kata
{
public static int DeepCount(object a)
{
if(a is object[] arr) {
int sum = 0;
foreach(var r in arr)
sum += DeepCount(r) + 1;
return sum;
}

return 0;
}
}
22 changes: 22 additions & 0 deletions codewars/Build Tower/Kata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
public class Kata
{
public static string[] TowerBuilder(int nFloors)
{
string[] building = new string[nFloors];
int offset = 0;
int length = (nFloors - 1) * 2 + 1;
char[] floor = new char[length];
for(int y = nFloors - 1; y >= 0; --y) {
for(int x = 0; x < length; ++x) {
if(x < offset || x >= length - offset)
floor[x] = ' ';
else
floor[x] = '*';
}
building[y] = new(floor);
++offset;
}

return building;
}
}
23 changes: 23 additions & 0 deletions codewars/Convert string to camel case/Kata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using System;
using System.Text;

public class Kata
{
public static string ToCamelCase(string str)
{
StringBuilder sb = new(str.Length);
bool shouldMakeCamel = false;
for(int x = 0; x < str.Length; ++x) {
if(str[x] == '_' || str[x] == '-')
shouldMakeCamel = true;
else if(shouldMakeCamel) {
shouldMakeCamel = false;
sb.Append(char.ToUpper(str[x]));
}
else
sb.Append(str[x]);

}
return sb.ToString();
}
}
24 changes: 24 additions & 0 deletions codewars/Duplicate Encoder/Kata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
using System.Collections.Generic;

public class Kata
{
public static string DuplicateEncode(string word)
{
char[] subword = new char[word.Length];
Dictionary<char, int> table = new();

for(int x = 0; x < word.Length; ++x) {
char c = char.ToLower(word[x]);
if(table.TryGetValue(c, out int index)) {
subword[x] = ')';
subword[index] = ')';
}
else {
subword[x] = '(';
table[c] = x;
}
}

return new string(subword);
}
}
14 changes: 14 additions & 0 deletions codewars/Find the missing letter/Kata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
public class Kata
{
public static char FindMissingLetter(char[] array)
{
char symbolToWait = array[0];
foreach(var currentSymbol in array) {
if(symbolToWait != currentSymbol)
return symbolToWait;

symbolToWait++;
}
return '-';
}
}
22 changes: 22 additions & 0 deletions codewars/Flatten a Nested Map/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

// Flattens an hierarchical map into a single level
function flattenMap(map) {
let result = {}
function flattenSubCoroutine(name, submap) {
if(typeof submap == 'object' && !Array.isArray(submap) && submap !== null) {
for(const [k,v] of Object.entries(submap)) {
let nestedName
if(name)
nestedName = name + '/' + k
else
nestedName = k
flattenSubCoroutine(nestedName, v)
}
}
else
result[name] = submap
}

flattenSubCoroutine('', map)
return result
}
25 changes: 25 additions & 0 deletions codewars/Fun with tree - max sum/main.improved.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
typedef struct TreeNode {
int value;
struct TreeNode *left, *right;
} TreeNode;

static inline int max(int a, int b) {
return a > b ? a : b;
}

int max_sum(const TreeNode *root) {
// Recursion is my mother!
if (root) {

int influence;

if(!root->left) influence = max_sum(root->right);
else if(!root->right) influence = max_sum(root->left);
else influence = max(max_sum(root->left), max_sum(root->right));

return root->value + influence;
}
else
return 0; // NULL-tree is not tree at it all!
// It has not influence.
}
29 changes: 29 additions & 0 deletions codewars/Fun with tree - max sum/main.old.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
typedef struct TreeNode {
int value;
struct TreeNode *left, *right;
} TreeNode;

static inline int max(int a, int b) {
return a > b ? a : b;
}

int max_sum(const TreeNode *root) {
// Recursion is my mother!
if (root) {
int buff[2] = { 0, 0 };
int buffTop = 0;

if(root->left) buff[buffTop++] = max_sum(root->left);
if(root->right) buff[buffTop++] = max_sum(root->right);

int influence;

if(buffTop > 1) influence = max(buff[0], buff[1]);
else influence = buff[0]; // If no leafs -- 0 influence 'coz buff =0, 0

return root->value + influence;
}
else
return 0; // NULL-tree is not tree at it all!
// It has not influence.
}
23 changes: 23 additions & 0 deletions codewars/Linked Lists - Sorted Insert/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
function Node(data) {
this.data = data;
this.next = null;
}

function sortedInsert(head, data) {
let prev = new Node(); // To exclude 'null' checks.
let curr = head;
while(curr) {
if(data < curr.data)
break;

prev = curr;
curr = curr.next;
}

let instance = new Node();
instance.data = data;

prev.next = instance;
instance.next = curr;
return curr === head ? instance : head;
}
35 changes: 35 additions & 0 deletions codewars/Merge two arrays/main.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stddef.h>

void merge_arrays (
size_t len_a, const int a[len_a],
size_t len_b, const int b[len_b],
int merged[len_a + len_b]
) {
// Let declare sources to copy from.
const int* sources[2] = { a, b };
const int lengths[2] = { len_a, len_b };

int* counts = merged;

int currentSource = 0;

// We only can mix general part. Can't go out of bounds.
int mixLimit = len_a < len_b ? len_a : len_b;
int mixLenght = mixLimit << 1; // Mixes are int pairs.

while(mixLenght--) {
*(counts++) = *(sources[currentSource]++);
currentSource = !currentSource;
}

// Finish our copy-pasta!
// Select source with larger size.
currentSource = len_b > len_a;
int copyPastaRemains = lengths[currentSource] - mixLimit;

while(copyPastaRemains--) {
*(counts++) = *(sources[currentSource]++);
}

// We're ready to go!
}
20 changes: 20 additions & 0 deletions codewars/Moving Zeros To The End/Kata.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
public class Kata
{
public static int[] MoveZeroes(int[] arr)
{
int x = arr.Length - 2;
while(x >= 0) {
if(arr[x] == 0 && arr[x + 1] != 0) {
int tmp = arr[x];
arr[x] = arr[x+1];
arr[x+1] = tmp;
if(x < arr.Length - 2)
x++;
}
else
x--;
}

return arr;
}
}
Loading
Loading