add answers for first two assignments

i just add my answers on my repo. this include solutions for first two assignments with example assignment

ok. the commented out code didn't work correctly. can you tell what is
wrong?




On Thu, Jun 12, 2014 at 8:08 PM, Rob Emanuele <notifications@github.com>
wrote:

> In progfun-patmat/src/main/scala/patmat/Huffman.scala:
>
> > +
> > +            val (frst,secnd) = list splitAt(n)
> > +            merge(mSort(frst), mSort(secnd))
> > +          }
> > +        }
> > +        val sortedList = mSort(freqs)
> > +
> > +    def makeLeaf(xs: List[(Char, Int)]): List[Leaf] = {
> > +      if (xs.isEmpty) Nil
> > +      else {
> > +        Leaf(xs.head._1, xs.head._2) :: makeLeaf(xs.tail)
> > +      }
> > +    }
> > +    //val sortedList = freqs.sortWith((x, y) => x._2 < y._2)
> > +    makeLeaf(sortedList)
> > +  }
>
> Good to practice writing merge sort, but definitely for readability and
> other reasons use library functionality in real code...I see you did this
> with a commented out code, but here's another way to do it:
>
> val sortedList = freqs.sortBy(_._2)
>
> Why not use a map?
>
> def makeOrderedLeafList(freqs: List[(Char, Int)]): List[Leaf] =
>   freqs.map { case(c, i) => Leaf(c, i) }.sortBy(_.weight)
>
> That's a lot less code! Always prefer map, filter, reduce, foldLeft, etc.
> when working with collections.
>
> —
> Reply to this email directly or view it on GitHub
> <https://github.com/lossyrob/kelum-coursera/pull/1/files#r13704300>.
>