Skip to content

Instantly share code, notes, and snippets.

View tombaranowicz's full-sized avatar

Tomasz Baranowicz tombaranowicz

View GitHub Profile
@tombaranowicz
tombaranowicz / SwiftCollectionsArray.swift
Created December 19, 2019 12:22
Swift Collections - Array
import Foundation
//CREATE
//declaration using type annotation syntax (arrays are strongly typed)
let stringArray : Array<String>
//initialiser syntax uses type inference mechanism
var intArray = Array<Int>()
let shorterIntArray = [Int]()
@tombaranowicz
tombaranowicz / SwiftCollectionsSet.swift
Created December 12, 2019 18:19
Swift Collections - Set
//SETS
//CREATE
//immutable - using array literal
let mazdas: Set = ["2", "3", "3", "6", "CX-3", "CX-5", "CX-9", "MX-5", "CX-30"]
let oldMazdas: Set<String>
oldMazdas = ["323", "626"]
@tombaranowicz
tombaranowicz / SwiftCollectionsDictionary.swift
Created December 6, 2019 08:54
Swift Collections - Dictionary
//DICTIONARIES
//1. Create
//IMMUTABLE (size and contents cannot be changed)
let immutableResponseMessages = [200: "OK",
403: "Access forbidden",
404: "File not found",
500: "Internal server error"]
@tombaranowicz
tombaranowicz / numberOfIslands.js
Created October 12, 2019 11:28
Google Coding Interviews Number of Islands (LeetCode) and explanation. One of Google's most commonly asked interview questions according to LeetCode. This coding interview question is commonly asked by companies like: Google, Facebook, Snapchat, Amazon, Uber, LinkedIn, Microsoft, Apple and many others.
/**
* @param {character[][]} grid
* @return {number}
*/
var dfs = function(grid, i, j) {
const w = grid.length;
const h = grid[0].length;
@tombaranowicz
tombaranowicz / longestPalindromicSubstring.js
Created September 26, 2019 13:13
How To Solve LeetCode Longest Palindromic Substring Problem With JavaScript
// OPTIMIZED
function expandAroundCenterSolution(s) {
let start = 0, end = 0;
for (let i = 0; i < s.length; i++) {
let center = getCenter(s, i);
let bounds = expandAroundCenter(s, center[0], center[1]);
let L = bounds[0], R = bounds[1];
if (R - L > end - start) {
start = L;
end = R;
@tombaranowicz
tombaranowicz / longestSubstringWithoutRepeatingCharacters.js
Created September 13, 2019 13:49
2 ways of solving "Longest Substring Without Repeating Characters" problem in JavaScript
// //BRUTE FORCE METHOD
// var lengthOfLongestSubstring = function(s) {
// let count = 0;
// for (let i=0; i< s.length; i++) {
// let char = s.charAt(i);
// let set = new Set([char]);
// for (let j=i+1; j<s.length; j++) {
@tombaranowicz
tombaranowicz / addTwoNumbers.js
Created August 16, 2019 09:10
"Add Two Numbers" leetcode coding interview problem & solution
function ListNode(val) {
this.val = val;
this.next = null;
}
/**
* @param {ListNode} l1
* @param {ListNode} l2
* @return {ListNode}
@tombaranowicz
tombaranowicz / twoSum.js
Created August 7, 2019 20:04
2 ways of solving "Two Sum" coding interview problem in JavaScript.
// var twoSum = function(nums, target) {
// for(index = 0; index < nums.length - 1; index++) {
// for(i = index+1; i< nums.length; i++) {
// let sum = nums[index] + nums[i];
// if (sum == target) {
// return [index, i];
// }
// }
// }
// };
@tombaranowicz
tombaranowicz / GeneticAlgorithm.swift
Created May 10, 2017 10:29
Simple Starter for experiments with Genetic Algorithms in Swift
//: Simple Genetic Algorithm Starter in Swift 3
import UIKit
import Foundation
let AVAILABLE_GENES:[Int] = Array(1...100)
let DNA_LENGTH = 6
let TOURNAMENT_SIZE = 5
let MAX_GENERATIONS_COUNT = 100