Skip to content

Commit ce116de

Browse files
author
alberto bollettin
committed
06/22/2022 add 4 kata solutions
1 parent f5a5c9f commit ce116de

File tree

4 files changed

+306
-0
lines changed

4 files changed

+306
-0
lines changed

CodewarsStyleRankingSystem/index.js

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
/*
2+
KATA LINK: https://www.codewars.com/kata/51fda2d95d6efda45e00004e/train/javascript
3+
*/
4+
5+
class User {
6+
constructor() {
7+
this.rank = -8
8+
this.progress = 0
9+
}
10+
11+
calculateRank() {
12+
this.rank = this.rank + parseInt(this.progress / 100)
13+
if (this.rank === 0) {
14+
this.rank = this.rank + 1
15+
}
16+
this.progress = this.progress % 100
17+
if (this.rank === 8) {
18+
this.progress = 0
19+
}
20+
}
21+
22+
incProgress(taskRank) {
23+
if (taskRank === 0 || taskRank > 8 || taskRank < -8) {
24+
throw new Error('!!!!!!')
25+
}
26+
if (taskRank === this.rank) {
27+
this.progress = this.progress + 3
28+
this.calculateRank()
29+
} else if (this.rank > 0 && taskRank < 0 ? taskRank + 2 === this.rank : taskRank + 1 === this.rank) {
30+
this.progress = this.progress + 1
31+
this.calculateRank()
32+
} else if (taskRank > this.rank) {
33+
let d = taskRank - this.rank
34+
if (this.rank < 0 && taskRank > 0) {
35+
d = d - 1
36+
}
37+
this.progress = this.progress + (10 * d * d)
38+
this.calculateRank()
39+
}
40+
}
41+
}
42+
43+
//tests
44+
const user = new User()
45+
console.log(user.rank)
46+
console.log(user.progress)
47+
user.incProgress(-7)
48+
console.log(user.rank)
49+
console.log(user.progress)
50+
user.incProgress(-5)
51+
console.log(user.rank)
52+
console.log(user.progress)

TextAlignJustify/index.js

+95
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
1+
/*
2+
KATA LINK: https://www.codewars.com/kata/537e18b6147aa838f600001b/train/javascript
3+
*/
4+
5+
function justify(text, width) {
6+
const addSpaces = (row) => {
7+
let result = []
8+
let currentWidth = 0
9+
if (row.length === 1) {
10+
return row
11+
}
12+
for (var i = 0; i < row.length; i++) {
13+
currentWidth = currentWidth + row[i].length
14+
}
15+
while (true) {
16+
if (currentWidth > width) {
17+
return result
18+
} else {
19+
for (var i = 0; i < row.length - 1; i++) {
20+
currentWidth = currentWidth + 1
21+
if (currentWidth > width) {
22+
return result
23+
}
24+
row[i] = row[i] + ' '
25+
}
26+
}
27+
}
28+
}
29+
30+
const textArray = text.split(' ')
31+
const justifyTextArray = []
32+
let a = []
33+
let count = 0
34+
for (var i = 0; i < textArray.length; i++) {
35+
if (count + textArray[i].length + a.length > width) {
36+
justifyTextArray.push(a)
37+
a = []
38+
count = 0
39+
}
40+
a.push(textArray[i])
41+
count = count + textArray[i].length
42+
}
43+
if (a.length > 0) {
44+
justifyTextArray.push(a)
45+
}
46+
for (var i = 0; i < justifyTextArray.length - 1; i++) {
47+
addSpaces(justifyTextArray[i])
48+
}
49+
for (var i = 0; i < justifyTextArray.length; i++) {
50+
if (i === justifyTextArray.length - 1) {
51+
justifyTextArray[i] = justifyTextArray[i].join(' ')
52+
} else {
53+
justifyTextArray[i] = justifyTextArray[i].join('')
54+
}
55+
}
56+
return justifyTextArray.join('\n')
57+
}
58+
59+
//tests
60+
console.log(justify('123 45 6', 7)/*, justify('123 45 6', 7) === '123 45\n6'*/)
61+
//console.log(justify('123', 7), justify('123', 7) === '123')
62+
//console.log(justify('', 10), justify('', 10) === '')
63+
64+
const LIPSUM = `Lorem ipsum dolor sit amet, consectetur adipiscing elit. Vestibulum sagittis dolor mauris, at elementum ligula tempor eget. In quis rhoncus nunc, at aliquet orci. Fusce at dolor sit amet felis suscipit tristique. Nam a imperdiet tellus. Nulla eu vestibulum urna. Vivamus tincidunt suscipit enim, nec ultrices nisi volutpat ac. Maecenas sit amet lacinia arcu, non dictum justo. Donec sed quam vel risus faucibus euismod. Suspendisse rhoncus rhoncus felis at fermentum. Donec lorem magna, ultricies a nunc sit amet, blandit fringilla nunc. In vestibulum velit ac felis rhoncus pellentesque. Mauris at tellus enim. Aliquam eleifend tempus dapibus. Pellentesque commodo, nisi sit amet hendrerit fringilla, ante odio porta lacus, ut elementum justo nulla et dolor.`
65+
66+
const LIPSUM_SOLUTION = `Lorem ipsum dolor sit amet,
67+
consectetur adipiscing elit.
68+
Vestibulum sagittis dolor
69+
mauris, at elementum ligula
70+
tempor eget. In quis rhoncus
71+
nunc, at aliquet orci. Fusce
72+
at dolor sit amet felis
73+
suscipit tristique. Nam a
74+
imperdiet tellus. Nulla eu
75+
vestibulum urna. Vivamus
76+
tincidunt suscipit enim, nec
77+
ultrices nisi volutpat ac.
78+
Maecenas sit amet lacinia
79+
arcu, non dictum justo. Donec
80+
sed quam vel risus faucibus
81+
euismod. Suspendisse rhoncus
82+
rhoncus felis at fermentum.
83+
Donec lorem magna, ultricies a
84+
nunc sit amet, blandit
85+
fringilla nunc. In vestibulum
86+
velit ac felis rhoncus
87+
pellentesque. Mauris at tellus
88+
enim. Aliquam eleifend tempus
89+
dapibus. Pellentesque commodo,
90+
nisi sit amet hendrerit
91+
fringilla, ante odio porta
92+
lacus, ut elementum justo
93+
nulla et dolor.`
94+
95+
console.log(justify(LIPSUM, 30), '\n\n*****************\n\n', LIPSUM_SOLUTION, justify(LIPSUM, 30) === LIPSUM_SOLUTION)

ValidateSudokuWithSizeNxN/index.js

+138
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,138 @@
1+
/*
2+
KATA LINK: https://www.codewars.com/kata/540afbe2dc9f615d5e000425/train/javascript
3+
*/
4+
5+
var Sudoku = function (data) {
6+
const isValidRow = (array, validSolution) => {
7+
const sortedArray = array.sort((a, b) => a - b)
8+
return JSON.stringify(sortedArray) === JSON.stringify(validSolution)
9+
}
10+
11+
const getValidSolution = (size) => {
12+
let a = []
13+
for (var i = 0; i < size; i++) {
14+
a.push(i + 1)
15+
}
16+
return a
17+
}
18+
19+
return {
20+
isValid: function () {
21+
let result = true
22+
let size = data.length
23+
const sqrtSize = Math.sqrt(size)
24+
let validSolution = getValidSolution(size)
25+
26+
// row control
27+
const normalData = JSON.parse(JSON.stringify(data))
28+
for (var i = 0; i < size; i++) {
29+
result = result && isValidRow(normalData[i], validSolution)
30+
}
31+
32+
// column control
33+
const transposeData = data[0].map((col, i) => data.map(row => row[i]))
34+
for (var i = 0; i < size; i++) {
35+
result = result && isValidRow(transposeData[i], validSolution)
36+
}
37+
38+
// NxN boxes control
39+
let boxesRowData = []
40+
for (var h = 0; h < sqrtSize; h++) {
41+
for (var k = 0; k < sqrtSize; k++) {
42+
let a = []
43+
for (var i = 0; i < size; i++) {
44+
for (var j = 0; j < size; j++) {
45+
if (i < (h * sqrtSize) + sqrtSize && i > (h * sqrtSize) - 1 && j < (k * sqrtSize) + sqrtSize && j > (k * sqrtSize) - 1) {
46+
a.push(data[i][j])
47+
}
48+
}
49+
}
50+
boxesRowData.push(a)
51+
}
52+
}
53+
for (var i = 0; i < size; i++) {
54+
result = result && isValidRow(boxesRowData[i], validSolution)
55+
}
56+
57+
return result
58+
}
59+
}
60+
}
61+
62+
//tests
63+
var goodSudoku1 = new Sudoku([
64+
[7, 8, 4, 1, 5, 9, 3, 2, 6],
65+
[5, 3, 9, 6, 7, 2, 8, 4, 1],
66+
[6, 1, 2, 4, 3, 8, 7, 5, 9],
67+
[9, 2, 8, 7, 1, 5, 4, 6, 3],
68+
[3, 5, 7, 8, 4, 6, 1, 9, 2],
69+
[4, 6, 1, 9, 2, 3, 5, 8, 7],
70+
[8, 7, 6, 3, 9, 4, 2, 1, 5],
71+
[2, 4, 3, 5, 6, 1, 9, 7, 8],
72+
[1, 9, 5, 2, 8, 7, 6, 3, 4]
73+
])
74+
75+
var goodSudoku2 = new Sudoku([
76+
[1, 4, 2, 3],
77+
[3, 2, 4, 1],
78+
[4, 1, 3, 2],
79+
[2, 3, 1, 4]
80+
])
81+
82+
var badSudoku1 = new Sudoku([
83+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
84+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
85+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
86+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
87+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
88+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
89+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
90+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
91+
[1, 2, 3, 4, 5, 6, 7, 8, 9]
92+
])
93+
94+
var badSudoku3 = new Sudoku([
95+
[1, 2, 3, 4, 5, 6, 7, 8, 9],
96+
[2, 3, 1, 5, 6, 4, 8, 9, 7],
97+
[3, 1, 2, 6, 4, 5, 9, 7, 8],
98+
[4, 5, 6, 7, 8, 9, 1, 2, 3],
99+
[5, 6, 4, 8, 9, 7, 2, 3, 1],
100+
[6, 4, 5, 9, 7, 8, 3, 1, 2],
101+
[7, 8, 9, 1, 2, 3, 4, 5, 6],
102+
[8, 9, 7, 2, 3, 1, 5, 6, 4],
103+
[9, 7, 8, 3, 1, 2, 6, 4, 5]
104+
])
105+
106+
var badSudoku2 = new Sudoku([
107+
[1, 2, 3, 4, 5],
108+
[1, 2, 3, 4],
109+
[1, 2, 3, 4],
110+
[1]
111+
])
112+
113+
114+
//console.log(goodSudoku1.isValid())
115+
console.log(goodSudoku2.isValid())
116+
117+
//console.log(badSudoku1.isValid())
118+
//console.log(badSudoku2.isValid())
119+
//console.log(badSudoku3.isValid())
120+
121+
/*console.log(validSolution([[5, 3, 4, 6, 7, 8, 9, 1, 2],
122+
[6, 7, 2, 1, 9, 5, 3, 4, 8],
123+
[1, 9, 8, 3, 4, 2, 5, 6, 7],
124+
[8, 5, 9, 7, 6, 1, 4, 2, 3],
125+
[4, 2, 6, 8, 5, 3, 7, 9, 1],
126+
[7, 1, 3, 9, 2, 4, 8, 5, 6],
127+
[9, 6, 1, 5, 3, 7, 2, 8, 4],
128+
[2, 8, 7, 4, 1, 9, 6, 3, 5],
129+
[3, 4, 5, 2, 8, 6, 1, 7, 9]]))
130+
console.log(validSolution([[5, 3, 4, 6, 7, 8, 9, 1, 2],
131+
[6, 7, 2, 1, 9, 0, 3, 4, 8],
132+
[1, 0, 0, 3, 4, 2, 5, 6, 0],
133+
[8, 5, 9, 7, 6, 1, 0, 2, 0],
134+
[4, 2, 6, 8, 5, 3, 7, 9, 1],
135+
[7, 1, 3, 9, 2, 4, 8, 5, 6],
136+
[9, 0, 1, 5, 3, 7, 2, 1, 4],
137+
[2, 8, 7, 4, 1, 9, 6, 3, 5],
138+
[3, 0, 0, 4, 8, 1, 1, 7, 9]]))*/

WeightForWeight/index.js

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
/*
2+
KATA LINK: https://www.codewars.com/kata/55c6126177c9441a570000cc/train/javascript
3+
*/
4+
5+
function orderWeight(string) {
6+
stringWeight = (s) => {
7+
let count = 0
8+
s.split('').map(c => count = count + parseInt(c))
9+
return count
10+
}
11+
12+
return string.split(' ').sort((a, b) => {
13+
return stringWeight(a) === stringWeight(b) ? a.localeCompare(b) : stringWeight(a) - stringWeight(b)
14+
}).join(' ')
15+
}
16+
17+
//tests
18+
console.log(orderWeight("103 123 4444 99 2000"),
19+
JSON.stringify(orderWeight("103 123 4444 99 2000")) === JSON.stringify("2000 103 123 4444 99"))
20+
console.log(orderWeight("2000 10003 1234000 44444444 9999 11 11 22 123"),
21+
JSON.stringify(orderWeight("2000 10003 1234000 44444444 9999 11 11 22 123")) === JSON.stringify("11 11 2000 10003 22 123 1234000 44444444 9999"))

0 commit comments

Comments
 (0)