Skip to content

Instantly share code, notes, and snippets.

View mredig's full-sized avatar
🤓
1s and 0s aren't going to put themselves in the right order.

Michael mredig

🤓
1s and 0s aren't going to put themselves in the right order.
View GitHub Profile
#!/usr/bin/perl
&getStatsFromNames;
&createSourceString;
&printStats;
$count = $ARGV[0];
if($count eq "")
{
$count = 5;
@mredig
mredig / vowelCount.swift
Created April 24, 2019 15:17
vowel counter
/**
This will count and return the number of vowels in a string. Vowels are defined as "a, e, i, o, and u" and sometimes "y"!
("y" is only counted when there's no other vowel in a word. A word is defined as a string of characters between spaces).
*/
func numberOfVowels(in string: String) -> Int {
let vowels: Set<Character> = ["a", "e", "i", "o", "u"]
let string = string.lowercased()
let words = string.split(separator: " ")
@mredig
mredig / anotherContains.swift
Created May 1, 2019 15:40
swift string contains without contains
import Cocoa
extension String {
func anotherContains(_ string: String) -> Bool {
let lcSelf = self.lowercased()
let lc = string.lowercased()
return lcSelf.range(of: lc) != nil
import Foundation
extension String {
func isAPalindrome() -> Bool {
var string = self.lowercased()
let characterSet = Set("abcdefghijklmnopqrstuvwxyz")
string = string.filter { characterSet.contains($0) }
return string == String(string.reversed())
}
@mredig
mredig / Int Components
Created May 15, 2019 15:36
separates an Int into each digit (signs negatives)
import Cocoa
extension Int {
func expandNumber() -> [Int] {
var input = self
var signed = false
if input < 0 {
input *= -1
signed = true
}
import Foundation
func AND(_ a: Bool, _ b: Bool) -> Bool {
if a == true {
if b == true {
return true
}
}
return false
}
import Cocoa
extension Int {
static func anotherRandom(_ range: Range<Int>) -> Int {
let floor = range.lowerBound
guard var ceiling = range.last else { fatalError("No last value in range") }
ceiling += 1
let normalCeiling = ceiling - floor
let rando = Int(arc4random_uniform(UInt32(normalCeiling)))
return rando + floor
@mredig
mredig / Roman numerals.swift
Created June 5, 2019 15:52
roman numerals - it's ugly but it works
import Foundation
extension Int {
subscript(digitIndex: Int) -> Int? {
print(self)
var decimalBase = 1
for _ in 0..<digitIndex {
decimalBase *= 10
}
@mredig
mredig / Is Twin Prime.swift
Last active June 12, 2019 15:36
checks to see if a number is a twin prime
// a twin prime is where a prime number +/- 2 is another prime number. For example, 3 is prime, and so is 5. They are twin primes. (Since 7 is 2 away from 5 as well, maybe that makes them triplets! But I'm not a mathematician. I'll let them decide.)
extension FixedWidthInteger {
func isPrime() -> Bool {
// negatives, 0, and 1 are all not prime and will break the range created below
guard self >= 2 else { return false }
// no even numbers are prime, except 2
guard !self.isMultiple(of: 2) else {
return self == 2 ? true : false
@mredig
mredig / sameNumberOfBinaryOnes.swift
Last active June 19, 2019 15:48
takes any positive integer as an argument and returns the next highest and next lowest integers having the same number of 1s as the passed argument's binary representation
import Foundation
extension FixedWidthInteger {
func sameNumberOfBinaryOnes() -> (Self?, Self?) {
guard self != 0 else { return (nil,nil) }
let myCount = binaryOneCount()
var nextHigher: Self?
for maybe in (self + 1)...Self.max {