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%  

Palindrome
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:  
 srevers  

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\README.md 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 "output.zip" 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 "wordcount.properties":
 S:\Workspaces\Github\scala-projects\Text>cat wordcount.properties  
 #Wed Apr 16 17:56:23 CDT 2014  
 properties=1  
 punctuation=1  
 new=4  
 java.io.FileOutputStream=1  
 import=3  
 read.")=1  
 \\\\p{Punct}*=1  
 //=1  
 HashMap[String,=1  
 line=1  
 "))=1  
 properties.store(output,=1  
 throw=1  
 (line=1  
 line.split("=1  
 from=1  
 provide=1  
 +\==1  
 \==6  
 else=1  
 all=2  
 v.toString)=1  
 IllegalArgumentException("Must=1  
 file=1  
 scala.collection.mutable.HashMap=1  
 1=2  
 lines)=1  
 output=1  
 val=5  
 ((k,v)=1  
 1)=1  
 \!\==1  
 ").toLowerCase()=1  
 wordCounts=1  
 wordCounts)=1  
 line.replaceAll("=1  
 Properties()=1  
 (\!wordCounts.contains(word))=1  
 "=3  
 lines=1  
 if=2  
 }=2  
 |\\\\.",=1  
 "."=1  
 {=2  
 properties.setProperty(k,=1  
 trimmedLine=1  
 null)=1  
 by=1  
 (args.size=1  
 for=3  
 surrounded=1  
 (word=1  
 and=1  
 Removing=1  
 scala.io.Source.fromFile(args(0)).getLines=1  
 FileOutputStream("wordcount.properties")=1  
 <-=3  
 Int]()=1  
 java.util.Properties=1  
 wordCounts(word)=2  
 a=1  
 to=1  
 =20  

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:
CalcPi
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  
 3.1415926536  

Fibonacci
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:  
 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377  

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:  
 0,1,1,2,3,5,8,13,21,34,55,89,144,233,377,610,987  

PrimeFactors
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:  
 1,3,9,17,51,153,113679  

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.