Skip to content

Instantly share code, notes, and snippets.

View rfprod's full-sized avatar
☯️

Vadim rfprod

☯️
View GitHub Profile
@rfprod
rfprod / ExtractRangesFromSortedIntegersList.markdown
Last active April 22, 2017 15:40
Extract Ranges from Sorted Integers List

Extract Ranges from Sorted Integers List

The function ranges(list) takes a sorted array of integers as input [-6, -3, -2, -1, 0, 1, 3, 4, 5, 7, 8, 9, 10, 11, 14, 15, 17, 18, 19, 20] and returns a string with ranges for the provided input "-6,-3-1,3-5,7-11,14,15,17-20". A range must consist of three item, i.e. [1, 2] is not considered a range. A range is formatted as A-B, e.g.: A = -7, B = -1, range is -7--1.

A script by V.

License.

@rfprod
rfprod / CircleAndSquareRasterizer.markdown
Created February 23, 2017 14:24
Circle and Square Rasterizer

Circle and Square Rasterizer

Function rasterizeCircleAndSquare(w, h, circleX, circleY, r, x1, y1, x3, y3) rasterizes a circle and a square.

It accepts parameters:

  • w - canvas width
  • h - canvas height
  • circleX, circleY - coordinates of the circle's center
  • r - circle radius
  • x1, y1, x3, y3 - coordinates of the opposite corners of the square
@rfprod
rfprod / DistanceBetweenGPSPoints.markdown
Last active December 30, 2017 22:24
Distance Between GPS Points

Distance Between GPS Points

Function getDistance(coord1, coord2) returns distance between GPS coordinates in kilometers.

Coordinates should be provided in format X-XX° X-XX′ X-XX″ (N|S), X-XX° X-XX′ X-XX″ (E|W).

To calculate distance between GPS coordinates in DD format, first convert it to DMS by using a function ddtoDms(lat, lng) and provide result for getDistance function, i.e. getDistance(ddToDms(lat1, lng1), ddToDms(lat2, lng2))

Scripts by V.

@rfprod
rfprod / Number2Words.markdown
Created January 27, 2017 11:14
Number 2 Words

Number 2 Words

Function number2words(n) returns number represented as a string of words.

Example: number2words(888888) returns "eight hundred eighty-eight thousand eight hundred eighty-eight"

Scripts by V.

License.

@rfprod
rfprod / Lists-Utils.markdown
Last active February 3, 2017 16:58
Lists utils

Lists utils

list-methods.js

Class nodeList has a static method fromArray(array) that generates an algebraic list from provided array.

Class nodeList instance has methods:

  • toArray() that generates an array from an algebraic list
  • filter(predicate) that returns a new algebraic list where list nodes' head values satisfy provided predicate function
  • map(mapper) that returns a new algebraic list with mapper function applied to each list nodes' head
@rfprod
rfprod / Primes.markdown
Last active January 24, 2017 12:20
Primes utils

Primes utils

prime-generator.js

Function isPrime(number) checks whether provided number is prime or not.

Function getPrimes(start, finish) generates prime numbers sequence in provided range. Range can be provided in wrong sequence, i.e. getPrimes(20,0).

prime-factors-decomposition.js

@rfprod
rfprod / KeywordCipher.markdown
Last active April 22, 2017 15:40
Keyword Cipher

Keyword Cipher

Methods encode(str)/decode(str) of the keywordCipher instance encode / decode input string based on previously provided alphabet and key.

Class keywordCipher operates as follows:

  1. on instantiation second alphabet is generated based on provided alphabet and keyword
  2. to form a second alphabet:

2.1 all characters present in keyword are removed from provided alphabet

@rfprod
rfprod / KamaSutraCipher.markdown
Last active April 22, 2017 15:40
KamaSutra Cipher

KamaSutra Cipher

Function KamaSutraCipher encodes / decodes input string based on previously provided key pairs dictionary.

It replaces a character in string if it is present in key array and leaves it as is if it is not.

Example key pairs dictionary: `[ ['d', 'p'], ['n', 'o'],

@rfprod
rfprod / DeepObjectsCompare.markdown
Last active January 28, 2020 11:25
Deep Objects Compare

Deep Objects Compare

Function deepObjectsCompare compares two objects provided as the function arguments.

Arguments can be of the following types: null, undefined, string, number, object, array.

Arguments may have nested objects. Arguments examples:

  • deepObjectsCompare(1, 1)
  • deepObjectsCompare([1, 2], [1, 2]);
  • deepObjectsCompare({ one: 'two' }, {one: 'two' });
@rfprod
rfprod / BattleshipFieldValidator.markdown
Last active April 22, 2017 15:41
Battleship Field Validator

Battleship Field Validator

Function validateBattleshipField validates battleships placement on the Battleship (game) field.

There must be:

  • a single battleship - size 4
  • 2 cruisers - size 3
  • 3 destroyers - size 2
  • 4 submarines - size 1