Hacker Rank Solutions: Anagram

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
tests = $stdin.gets.to_i

tests.times do

  string = $stdin.gets.chomp
  if string.length % 2 == 0
    middle = string.length / 2

    left = string[0..(middle - 1)]
    right = string[middle..(string.length - 1)]

    left_sort = left.chars.sort
    right_sort = right.chars.sort

    if left_sort == right_sort
      return 0
    else
      left_sort.each do |x|
        index = right_sort.index(x)
        right_sort.delete_at(index) unless index.nil?
      end
      puts right_sort.length
    end
  else
    puts -1
  end

end

Hacker Rank Solutions: Chocolate Feast

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
def exchange(wrappers, number_for_free)
  return 0 if wrappers < number_for_free
  free_sweets = wrappers / number_for_free
  left_over =  wrappers % number_for_free

  if left_over == 0 && free_sweets < number_for_free
    return free_sweets
  else
    return free_sweets + exchange(free_sweets + left_over, number_for_free)
  end
end


test_cases = $stdin.gets.to_i

test_cases.times do
  money, chocolate_price, number_for_free = $stdin.gets.split(' ').collect{ |x| x.to_i }

  bought_sweets = money/chocolate_price

  $stdout.puts  bought_sweets + exchange(bought_sweets, number_for_free)
end

Hacker Rank Solutions: Gem Stones

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
lines = $stdin.gets.to_i

char_counts = Hash.new

lines.times do

  visted_chars =  Hash.new

  line = $stdin.gets.chomp
  line.unpack('C*').each do |char|
    if visted_chars[char].nil?
      char_counts[char] = 0 if  char_counts[char].nil?
      char_counts[char] += 1
      visted_chars[char] = true
    end
  end

end

output = []
char_counts.each { |key, value| output << key if value == lines   }

puts output.length

Inplace quicksort algorithum implemented in Ruby

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
def quicksort(array, first, last)
  if array.length > 1
    return if first > last

    pivot = array[(first + last) * 0.5]
    left = first
    right = last

    while left <= right
      while(array[left] < pivot) do
        left += 1
      end
      while(array[right] > pivot) do
        right -= 1
      end

      if left <= right
        tmp = array[left]
        array[left] = array[right]
        array[right] = tmp

        left += 1
        right -= 1
      end
    end

    quicksort(array, first, right)
    quicksort(array, left, last)
  end
end

Greatest Common Divisor

Ruby has a native implementation of the greatest common divisor.

1
2
  20.gcd(15)
  => 5

However as a pure academic exercise lets implement Euclid’s algorithm for finding the greatest common divisor between any number of numbers in ruby!

1
2
3
4
5
6
7
  def gcd(u, v)
    if v.zero?
      return u
    else
      gcd(v, u.modulo(v))
    end
  end

Greatest Common Divisor for three numbers

1
2
3
  def gcdt(u, v, w)
    gcd(gcd(u, v), w)
  end

Extending this to a generic algorithm that can take any number of numbers

1
2
3
4
5
6
7
8
  def gcd_any(*args)
    raise ArgumentError 'Need at least two numbers' if args.length < 2

    start = args.shift
    args.inject(start) do |result, value|
      gcd(result, value)
    end
  end

A recent hack / sketchy 1st product draft required datasets that live in two different database to be joined together to do some analysis on. Initially thoughts of denormalization or key/value lookup crossed peoples minds but wanting a quick win to try and get the idea validated as quickly as possible, I kept on digging.

As always stackoverflow provided the start of the journey of discovery with something called dblink.

dblink to the rescue!

dblink is a hidden gem of a extension that made doing a join across two databases (weather there local or remote) a total breeze. Firstly the extension needs to be installed on the database with the following command.

1
  CREATE EXTENSION dblink

Once added to your database using it requires three pieces of information

1
2
3
  SELECT * FROM dblink('CONNECTION STRING',
    'QUERY STRING')
  AS t( CASTING RESTULS )

Connection String

I personally use this format of string as it allows easy access to both local and remote DBs.

postgres://PG_USER:PG_PASSWORD@HOST:PORT/DATABSE_NAME'

eg

postgres://pguser:abcd@127.0.0.1:5432/test_db'
Read on →

Notes from Refactoring Ruby by Jay Fields, Shane Harvie, Martin Fowler & Kent Beck.

Duplicated Code

Problem – This stinks, when you have the same expression in two different methods in the same class

Solution – Extract the method to a new method in the class and use in place

Duplication in Sibling Classes

Problem – Two methods in sibling class that that have the same expression.

Solution – Extract the method into one on the shared base class and use this instead.

Duplication across unrelated classes

Problem – The same method or expression across two unrelated classes

Solution – Extract the method into a new class or module and then use either inheritance or composition to give the class the new method.

Read on →
vim


Basic Commands

i = insert mode

h,j,k,l = move left, up, down, left
w = move from word to word
b = move back word to word
W = jumps the whole word
B = jusmps back a word to word

$ = go to end of the line
^ = go to the begining of the line
0 = goes to the very beginning

gg = goes to the begginig of the file
G = goes to the bottom of the file
{ } = jumps paragraphs

f = finds the first occurence of a string in the file
F = fins the first occurance of a string backward

number followed by comman (4dd) – deletes four lines

3gg = goes 3 lines from the top
:13 = goes to line 13
3 G = goes three lines from the bottom

Read on →

For when i cant find it, here is the best instructions i found for removing

  1. Go to /usr/local/lib and delete any node and node_modules
  2. Go to /usr/local/include and delete any node and node_modules directory
  3. If you installed with brew install node, then run brew uninstall node in your terminal
  4. Check your Home directory for any “local” or “lib” or “include” folders, and delete any “node” or “node_modules” from there
  5. Go to /usr/local/bin and delete any node executable

source

There are numerous guides available on line for setting up a fresh install of Jenkins with numerous different configurations but i wanted to create a chef recipe that would give me a clean Jenkins install with my required project dependencies (excluding rvm) at the click of a button.

The reasons the excluding RVM in the chef process, even though the chef-rvm cookback is fantastic having used it before in the creation of a Rails stack is that i just could get system level RVM to play nice with Jenkins and user specific RVM (to the Jenkins user) to automatically install without asking for the Jenkins password. Better minds will probably be able to solve this issue but i opted for a different approach.

Read on →