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

add codewars #28

Open
wants to merge 2 commits into
base: Baknina_Elizaveta_Aleksandrovna
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions codewars/Adding Big Numbers/Adding Big Numbers.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function add(num1, num2) {
let res = "";
let carry = 0;
let len1 = num1.length, len2 = num2.length;
let i = len1 - 1, j = len2 - 1;
while (i >= 0 || j >= 0 || carry > 0) {
let sum = (i >= 0 ? parseInt(num1[i]) : 0) +
(j >= 0 ? parseInt(num2[j]) : 0) +
carry;
carry = Math.floor(sum / 10);
res = (sum % 10) + res;
i--; j--;
}
return res;
}
13 changes: 13 additions & 0 deletions codewars/Array Deep Count/Array Deep Count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function deepCount(arr) {
let count = 0;

for (let i = 0; i < arr.length; i++) {
if (Array.isArray(arr[i])) {
count += deepCount(arr[i]);
} else {
count++;
}
}

return count;
}
10 changes: 10 additions & 0 deletions codewars/Build Tower/Build Tower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
function towerBuilder(nFloors) {
let tower = [];

for (let i = 0; i < nFloors; i++) {
let row = " ".repeat(nFloors - i - 1) + "*".repeat(2 * i + 1) + " ".repeat(nFloors - i - 1);
tower.push(row);
}

return tower;
}
14 changes: 14 additions & 0 deletions codewars/Duplicate Encoder/Duplicate Encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function duplicateEncode(word) {
const lowerCaseWord = word.toLowerCase();
let result = '';

for (let i = 0; i < lowerCaseWord.length; i++) {
if (lowerCaseWord.indexOf(lowerCaseWord[i]) === lowerCaseWord.lastIndexOf(lowerCaseWord[i])) {
result += '(';
} else {
result += ')';
}
}

return result;
}
13 changes: 13 additions & 0 deletions codewars/Find the missing letter/Find the missing letter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function findMissingLetter(array) {
let sortedArray = array.sort();
let result = '';

for (let i = 0; i < sortedArray.length - 1; i++) {
if (sortedArray[i + 1].charCodeAt(0) !== sortedArray[i].charCodeAt(0) + 1) {
result = String.fromCharCode(sortedArray[i].charCodeAt(0) + 1);
break;
}
}

return result;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function Node(data) {
this.data = data;
this.next = null;
}

function sortedInsert(head, data) {
if (head === null || head.data > data) {
let newNode = new Node(data);
newNode.next = head;
return newNode;
}

if (head.next === null) {
let newNode = new Node(data);
head.next = newNode;
return head;
}

head.next = sortedInsert(head.next, data);
return head;
}
15 changes: 15 additions & 0 deletions codewars/Merge two arrays/Merge two arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function mergeArrays(a, b) {
let result = [];
let maxLength = Math.max(a.length, b.length);

for (let i = 0; i < maxLength; i++) {
if (i < a.length) {
result.push(a[i]);
}
if (i < b.length) {
result.push(b[i]);
}
}

return result;
}
15 changes: 15 additions & 0 deletions codewars/Moving Zeros To The End/moving zeros to the end.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function moveZeroes(arr) {
let index = 0;

for (let i = 0; i < arr.length; i++) {
if (arr[i] !== 0) {
arr[index] = arr[i];
if (i !== index) {
arr[i] = 0;
}
index++;
}
}

return arr;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function productFib(prod) {
let n = 0;
let n1 = 1;
let n2 = 1;
let arr = [0, 1, 1];

while (n2 < prod) {
n = n1;
n1 = n2;
n2 = n + n1;
arr = [n, n1, n2];
}

if (n2 * n1 === prod) {
arr[2] = true;
} else {
arr[2] = false;
}

return arr;
}
11 changes: 11 additions & 0 deletions codewars/Simple Pig Latin/Simple Pig Latin.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function pigIt(str) {
let words = str.split(' ');
let piggedWords = words.map(word => {
if (/^[aeiou]/i.test(word)) {
return word + 'ay';
} else {
return word.slice(1) + word.charAt(0) + 'ay';
}
});
return piggedWords.join(' ');
}
21 changes: 21 additions & 0 deletions codewars/Snail/Snail.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
let snail = function(array) {
if (!array.length) return [];
const R = array.length, C = array[0].length;
const seen = Array.from({length: R}, () => Array(C).fill(false));
const dr = [0, 1, 0, -1], dc = [1, 0, -1, 0];
let ans = [], r = 0, c = 0, di = 0;
for (let _ = 0; _ < R * C; _++) {
ans.push(array[r][c]);
seen[r][c] = true;
const cr = r + dr[di], cc = c + dc[di];
if (0 <= cr && cr < R && 0 <= cc && cc < C && !seen[cr][cc]) {
r = cr;
c = cc;
} else {
di = (di + 1) % 4;
r += dr[di];
c += dc[di];
}
}
return ans;
}
11 changes: 11 additions & 0 deletions codewars/Sum of Digits - Digital Root/Sum of Digits.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function digitalRoot(n) {
while (n >= 10) {
let sum = 0;
while (n > 0) {
sum += n % 10;
n = Math.floor(n / 10);
}
n = sum;
}
return n;
}
21 changes: 21 additions & 0 deletions codewars/Sum of Intervals/sum of intervals.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function sumIntervals(intervals) {
// Объединение перекрывающихся интервалов
intervals.sort((a, b) => a[0] - b[0]);

let merged = [];
for (let i = 0; i < intervals.length; i++) {
if (merged.length === 0 || merged[merged.length - 1][1] < intervals[i][0]) {
merged.push(intervals[i]);
} else {
merged[merged.length - 1][1] = Math.max(merged[merged.length - 1][1], intervals[i][1]);
}
}

// Вычисление суммы длин интервалов
let sum = 0;
for (let i = 0; i < merged.length; i++) {
sum += merged[i][1] - merged[i][0];
}

return sum;
}
16 changes: 16 additions & 0 deletions codewars/Sum of pairs/sum of pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function sumPairs(ints, s) {
let obj = {};
let arr;

for (let i = 0; i < ints.length; i++) {
let complement = s - ints[i];
if (complement in obj) {
arr = [ints[i], complement];
break;
} else {
obj[ints[i]] = true;
}
}

return arr || undefined;
}
29 changes: 29 additions & 0 deletions codewars/Valid Parentheses/Valid Parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
function validParentheses(parens) {
const stack = [];

for (let i = 0; i < parens.length; i++) {
if (parens[i] === '(') {
stack.push(parens[i]);
}

else if (parens[i] === ')') {
if (stack.length === 0) {
return false;
}

if (stack[stack.length - 1] === '(') {
stack.pop();
}

else {
return false;
}
}
}

if (stack.length !== 0) {
return false;
}

return true;
}
34 changes: 34 additions & 0 deletions rpgsaga/saga/src/fox.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
class Fox {
private name: string;
private age: number;
private color: string;

constructor(name: string, age: number, color: string) {
if (!name || !color || age < 0) {
throw new Error("Invalid arguments");
}
this.name = name;
this.age = age;
this.color = color;
}

public introduce(): string {
return `Hello, I am ${this.name}, a ${this.color} fox and I am ${this.age} years old.`;
}

public makeSound(): string {
return "Ring-ding-ding-ding-dingeringeding!";
}

public getView(): number[] {
const asciiCodes: number[] = [];
for (let i = 0; i < this.name.length; i++) {
asciiCodes.push(this.name.charCodeAt(i));
}
return asciiCodes;
}

public toString(): string {
return this.introduce();
}
}