Skip to content

Instantly share code, notes, and snippets.

View ynechaev's full-sized avatar
🏠
Working from home

Yuri ynechaev

🏠
Working from home
View GitHub Profile
@ynechaev
ynechaev / MagicSquare.swift
Created November 20, 2023 00:09
Magic square diff - Swift
/*
We define a magic square to be an `n x n` matrix of distinct positive integers from `1` to `n^2` where the sum of any row, column, or diagonal of length `n` is always equal to the same number: the magic constant.
You will be given a `3x3` matrix `s` of integers in the inclusive range `[1,9]`. We can convert any digit to any other digit `b` in the range `[1,9]` at cost of `|a-b|`. Given `s`, convert it into a magic square at minimal cost. Return this minimal cost.
Note: The resulting magic square must contain distinct integers in the inclusive range `[1,9]`.
Example: $s = [[5, 3, 4], [1, 5, 8], [6, 4, 2]]
The matrix looks like this:
5 3 4
@ynechaev
ynechaev / sherlock.swift
Created November 15, 2023 18:01
Sherlock anagrams - Swift solution
// Given a string return a number of all possible anagrams
func sherlockAndAnagrams(s: String) -> Int {
var anagramCount = 0
var frequencyMap = [String: Int]()
// Iterate through all possible substrings
for i in 0..<s.count {
for j in i+1..<s.count + 1 {
let substring = String(s[s.index(s.startIndex, offsetBy: i)..<s.index(s.startIndex, offsetBy: j)])
@ynechaev
ynechaev / CoinChange.swift
Last active November 14, 2023 23:39
The Coin Change Problem - Swift solution (Dynamic programming)
/*
Given an amount and the denominations of coins available, determine how many ways change can be made for amount.
There is a limitless supply of each coin type.
Example
n = 3
c = [8,3,1,2]
There are 3 ways to make change for n=3: {1,1,1}, {1,2}, and {3}.
*/
@ynechaev
ynechaev / index.vue
Created March 13, 2021 19:27
Subdomain index vue test page
<template>
<div>
<h1>subdomain host: {{ this.host }}</h1>
</div>
</template>
<script>
export default {
async asyncData({ req }) {
if (process.server) {
@ynechaev
ynechaev / nuxt.config.js
Created March 13, 2021 18:43
Nuxt config file with router module
export default {
buildModules: [
['@nuxtjs/router', { keepDefaultRouter: true }]
],
}
@ynechaev
ynechaev / router.js
Created March 13, 2021 18:42
Nuxt.js subdomains router
import Router from 'vue-router'
export function createRouter(ssrContext, createDefaultRouter, routerOptions) {
const options = routerOptions || createDefaultRouter(ssrContext).options
let routesDirectory = null
if (process.server && ssrContext && ssrContext.nuxt && ssrContext.req) {
const req = ssrContext.req
@ynechaev
ynechaev / implementation.m
Created April 4, 2016 12:58
GCD group dispatch and wait
dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
dispatch_group_t group = dispatch_group_create();
dispatch_group_enter(group); // pair 1 enter
[self computeInBackground:1 completion:^{
NSLog(@"1 done");
dispatch_group_leave(group); // pair 1 leave
}];
dispatch_group_enter(group); // pair 2 enter
for i in `hg branches |grep -v 'default' |grep -v 'develop' |grep inactive |awk 'NF>1{print $(NF-1)}'`; do hg update -r "$i" && hg commit -m "Closed branch" --close-branch; done
@ynechaev
ynechaev / gist:8123997
Last active June 9, 2017 23:18
UITableViewCell popular animation (flipping around Y axis, depending on scroll direction)
@interface YourViewController : UIViewController <UITableViewDelegate, UITableViewDataSource> {
UIImageOrientation scrollOrientation;
CGPoint lastPos;
}
- (void) tableView:(UITableView *)tableView willDisplayCell:(UITableViewCell *)cell forRowAtIndexPath:(NSIndexPath *)indexPath {
if (tableView.isDragging) {
UIView *myView = cell.contentView;
CALayer *layer = myView.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
@ynechaev
ynechaev / gist:8123871
Created December 25, 2013 14:48
UIView 3d transform animation style: horizontal flip
UIView *myView = cell.containerView;
CALayer *layer = myView.layer;
CATransform3D rotationAndPerspectiveTransform = CATransform3DIdentity;
rotationAndPerspectiveTransform.m34 = 1.0 / -1000;
rotationAndPerspectiveTransform = CATransform3DRotate(rotationAndPerspectiveTransform, M_PI / 0.3, 0.0f, 1.0f, 0.0f);
layer.transform = rotationAndPerspectiveTransform;
[UIView animateWithDuration:1.0 animations:^{
layer.transform = CATransform3DIdentity;
}];