A “Hanging With Friends” cheater

The first project for my CSCi 1302 class is a “Hanging with Friends” cheater. It should suggest the best words to play when provided with commandline argument.

When this program is run, it should accept at least 1 commandline argument and at most, 2. The first commandline argument should contain a string of question marks and letters. Presumably, there should be at least 1 question mark and 1 character present in the first argument. In the second argument, this should contain just a string of letters you DON’T want in your matches. We have been provided with a dictionary file and told to make it happen.

For starters, 

Advertisements

Getting comfortable with the CLI

For two of my classes (C++/C and more Java) this semester, I’ll be diving into the CLI. If you’ve read my other site (Jane Talks Tech!), you’ll know that I’ve been exposed to the Linux but for the most part, my stomping grounds have been the Microsoft Windows OS. Typing commands is something I haven’t done a lot of but now, I have an added incentive to learn how to use the terminal and the Unix environment!

The first thing I did was image my notebook using Acronis True Image. Then, I installed Ubuntu Oneiric on the entire disk. I have learned that the best way for me to learn is total immersion so that’s why I went for Ubuntu as my main OS and using VirtualBox to add any other OSes as needed. I had it the other way around i.e. Windows 7 as the main OS and Ubuntu as a VM but it wasn’t quite the same.

After importing my files, I’ve decided that I create way too many folders for my own good but it helps with organization. So as a shortcut, I installed nautilus-open-terminal which will let me open a terminal in whatever folder/directory I am browsing. This has saved a lot of unnecessary cd-ing commands and my fingers love me for it. 🙂 I would like to give a serious shout-out to the people who write the documentation at help.ubuntu.com. Besides the man page, the Ubuntu help site has been invaluable for simply finding the right commands to figuring out how to make my external monitor work with Ubuntu

Beyond getting comfortable with the CLI, we are actively making use of text editors and have been encouraged to use either vi or emacs. I selected emacs (for now) and so far, I have learned the key commands to be able to work well. All that needs to happen now is for the shortcut keys to become second nature. 🙂 Emacs integrates quite nicely into nautilus so that all I need to do is rightclick a file to edit it in Emacs. Of course, I could cd to the file and type “emacs filename” but I’ve quickly found out that unless I quit the file, I can’t use that particular terminal for anything else! Hence, my preference for navigating to the file in nautilus and rightclicking to edit in emacs. Please feel free to correct me if I’ve been “doing it wrong”. 🙂

Update (1/26): A way to start emacs from the terminal and get a Unix prompt back is to type: emacs & .Awesome. 🙂 

I’ve also been learning about make (really Makefiles which are a nifty way to compile & build executables). At first, I pooh-poohed the idea and wondered how hard typing “javac filename.java” was. However, as software applications increase in complexity and require more dependencies, having a Makefile totally makes sense. It’s quite powerful and I am sure I’ll become very intimate with its quirks.

Menu_070Synaptic_package_manager_062

Die Rich Craps game in Java

For one of my programming projects last year, our task was to create a version of the Die Rich Craps game. I finally decided to put my code for that project (and others) on Github.

My submitted project looks, in retrospect, a little unreadable but I did try to comment the code appropriately. My Die Rich craps implementation is contains:

  1. 2 while loops
  2. 1 for loop
  3. 4 if/else statements
  4. 2 if statements
  5. 4 if-else-if laddered statements.

So, I am in the process of rewriting it to be a little shorter and slightly more testable. I have a rough & incomplete sketch which goes:

1) Create a Players class which will contain key variables that are unique to each instance of the Players class and a final static class variable which applies to all members of the Players class.

2) Create the DieRich class file (which includes the main method)

That’s as far as I’ve gotten. So far, it’s a good experiment to think about breaking the program/project into tasks and writing code accordingly.

I visited a software product company recently and I learned that tests (regression and unit tests) are exceedingly important & take up just as much time as coding does! The developer I ‘shadowed’ walked me through the process of unit testing which I will try to implement in my projects this semester.

Verifying Input

When creating constructors and adding setters, I’ve been taught to verify all input passed to the method. In working through the ProjectEuler exercises, I’ve made use of helper methods that hadformal parameters but didn’t necessarily verify the input (bad habit). With Problem8, I decided to put this into practice. The problem was: “find the greatest product of five consecutive digits in a 1000-digit number.’ I solved this problem using 2 for loops:

  1. With the first for loop, I created a String object that contained 5 characters at one time. With every iteration of the for loop, the sentinel would be updated by 1 and thus, the String would be updated to contain a new sequence of 5 characters e.g.  startingString.substring(i,i+4).
  2. Rather than perform the required computation in the 1st for loop which would made my program faster, I decided to create a helper method which would accept a String and return the product of all the digits in that String. 
  3. The first iteration of my helper method didn’t include any checks to verify that the String didn’t have any non-word characters which would have caused a NumberFormat exception (I converted the digits in the String object using Integer.parseInt(String name)).
    Java-numberformat

     So, I added some to the helper method I used to make sure that the algorithm would return correct/appropriate output when provided input that fits the formal parameter specifications. 

  4. First, I stripped the String of all non-word characters with this command: digits.replaceAll(“\W”,””); An alternate and informative way to alert the user to the ‘bad’ input is via the try { } catch { } feature of Java.
  5. Another thing to check for is if, after stripping all non-word characters from the String passed to the helper method, the String’s length is 0 i.e. the String is an empty String, I would return 0 as the product of the String in that case instead of the value of Product which was initialized to 1. That was taken care of via a simple conditional statement. Here’s the code for my helper method: 

    https://gist.github.com/1531024

Overall, it’ll be good practice for me to view my code with an eye to splitting off ‘tasks’ into helper methods and making sure that input is verified. 

Happy December break! :)

I’m thankful for family and thankful that my faculties are still sharp. 🙂 Long way of saying my semester ended on a very strong note.

Taking a break from stuffing myself, I thought of a crude solution to Problem 5 from ProjectEuler.net. Caution: With the way I solved the problem, I’ll run into an integer oveflow situation for N > 23. Without further ado, here’s the gist of my rough solution:

https://gist.github.com/1522663

I’ve started using Github (@janoulle) and I know the basics of creating/cloning a repo, adding, committing and pushing. I may have to pony up for a private repo for placing my class assignments but the cheapskate in me thinks it might be better to move to Bitbucket which has free & unlimited private repos. 

Edit: Problem 6 was relatively straightforward and I solved that quickly. It requires summation formula which, if you’ve taken Calculus I or Discrete Math, should be easy to do. I’ve pushed it to the Github repo which contains  all my solutions. 

https://gist.github.com/1522780

Edit: Problem 7 is done.

https://gist.github.com/1522955

Code Critiques

So, I finished the semester on a strong note: aced all 4 of my classes. Woohoo! 

The relevant CS courses I took were: Intro to Programming (using Java), Discrete Math and Calculus I. I heartily recommend this combination of classes because the concepts I learned in Discrete Math served me well in my Java class (think propositional & predicate logic) and my Calculus class (functions/relations)!