Codehire Cup Solutions: Codehire Cup Preliminary

A solution in Ruby to the Codehire Cup Preliminary problem in the inaugural Codehire Cup held in September 2012 at the University of Adelaide.

In September 2012, Codehire held the inaugural Codehire Cup in Adelaide, featuring some relatively simple coding problems. Contestants could choose to use C#, Java, JavaScript, PHP, or Ruby to submit solutions through a web-based interface. Fastest contestant wins.

The Problem

The Codehire Cup Preliminary problem read:

A train travels from point A to point B, C, D and so on. The travel between two points is a straight line and can be described as a vector (heading and distance). Heading can be any of N, S, E, W, NE, SE, SW or NW (referring to points on the compass) and distance is an integer in km.

The train’s travel can be read from input in the following format:

(<start point>:<end point>)[<heading>:<distance>],(<start point>:<end point>)[<heading>:<distance>],...

For example:

(A:B)[N:100],(B:C)[NW:50],(C:D)[E:75]

Your program should calculate the distance the train has traveled as the crow flies (i.e.; a straight line) between the first point encountered and the last (in this case between points A and D).

Simply write your result in km as a rounded integer converted to a string.

Eg:

output.write("100")

The Solution

This is a simple vector addition problem, so the order that you add the individual hops and the names and locations of the stops are unimportant.

angles = { "SW" => -135, "W" => -90, "NW" => -45, "N" => 0,
           "NE" =>   45, "E" =>  90, "SE" => 135, "S" => 180 }

x, y = 0, 0
input.scan /\[(.*?):(.*?)\]/ do |heading, dist|
    angle = angles[heading] * Math::PI / 180
    x += dist.to_i * Math.cos(angle)
    y += dist.to_i * Math.sin(angle)
end
output << Math.hypot(x, y).round.to_s

Because we treat the problem as a vector addition problem, we assume that the input string specifies a valid route.

For example, the solution does not check if the train travels from A to B, and then from C to D. The algorithm should return an error given such an input ordinarily, but under contest conditions there is not time for such checks unless they are explicitly required.

More Codehire Cup Solutions

Read full post »

Tags: Codehire Cup, coding contests

Codehire Cup Solutions: Practice Challenge

A solution in Ruby to the Practice Challenge in the inaugural Codehire Cup held in September 2012 at the University of Adelaide.

In September 2012, Codehire held the inaugural Codehire Cup in Adelaide, featuring some relatively simple coding problems. Contestants could choose to use C#, Java, JavaScript, PHP, or Ruby to submit solutions through a web-based interface. Fastest contestant wins.

The Problem

The Practice Challenge read:

Given an input string (of no more than 300 characters in length), parse the string and count the occurrences of vowels and consonants. The data should be output in the format below (ie; JSON). Punctuation, numbers and any other characters should be ignored.

{vowels:<count>,consonants:<count>}

eg:

{vowels:10,consonants:9}

The Solution

The simplest solution is to use the built-in Ruby count method to count the vowels and consonants:

str = input.downcase
num_vowels = str.count "aeiou"
num_consonants = str.count "bcdfghjklmnpqrstvwxyz"

output << "{vowels:#{num_vowels},consonants:#{num_consonants}}"

Another Solution

It became somewhat of a contest to see who could squeeze a solution to this problem into the fewest number of characters. Bearing in mind that the input string may contain characters that are neither vowels nor consonants (such as whitespace or punctuation), my solution in 93 characters is set out below:

output<<"{vowels:#{v=input.scan(/[aeiou]/i).size},consonants:#{input.scan(/[a-z]/i).size-v}}"

More Codehire Cup Solutions

Read full post »

Tags: Codehire Cup, coding contests

Install php5-gd to Enable Image Resizing in WordPress

If automatic resizing of uploaded images in WordPress isn’t working, try installing and enabling php5-gd on your Linux server.

To install and enable php5-gd on Ubuntu to enable automatic resizing of uploaded images in WordPress, open a Terminal or connect to the server by SSH and type:

$ sudo apt-get update
$ sudo apt-get install php5-gd
$ /etc/init.d/apache2 restart

WordPress resizes images at the time that they are uploaded, so if you have already uploaded some images, you’ll need to upload the images again or use a plugin like the Dynamic Image Resizer.

A similar process works for other Linux distributions.

Read full post »

Tags: Ubuntu, WordPress

Fix External Monitor Resolution on MacBook Pro with Retina Display

Show more resolution choices for external monitors on the new MacBook Pro with Retina Display by holding the Option key and clicking on the Scaled option in the Displays preference pane.

The redesigned resolution and scaling options pane in System Preferences on the new MacBook Pro with Retina Display may be appropriate for the 2880x1800-pixel resolution of the new 15.4-inch retina display, but it often presents counterintuitive options for the resolution of external monitors.

Connecting a 30-inch Dell 3007WFP-HC monitor using a Mini DisplayPort to DVI Adapter reveals the following resolution options:

Display preferences on MacBook Pro with Retina Display

The effect is somewhat underwhelming, given that the native resolution of the Dell 3007WFP-HC monitor is 2560x1600 pixels.

The Solution

To fix the external monitor resolution by showing more sensible resolution options for this monitor (including the correct 2560x1600-pixel resolution), hold the Option key while clicking on the Scaled radio button.

Display preferences on MacBook Pro with Retina Display with correct resolution settings

The same trick works for other external monitors too, of course.

Although not a showstopper, all of this is quite annoying.

Read full post »

Tags: Apple, MacBook Pro, retina display

Enable Double-Tap to Drag in Mac OS X Lion

You can enable double-tap to drag in Mac OS X Lion in the Unverisal Access preference pane.

System Preferences icon

Apple made a number of improvements to trackpad gestures in Mac OS X Lion. One of the casualties, however, was the ‘double tap to drag’ gesture, which has been replaced with a ‘move with three fingers’ gesture by default.

You can re-enable the double tap to drag gesture in Mac OS X Lion by going to System Preferences > Universal Access > Mouse & Trackpad > Trackpad Options… and enabling one of the relevant options:

  1. Dragging without Drag Lock
    Double-tap to drag and release for a short period to stop dragging.
  2. Dragging with Drag Lock
    Double-tap to drag and single tap to stop dragging.

Step-by-Step Instructions

Step 1 — Open System Preferences by clicking the System Preferences icon on the Dock or searching for ‘System Preferences’ in Spotlight.

System Preferences

Step 2 — Double click the Universal Access icon.

Universal Access preferences

Step 3 — Click on the Mouse & Trackpad tab.

Mouse & Trackpad Universal Access preferences

Step 4 — Click on the Trackpad Options… button.

Trackpad Options

Step 5 — Check the Dragging checkbox and select either without Drag Lock or with Drag Lock from the combo box.

Drag Lock (double tap to drag) preferences

The relevant options are:

  1. Dragging without Drag Lock
    Double-tap to drag and release for a short period to stop dragging.
  2. Dragging with Drag Lock
    Double-tap to drag and single tap to stop dragging.

Read full post »

Tags: Apple, Mac OS X, trackpad gestures