Skip to content

Instantly share code, notes, and snippets.

@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 / 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 / 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 / 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 / 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 / !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],
<?php
/**
* Mehen games test
*
* @author sagittaracc <[email protected]>
*/
// Массив имеет произвольные значения
$array = [-1, 2, 10, 3, -5, 3];
@sagittaracc
sagittaracc / !index.php
Last active June 29, 2021 12:35
Remove islands (Google interview)
<?php
require 'Map.php';
$map = new Map([
[1, 1, 0, 0, 0, 0, 0, 1],
[0, 1, 0, 0, 1, 1, 1, 0],
[1, 0, 1, 0, 0, 0, 0, 0],
[1, 0, 0, 1, 1, 0, 1, 1],
[0, 0, 1, 1, 1, 0, 0, 1],
@sagittaracc
sagittaracc / translit.php
Last active June 10, 2021 07:03
Транслитерировать все имена файлов в указанной директории
<?php
function translit_folder($folder)
{
$converter = array(
'а' => 'a', 'б' => 'b', 'в' => 'v', 'г' => 'g', 'д' => 'd',
'е' => 'e', 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', 'и' => 'i',
'й' => 'y', 'к' => 'k', 'л' => 'l', 'м' => 'm', 'н' => 'n',
'о' => 'o', 'п' => 'p', 'р' => 'r', 'с' => 's', 'т' => 't',
'у' => 'u', 'ф' => 'f', 'х' => 'h', 'ц' => 'c', 'ч' => 'ch',
@sagittaracc
sagittaracc / sag_pack.php
Created May 11, 2021 18:41
Упаковка и распаковка битов
<?php
$a = [0, 1, 1, 1, 0, 1, 0, 1, 1, 0, 1, 1];
function sag_pack($a) {
$sum = 0;
foreach (array_reverse($a) as $index => $value) {
$sum += $value * (1 << $index);
}