[Pharo-users] mentor question 4

2020-04-29 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Hello, 

I hope I can discuss my approch to this problem : 

Given a number determine whether or not it is valid per the Luhn
  formula.
The Luhn algorithm is
  a simple checksum formula used to validate a variety of
  identification
  numbers, such as credit card numbers and Canadian Social Insurance
  Numbers.
The task is to check if a given string is valid.
Validating a Number
Strings of length 1 or less are not valid. Spaces are allowed in
  the input,
  but they should be stripped before checking. All other non-digit
  characters
  are disallowed.
Example 1: valid credit card number
4539 1488 0343 6467

The first step of the Luhn algorithm is to double every second
  digit,
  starting from the right. We will be doubling
4_3_ 1_8_ 0_4_ 6_6_

If doubling the number results in a number greater than 9 then
  subtract 9
  from the product. The results of our doubling:
8569 2478 0383 3437

Then sum all of the digits:
8+5+6+9+2+4+7+8+0+3+8+3+3+4+3+7 = 80

If the sum is evenly divisible by 10, then the number is valid.
  This number is valid!


my idea was to do these steps 

1)  reverse the input. 

2)   use this to double every second  digit and calculate the sum
  of all the numbers :   

checkNumber :=  (collection reverse selectwith index: [:item
  :index | (index % 2 == 0) . IfTrue: [item *2]] )  sumNumbers 

3) check if its a valid  number by doing this : 
   
^ (checkNumber % 10 == 0)  



is this a good game plan or has it flaws or can I do it better ?
Regards, 


  Roelof


  


--- End Message ---


Re: [Pharo-users] mentor question 3 is there a formula for this

2020-04-29 Thread Richard Sargent
On Wed, Apr 29, 2020 at 11:30 AM Roelof Wobben  wrote:

> Am I right here or terrible wrong ?
>

If you are referring to calculating the size of the square, you are correct.


> Roelof
>
>
>
> Op 28-4-2020 om 20:53 schreef Roelof Wobben:
>
> Thanks,
>
> And the 5 can also be calculated by   2 * (char - $a)  + 1
>
> Roelof
>
>
>
> Op 28-4-2020 om 18:31 schreef Richard Sargent:
>
> Formula? Well, it's pretty straight forward.
>
> Let's start with the size of the diamond (it field, actually). In the
> example, there are two lines above and two lines below the "C" line.
> C-A = 2. This number looks useful.
> The dimension is 2+1+2 square.
>
> Yes. That is what I wrote.

>
> How many spaces before a letter? C-letter i.e. C-A = 2 C-B = 1 and C-C = 0
> The same number of spaces after the second/last occurrence of the letter
> on the line.
> The number of spaces between the letters on a line is the size minus the
> number of spaces before and after plus the two copies of the current line's
> letter. For the "B" line, there is one space before and after, 2x"B",
> leaving 5 - 1 -1 - 2 = 1 space between. For the "A" line, the simple
> arithmetic would give you -1, which also confirms that just one "A" is
> appropriate with no space between first and last letter on the line. (For
> "A", it is first and last, once only.)
>
> After that, you just need to iterate ($A to: $C), ($(C-1) to: $A by: -1)
> and do the arithmetic.
>
>
> On Tue, Apr 28, 2020 at 1:23 AM Roelof Wobben via Pharo-users <
> pharo-users@lists.pharo.org> wrote:
>
>> Hello,
>>
>> I try now to solve this one :
>>
>> ntroduction
>>
>> The diamond kata takes as its input a letter, and outputs it in a diamond
>> shape. Given a letter, it prints a diamond starting with 'A', with the
>> supplied letter at the widest point.
>> Requirements
>>
>>- The first row contains one 'A'.
>>- The last row contains one 'A'.
>>- All rows, except the first and last, have exactly two identical
>>letters.
>>- All rows have as many trailing spaces as leading spaces. (This
>>might be 0).
>>- The diamond is horizontally symmetric.
>>- The diamond is vertically symmetric.
>>- The diamond has a square shape (width equals height).
>>- The letters form a diamond shape.
>>- The top half has the letters in ascending order.
>>- The bottom half has the letters in descending order.
>>- The four corners (containing the spaces) are triangles.
>>
>> Examples
>>
>> In the following examples, spaces are indicated by · characters.
>>
>> Diamond for letter 'A':
>>
>> A
>>
>> Diamond for letter 'C':
>>
>> ··A··
>> ·B·B·
>> C···C
>> ·B·B·
>> ··A··
>>
>>
>> I noticed that if you take a quarter of it. you see this pattern
>>
>> 001
>> 010
>> 100
>>
>> where a 0 is a space and a 1 is the character.
>>
>> Is there a easy way to make some sort of formula so I can make a output of 
>> this ?
>>
>>
>> Roelof
>>
>>
>>
>
>


Re: [Pharo-users] mentor question 3 is there a formula for this

2020-04-29 Thread Roelof Wobben via Pharo-users
--- Begin Message ---

  
  
Am I right here or terrible wrong ?
  
  Roelof
  
  
  
  Op 28-4-2020 om 20:53 schreef Roelof Wobben:


  
  Thanks, 

And the 5 can also be calculated by   2 * (char - $a)  + 1 

Roelof



Op 28-4-2020 om 18:31 schreef Richard Sargent:
  
  


  Formula? Well, it's pretty straight forward.
  
  
  Let's start with the size of the diamond (it field,
actually). In the example, there are two lines above and two
lines below the "C" line. 
  
  C-A = 2. This number looks useful.
  The dimension is 2+1+2 square.
  
  
  How many spaces before a letter? C-letter i.e. C-A = 2
C-B = 1 and C-C = 0
  The same number of spaces after the second/last
occurrence of the letter on the line.
  The number of spaces between the letters on a line is the
size minus the number of spaces before and after plus the
two copies of the current line's letter. For the "B" line,
there is one space before and after, 2x"B", leaving 5 - 1 -1
- 2 = 1 space between. For the "A" line, the simple
arithmetic would give you -1, which also confirms that just
one "A" is appropriate with no space between first and last
letter on the line. (For "A", it is first and last, once
only.)
  
  
  After that, you just need to iterate ($A to: $C), ($(C-1)
to: $A by: -1) and do the arithmetic.
  
  



  On Tue, Apr 28, 2020 at 1:23
AM Roelof Wobben via Pharo-users 
wrote:
  
  
 Hello, 
  
  I try now to solve this one : 
  
  ntroduction
  
The diamond kata takes as its input a letter, and
  outputs it in a diamond shape. Given a letter, it
  prints a diamond starting with 'A', with the supplied
  letter at the widest point.
Requirements

  The first row contains one 'A'.
  The last row contains one 'A'.
  All rows, except the first and last, have exactly
two identical letters.
  All rows have as many trailing spaces as leading
spaces. (This might be 0).
  The diamond is horizontally symmetric.
  The diamond is vertically symmetric.
  The diamond has a square shape (width equals
height).
  The letters form a diamond shape.
  The top half has the letters in ascending order.
  The bottom half has the letters in descending
order.
  The four corners (containing the spaces) are
triangles.

Examples
In the following examples, spaces are indicated by ·
  characters.
Diamond for letter 'A':
A

Diamond for letter 'C':
··A··
·B·B·
C···C
·B·B·
··A··


I noticed that if you take a quarter of it. you see this pattern 

001
010
100

where a 0 is a space and a 1 is the character.

Is there a easy way to make some sort of formula so I can make a output of this ? 


Roelof


  

  

  
  


  


--- End Message ---


Re: [Pharo-users] mentor question 3 is there a formula for this

2020-04-29 Thread Ben Coman
Building on Richard's advice, one way to break this down into a series of
simpler tasks is
- first generate a square without any dots, i.e. ALL A's on the first line,
ALL B's on second line, ALL C's on third line.
- second, work out when to output a dot rather than a letter.

cheers -ben

On Wed, 29 Apr 2020 at 00:32, Richard Sargent <
richard.sarg...@gemtalksystems.com> wrote:

> Formula? Well, it's pretty straight forward.
>
> Let's start with the size of the diamond (it field, actually). In the
> example, there are two lines above and two lines below the "C" line.
> C-A = 2. This number looks useful.
> The dimension is 2+1+2 square.
>
> How many spaces before a letter? C-letter i.e. C-A = 2 C-B = 1 and C-C = 0
> The same number of spaces after the second/last occurrence of the letter
> on the line.
> The number of spaces between the letters on a line is the size minus the
> number of spaces before and after plus the two copies of the current line's
> letter. For the "B" line, there is one space before and after, 2x"B",
> leaving 5 - 1 -1 - 2 = 1 space between. For the "A" line, the simple
> arithmetic would give you -1, which also confirms that just one "A" is
> appropriate with no space between first and last letter on the line. (For
> "A", it is first and last, once only.)
>
> After that, you just need to iterate ($A to: $C), ($(C-1) to: $A by: -1)
> and do the arithmetic.
>
>
> On Tue, Apr 28, 2020 at 1:23 AM Roelof Wobben via Pharo-users <
> pharo-users@lists.pharo.org> wrote:
>
>> Hello,
>>
>> I try now to solve this one :
>>
>> ntroduction
>>
>> The diamond kata takes as its input a letter, and outputs it in a diamond
>> shape. Given a letter, it prints a diamond starting with 'A', with the
>> supplied letter at the widest point.
>> Requirements
>>
>>- The first row contains one 'A'.
>>- The last row contains one 'A'.
>>- All rows, except the first and last, have exactly two identical
>>letters.
>>- All rows have as many trailing spaces as leading spaces. (This
>>might be 0).
>>- The diamond is horizontally symmetric.
>>- The diamond is vertically symmetric.
>>- The diamond has a square shape (width equals height).
>>- The letters form a diamond shape.
>>- The top half has the letters in ascending order.
>>- The bottom half has the letters in descending order.
>>- The four corners (containing the spaces) are triangles.
>>
>> Examples
>>
>> In the following examples, spaces are indicated by · characters.
>>
>> Diamond for letter 'A':
>>
>> A
>>
>> Diamond for letter 'C':
>>
>> ··A··
>> ·B·B·
>> C···C
>> ·B·B·
>> ··A··
>>
>>
>> I noticed that if you take a quarter of it. you see this pattern
>>
>> 001
>> 010
>> 100
>>
>> where a 0 is a space and a 1 is the character.
>>
>> Is there a easy way to make some sort of formula so I can make a output of 
>> this ?
>>
>>
>> Roelof
>>
>>
>>


Re: [Pharo-users] can I divide a string into part of exactly n length

2020-04-29 Thread Ben Coman
On Tue, 28 Apr 2020 at 01:08, Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> Op 27-4-2020 om 19:05 schreef Richard Sargent:
>
> On Mon, Apr 27, 2020 at 9:27 AM Roelof Wobben via Pharo-users <
> pharo-users@lists.pharo.org> wrote:
>
>> Hello,
>>
>> I wonder if it is possible in Pharo to divide a string in lets say part
>> of 3.
>> so this :  'abcdefgh'
>>
>> would be  'abc def gh`
>>
>
> Do you really want a single string with spaces inserted or do you want a
> collection of substrings, each three characters (or less, for the last one)?
>
>
> I need a single string with spaces inserted.
> So maybe I have formulate my question wrong. Very difficult if English is
> not your mother language and you were bad at school in languages a very
> very long ago.
>

I didn't previously know of a method to do this, but I "guessed" what you
needed might be related to "grouping", so I did...
'abcd' inspect.
then in the Meta tab, starting at Collection, filtered on
 group*
and worked up the hierarchy discovering #groupsOf:atATimeDo: in
SequenceableCollection,
which guesswork found could work like this...
```
input := 'abcdefghijk'.
stream := String streamContents: [:s | input groupsOf: 3 atATimeDo: [
:group | s nextPutAll: group; nextPutAll: ' ']].
stream contents inspect.
```

One of the skills a new programmer needs to develop is to make those
educated "guesses"
and know where/how to test them.

So I learnt a new method today. thx.
cheers -ben


Re: [Pharo-users] mentor question 1

2020-04-29 Thread Ben Coman
>  but I do not have a clue what the aBlock is

You say the method definition is...
> linksTowards: anAddressdo: aBlock

and then later indicate its called like this...
>  linksTowards: aPacket destinationAddressdo: [ :link | self
send: aPacket via: link ]

so in this case aBlock would be "[ :link | self send: aPacket via: link ]"
Or was that not your question?
Remember that "blocks are objects too".
In the same way that curly brackets create an instance of Array...
{ 1 . 2 . 3 } inspect
the square brackets create an instance of BlockClosure, or "block" for
short.

In Playground, compare evaluating the above inspect with evaluating the
following... (i.e. type "inspect" and evaluate the whole thing)
[ :x | x + 1 ] inspect
then in bottom pane of that inspector, DoItAndGo evaluate the following...
   self value: 5
and you will see the result is 6 because #value: was sent to the block
object that you inspected.

HTH
cheers -ben



On Sun, 26 Apr 2020 at 00:48, Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> Hello,
>
> Im doing the OOP book written by Ducassse and im now stuck at the
> network-simulator.
> Code so far can be found here :
> https://github.com/RoelofWobben/Network-simulator
>
> Im stuck at 2 places
>
> 1) What for datatype is loopback ?  Is it a string or something else.
> The book is not telling that.
>
> 2)  I have to make this method/function
>
> linksTowards: anAddress do: aBlock
> "Simple flood algorithm: route via all outgoing links.
>However, just loopback if the receiver node is the routing destination."
>(address destination == loopback)
>ifTrue: [self send:  via: loopback ];
>ifFalse: [self send: via: anAddress]
>
> but I do not have a clue what the aBlock is  and if im on the right path.
> I know the solutions can be found online but then I do not learn anything.
>
> This is what is stated in the book :
>
> This method has to rely on some routing algorithm to identify which links
> will transmit the packet
> closer to its destination. Since some routing algorithms select more than
> one link, we will implement routing as an iteration method, which
> evaluates the given block for each selected link.
> KANetworkNode >> send: aPacket
> "Send aPacket, leaving the responsibility of routing to the node."
> self
> linksTowards: aPacket destinationAddress
> do: [ :link | self send: aPacket via: link ]
> One of the simplest routing algorithm is flooding: just send the packet
> via every outgoing link. Obviously, this is a waste of bandwidth, but it
> works without any knowledge of the network topology
> beyond the list of outgoing links.
> However, there is one case where we know how to route the packet: if the
> destination address
> matches the one of the current node, we can select the loopback link. The
> logic of linksTowards:do:
> is then: compare the packet’s destination address with the one of the
> node, if it is the same, we
> execute the block using the loopback link, else we simply iterate on the
> outgoing links of the receiver.
> KANetworkNode >> linksTowards: anAddress do: aBlock
> "Simple flood algorithm: route via all outgoing links.
> However, just loopback if the receiver node is the routing destination."
> ... Your code ...
>
>
> I hope someone can help me on the way or let me see which steps I had to
> take to solve this on my own.
>
> Roelof
>
>
>