Skip to content

Instantly share code, notes, and snippets.

@rogerbraun
Created February 10, 2012 17:47
Show Gist options
  • Select an option

  • Save rogerbraun/1791212 to your computer and use it in GitHub Desktop.

Select an option

Save rogerbraun/1791212 to your computer and use it in GitHub Desktop.

Revisions

  1. rogerbraun revised this gist Feb 10, 2012. 1 changed file with 2 additions and 2 deletions.
    4 changes: 2 additions & 2 deletions find_8001.rb
    Original file line number Diff line number Diff line change
    @@ -1,9 +1,9 @@
    def check_number(n)
    n_f = 1.upto(n).inject(&:*)
    quersumme = n_f.to_s.each_char.inject(0) do |res, v|
    sum_of_digits = n_f.to_s.each_char.inject(0) do |res, v|
    res += v.to_i
    end
    [n_f, quersumme]
    [n_f, sum_of_digits]
    end

    found = false
  2. rogerbraun created this gist Feb 10, 2012.
    16 changes: 16 additions & 0 deletions find_8001.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,16 @@
    def check_number(n)
    n_f = 1.upto(n).inject(&:*)
    quersumme = n_f.to_s.each_char.inject(0) do |res, v|
    res += v.to_i
    end
    [n_f, quersumme]
    end

    found = false
    i = 1
    while not found do
    res = check_number(i)
    puts "Checking #{i}!, is #{res[0]}, sum of digits #{res[1]}"
    i += 1
    found = true if res[1] == 8001
    end
    34 changes: 34 additions & 0 deletions std_dev.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,34 @@
    require "open-uri"
    require "nokogiri"

    url = "http://apply.embed.ly/static/data/2.html"

    doc = Nokogiri::HTML(open(url))

    article = doc.css("article").first

    def mark_depth(start, n)
    if start.name == "p" then
    res = n
    else
    res = nil
    end

    children = start.children.map do |child|
    mark_depth(child, n+1)
    end

    [res, children].flatten.compact
    end

    res = mark_depth(article, 1)

    mean = res.inject(&:+) / res.count.to_f

    deviations = res.map{|n| (n - mean) * (n - mean)}

    deviations_avg = deviations.inject(&:+) / deviations.count.to_f

    std_dev = Math.sqrt(deviations_avg)

    puts std_dev
    22 changes: 22 additions & 0 deletions zipf.rb
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,22 @@
    require "pry"
    nums = 1.upto(900)

    multiplier = nums.inject(&:+)

    frequencies = nums.each_with_index.map{|n, index|
    multiplier / (index + 1)
    }

    all = frequencies.inject(&:+)

    half = all / 2

    frequencies.each_with_index.inject(0) do |result, (value, index)|
    result += value
    if result >= half
    puts index + 1
    break
    end
    result
    end