Skip to content

Instantly share code, notes, and snippets.

View dudo's full-sized avatar
🤙

Brett Dudo dudo

🤙
View GitHub Profile
const audio = new AudioContext(),
sound = audio.createMediaStreamSource(stream),
analyser = audio.createAnalyser();
analyser.minDecibels = -60;
// microphone -> filter -> destination.
sound.connect(analyser);
analyser.connect(audio.destination);
const frequencyData = new Uint8Array(analyser.frequencyBinCount).slice(0, 32),
spectrum = document.querySelector('#spectrum');
# Given n points on a 2D plane, find the maximum number of points that lie on the same straight line.
# y = mx + b (Equation of a straight line)
class Plot
attr_accessor :coordinates
def initialize(points=[])
@coordinates = points
class PascalTriangle
attr_accessor :height
def initialize(height)
@height = height.to_i
end
def self.next_row(current_row)
([0] + current_row).zip(current_row + [0]).map{ |a| a.inject(:+) }
class PostFixCalculator
attr_accessor :list, :original_list
def initialize(list)
@original_list = list
@list = list.split(' ')
end
def evaluation
@dudo
dudo / tags.rb
Last active January 19, 2018 22:06
class items
has_many :tags, through: :item_tags
has_many :item_tags
belongs_to :city
end
class cities
has_many_ :items
end
# frozen_string_literal: true
class CsvSerializer
attr_accessor :data
def initialize(serializer)
@serializer = serializer
@data = serializer&.serializable_hash&.dig(:data) || []
end
@dudo
dudo / app-constraints-api_constraint.rb
Last active September 15, 2019 23:59
rails api versioned
class ApiConstraint
def initialize(options)
@version = options[:version]
@default = options[:default]
end
def matches?(request)
@default || request.headers.fetch(:accept).include?("version=#{version}")
end
end
module FastJsonapi
module ErrorSerializer
extend ActiveSupport::Concern
included do
include FastJsonapi::ObjectSerializer
attr_accessor :with_root_key
set_id :title
class String
def encrypt(k)
lower_alphabet = [*'a'..'z']
lower_translation_map = lower_alphabet.zip(lower_alphabet.rotate(k%26)).to_h
upper_alphabet = [*'A'..'Z']
upper_translation_map = upper_alphabet.zip(upper_alphabet.rotate(k%26)).to_h
translation_map = lower_translation_map.merge(upper_translation_map)
chars.map { |char| translation_map[char] || char }.join
end
# Return a boolean indicating whether the number is a palindrome. Raises a
# TypeError if the argument is not an integer
#
# Caveat: Try to accomplish this without converting the number to a string.
# The goal is to understand your approach to a problem with constraints
# rather than seeing how quickly you can jump to an easy solution.
#
# Example
# palindrome?(1001) => true
# palindrome?(1234) => false