Skip to content

Instantly share code, notes, and snippets.

<?php
/**
* Mehen games test
*
* @author sagittaracc <sagittaracc@gmail.com>
*/
// Массив имеет произвольные значения
$array = [-1, 2, 10, 3, -5, 3];
@sagittaracc
sagittaracc / !index.php
Last active June 29, 2021 08:25
Calc all the rectangles by points on the plane (Google interview)
<?php
require 'Point.php';
require 'PointArray.php';
require 'Rectangle.php';
$points = new PointArray([
[0, 0], [0, 1], [0, 2], [0, 3],
[1, 0], [1, 1], [1, 2], [1, 3],
[2, 0], [2, 1], [2, 2], [2, 3],
@sagittaracc
sagittaracc / index.js
Created June 30, 2021 15:57
Google interview
var houses = [
{
bk: true,
mac: false,
kfc: false
},
{
bk: false,
mac: false,
kfc: false
@sagittaracc
sagittaracc / shrimp-graph.js
Created July 6, 2021 13:45
Сжатие зацикленных участков в графе
var vertexes = [[1], [2], [3]];
var routes = [
[[1], [2]],
[[2], [1]],
[[2], [3]],
[[3], [2]],
];
/**
@sagittaracc
sagittaracc / search-for-pairs-in-sorted-array.js
Last active July 15, 2021 08:47
Search for pairs in sorted array
function searchForPairsBySum(sortedArray, sum) {
var leftIndex = 0;
var rightIndex = sortedArray.length - 1
while (leftIndex !== rightIndex) {
if (sortedArray[leftIndex] + sortedArray[rightIndex] === sum) {
console.log(sortedArray[leftIndex], sortedArray[rightIndex], "Indexes: ", leftIndex, rightIndex);
leftIndex++;
rightIndex++;
}
@sagittaracc
sagittaracc / LonelyNumberFilter.php
Last active November 4, 2021 17:15
Filter the numbers that have no pair (Yandex interview)
<?php
class LonelyNumberFilter {
private $numbers;
private $filtered = [];
function __construct($numbers) {
$this->numbers = $numbers;
}
@sagittaracc
sagittaracc / frand_by_sum.php
Created December 3, 2021 21:01
Генерация массива случайных чисел так чтобы их сумма была определенным числом
<?php
function frand($min, $max, $decimals = 5) {
$scale = pow(10, $decimals);
return mt_rand($min * $scale, $max * $scale) / $scale;
}
function frand_by_sum($sum, $length = 2) {
$numbers = [];
@sagittaracc
sagittaracc / observer.js
Created December 24, 2021 13:37
Observer pattern in javascript
class Observer {
constructor (name) {
this.name = name
}
subscribe (event) {
event.addObserver(this)
}
notify (event) {
@sagittaracc
sagittaracc / riverSizes.js
Last active February 17, 2022 07:58
River Sizes | AlgoExpert
/**
* Проверяет выход за границы матрицы при её обходе по рекам
* @param array matrix исходная матрица
* @param int line позиция по горизонтали в матрице
* @param int column позиция по вертикали в матрице
* @return boolean
*/
function isOffMatrix(matrix, line, column) {
return line < 0
|| column < 0
@sagittaracc
sagittaracc / noun_case.php
Last active March 25, 2022 13:57
Склонение существительного в соответствии с числительным (множественный падеж)
<?php
/**
* Возвращает существительное в соответствующей форме по числительному
* @param int $number
* @param array $cases формы существительного
* При склонении по числительному существительное имеет три формы
*
* Пример: слово "программа"
*