This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//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"] |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
* @param {character[][]} grid | |
* @return {number} | |
*/ | |
var dfs = function(grid, i, j) { | |
const w = grid.length; | |
const h = grid[0].length; | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// //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++) { |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function ListNode(val) { | |
this.val = val; | |
this.next = null; | |
} | |
/** | |
* @param {ListNode} l1 | |
* @param {ListNode} l2 | |
* @return {ListNode} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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]; | |
// } | |
// } | |
// } | |
// }; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//: 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 |