Skip to content

Instantly share code, notes, and snippets.

View primaryobjects's full-sized avatar

Kory Becker primaryobjects

View GitHub Profile
@primaryobjects
primaryobjects / buckets.R
Created March 14, 2018 20:26
Breaking a continuous data value into buckets by range in R.
data <- read.csv('https://storage.googleapis.com/mledu-datasets/california_housing_train.csv')
data$lat_bin <- cut(data$latitude, breaks=c(floor(min(data$latitude)):round(max(data$latitude))), include.lowest=T)
data$lat_bin <- as.numeric(data$lat_bin)
divisors <- function(a) {
pairs <- data.frame()
smallest <- NA
for (b in 1:999999) {
# No need to check further, as b*c = c*b.
if (!is.na(smallest) && b >= smallest)
break;
# Find the next factor.
hamming <- function(s1, s2) {
distances <- NA
# Ensure the strings are equal length.
if (nchar(s1) == nchar(s2)) {
# Split the strings into characters.
s1 <- unlist(strsplit(s1, ''))
s2 <- unlist(strsplit(s2, ''))
# Mark characters that do not match.
@primaryobjects
primaryobjects / base62.R
Created March 7, 2018 16:51
[2018-02-20] Challenge #352 [Easy] Making Imgur-style Links: Converting a Number to Base64 https://www.reddit.com/r/dailyprogrammer/comments/7yyt8e/20180220_challenge_352_easy_making_imgurstyle/
base62 <- function(num, n = 62) {
result <- c()
symbols <- unlist(strsplit('0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ', ''))
total <- 0
place <- 0
index <- 0
# Find the most significant place and index that doesn't exceed our number.
while (total < num) {
# Start with the max value for this place.
@primaryobjects
primaryobjects / housing.R
Last active March 6, 2018 19:33
Linear regression on California housing data for median house value. Creation of a synthetic variable. Plotting predictions vs actuals and removing outliers. See also https://colab.research.google.com/notebooks/mlcc/first_steps_with_tensor_flow.ipynb and https://colab.research.google.com/notebooks/mlcc/synthetic_features_and_outliers.ipynb
library(Metrics)
# Load dataset.
data <- read.csv('https://storage.googleapis.com/mledu-datasets/california_housing_train.csv')
# Scale housing_median_value for an easier learning rate.
data$median_house_value_scaled <- data$median_house_value / 1000
# Randomize the dataset.
data <- data[sample(nrow(data)),]
@primaryobjects
primaryobjects / scrollIntoView.js
Last active March 1, 2018 18:27
Scroll an element into view on the table/web page within its parent container, only if it is not currently in view. Demo at https://plnkr.co/edit/2qvJiF8TuUM09XhslcXw?p=preview. See also https://stackoverflow.com/a/37829643
//
// parent is a jQuery element of the parent container.
// target is the DOM element to scroll into view.
//
scrollIntoViewIfNeeded = (parent, target) => {
const rect = target.getBoundingClientRect();
// Check the bottom border to scroll view down.
if (rect.bottom > window.innerHeight) {
target.scrollIntoView(false);
shelfCount <- function(shelves, books) {
totalShelves <- sum(shelves)
totalBooks <- sum(books[,1])
# Check if enough shelf space exists.
if (totalShelves < totalBooks) {
'impossible'
}
else {
# Sort shelf sizes.
https://medium.freecodecamp.org/elegant-patterns-in-modern-javascript-roro-be01e7669cbd
change <- function(target, coins, condition, numCoins) {
# Finds all combinations of coins to equal the target value while using the condition number of coins.
solutions <- c()
for (h in 1:max(numCoins, length(coins))) {
# Start by considering h = 1, 2, 3, ... coins.
combinations <- combn(coins, h)
solutions <- c(solutions, sapply(1:ncol(combinations), function(colIndex) {
combination <- combinations[,colIndex]
total <- sum(combination)
library(ggplot2)
simulate <- function(numMales, numFemales, goal) {
result <- data.frame()
# Create rabbits.
males <- c(0, numMales)
females <- c(0, numFemales)
month <- 0