Skip to content

Instantly share code, notes, and snippets.

@vanhalt
Last active December 18, 2015 20:59
Show Gist options
  • Save vanhalt/5844145 to your computer and use it in GitHub Desktop.
Save vanhalt/5844145 to your computer and use it in GitHub Desktop.

Revisions

  1. vanhalt revised this gist Aug 29, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion fizz_buzz.rb
    Original file line number Diff line number Diff line change
    @@ -1,6 +1,6 @@
    [*1..100].each do |number|
    text = ""
    text << "fist " if number % 3 == 0
    text << "fizz " if number % 3 == 0
    text << "buzz " if number % 5 == 0
    unless text.empty?
    puts text
  2. vanhalt renamed this gist Aug 29, 2013. 1 changed file with 1 addition and 1 deletion.
    2 changes: 1 addition & 1 deletion first_boost.rb → fizz_buzz.rb
    Original file line number Diff line number Diff line change
    @@ -1,7 +1,7 @@
    [*1..100].each do |number|
    text = ""
    text << "fist " if number % 3 == 0
    text << "boost " if number % 5 == 0
    text << "buzz " if number % 5 == 0
    unless text.empty?
    puts text
    else
  3. vanhalt revised this gist Jul 23, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions first_boost.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    [*1..100].each do |number|
    text = ""
    text << "fist " if number % 3 == 0
    text << "boost " if number % 5 == 0
    unless text.empty?
    puts text
    else
    puts number
    end
    end
  4. vanhalt revised this gist Jun 23, 2013. 1 changed file with 10 additions and 0 deletions.
    10 changes: 10 additions & 0 deletions index_sort.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,10 @@
    class IndexSort
    def self.sort(ary, ascend=true)
    ary_ = []
    ary.each do |num|
    ary_[num] = num
    end
    ary_ = ary_.select { |value| !value.nil? }
    ascend ? ary_ : ary_.reverse
    end
    end
  5. vanhalt revised this gist Jun 23, 2013. 1 changed file with 3 additions and 3 deletions.
    6 changes: 3 additions & 3 deletions linked_list.rb
    Original file line number Diff line number Diff line change
    @@ -29,16 +29,16 @@ def contents
    @hsh.values.collect(&:content)
    end

    def unlinked?(item)
    def deleted?(item)
    item.prev.nil? && item.next.nil?
    end

    def last
    @hsh.each_pair.select { |index, item | !unlinked?(item) && item.next.nil? }.first.last.content
    @hsh.each_pair.select { |index, item | !deleted?(item) && item.next.nil? }.first.last.content
    end

    def first
    @hsh.each_pair.select { |index, item | !unlinked?(item) && item.prev.nil? }.first.last.content
    @hsh.each_pair.select { |index, item | !deleted?(item) && item.prev.nil? }.first.last.content
    end

    private
  6. vanhalt revised this gist Jun 23, 2013. 1 changed file with 15 additions and 4 deletions.
    19 changes: 15 additions & 4 deletions linked_list.rb
    Original file line number Diff line number Diff line change
    @@ -7,10 +7,14 @@ def initialize(content)
    @hsh[0].content = content
    end

    def add(content)
    index = @hsh.length
    @hsh[index-1].next = index if index > 1
    @hsh[index] = ITEM.new(index-1, nil, content)
    def add(element)
    if element.instance_of? LinkedList
    element.contents.each do |value|
    add_content value
    end
    else
    add_content element
    end
    end

    def delete(index)
    @@ -36,4 +40,11 @@ def last
    def first
    @hsh.each_pair.select { |index, item | !unlinked?(item) && item.prev.nil? }.first.last.content
    end

    private
    def add_content(content)
    index = @hsh.length
    @hsh[index-1].next = index if index > 1
    @hsh[index] = ITEM.new(index-1, nil, content)
    end
    end
  7. vanhalt created this gist Jun 23, 2013.
    39 changes: 39 additions & 0 deletions linked_list.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,39 @@
    class LinkedList
    ITEM = Struct.new :prev, :next, :content

    def initialize(content)
    @hsh = {}
    @hsh[0] = ITEM.new(nil, nil, content)
    @hsh[0].content = content
    end

    def add(content)
    index = @hsh.length
    @hsh[index-1].next = index if index > 1
    @hsh[index] = ITEM.new(index-1, nil, content)
    end

    def delete(index)
    item = @hsh[index]
    @hsh[item.prev].next = item.next
    @hsh[item.next].prev = item.prev
    item.prev = nil
    item.next = nil
    end

    def contents
    @hsh.values.collect(&:content)
    end

    def unlinked?(item)
    item.prev.nil? && item.next.nil?
    end

    def last
    @hsh.each_pair.select { |index, item | !unlinked?(item) && item.next.nil? }.first.last.content
    end

    def first
    @hsh.each_pair.select { |index, item | !unlinked?(item) && item.prev.nil? }.first.last.content
    end
    end