Skip to content

Instantly share code, notes, and snippets.

Avatar

Jon Bash jonbash

View GitHub Profile
View CodeChallengeTestCases.swift
struct CodeChallengeTestCases<Input, Output> {
var title: String?
var expected: KeyValuePairs<Input, Output>
var solution: (Input) -> Output
init(
title: String? = nil,
expected: KeyValuePairs<Input, Output> = [:],
solution: @escaping (Input) -> Output)
{
@jonbash
jonbash / CommonChars.Swift
Created Jan 29, 2020
Unit 4 Sprint 3 - Swift Code Challenge
View CommonChars.Swift
func commonChars(_ inputs: [String]) -> [String] {
guard let lastString = inputs.last else { return [] }
var commonCountedChars = CountedSet<String>()
for char in lastString {
commonCountedChars.add(String(char))
}
var strings = inputs
strings.removeLast()
// removing last is more computationally efficient than removing first in Swift
@jonbash
jonbash / reverseOnlyLetters.swift
Created Jan 15, 2020
iOS11 Code Challenge (2020-01-15) - Reverse Only Letters
View reverseOnlyLetters.swift
func reverseOnlyLetters(_ inputString: String) -> String {
let lettersReversed = [Character](
inputString.compactMap { char -> Character? in
return char.isLetter ? char : nil
}.reversed()
)
var outputCharacters = [Character](inputString)
var i = 0
while i < lettersReversed.count {
@jonbash
jonbash / FlipMatrix.swift
Last active Dec 18, 2019
Flip Matrix (Lambda School iOS11 Code Challenge)
View FlipMatrix.swift
/*
Given a matrix A, return the transpose of A.
The transpose of a matrix is the matrix flipped over it's main diagonal,
switching the row and column indices of the matrix.
Example 1:
Input: [[1,2,3],[4,5,6],[7,8,9]]
Output: [[1,4,7],[2,5,8],[3,6,9]]
Example 2:
View FindMissingNumbers.swift
func findMissingNumbers(_ nums: [Int]) -> [Int] {
var includedNums = [Int:Int]()
var missingNums = [Int]()
for num in nums {
includedNums[num] = num
}
for i in 1...nums.count {
if includedNums[i] == nil {
@jonbash
jonbash / Sum.swift
Created Nov 13, 2019
u2s3-codingChallenge - Sum
View Sum.swift
func indicesToSum(from array: [Int], to target: Int) -> [Int] {
for i in 1 ..< array.count {
for j in 0 ..< i {
if array[i] + array[j] == target { return [j,i] }
}
}
print("ERROR: No sum possible!")
return []
}
@jonbash
jonbash / CGExtensions.swift
Last active Aug 21, 2020
CGContext + Add Curved Polygon (+ vector helper methods) — Core Graphics extensions
View CGExtensions.swift
//
// CGExtensions.swift
//
// Created by Jon Bash on 2019-11-06.
// Copyright © 2019 Jon Bash. All rights reserved.
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to
// deal in the Software without restriction, including without limitation the
// rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
@jonbash
jonbash / FizzBuzz.swift
Created Nov 6, 2019
Coding Challenge U2S2 (2019-11-06)
View FizzBuzz.swift
func fizzBuzz(_ n: Int) -> [String] {
if n < 1 {
return []
}
var output = [String]()
for i in 1...n {
var stringForI: String = ""
if i % 3 == 0 {
stringForI += "Fizz"
@jonbash
jonbash / FizzBuzz.swift
Created Nov 6, 2019
Coding Challenge U2S2 (2019-11-06)
View FizzBuzz.swift
func fizzBuzz(_ n: Int) -> [String] {
if n < 1 {
return []
}
var output = [String]()
for i in 1...n {
var stringForI: String = ""
if i % 3 == 0 {
stringForI += "Fizz"
@jonbash
jonbash / u2s1-codingChallenge.swift
Last active Nov 8, 2019
Lambda School Unit 2 Sprint 1 Coding Challenge
View u2s1-codingChallenge.swift
import Foundation
/*
Given two arrays arr1 and arr2, the elements of arr2 are distinct, and all elements in arr2 are also in arr1.
Sort the elements of arr1 such that the relative ordering of items in arr1 are the same as in arr2. Elements that don't appear in arr2 should be placed at the end of arr1 in ascending order.
Example 1:
You can’t perform that action at this time.