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

Ryzhik daniil dmitrievich #34

Closed
wants to merge 2 commits into from
Closed
Changes from 1 commit
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
Next Next commit
Solved some tasks
ZinixZay committed Sep 27, 2023
commit bf2cbe3f66ed12028e68a1713716bbd14d7cbc3b
11 changes: 11 additions & 0 deletions codewars/Anagram difference/anagram_difference.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function anagramDifference(w1,w2){
let count = 0;
for (let i = 0; i < w1.length; i++){
if (w2.indexOf(w1[i]) >= 0){
w2 = w2.replace(w1[i], '')
} else {
count++
}
}
return w2.length + count
}
11 changes: 11 additions & 0 deletions codewars/Array Deep Count/aray_deep_count.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function deepCount(a){
let answer = 0
for (let i = 0; i < a.length; i++){
if (typeof(a[i]) == "object"){
answer += 1 + deepCount(a[i])
} else {
answer += 1
}
}
return answer
}
17 changes: 17 additions & 0 deletions codewars/Build Tower/build_tower.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function towerBuilder(nFloors) {
let tower = []
for (let i = 0; i < nFloors; i++){
let floor = "";
for (let spacers = 0; spacers < (nFloors - 1 - i); spacers++){
floor += " "
}
for (let stars = 0; stars < (1 + i * 2); stars++){
floor += "*"
}
for (let spacers2 = 0; spacers2 < (nFloors - 1 - i); spacers2++){
floor += " "
}
tower.push(floor)
}
return tower
}
17 changes: 17 additions & 0 deletions codewars/Convert string to camel case/convert_string.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
function toCamelCase(sentence){
let uppered_one = "";
let skip_char = false;
for (let i = 0; i < sentence.length; i++){
if (!skip_char){
if (sentence[i] == "-" | sentence[i] == "_"){
skip_char = true
} else {
uppered_one += sentence[i]
}
} else {
uppered_one += sentence[i].toUpperCase()
skip_char = false
}
}
return uppered_one
}
19 changes: 19 additions & 0 deletions codewars/Duplicate Encoder/duplicate_encoder.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
function duplicateEncode(word){
let c = ""
let count = 0
z = word.toLowerCase()
for (let i = 0; i < z.length; i++) {
count = 0
for (let j = 0; j < z.length; j++) {
if (z[j] === z[i]){
count ++
}
}
if (count > 1){
c += ")"
} else {
c += "("
}
}
return c
}
13 changes: 13 additions & 0 deletions codewars/Find the missing letter/find_the_missing.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
function findMissingLetter(char_arr)
{
var index = 0;
first_code = char_arr[0].charCodeAt(index)
for (let i = 1; i < char_arr.length; i++){
current_code = char_arr[i].charCodeAt(index)
if ((current_code - first_code) > 1){
return String.fromCharCode(current_code - 1)
} else {
first_code = current_code
}
}
}
24 changes: 24 additions & 0 deletions codewars/Flatten a Nested Map/flatten_a_nested.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
var curr_prefix = ''
var n_m = {}
function flattenMap(map) {
for (obj in map){
if (map[obj].constructor === Object){
if (curr_prefix.length == 0){
curr_prefix += obj
} else{
curr_prefix += '/' + obj
}
flattenMap(map[obj])
curr_prefix = curr_prefix.slice(0, curr_prefix.length - 2)
} else {
curr_prefix += '/' + obj
n_m[curr_prefix] = map[obj]
if (curr_prefix.length > 1){
curr_prefix = curr_prefix.slice(0, curr_prefix.length - 2)
} else {
console.log('smt went wrong')
}
}
}
return n_m
}
14 changes: 14 additions & 0 deletions codewars/Merge two arrays/merge_arrays.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function mergeArrays(a, b) {
let result = []
for (let i = 0; i < Math.max(a.length, b.length); i++){
if (a.length > i){
result.push(a[i])
}
if (b.length > i){
result.push(b[i])
}
}
return result
}


15 changes: 15 additions & 0 deletions codewars/Moving Zeros To The End/moving_zeros.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function moveZeros(arr) {
let new_arr = []
let count = 0;
for (let i = 0; i < arr.length; i++){
if (arr[i] != 0){
new_arr.push(arr[i])
} else {
count += 1
}
}
for (let j = 0; j < count; j ++){
new_arr.push(0)
}
return new_arr
}
16 changes: 16 additions & 0 deletions codewars/Product of consecutive Fib numbers/consecutive_fib.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
function productFib(prod){
let fl = 0
let fr = 1
while (fl * fr < prod){
s = fl + fr
fl = fr
fr = s
}
if (fl * fr == prod){
return [fl, fr, true]
} else {
return [fl, fr, false]
}
}

productFib(500)
11 changes: 11 additions & 0 deletions codewars/Simple Pig Latin/simple_pig.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
function pigIt(str){

let words = str.split(' ')
for (i = 0; i < words.length; i++){
words[i] = words[i].slice(1, words[i].length) + words[i][0] + 'ay'
}
return words.join(' ')
}


console.log(pigIt('Pig latin is cool'))
13 changes: 13 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,13 @@
function digitalRoot(n) {
if (String(n).length > 1){
let n_copy = n
let new_n = 0
while (n_copy > 0){
new_n += n_copy % 10
n_copy = Math.floor(n_copy/10)
}
return digitalRoot(new_n)
} else {
return n
}
}
15 changes: 15 additions & 0 deletions codewars/Sum of pairs/sum_of_pairs.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
function sumPairs(ints, s) {
var second_index = +Infinity
var answer = undefined;
for (let i = 0; i < ints.length - 1; i++){
for (let j = i + 1; j < ints.length; j++){
if ((ints[i] + ints[j]) == s){
if (second_index > j){
second_index = j
answer = [ints[i], ints[j]]
}
}
}
}
return answer
}
35 changes: 35 additions & 0 deletions codewars/Tic-Tac-Toe Checker/tic_tac.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
function isSolved(board) {
draw_possibility = false
const win_positions = [[[0, 0], [0, 1], [0, 2]],
[[1, 0], [1, 1], [1, 2]],
[[2, 0], [2, 1], [2, 2]],
[[0, 0], [1, 1], [2, 2]],
[[0, 2], [1, 1], [2, 0]],
[[0, 0], [1, 0], [2, 0]],
[[0, 1], [1, 1], [2, 1]],
[[2, 0], [2, 1], [2, 2]]]
for (let way = 0; way < win_positions.length; way++){
let values = ''
for (let position = 0; position < 3; position++){
let row = win_positions[way][position][0]
let column = win_positions[way][position][1]
values += String(board[row][column])
}
if (values.split('1') == 4){
return 'X';
} else if (values.split('2') == 4){
return 'O'
} else if (values.split('0') > 0){
draw_possibility = true
}
}
if (draw_possibility) {
return -1
} else {
return 0
}
}


console.log(isSolved([[0,0,1],
[0,1,2], [2, 1, 0]]))
14 changes: 14 additions & 0 deletions codewars/Valid Parentheses/valid_parentheses.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
function validParentheses(parens) {
let state = 0
for (i = 0; i < parens.length; i++){
if (parens[i] == '('){
state++
} else {
state--
}
}
if (state == 0){
return true
}
return false
}
21 changes: 21 additions & 0 deletions codewars/Where my anagrams at/where_is_anagram.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
function anagrams(word, words) {
let answer = []
for (let words_key in words){
let candidate = words[words_key]
let anagram = true
for (let word_key in word){
if (words[words_key].indexOf(word[word_key]) >= 0){
words[words_key] = words[words_key].replace(word[word_key], '')
} else {
anagram = false
break
}
}
if (words[words_key].length == 0 && anagram){
answer.push(candidate)
}
}
return answer
}

console.log(anagrams('racer', ['crazer', 'carer', 'racar', 'caers', 'racer']))