Skip to content

Instantly share code, notes, and snippets.

View lingoslinger's full-sized avatar

Allan Evans lingoslinger

  • Chicago USA
View GitHub Profile
@lingoslinger
lingoslinger / NumericCharacterCount.swift
Created April 1, 2024 23:51
Given an array of integers: find the first value with the largest character count when spelled out
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// use this in an Xcode playground or the code challenge repl of choice
// Given an array of integers: find the first value with the largest character count when spelled out
// format the result like this example: "one hundred seventy-eight (178)
import UIKit
let numberFormatter = NumberFormatter()
numberFormatter.numberStyle = .spellOut // TIL this exists...
@lingoslinger
lingoslinger / CheckSOSLetters.swift
Last active October 31, 2023 21:44
Check SOS Letters
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// in a string of what should be repeating instances of "SOS", detect any errors in the SOS sequence and report the total
// number of errors
func checkSOSLettere(_ message: String) -> Int {
var totalErrors = 0
var currentLetterNumber = 1
for char in message {
@lingoslinger
lingoslinger / MergeArraysAndSort.swift
Last active September 15, 2023 23:41
Swift: Merge two sorted arrays into a single sorted array
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// the problame did not say what kind of elements are in these arrays, so time to get generic!
// assume both arrays contain the same type of elements
func mergeArraysAndSort<T: Comparable>(_ array1: [T], _ array2: [T]) -> [T] {
let merged = array1 + array2
return merged.sorted()
}
@lingoslinger
lingoslinger / Seconds.swift
Last active September 15, 2023 23:40
Swift: Find second smallest and second largest elements in an array of integers
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// Implement a function in Swift that finds the second smallest and second largest elements in an array of integers.
// this is a trick question designed to make you waste time if you haven't memorized Foundation...
func seconds(_ numbers: [Int]) -> (Int?, Int?){
let sorted = numbers.sorted()
return (sorted.dropFirst().first, sorted.dropLast().last )
@lingoslinger
lingoslinger / Palindrome.swift
Last active September 15, 2023 23:40
Swift: Palindrome with bonus reversed() function
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// Write a function in Swift that checks if a given string is a palindrome (reads the same forwards and backwards),
// ignoring whitespace and punctuation.
func palindrome(_ input: String) -> Bool {
return input == String(input.reversed())
}
@lingoslinger
lingoslinger / Frequency.swift
Last active September 15, 2023 23:39
Swift: Most frequently occurring character in a string
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
// Write a function in Swift that takes in a string as input and returns the most frequently occurring character in the string.
// If multiple characters have the same highest frequency, return any one of them.
func frequency(_ input: String) -> Character {
let charFrequency = input.reduce(into: [:]) {counts, char in
counts[char, default: 0] += 1
}
@lingoslinger
lingoslinger / EvenValues.swift
Last active September 15, 2023 23:38
Swift: Return the product of the even values in a dictionary where the values are inside of strings
// I publicly share any answers to "coding challenges" I receive as a part of the interview process
// copy/paste this into an Xcode playground or the code challenge repl of choice
let values = [5:"4", 6:"5", 2:"6", nil:nil, 4:"5", 3:"apple"]
func evenProduct(_ numbers: [Int?: String?]) -> Int {
return Array(numbers.values)
.compactMap{$0} // remove nil values and unwrap optionals
.compactMap{Int($0)} // remove strings that return nil when converted to an Int, leaving an array of Ints
.filter{$0 % 2 == 0} // even numbers
@lingoslinger
lingoslinger / parallaxwebservice.ino
Last active December 10, 2015 05:08
Modification of https://gist.github.com/4112791 by @cieslak for use with the Parallax 16x2 LCD display with backlight and piezo speaker. Code in the repeat loop is all I needed to change. See orginal gist for more info.
#include "SPI.h"
#include "Ethernet.h"
#include "SoftwareSerial.h"
#include <WebServer.h>
static uint8_t mac[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; // change this to your Ethernet shield's MAC address
static uint8_t ip[] = { 0, 0, 0, 0 }; // change to your network
#define PREFIX ""
WebServer webserver(PREFIX, 80);