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:
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":
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:
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:
No comments:
Post a Comment