Last active
December 20, 2015 12:39
-
-
Save cronin101/6132983 to your computer and use it in GitHub Desktop.
Graph plotting code example.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env ruby | |
| require 'asymptotic' | |
| class Array | |
| def custom_uniq_1 | |
| new_array = [] | |
| each do |elem| # you call each method on self here | |
| new_array << elem unless new_array.include?(elem) | |
| end | |
| new_array | |
| end | |
| def custom_uniq_2 | |
| seen = Hash.new | |
| reject do |item| | |
| item_is_duplicate = seen[item] # Check if item has occured already | |
| seen[item] = true unless item_is_duplicate # Remember seeing new item | |
| item_is_duplicate # Reject the item from returned array if duplicate | |
| end | |
| end | |
| end | |
| defaults = { | |
| input_seeds: (1..10), | |
| input_function: ->(pow){ (1..2**pow).map { rand(2**pow) } } | |
| } | |
| Asymptotic::Graph.plot 10, "Array#uniq Replacements", | |
| "Checking if a new array includes an element already before adding it" => { | |
| function: ->(array){ array.custom_uniq_1 } | |
| }.merge(defaults), | |
| "Checking a hash flag and rejecting if the flag is set" => { | |
| function: ->(array){ array.custom_uniq_2 } | |
| }.merge(defaults), | |
| "Array#uniq" => { | |
| function: ->(array){ array.uniq } | |
| }.merge(defaults) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment