Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / error.js
Last active June 19, 2021 08:13
Redirect 404 errors to a static web page in node.js Express.
var fs = require('fs');
// In your app.js, include a route handler for all other routes (*) to go to error404.
// app.get('*', error.error404);
exports.error404 = function(req, res) {
if (req.accepts('html')) {
// Respond with html page.
fs.readFile(__dirname + '/../../public/404/index.html', 'utf-8', function(err, page) {
res.writeHead(404, {'Content-Type': 'text/html'});
res.write(page);
@primaryobjects
primaryobjects / funnyPlant.R
Created November 28, 2015 16:56
Funny Plant: Reddit Daily Programmer Challenge #242 2005-11-23
funnyPlant <- function(people, plants) {
fruit <- 0
weeks <- 1
while (fruit < people) {
fruit <- fruit + plants
plants <- plants + fruit
weeks <- weeks + 1
}
@primaryobjects
primaryobjects / abundef.R
Created December 1, 2015 02:30
[2015-11-30] Challenge #243 [Easy] Abundant and Deficient Numbers
#
# https://www.reddit.com/r/dailyprogrammer/comments/3uuhdk/20151130_challenge_243_easy_abundant_and/
#
divisors <- function(x){
# Vector of numberes to test against
y <- seq_len(x)
# Modulo division. If remainder is 0 that number is a divisor of x so return it
y[ x%%y == 0 ]
}
@primaryobjects
primaryobjects / fruitBasket.R
Last active December 6, 2015 02:11
[2015-12-02] Challenge #243 [Intermediate] Jenny's Fruit Basket
# https://www.reddit.com/r/dailyprogrammer/comments/3v4zsf/20151202_challenge_243_intermediate_jennys_fruit/
require(plyr)
data <- read.csv('input2.txt', sep = ' ', header = FALSE, col.names = c('name', 'price'))
money <- 500
orders <- data.frame()
for (i in seq(from=1, to=6)) {
print(paste('Level', i))
@primaryobjects
primaryobjects / Ggg.R
Created December 17, 2015 23:20
[2015-12-16] Challenge #245 [Intermediate] Ggggggg gggg Ggggg-ggggg!
# https://www.reddit.com/r/dailyprogrammer/comments/3x3hqa/20151216_challenge_245_intermediate_ggggggg_gggg/
# Builds a key/value dictionary.
load <- function(input, valuesAsKeys = FALSE) {
# Read first line of input.
key <- unlist(strsplit(input, '\n'))[1]
# Split tokens by space.
keys <- unlist(strsplit(key, ' '))
@primaryobjects
primaryobjects / neuralnet-sqrt.R
Last active June 5, 2019 11:45
A simple neural network in R to predict the square root of numbers.
library(caret)
# Generate 50 random numbers uniformly distributed between 0 and 100.
training <- data.frame(x = runif(50, min=0, max=100))
training$y <- sqrt(training$x)
# Generate some squared numbers.
cv <- data.frame(x = (1:10)^2)
cv$y <- sqrt(cv$x)
@primaryobjects
primaryobjects / 1-nnsort_learning_curve.R
Last active October 3, 2017 17:14
Neural Network Sort, generation of a learning curve.
# See also https://gist.github.com/primaryobjects/3b41f8b2f122eb16a65b
library(neuralnet)
library(ggplot2)
library(reshape2)
# Helper method to generate a training set containing size random numbers (a, b, c) and sorted (x, y, z).
generateSet <- function(size = 100, max = 100) {
# Generate size random numbers between 1 and max.
training <- data.frame(a=sample(1:max, size, replace=TRUE),
b=sample(1:max, size, replace=TRUE),
@primaryobjects
primaryobjects / nnsort_method.R
Last active January 2, 2016 16:24
Neural Network Sort helper method
# See also https://gist.github.com/primaryobjects/5a28e0c27fd433123f1a
# fit: Trained neural network. scaleVal: Original scaled data used in training the network. a, b, c: Numbers to sort.
nnsort <- function(fit, scaleVal, a, b, c) {
numbers <- data.frame(a=a, b=b, c=c, x=0, y=0, z=0)
numbersScaled <- as.data.frame(scale(numbers, attr(scaleVal, 'scaled:center'), attr(scaleVal, 'scaled:scale')))
round(unscale(compute(fit, numbersScaled[,1:3])$net.result, scaleVal))[,4:6]
}
@primaryobjects
primaryobjects / nnsort.R
Last active January 2, 2016 16:34
Neural Network Sort
library(neuralnet)
# Helper method to generate a training set containing size random numbers (a, b, c) and sorted (x, y, z).
generateSet <- function(size = 100, max = 100) {
# Generate size random numbers between 1 and max.
training <- data.frame(a=sample(1:max, size, replace=TRUE),
b=sample(1:max, size, replace=TRUE),
c=sample(1:max, size, replace=TRUE))
# Generate output examples by sorting the numbers.
@primaryobjects
primaryobjects / nnsort_learning_curve_4.R
Last active January 2, 2016 16:56
Neural Network Sort, generation of a learning curve for sorting 4 numbers.
# See also https://gist.github.com/primaryobjects/3b41f8b2f122eb16a65b
library(neuralnet)
library(ggplot2)
library(reshape2)
# Helper method to generate a training set containing size random numbers (a, b, c) and sorted (x, y, z).
generateSet <- function(size = 100, max = 100) {
# Generate size random numbers between 1 and max.
training <- data.frame(a=sample(1:max, size, replace=TRUE),
b=sample(1:max, size, replace=TRUE),