Skip to content

Instantly share code, notes, and snippets.

View AnthonyBY's full-sized avatar

Anton Marchanka AnthonyBY

View GitHub Profile
@AnthonyBY
AnthonyBY / Objective-C HackerRank snippets
Last active August 30, 2016 15:24
Objective-C templates (snippets) for solve problems on HackerRank
// Example of using code for HackerRanks
// Input and Output array and methods
#import <Foundation/Foundation.h>
@interface Solution : NSObject
+ (NSArray *)calculateDeltaEncoding:(NSArray *)numbers;
@end
@AnthonyBY
AnthonyBY / Swift HackerRank (with Swift 3.0 update)
Last active February 22, 2017 15:04
snippets for HackerRank on swift
//Read 2D Array of Int 5x5
var arr : [[Int]] = Array(repeating: Array(repeating: 0, count: 6), count: 6)
for i in 0...5 {
arr[i] = readLine()!.characters.split(separator: " ").map{ Int(String($0))! }
}
//Read Number
let mealCost = Double(readLine()!)!
//Read Int
let tipPercent = Int(readLine()!)!
@AnthonyBY
AnthonyBY / gist:3e9e52620fa2f31dcaf2caf696b0dba3
Created February 22, 2017 15:46
Arrays: Left Rotation Swift 3.1 Cracking the Coding Interview
// MARK : This is not very elegant solution, but working with input/output is really not nice on Swift 3.1
// so this just an example for working with input Sample Input like:
// 5 4
// 1 2 3 4 5
// in Arrays: Left Rotation (
import Foundation
func rotate(arr: [Int], offset: Int) -> [Int] {
var newArray = [Int]()
@AnthonyBY
AnthonyBY / gist:1a81c8e2b98d381f06e074cbb7986841
Created February 23, 2017 17:54
Strings: Making Anagrams Swift 3.1 Cracking the Coding Interview
import Foundation
var string1 = String(readLine()!)!
var string2 = String(readLine()!)!
var count = string1.characters.count + string2.characters.count
var tempString1 = string1
var tempString2 = string2
for character in string1.characters {
@AnthonyBY
AnthonyBY / gist:9014c2b867dacfb5c5b4d4d68078e1af
Created March 1, 2017 15:22
HackerRank: Sorting: Bubble Sort (Swift 3)
import Foundation
// read the integer n
let n = Int(readLine()!)!
// read the array
var arr = readLine()!.components(separatedBy: " ").map{ Int($0)! }
var swapCount = 0
@AnthonyBY
AnthonyBY / Fibonacci.txt
Last active December 29, 2022 11:44
Recursion: Fibonacci Numbers (Swift 3)
import Foundation
func fibonacci (n: Int) -> Int {
// Write your code here.
if (n == 0 || n == 1) {
return 1;
}
return fibonacci(n: n-1) + fibonacci(n: n-2)
}
@AnthonyBY
AnthonyBY / SwiftRegularExpressionExample.txt
Last active November 4, 2017 16:28
Valid PAN format. Swift 3. HackerRank
import Foundation
// read the integer n
let n = Int(readLine()!)!
var arr = Array(repeating: "", count: n)
for index in 0...n-1 {
arr[index] = String(readLine()!)
}
@AnthonyBY
AnthonyBY / BinarySearch.txt
Last active November 4, 2017 16:26
Day 22: Binary Search Trees (Swift 3.1)
// Start of Node class
class Node {
var data: Int
var left: Node?
var right: Node?
init(d : Int) {
data = d
}
} // End of Node class
@AnthonyBY
AnthonyBY / Find max sub array. Naive, Kadane, Divide and Conquer.txt
Last active November 4, 2017 16:29
Swift 4: Find max sub array. Naive, Kadane, Divide and Conquer
//
// main.swift
// MaxSubArray
//
// Created by Anthony Marchenko on 10/3/17.
// Copyright © 2017 Anthony Marchenko. All rights reserved.
//
import Foundation
@AnthonyBY
AnthonyBY / InversionCount
Last active October 7, 2017 16:28
Swift 4: Inversion Counting Divide and Concur Algorithms (based on MergeSort)
//
// main.swift
// CountInversionWithMergeSort
//
// Created by Anthony Marchenko on 10/3/17.
// Copyright © 2017 Anthony Marchenko. All rights reserved.
//
import Foundation