Showing posts with label CircleBear. Show all posts
Showing posts with label CircleBear. Show all posts

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.

Friday, June 21, 2013

Babou

Starting work on a GUI framework for ocelot this weekend with my good buddy Josh Branchaud. Hopefully we have something up by the end of this weekend to show off.

Wednesday, March 27, 2013

SCGet Status Update

Recently both Josh and I have been busy with soundcloud-get (scget) and it has been coming along quite nicely. We feel confident that it is currently at a stable place and are looking into how to release a python project and tag it as version 1.0.

The current command line options that scget supports are:

  • -u <song_url>- Downloads a song from the specified song url. This parameter can be provided multiple times.
  • -a <artist_url> - Downloads all songs from the specified artist url.
  • -o <folder> - Folder to download all songs to. If not provided or doesn't exist, all songs are downloaded to the current directory.
  • -m - If provided, downloads are multi-threaded.
  • -st <track_name> - Searches for tracks that are similar to the provided name and prints their titles.
  • -sa <artist_name> - Searches for artists that are similar to the provided name and prints their names.
  • -l <number> - Limits the number of results pulled back from either the search track or search artist queries. If not provided, each search defaults to 20 results.
Once we figure out how to release this project we do have a few ideas of what still needs to come for this project:
  • Ways for the user to choose songs/artists to download from the provided search results.
  • Being able to provide a yaml configuration instead of having so many command line arguments.
  • Add a default download directory structure instead of just the current directory.
  • Thread pooling for multi-threaded downloads to better manage the many download threads.
  • Track down Unicoder Coercion errors that happen on occasion.
Though, for now it is all working fine and as expected for almost all cases which is why we want to release it now and then tackle these other issues.

Remember you can always watch the progress of this project on our github page.

Wednesday, March 6, 2013

SCGet

I currently started to work on a new side project, scget. It's a command line tool that will allow users to easily download songs from SoundCloud. SoundCloud does allow downloads from the browser, but that requires one to navigate to the page, click download on each song they want, and specify an output folder. This command line tool is going to be simple, yet powerful alternative to downloading from the browser.

Some of the options that have already been thought of are:
  • Download a single song
  •  scget <trackUrl>  
    
  • Download multiple songs
  •  scget <trackUrl1> <trackUrl2> ... <trackUrlN>  
    
  • Download all songs from a particular artist
  •  scget -dj <userUrl>  
  • And of course, the ability to specify a download folder
  •  scget <trackUrl> -o <folder>  
    
Obviously this would be easier than having to navigate the webpage because you could just queue up a bunch of urls to download and then hit go. It will do all of the processing behind the scenes while you get back to what you were doing before.

For this project, I am working in collaboration with Josh Branchaud on this project because not only are we great friends, but we both love SoundCloud. You can follow our progress on this command line tool on our github page.

P.S. - If you have any suggestions about other functionality that should be included in this tool feel free to leave it in a comment.