Skip to content

Instantly share code, notes, and snippets.

View danielpowell4's full-sized avatar

Daniel Powell danielpowell4

View GitHub Profile
@danielpowell4
danielpowell4 / price_is_right.rb
Created September 11, 2016 18:51
Price is right calculator in Ruby
# selecting
def choose_best_guess(ary)
ary.select{ |v| v <= t }.max
end
# rejecting
def choose_best_guess(ary)
ary.reject{ |v| v > t }.max
end
@danielpowell4
danielpowell4 / linked_list.rb
Last active September 12, 2016 02:51
Linked List Exercise in Ruby. Handles infinite lists via Floyd Cycle Detection as well as reversing list with and without mutation.
class LinkedListNode
attr_accessor :value, :next_node
def initialize(value, next_node=nil)
@value = value
@next_node = next_node
end
end
def print_list(list_node)
@danielpowell4
danielpowell4 / map.js
Created September 28, 2016 20:45
Uses a callback and loop to implement the map functionality of lodash and ruby in Javascript
"use strict";
var _ = {
// Implements:
// https://lodash.com/docs#map
map: (array, callback) => {
var newArray = []
array.forEach(function(item){
newArray.push(callback(item))
@danielpowell4
danielpowell4 / longestPalindrome.js
Last active October 5, 2016 17:33
Finds length of the longest substring of a string that is a palindrome in Javascript.
// Palindromes are the same forwards as in reverse
// As an example, if the input was “I like racecars that go fast”,
// the largest palindrom substring (racecar)
// would have the length of 7.
var longestPalindrome = function(s){
let biggest = 0;
const stringLength = s.length;
@danielpowell4
danielpowell4 / alphabetShift.js
Created October 21, 2016 06:04
Shifts letters to next letter in alphabet (c -> d, z -> a) and ensures vowels in new string are CAPS in Javascript ES6. Includes tests!
/* -- Problem --
* Have the function alphabetShift(str) take the str parameter
* being passed and modify it using the following algorithm.
* Replace every letter in the string with the letter following it
* in the alphabet (ie. c becomes d, z becomes a). Then capitalize
* every vowel in this new string (a, e, i, o, u) and finally return
* this modified string.
*/
/* -- Examples --
@danielpowell4
danielpowell4 / mergeSort.js
Last active October 21, 2016 06:48
Merge sort algorithm in javascript with mini-test suite. Breaks into pieces and then merges back together. Uncomment inner console logs to see it in action.
/* mergeSort
*
* basic idea is as follows:
* break an array of integrers into pieces recursively
* and merges it back together using its sorted pieces
*
* this implementation uses "take from the front put in
* the back" logic while merging
*
* find diagram at http://quiz.geeksforgeeks.org/merge-sort/
@danielpowell4
danielpowell4 / linearRegression.js
Last active November 24, 2016 05:25
Linear Regression in simple structure. Accepts a data object with x + y attributes. Returns a y = mx + b style line with an r squared value. Great for D3
const linearRegression = (data, y_attr, x_attr) => {
//separate data into x and y sets
let y_data = [];
let x_data = [];
for (let i = 0; i < data.length; i++) {
y_data.push(data[i][y_attr]);
x_data.push(data[i][x_attr]);
@danielpowell4
danielpowell4 / pre-commit-rubocop
Created February 2, 2017 19:16
Rubocop precommit git hook for ruby apps that need cleanup as new features are coming along
# Place in your project here: .git/hooks/pre-commit
# Runs all cops on newly added files
# Runs standard cops on files that have been modified
#!/usr/bin/env ruby
require 'rubocop'
def get_files(selector)
@danielpowell4
danielpowell4 / paper_clip_extensions.rb
Created March 2, 2017 19:32
Verbose paperclip file extensions Basic: csv, pdf, txt | Microsoft: doc, docx, ppt, pptx, xls, xlsx | Images: gif, jpg, jpeg, png | Apple: pages, numbers
# If you want to add to your list, you can sniff using a binding.pry
# Look at the ActionDispatch::Http::UploadedFile object's Content-Type in the @header variable
validates_attachment :attachment,
allow_blank: false,
content_type: { content_type: ['image/jpg', #image
'image/jpeg', #image
'image/gif', #image
'image/png', #image
'application/pdf', #pdf
@danielpowell4
danielpowell4 / query_to_csv.rb
Created March 9, 2017 22:16
This ruby script runs a SQL query and writes it as a CSV in the tmp folder using ruby's CSV library in a Ruby on Rails app
# run from console with `rails runner /path/to/this/file.rb`
# a timestamped file is placed in a Rails' app's tmp folder
require 'csv'
def file_path(name_of_file)
Rails.root.join('tmp', "#{name_of_file}_#{sanitize_time(Time.zone.now)}.csv") # don't edit name set on line 22
end
def sanitize_time(time)