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 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.

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!

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!

Did a rm -R / to find out that I lost my entire Linux installation!

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?

Did a rm -R / to find out that I lost my entire Linux installation!

- 1

Did a rm -R / to find out that I lost my entire Linux installation!

Did a rm -R / to find out that I lost my entire Linux installation!

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"!

No more Blub for me, thank you, Vicar.

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.

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.

No more Blub for me, thank you, Vicar.

Did a rm -R / to find out that I lost my entire Linux installation!

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

Did a rm -R / to find out that I lost my entire Linux installation!

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

No more Blub for me, thank you, Vicar.

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?

Did a rm -R / to find out that I lost my entire Linux installation!

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.

Did a rm -R / to find out that I lost my entire Linux installation!

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

Other Certs: SCEA Part 1, Part 2 & 3, Core Spring 3, TOGAF part 1 and part 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.

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)

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.

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.

Did a rm -R / to find out that I lost my entire Linux installation!

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!

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!

Did a rm -R / to find out that I lost my entire Linux installation!

Did a rm -R / to find out that I lost my entire Linux installation!

Vouching for help! Dive in please!

Did a rm -R / to find out that I lost my entire Linux installation!

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).

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!

Did a rm -R / to find out that I lost my entire Linux installation!

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.

Did a rm -R / to find out that I lost my entire Linux installation!

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!

No more Blub for me, thank you, Vicar.

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.

No more Blub for me, thank you, Vicar.

- 1

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.

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.

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.

Did a rm -R / to find out that I lost my entire Linux installation!

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.