Wednesday, April 16, 2014

More Scala Projects

As I stated in my last post, my buddy Josh and are I currently working on small little scripts/projects while learning new languages. He is choosing to learn Ruby while I am learning Scala. Since that last post we have done a few more scripts/projects:
Count Vowels
This problem involved reading a file, counting all of the vowels, and then printing out a report. Currently, this program works completely as expected and I'm very happy with my solution.

Example execution using itself as the input file:
 S:\Workspaces\Github\scala-projects\Text>scala VowelCount.scala VowelCount.scala  
 Vowel: A    Count: 29    Percentage: 0.2437%  
 Vowel: E    Count: 32    Percentage: 0.2689%  
 Vowel: I    Count: 19    Percentage: 0.1597%  
 Vowel: O    Count: 31    Percentage: 0.2605%  
 Vowel: U    Count: 8    Percentage: 0.0672%  

As one might expect, this challenge was to determine if a file/string is a palindrome or not; palindrome meaning it's spelled the same was forwards as it is backwards (e.g. "racecar"). Now, to make this challenge a little more difficult, Josh and I added some "bonus point opportunities":
  • Ignore white space so "racecar" and "race car" both return true
  • Ignore case so "Race car" and "race car" both return true
  • If the whole string is not a palindrome, find the largest substring which is a palindrome
I conquered this challenge by breaking this down into a few steps and then putting them altogether:
  • Read the whole text into a string and then remove anything which is not a standard character
  • Find all substrings of the character string
  • Order the substrings by length
  • Loop through them until a palindrome is found, indicating this is the largest substring which is a palindrome
Example execution using itself as the input file:
 S:\Workspaces\Github\scala-projects\Text>scala Palindrome.scala Palindrome.scala  
 Largest substring of file which is a palindrome:  

Zip File Maker
This problem was not necessarily a typical problem but more of a learning challenge. As stated it was: given a file or directory, zip up that file or directory. Now, I say this is more of a learning challenge because there's really not much to solve, but a lot to learn. For my solution, if a directory is given as input, I recursively found all of the sub-directories and files and added them to the zip as well.

Example executing using a file and directory as input:
 S:\Workspaces\Github\scala-projects\Files>scala Zip.scala ..\Text\ Zip.scala  
 Adding file S:\Workspaces\Github\scala-projects\Text\Palindrome.scala to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Text\ to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Text\VowelCount.scala to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Text\WordCount.scala to zip.  
 Adding file S:\Workspaces\Github\scala-projects\Files\Zip.scala to zip.  

The zip is created in the local directory called "" and uses the full path of each file when adding them and zipping them up.

Word Count
This problem was just an extension of the "Vowel Count" we did, except this time we counted all words in a file. Now, this problem was up for more individual interpretation than the others because each person could have their own rules on hyphenated words, contractions, punctuation, etc. Though, for my solution the approach I took was:
  • Read all of the lines from the input file
  • Remove all punctuation that had a space on either side of it; this allows hyphenated and contractions to remain
  • Remove all periods - this could be extended to include commas, question marks, and exclamation marks in the future
  • Count how many times each word was found
  • Write to a properties file
Now, I know the solution approach I made is not as elegant as it can be, but it's a start and maybe when I get better with Scala, I'll come back later and spruce it up a little.

Example execution using itself as the input file:
 S:\Workspaces\Github\scala-projects\Text>scala WordCount.scala WordCount.scala  

This will put the output of all the words to their total count in a file called "":
 #Wed Apr 16 17:56:23 CDT 2014  

So ya, that's what Josh and I have been up to. If you want more up to date information on how our projects are going you can always look at our code on our github repos:

Saturday, April 5, 2014

Scala Projects

So a Josh Branchaud (a friend of mine) and I have currently started working on learning new languages through a series of small scale problems. We stumbled upon this github repo that has a lot of both small and large problem ideas that this guy wrote solutions to in python for his own learning. We have decided to work through the same problems he outlines but in the languages we want to learn: I have chosen to write the solutions in scala while Josh is working with ruby. Currently, I have completed 3 of the problems we have decided to do:
This problem is to write an algorithm that will compute the digits of pi to 'n' precision (digits). Currently, my solution supports up to 16 digits of precision for pi and I have yet to find the effort to support more. I might come back and re-visit this one in the future when I'm more familiar with floating point precision in scala.

Example execution with 10 digits of precision:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala CalcPi.scala 10  

With this problem, the user enters a number 'n' where 'n' can either mean to calculate the Fibonacci sequence up to the Nth term or calculate the Fibonacci sequence up to 'n'. My solution works fine except that with large inputs of 'n', it takes exponential time to compute due to the recursive nature of the Fibonacci sequence.

Example execution of the first 15 Fibonacci numbers:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala Fibonacci.scala 15 Nth  
 The first 15 Fibonacci numbers are:  

Example execution of the Fibonacci sequence up to 1000:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala Fibonacci.scala 1000 toN  
 The Fibonacci numbers up to 1000 are:  

In this problem, a user is allowed to enter a number 'n' and the program will output all of the prime factors of 'n'. The algorithm I used to computer whether or not a number was prime I found on Wikipedia.

Example execution with 'n' = 113679:
 S:\Workspaces\Github\scala-projects\Numbers\scala>scala PrimeFactors.scala 113679  
 The prime factors of 113679 are:  

Hopefully Josh and I will continue working on these sets of problems for the foreseeable future because they are fun, simple, and definitely are a huge help in learning a new programming language.