any ihint how to do this

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
6 messages Options
Reply | Threaded
Open this post in threaded view
|

any ihint how to do this

Roelof
Hello,

is there a way I can check if a word has 2 the same chars after each other
I was hoping that '#groupby could help me but that one sorts
so abba the aa is found 
or do I have to use a stream for that

could I then also use that stream to check if a word contains 3 vowels ?

I ask also on the discord app but no answer for a long time


Regards,

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: any ihint how to do this

Richard Sargent
Administrator


On Mon, Dec 10, 2018 at 8:52 AM Roelof Wobben <[hidden email]> wrote:
Hello,

is there a way I can check if a word has 2 the same chars after each other
I was hoping that '#groupby could help me but that one sorts
so abba the aa is found 
or do I have to use a stream for that

could I then also use that stream to check if a word contains 3 vowels ?

I'll start with this question first, since it is the easiest to discuss. The brute-force approach would be to use #select: against the word to extract all the vowels. But a better approach, would be to imagine what you need and delegate it. So, we already have #select:, #reject, #detect:, and #collect: as patterns. Imagine a new method called #count:, and implement that. (For the purpose of the assignment, implementing #count: is probably overkill, unless you have many, many words to check.)


Now, back to the first question. You want to iterate through the word "pairwise", where you examine each successive pair of letters.
So, you want to iterate from 1 through n-1 and check whether the letter at the index is the same as the letter at the index + 1.
That should be enough to get you going.


I ask also on the discord app but no answer for a long time


Regards,

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: any ihint how to do this

Roelof
There are manuy words I think some 1000 .
Aoc uses a lot of data for a challenge.

I have to think well how to implent that new function

im thinking now about select and size after that.

Regards,

Roelof



Op 10-12-2018 om 18:04 schreef Richard Sargent:


On Mon, Dec 10, 2018 at 8:52 AM Roelof Wobben <[hidden email]> wrote:
Hello,

is there a way I can check if a word has 2 the same chars after each other
I was hoping that '#groupby could help me but that one sorts
so abba the aa is found 
or do I have to use a stream for that

could I then also use that stream to check if a word contains 3 vowels ?

I'll start with this question first, since it is the easiest to discuss. The brute-force approach would be to use #select: against the word to extract all the vowels. But a better approach, would be to imagine what you need and delegate it. So, we already have #select:, #reject, #detect:, and #collect: as patterns. Imagine a new method called #count:, and implement that. (For the purpose of the assignment, implementing #count: is probably overkill, unless you have many, many words to check.)


Now, back to the first question. You want to iterate through the word "pairwise", where you examine each successive pair of letters.
So, you want to iterate from 1 through n-1 and check whether the letter at the index is the same as the letter at the index + 1.
That should be enough to get you going.


I ask also on the discord app but no answer for a long time


Regards,

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: any ihint how to do this

Richard O'Keefe
In reply to this post by Richard Sargent
Having imagined collection count: [:each | ...]
DO NOT IMPLEMENT IT!
Check to see if it already exists.
Hint:  it DOES exists and has done since before Pharo.
So 'pangrams are fun but this isn''t one' count: [:each | each isVowel]
already answers 10.

Something that may help you a lot is to visit the Collection
class in the browser, and read every method in the 'enumerating'
category.  (You will benefit from looking at the others too, but
we are staying focussed.)  When you have done that, visit the
SequenceableCollection class, and read every method in its
'enumerating' category.

For example, you will certainly find #overlappingPairsDo:.
Given that, you can add

overlappingPairsAnySatisfy: testBlock
  self overlappingPairsDo: [:x :y |
    (testBlock value: x value: y) ifTrue: [^true]].
  ^false

And now you can test for two adjacent equal elements using
  aSequence oberlappingPairsAnySatisfy: [:x :y | x = y]



On Tue, 11 Dec 2018 at 06:05, Richard Sargent <[hidden email]> wrote:


On Mon, Dec 10, 2018 at 8:52 AM Roelof Wobben <[hidden email]> wrote:
Hello,

is there a way I can check if a word has 2 the same chars after each other
I was hoping that '#groupby could help me but that one sorts
so abba the aa is found 
or do I have to use a stream for that

could I then also use that stream to check if a word contains 3 vowels ?

I'll start with this question first, since it is the easiest to discuss. The brute-force approach would be to use #select: against the word to extract all the vowels. But a better approach, would be to imagine what you need and delegate it. So, we already have #select:, #reject, #detect:, and #collect: as patterns. Imagine a new method called #count:, and implement that. (For the purpose of the assignment, implementing #count: is probably overkill, unless you have many, many words to check.)


Now, back to the first question. You want to iterate through the word "pairwise", where you examine each successive pair of letters.
So, you want to iterate from 1 through n-1 and check whether the letter at the index is the same as the letter at the index + 1.
That should be enough to get you going.


I ask also on the discord app but no answer for a long time


Regards,

Roelof

Reply | Threaded
Open this post in threaded view
|

Re: any ihint how to do this

Roelof
Thanks,

next on my learning lists. Look for methods in both classes that can help me.

Roelof



Op 10-12-2018 om 23:30 schreef Richard O'Keefe:
Having imagined collection count: [:each | ...]
DO NOT IMPLEMENT IT!
Check to see if it already exists.
Hint:  it DOES exists and has done since before Pharo.
So 'pangrams are fun but this isn''t one' count: [:each | each isVowel]
already answers 10.

Something that may help you a lot is to visit the Collection
class in the browser, and read every method in the 'enumerating'
category.  (You will benefit from looking at the others too, but
we are staying focussed.)  When you have done that, visit the
SequenceableCollection class, and read every method in its
'enumerating' category.

For example, you will certainly find #overlappingPairsDo:.
Given that, you can add

overlappingPairsAnySatisfy: testBlock
  self overlappingPairsDo: [:x :y |
    (testBlock value: x value: y) ifTrue: [^true]].
  ^false

And now you can test for two adjacent equal elements using
  aSequence oberlappingPairsAnySatisfy: [:x :y | x = y]



On Tue, 11 Dec 2018 at 06:05, Richard Sargent <[hidden email]> wrote:


On Mon, Dec 10, 2018 at 8:52 AM Roelof Wobben <[hidden email]> wrote:
Hello,

is there a way I can check if a word has 2 the same chars after each other
I was hoping that '#groupby could help me but that one sorts
so abba the aa is found 
or do I have to use a stream for that

could I then also use that stream to check if a word contains 3 vowels ?

I'll start with this question first, since it is the easiest to discuss. The brute-force approach would be to use #select: against the word to extract all the vowels. But a better approach, would be to imagine what you need and delegate it. So, we already have #select:, #reject, #detect:, and #collect: as patterns. Imagine a new method called #count:, and implement that. (For the purpose of the assignment, implementing #count: is probably overkill, unless you have many, many words to check.)


Now, back to the first question. You want to iterate through the word "pairwise", where you examine each successive pair of letters.
So, you want to iterate from 1 through n-1 and check whether the letter at the index is the same as the letter at the index + 1.
That should be enough to get you going.


I ask also on the discord app but no answer for a long time


Regards,

Roelof


Reply | Threaded
Open this post in threaded view
|

Re: any ihint how to do this

Torsten Bergmann
>is there a way I can check if a word has 2 the same chars after each other

Use #pairsCollect:

'hello' pairsCollect: [:a :b | a = b ]


There is also a #pairsDo:

'hello' pairsCollect: [:a :b | Transcript show: a asString; show: b asString ]


Have fun
T. (aka astares)