Win a copy of Design for the Mind this week in the Design forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Scala Coursera Week 6

 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Time to get those brains in gear again, guys.
 
Joe Harry
Ranch Hand
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
No video lectures nor any assignments yet!
 
chris webster
Bartender
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Do you guys see the Week6 videos and assignments? I can't see them yet!
 
Joe Harry
Ranch Hand
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Week6 videos are out! We have Anagrams for this week!
 
Joe Harry
Ranch Hand
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I'm calling it a day. Would continue tomorrow with the combinations.
 
Matthew Brown
Bartender
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 34178
340
Eclipse IDE Java VI Editor
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I spent two hours trying to get the hint to work. Then I went with Matthew's approach. So much easier.
 
Joe Harry
Ranch Hand
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 4566
8
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Joe Harry
Ranch Hand
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 24
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2407
32
Linux Oracle Postgres Database Python Scala
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 10106
3
Eclipse IDE Mac PPC Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 15214
36
Android IntelliJ IDE Java Scala Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic