Skip to content

Instantly share code, notes, and snippets.

@notjames
Forked from mark665/fullbeaker.md
Last active May 26, 2017 05:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save notjames/5c7f5c3e10726169884f3aaab39b8ab0 to your computer and use it in GitHub Desktop.
Save notjames/5c7f5c3e10726169884f3aaab39b8ab0 to your computer and use it in GitHub Desktop.
Full Beaker "Homework"

1. The table below shows the formulas for triangular, pentagonal, and hexagonal numbers.

... ... ...
Triangular Tn = n(n+1)/2 1, 3, 6, 10, 15, ...
Pentagonal Pn = n(3n-1)/2 1, 5, 12, 22, 35, ...
Hexagonal Hn = n(2n-1) 1, 6, 15, 28, 45, ...

The number T285 = P165 = H143 = 40755 is a triangular number, a pentagonal number, and a hexagonal number. Write code to find the next number like this.

#!/usr/bin/env ruby
# By Jim Conner
# This is a very primitive script. There's probably 
# some other super sexy mathematical way of solving
# this, but given my time, this was what I was able
# to come up with.

require 'awesome_print'

triangles = []
pentagons = []
hexagons  = []
t         = []

# This was a totally arbitrary choice
# which just happened to get me the
# answer to the problem
ceil_stop = 100000
start_from= 2

def tn(n)
  raise ArgumentError, 'tn(): Need n peeps' if n.nil?
  n * (n + 1) / 2
end

def pn(n)
  raise ArgumentError, 'pn(): Need n peeps' if n.nil?
  n * (3 * n - 1) / 2
end

def hn(n)
  raise ArgumentError, 'hn(): Need n peeps' if n.nil?
  n * (2 * n-1)
end

start_from.upto(ceil_stop).each\
{|idx|
  triangles[idx] = tn(idx)
  pentagons[idx] = pn(idx)
  hexagons[idx]  = hn(idx)
}

# do some intersecting
t = (triangles & pentagons & hexagons) - [nil]

t.each\
{|triangular|
  tri_sub = triangles.index(triangular)
  pen_sub = pentagons.index(triangular)
  hex_sub = hexagons.index(triangular)

  puts 'T%s = P%s = H%s = %s' % [tri_sub, pen_sub, hex_sub, triangular]
}

$ time ./fullbeaker
T285 = P165 = H143 = 40755
T55385 = P31977 = H27693 = 1533776805

real    0m0.482s
user    0m0.444s
sys     0m0.032s

2. Describe a situation where something you wrote was unacceptably slow and/or memory intensive. What did you do to fix it?

I wrote a Perl script years ago that moved gigs (before gigs were a common thing) of data from a recursively traversed source directory to a set of dest directories. The way the script was initially written was that it would slurp up the list files to be moved into memory and then churn on the in-memory data structure of those file names until the work was done. The list of files was so large that it could potentially use too much memory on the server,which was not scalable because the list of files was predicted to get larger on a daily basis. So each iterative run would potentially take exponentially longer. Fortunately, this issue was found during testing.

I fixed this problem by setting a data structure element count threshold such that the script would fill the in-memory structure of file names to the newly created threshold, set a marker about where it left off in reading the files, worked on the set of files in memory, and once completed would reset the in-memory data structure of filenames over with files from where it left off. This allowed me to keep a constant and predictably-sized usage of memory while not killing the server and not affecting the productivity of the script.

3. The following javascript is intended to print twenty asterisks, but is incorrect.

    var i = 0, n = 20;
    do
    {
        document.write("*");
        i--;
    } while ( i < n );

There are at least 4 ways to fix it by replacing exactly one character. What are they?

  1. replace 'i--' 'n--' (change i to n)
  2. replace 'while ( i < n )' with 'while ( -i < n )' (add sign to variable 'i')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment