my dog learned polymorphism*
The moose likes Scala and the fly likes Scala Coursera Week 6 Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCA/OCP Java SE 7 Programmer I & II Study Guide this week in the OCPJP forum!
JavaRanch » Java Forums » Languages » Scala
Bookmark "Scala Coursera Week 6" Watch "Scala Coursera Week 6" New topic
Author

Scala Coursera Week 6

chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Time to get those brains in gear again, guys.


No more Blub for me, thank you, Vicar.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

No video lectures nor any assignments yet!


SCJP 1.4, SCWCD 1.4 - Hints for you, Certified Scrum Master
Did a rm -R / to find out that I lost my entire Linux installation!
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Joe Harry wrote:No video lectures nor any assignments yet!

Well, you can fill in the time with MongoDB - the M101 "courseware" has finally come on line.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

chris webster wrote:
Joe Harry wrote:No video lectures nor any assignments yet!

Well, you can fill in the time with MongoDB - the M101 "courseware" has finally come on line.


MongoDB Week1 assignment was a no brainer. I've skipped it for the weekend. I'm definitely looking forward to this Week's assignment for Scala. It has been almost 2 weeks that I'm not looking at functional programming and turned the imperative way.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Do you guys see the Week6 videos and assignments? I can't see them yet!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Week6 videos are out! We have Anagrams for this week!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Stuck with the combinations and sentenceAnagrams method. Have to think of an elegant way to solve it!

Guys, looks to me like this week is a bit tougher than the other week's assignments. The approach to the problems for this week seems simple to me, but I lack the Scala depth of syntax at the moment and this is what makes the assignment look tougher. How is it with you all?

Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

I'm already vouching for a Head First Scala. Head First guys, are you listening? There are a lot of beautiful concepts that make a perfect fit for a Head First title to be written in Scala. I would even pay double the amount of money to purchase this book if written.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

I'm calling it a day. Would continue tomorrow with the combinations.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

Haven't had time to look at it yet. A bit swamped this week (currently marking exam papers!), so may have to catch up next week.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Just started the assignment this evening. Got the two "occurrences" functions working, although "wordOccurrences" would have been a lot easier if I hadn't had to use "groupBy"!

Found the material slightly more familiar this week, as "for expressions" are pretty similar to comprehensions in other languages, but the ever-present "functional judo" still has my brain in knots - I'm following the solutions to the exercises but couldn't have come up with most of the solutions myself. I think I'll need to decompress from this course by reading up on basic techniques for functional programming, preferably in a maths-lite form: never mind "Head First Scala", I need "Functional Programming For Dummies"!
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

chris webster wrote:Got the two "occurrences" functions working, although "wordOccurrences" would have been a lot easier if I hadn't had to use "groupBy"!


How else would you have done it? Got a chance to do some of it last night, and groupBy seemed the obvious choice.

I got as far as getting combinations working - I think that's a step up in complexity from the earlier steps. Or, at least, it is if my approach is a sensible one. I got "the idea" fairly quickly, but it took a few iterations to get the bits to join up properly.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Matthew Brown wrote:
chris webster wrote:Got the two "occurrences" functions working, although "wordOccurrences" would have been a lot easier if I hadn't had to use "groupBy"!

How else would you have done it? Got a chance to do some of it last night, and groupBy seemed the obvious choice.

Initially I did it with a simple for comprehension because I didn't notice the groupBy hint. Then I converted that to a map along the lines of the for/map translations in the videos. Finally I hacked about for ages trying to get groupBy to yield the right key/value combinations. In the end my version doesn't really produce the map I would want (or expect), but I just fish the bits I want out of there and return those. I still think the for comprehension was the cleanest approach.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

I would take a look at the n queens video. I actually started straight with the assignments without taking a look at any of the videos. The hints given for the assignments were more than enough to solve most of them other than combinations. Taking clue from the discussion forums today, looks like the combinations is more or less like the coinChange problem that we had for Week1. Hope this does not give me much of a headache solving it.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

How do we add an elem (x._1, x._2) to a List of Occurrences?

For example., I have an accumulator and want to add the elem to this.

chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

I get a lot of those as well! Not sure if it will help you here, but remember that we are given:

So a "list of Occurrences" would actually be a list of lists.

I often find the source of this kind of error in my recursive functions is in the "if (somelist.isEmpty) return List()" check i.e. I'm not returning the right kind of "empty" list.

HTH
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

chris webster wrote:I get a lot of those as well! Not sure if it will help you here, but remember that we are given:

So a "list of Occurrences" would actually be a list of lists.

I often find the source of this kind of error in my recursive functions is in the "if (somelist.isEmpty) return List()" check i.e. I'm not returning the right kind of "empty" list.

HTH


To put it other way, how do I add a tuple to a List of Tuples?
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Joe Harry wrote:To put it other way, how do I add a tuple to a List of Tuples?

OK, sorry for being obtuse.

Try:
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

chris webster wrote:
Joe Harry wrote:To put it other way, how do I add a tuple to a List of Tuples?

OK, sorry for being obtuse.

Try:


I got that.

My subtract method implementation is actually composed of 3 inner functions. Here is what I do:


I repeat step 2 and 3 until I have traversed through all elements of y against all elements of the filtered List in step1. Once I have this, I add back the result of this recursion to my original x list which was filtered again by removing the elements that were common in x and y (on the basis of common characters).

I'm sure that there is a shorter way to do this, but can anyone give me some hints to shorten this? This works but I hate the 3 methods that I have written for something that should be really simple.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

My approach was just to start with the first occurrence list, and for each element substract the corresponding value in the second list. Which is easier if the second list is actually a map, and you've used the trick Martin used in the lectures to avoid NoSuchElement exceptions. Then it's a simple matter to filter out the ones that have reached zero.

You'd need to be more careful if you couldn't assume the pre-condition given for the method...but you can .
Jeanne Boyarsky
author & internet detective
Marshal

Joined: May 26, 2003
Posts: 30789
    
157

I spent two hours trying to get the hint to work. Then I went with Matthew's approach. So much easier.


[Blog] [JavaRanch FAQ] [How To Ask Questions The Smart Way] [Book Promos]
Blogging on Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, OCAJP, OCPJP beta, TOGAF part 1 and part 2
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Matthew Brown wrote:My approach was just to start with the first occurrence list, and for each element substract the corresponding value in the second list. Which is easier if the second list is actually a map, and you've used the trick Martin used in the lectures to avoid NoSuchElement exceptions. Then it's a simple matter to filter out the ones that have reached zero.

You'd need to be more careful if you couldn't assume the pre-condition given for the method...but you can .


Which lecture was that? So if I understand you correctly, you are matching your first list against a Map and filter out the ones from the result that has reached Zero. I would try that.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

I did it with a recursive call to a helper function, using the two lists of occurrences (i.e. not Maps), but otherwise the logic was similar to Matthew's approach.
Matthew Brown
Bartender

Joined: Apr 06, 2010
Posts: 4425
    
    8

Joe Harry wrote:Which lecture was that?

The one on Maps - providing a default value for when the key isn't in the Map.
Joe Harry wrote:So if I understand you correctly, you are matching your first list against a Map and filter out the ones from the result that has reached Zero.

I'm not matching anything, just performing a map transformation on the first list.

(I actually did this before realising there was a hint - at some point I should see if I can get that approach working, but I still like this one)
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

I've done everything except the last method (sentenceAnagrams), I'm going to think about it at home tonight.

I found the combinations method difficult. Yesterday afternoon I was at a café with a friend and worked out the solution on the back of a coaster...

I guess my subtract method is similar to Matthew's approach; I didn't have to use what was mentioned in the hint (Map, foldLeft, -, apply, updated). I just map over x, subtracting the frequency in y if there is a corresponding element in y; if not, then I subtract 0, and then I filter the result to take out all elements that have frequency <= 0.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Jesper de Jong wrote:I've done everything except the last method (sentenceAnagrams), I'm going to think about it at home tonight.

I found the combinations method difficult. Yesterday afternoon I was at a café with a friend and worked out the solution on the back of a coaster...

I guess my subtract method is similar to Matthew's approach; I didn't have to use what was mentioned in the hint (Map, foldLeft, -, apply, updated). I just map over x, subtracting the frequency in y if there is a corresponding element in y; if not, then I subtract 0, and then I filter the result to take out all elements that have frequency <= 0.


I acheived that as well for the subtract method. I also have another version of combinations implemented using the combination Scala API in the StringLike trait. With this approach, I praciced a lot of flattening, converting, mapping and filtering.
Monisha Tytler
Greenhorn

Joined: Oct 29, 2012
Posts: 1
Guys,

I am struggling with the combinations part. I get the subsets for the following:

List(('a', 2), ('b', 2)

As follows - and which seems correct :

a) List(List(), List((b,2)), List((a,2)), List((a,2), (b,2)))

But I need to get the following as par the instructions:

List(
List(),
List(('a', 1)),
List(('a', 2)),
List(('b', 1)),
List(('a', 1), ('b', 1)),
List(('a', 2), ('b', 1)),
List(('b', 2)),
List(('a', 1), ('b', 2)),
List(('a', 2), ('b', 2))
)

So, looks like I have do some more on the subsets at (a).

And I am making no progress here.

Can you guys help me here!



Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Monisha Tytler wrote:Guys,

I am struggling with the combinations part. I get the subsets for the following:

List(('a', 2), ('b', 2)

As follows - and which seems correct :

a) List(List(), List((b,2)), List((a,2)), List((a,2), (b,2)))

But I need to get the following as par the instructions:

List(
List(),
List(('a', 1)),
List(('a', 2)),
List(('b', 1)),
List(('a', 1), ('b', 1)),
List(('a', 2), ('b', 1)),
List(('b', 2)),
List(('a', 1), ('b', 2)),
List(('a', 2), ('b', 2))
)

So, looks like I have do some more on the subsets at (a).

And I am making no progress here.

Can you guys help me here!





I can give you a hint. Either you do it the n queens way as marting describes in his lecture or use the combinations api in the StringLike trait.

With the n queens way, you use a for expression and loop until all combinations with each combination reading values from 1 to the nth element in the head tuple. You will then yield the output by combining the results of the tail. If this explanation is not clear, read on.

With the combinations api, you first get the String sequence of all the Chars from your Occurrences, use the combinations method in the StringLike trait and get the results back to a List of Occurrences.



Hope you get the idea now with this approach!
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Did anyone face any issues with the last assignment problem? I keep getting Java heap space error every time when I run my tests against the sentenceAnagrams method. Not sure where to look for problems? All other implementations work like like a charm.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

I just submitted my assignments. Have a couple of test fails with the sentenceAnagrams method.



Vouching for help! Dive in please!
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Finished it! My sentenceAnagrams method looks a bit complicated and I had to put in a special case for the empty sentence... but it works, got 10 out of 10.

Joe Harry: You probably get NoSuchElementException because you're trying to do a lookup in dictionaryByOccurrences and it doesn't contain the key you're looking for.

Try running the test suite of JUnit tests before you submit your assignment; add your own tests if you think you need more (the grading process runs more tests than what's included in the test suite).
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Jesper de Jong wrote:Finished it! My sentenceAnagrams method looks a bit complicated and I had to put in a special case for the empty sentence... but it works, got 10 out of 10.

Joe Harry: You probably get NoSuchElementException because you're trying to do a lookup in dictionaryByOccurrences and it doesn't contain the key you're looking for.

Try running the test suite of JUnit tests before you submit your assignment; add your own tests if you think you need more (the grading process runs more tests than what's included in the test suite).


I did run all my tests. I had some problems with sentenceAnagrams as it took a lot of time to execute. Just was to cross check your implementation of dictionaryByOccurrence. Do you find the following map in your dictionaryByOccurence map? Please let me know!

Wen Tong Lin
Greenhorn

Joined: Sep 29, 2012
Posts: 24
i'm stuck with the combination method. I have read the hint about n-queens, but it is still not working. My implementation is like this:

if occurrences is empty return a list of empty list

for occs <- combinations of occurrences.tail (recursive call) and
occ is occs.head and
i <- 1 to occ._2
yield List of ( occ._1, i )

any help will be greatly appreciated.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

I noticed a strange issue when trying to test the sentenceAnagrams method. It runs into a recursive loop for ever and I do not understand where the issue could be. Debugging using the debugger seems horrible.
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Joe Harry wrote:Did anyone face any issues with the last assignment problem? I keep getting Java heap space error every time when I run my tests against the sentenceAnagrams method. Not sure where to look for problems? All other implementations work like like a charm.

Didn't get any visible errors on my own tests, but I got 9.69 on submission. The submission checker reported an error but didn't give any clear source except to say it was in the "Xerxes yell" test, but when I ran the same test locally it seemed OK. Anyway, I decided after several days wrestling with this assignment I could live without the extra 0.31 points!
chris webster
Bartender

Joined: Mar 01, 2009
Posts: 1786
    
  16

Wen Tong Lin wrote:i'm stuck with the combination method. I have read the hint about n-queens, but it is still not working. My implementation is like this:

if occurrences is empty return a list of empty list

for occs <- combinations of occurrences.tail (recursive call) and
occ is occs.head and
i <- 1 to occ._2
yield List of ( occ._1, i )

any help will be greatly appreciated.

One thing you need to do is allow for the combinations where one of the letters isn't there at all e.g. if you start with "abba", then the valid combinations include List(("a",1)) or List(("b",1)) etc. I did this by generating the "0" elements in my for expression, then filtering out the elements like ("a",0) afterwards. I'm sure there's a more elegant way to do this, but it worked for me.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Joe Harry wrote:Just was to cross check your implementation of dictionaryByOccurrence. Do you find the following map in your dictionaryByOccurence map? Please let me know!


That one is not in my dictionaryByOccurrences, I get a NoSuchElementException if I try to look it up. This looks like the occurrence list of "Linux rulez". Note that dictionaryByOccurrences only contains the occurrence lists of single words, not of combinations of words.

My implementation of dictionaryByOccurrences is super simple, it's just a single method call.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Wen Tong Lin wrote:i'm stuck with the combination method. I have read the hint about n-queens, but it is still not working. My implementation is like this:

if occurrences is empty return a list of empty list

for occs <- combinations of occurrences.tail (recursive call) and
occ is occs.head and
i <- 1 to occ._2
yield List of ( occ._1, i )

any help will be greatly appreciated.

That's going in the direction of my solution, but some things are missing. Instead of returning a List of (occ._1, i), you have to return all combinations of (occ._1, i) with the combinations of the tail, and you also have to return the combinations of the tail itself.
Joe Harry
Ranch Hand

Joined: Sep 26, 2006
Posts: 9468
    
    2

Jesper de Jong wrote:
Joe Harry wrote:Just was to cross check your implementation of dictionaryByOccurrence. Do you find the following map in your dictionaryByOccurence map? Please let me know!


That one is not in my dictionaryByOccurrences, I get a NoSuchElementException if I try to look it up. This looks like the occurrence list of "Linux rulez". Note that dictionaryByOccurrences only contains the occurrence lists of single words, not of combinations of words.

My implementation of dictionaryByOccurrences is super simple, it's just a single method call.


Even my dictionary by occurrences is simple with a groupBy. But when I run my sentenceAnagrams I get the failure message which tries to look for the above Occurrence (Linux rulez) in the dictionaryByOccurrence. This is where it fails. But before calling this, I have a call to combinations.
Jesper de Jong
Java Cowboy
Saloon Keeper

Joined: Aug 16, 2005
Posts: 14278
    
  21

Yes, my dictionaryByOccurrences is also just a groupBy.

My sentenceAnagrams is a bit complicated, but it starts with: (1) compute the occurrence list of the whole sentence using 'sentenceOccurrences', (2) make all combinations out of it using 'combinations', (3) from these, find the combinations that are valid words by checking if they exist in dictionaryByOccurrences. Note: to check if a combination exists in dictionaryByOccurrences, you don't have to look it up; just use 'contains'.

As a result you have the combinations that represent valid words and that are in the occurrence list of the sentence. You can then make anagram sentences with these words; every time you use a word, you'll want to subtract it from the occurrence list of the sentence and call the method recursively with the rest, and the result of that is the rest of the anagram sentence.
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Scala Coursera Week 6