Skip to content

Instantly share code, notes, and snippets.

@bryceac
bryceac / count_where_ext.rs
Created June 6, 2021 01:38
CountWhere method for Rust Iterators
/* fn main() {
// test array to make sure things work right
let numbers = [5, 5, 5, 2, 1];
/* use newly created method to do a count elements match a predicate.
Due to elements being borrowed once for the predicate and again for underlying running, parameter in closure must be dereferenced twice. */
println!("{}", numbers.iter().count_where(|n| **n == 1));
@bryceac
bryceac / Int+isPrime.swift
Created June 4, 2020 19:19
Integer extension that helps in determining if number is prime in Swift 5.
import Foundation
// create extension of the Int type
extension Int {
// computed property that grab factors found in number
var factors: [Int] {
// cause property to fail if number is 0.
guard self != 0 else { preconditionFailure("0 has infinite factors.") }
@bryceac
bryceac / String+matching.swift
Created October 23, 2019 17:42
Swift String extension that allows easy retrieval of captured groups via multidimensional array.
import Foundation
extension String {
// function that can easily perfom regex and grab groups
func matching(regexPattern: String) -> [[String]]? {
// attempt to create an NSRegular expression object
guard let regex = try? NSRegularExpression(pattern: regexPattern) else { return nil }
@bryceac
bryceac / multiplicative_persistence.swift
Created July 26, 2019 20:12
functions to calculate multiplicative persistence of numbers in swift
import Foundation
// digits will retrieve the digits in a number
func digits(inNumber number: Int) -> [Int] {
return String(number).compactMap { $0.wholeNumberValue }
}
// product of digits will iterate over every digit in a number and multiply them together
func productOfDigits(_ number: Int) -> Int {
return digits(inNumber: number).reduce(1) { $0*$1 }
@bryceac
bryceac / Float+Double+e.swift
Created July 26, 2019 20:02
Float and double extensions to allow easy retrieval of euler's number (aka. e)
import Foundation
// the following extensions allow the easy retrieval of euler's number as a float or Double, just like pi
extension Double {
// create type constant and set it to the value of e, by passing 1 as an argument to exp function, which is equivalent to e^1.
static let e: Double = exp(1.0)
}
extension Float {
@bryceac
bryceac / powTower.swift
Last active October 1, 2018 03:13
function for Swift to do power towers, due issues of numbers getting so large, it is not going to be able to calculate Graham's Number.
import Foundation // import Foundation, so that pow function can be used
// powTower function takes a number and return the result of a tower with the specified number of copies
func powTower(number: Double, withCopies: Int) -> Double {
var result: Double = number // variable to hold results, with an initial value being the same as the number specified
if (withCopies == 1) {
return number
} else if (withCopies > 1) {
// create a loop that will repeat for less than specified number of copies, so that math is accurate
@bryceac
bryceac / wifi.py
Created June 27, 2018 17:25
script for Python 2.7 that allows easy creation of QR Codes for WFI
# This script requies the python module wifi_qrcode_generator and is written for Python version 2.7.
# instructions to get module can be found at https://github.com/lakhanmankani/wifi_qrcode_generator
import wifi_qrcode_generator
name = raw_input("filename: ") # holds name of file, so there can be some measure of security
ssid = raw_input("SSID: ") # holds network name
while True:
hidden = bool(raw_input("SSID is hidden (press enter if not hidden): ")) # variable hold boolean for if network is hidden
@bryceac
bryceac / gist:5505181
Last active December 16, 2015 22:09
the following sets up YouTube shortcode for Habari. user can type [youtube width="<particular width>" height="<particular height>" video="<specify video>"/] to embed a video.
<?php
class YouTubeHabari extends Plugin
{
private $output;
// the following function will make youtube shortcode work
function filter_shortcode_youtube($content, $code, $attrs, $context)
{
// the following assigns embed code to variable and embeds video specfied by user and at specified dimensions
$this->output = '<iframe width="' . $attrs['width'] . '" height="' . $attrs['height'] . '" src="http://youtube.com/embed/' . $attrs['video'] . '" frameborder="0" allowfullscreen></iframe>';
@bryceac
bryceac / socialink.plugin.php
Created April 30, 2013 01:30
Add Tumblr support to Socialink Habari plugin. Code is based on Stuchl4n3k's script that includes the Google+ plus one button.