for all things technical

Ruby vs. Java in Solving The Perfect Shuffle Problem, Part 3 of 3

Prev: The Perfect Shuffle Problem

The Code

Both Java and Ruby based solutions to the Perfect Shuffle Problem are available for download below. In addition to comparing styles, they are informative for anyone learning Ruby with a Java background, or vice versa.

The Java based solution is 82 lines of code, while the Ruby version is 76 lines. Not a huge discrepancy. The character count is quite different though: 1,943 (Java) versus 1,615 (Ruby). Looking at the two code excerpts, we see why.

Read More »


Ruby vs. Java in Solving The Perfect Shuffle Problem, Part 2 of 3

Prev: Introduction

The Perfect Shuffle Problem

The Perfect Shuffle Problem involves determining how many shuffles are required to return a deck of cards to its original order . The following is a formal definition from halfbaked.net.

Given a deck of nCards unique cards, cut the deck iCut cards from top and perform a perfect shuffle. A perfect shuffle begins by putting down the bottom card from the top portion of the deck followed by the bottom card from the bottom portion of the deck followed by the next card from the top portion, etc., alternating cards until one portion is used up. The remaining cards go on top. The problem is to find the number of perfect shuffles required to return the deck to its original order.

Your function should be declared as:

static long shuffles(int nCards,int iCut);

What is the result of shuffles(1002,101)?

We need to conceptualize what we are going to solve, and we are not going to do that thinking about a 1002 card deck and a 101 card cut. Most of us are familiar with a 52 card deck. But even shuffling 52 cards is difficult to visualize. So, let’s first investigate how many perfect shuffles are required to bring a 6 card deck with a 2 card cut to its original order.

Read More »


Ruby vs. Java in Solving the Perfect Shuffle Problem, Part 1 of 3

Introduction

I once read a comparison of Java and Ruby that said, “Ruby is terse, Java is not.” And that was it.

OK thanks. I’ve read equally uninformative statements about English and Japanese. Such generalizations do not answer my main concern as a developer, which is why and when to use either language.

A more educational way to compare the two languages is to write a Java and Ruby program to solve the same challenging problem. Ideally, the problem’s solution would be relatively short.

Therefore, in part 2 of this article I describe the Perfect Shuffle Problem, and how to solve it without using brute force. I follow this up in part 3 by presenting both a Java and Ruby based solution.

If you are impatient to get to the comparison, feel free to skip to Part 3.

Next: The Perfect Shuffle Problem


Installing And Configuring Mint For Wordpress

Someone told you installing Mint was straightforward. Someone was right. I tell you installing and configuring Mint for WordPress is more straightforward. I am more right.

Below I show you why. And how.

Read More »


The Best Way to Upgrade a Manual WordPress Installation — Importing WordPress to SimpleScripts

WordPress features an automatic upgrade option since version 2.7, which apparently often works. Great. But what if it doesn’t work? Or what if you manually installed a prior version?

The detailed WordPress manual upgrade instructions inform you that indeed, you don’t want to perform a manual upgrade. So you feel stuck with say, Wordpress 2.3.2. And you are depressed.

Actually, you are merely a few clicks away from an up-to-date Wordpress installation. I show you what to do, below.  I do however make one assumption: your web host features SimpleScripts. Most do.

Read More »


The Best Way To Install Wordpress — using SimpleScripts

If you are looking for a WordPress installation guide, you found it. And if you’re on the fence about hosting and self-installation, this guide illustrates the simplicity of the process.

Read More »