Re: [Pharo-users] mentor question 4

2020-04-30 Thread Richard O'Keefe
(oddSum + evenSum) dividedBy: 10

You previously had _ isDivisibleBy: 10
which certainly works.

Squeak, Pharo, and ST/X have #isDivisibleBy:
VisualWorks. Dolphin, and GNU Smalltalk do not.

Here's the code from Number.st in ST/X.
isDivisibleBy:aNumber
"return true, if the receiver can be divided by the argument,
aNumber without a remainder.
 Notice, that the result is only worth trusting, if the receiver
is an integer."

aNumber = 0 ifTrue: [^ false].
aNumber isInteger ifFalse: [^ false].
^ (self \\ aNumber) = 0

The comment is wrong: the question makes sense for any combination
of exact numbers.
When, as in this case, aNumber is a literal integer, all
#isDivisibleBy: really adds is overhead.

(oddSum + evenSum) \\ 10 = 0

is quite clear, and completely portable.



On Fri, 1 May 2020 at 02:16, Roelof Wobben  wrote:
>
> Op 30-4-2020 om 16:06 schreef Richard O'Keefe:
> > This sounds very much like the Luhn test task at RosettaCode.
> > https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
> > except that there it is described as working on the digits of an
> > integer.
> >
> > (1) There are two approaches to traversing a sequence in reverse.
> >  (A) Reverse the sequence, then traverse the copy forward.
> >  aString reverse do: [:each | ...]
> >  (B) Just traverse the sequence in reverse
> >  aString reverseDo: [:each | ...]
> >  My taste is for the second.
> >
> > (2) There are two approaches to deleting spaces.
> >  (A) Make a copy of the string without spaces.
> >  x := aString reject: [:each | each = Character space].
> >  x do: ...
> >  (B) Ignore spaces as you go:
> >  (i) aString do: [:each | each = Character space ifFalse: [...]]
> >  (ii) aString select: [:each | each ~= Character space] thenDo:
> > [:each | ...]
> >
> > Combining (1A) and (2A) you get very obvious code:
> >  (aString reject: [:each | each = Character space]) reverse do:
> > [:digit } ...]
> > Combining (1B) and (2Bi) you get more efficient code:
> >  aString reverseDo: [:digit |
> >  digit = Character space ifFalse: [ ...]]
> >
> > By the way, let's start by checking that the character in the string *are*
> > digits or spaces:
> >  (aString allSatisfy: [:each | each isDigit or: [each = Character 
> > s[ace]])
> >  ifFalse: [^false],
> >
> > (3) There are two approaches to doubling the even digits.
> >  (A) Make a new string that starts as a copy and change every second
> >   digit from the right.
> >  (B) Simply *act* as if this has been done; keep track of whether the
> >  current digit position is even or odd and multiply by 1 or 2 as
> >  appropriate.
> >  nextIsOdd := true.
> >  aString reverseDo: [:digit |
> >  digit = Character space ifFalse: [
> >  nextIsOdd
> >  ifTrue:  [oddSum := ...]
> >  ifFalse: [evenSum := ...].
> >  nextIsOdd := nextIsOdd not]].
> >
> > I *like* code that traverses a data structure exactly once and
> > allocates no intermediate garbage, so I'd be making (B) choices.
> >
> >
>
> For me  , I use this to practice solving problems  and doing the "right"
> steps.
> So I love it , that so many people share there way of solving it.
> I can learn a lot from it
> Expecially when they explain there thinking process so detailed.
>
> I like this code also a lot.
> Am  I correct for testing if it is a valid string by doing this ^
> (oddSum + evenSum) dividedBy: 10
>
> Roelof
>



[Pharo-users] [ANN] Willow v13.0.0 [v13.0.0] released!

2020-04-30 Thread Buenos Aires Smalltalk
Willow, the web interaction library that eases the burden of creating ajax-based web applications reached it's v13.0.0 version.
Breaking Changes

Updated Seaside to v3.4.x
Updated Boardwalk to v5
Updated RenoirSt to v7

Non breaking changes

Updated jQuery to v3.5.0 (fixes some security vulnerabilities)
Improved portability to VA Smalltalk
Use GitHub Actions instead of TravisCI
Use CodeCov instead of Coveralls
Update some internals in TemporarilyDisablingCommand
New releases are automatically notified by mail

Regards,
The Buenos Aires Smalltalk team



[Pharo-users] Package Browser (AKA 6-paned Browser) and Pharo package organization

2020-04-30 Thread step...@heaveneverywhere.com

Hello friends,

I’m getting started with Pharo after decades using VisualWorks and Squeak; it’s 
pretty wonderful what you all have assembled!

My question is related to what we used to teach as the first law of software 
reuse: “You can’t reuse it if you can’t find it,” and the related software 
engineering "principle of least astonishment."

When I fire up Pharo, the system browser presents me with a list of several 
hundred categories (from AST to Zodiac) in a system with over 8000 classes.  
The system categorization makes no sense since I don’t know the naming 
conventions and so many packages have cute but quite non-descriptive names 
(Zinc? Metacello? Calypso?).

In Smalltalk-80, the class category names were organized as a 2-level hierarchy 
where the top-level were items such as Magnitudes, Collections, Streams, 
Graphics, Text, System, Tools, Files, etc.  This made it easy to find (e.g.,) 
the browser source code by looking in the Tools package for the class category 
Tools-Browser.  Even packages with cute names (like my own “Siren”), were 
categorized for ease of finding; e.g., the Siren classes were in class 
categories like Music-Events and Music-Magnitudes.

Parsing the class category names on the first instance of $- made it possible 
to build 6-paned Browsers (called package pane browser in Squeak).  (We 
acknowledged that this violates the “zero/one/infinity" rule.) Is something 
like this available for Pharo? I looked through the Calypso browser code and 
it’s so over-engineered (IMHO) that it’d take me several days to figure out how 
to implement this (it was about 1.5 pages of code in Smalltalk-80).

If Pharo had a browser that scaled better and a reorganization/simplification 
of the class categories to use names that were more self-explanatory, it would 
be *much* easier for new users (in fact, for all users) to find their way 
around.

I apologize for the stepping on toes...

Stephen Pope


--

Stephen Travis Pope   Santa Barbara, California, USA
  http://HeavenEverywhere.com 
http://FASTLabInc.com 
   https://vimeo.com/user19434036/videos 
  
http://heaveneverywhere.com/Reflections

--





Re: [Pharo-users] mentor question 4

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

  
  
Op 30-4-2020 om 20:19 schreef Richard
  Sargent:


  
  
See below.



  On Thu, Apr 30, 2020 at
10:58 AM Roelof Wobben via Pharo-users 
wrote:
  
  and also not with this
one :

cardNumber := '4539 1488 0343 6467'.
oddSum := 0.
evenSum := 0.
nextIsOdd := false.
  cardNumber reverseDo: [:digit |
  digit = Character space ifFalse: [
  nextIsOdd
  ifFalse:  [oddSum := oddSum + (digit asString
asInteger ) ]
  ifTrue: [(((digit asString asInteger) * 2)
> 9)
 ifTrue: [evenSum := evenSum + ((digit asString
asInteger)  * 2) - 9 ]
 ifFalse: [ evenSum := evenSum + (digit asString
asInteger)  * 2 ]].
 nextIsOdd := nextIsOdd not]].
^ evenSum




Op 30-4-2020 om 18:30 schreef Roelof Wobben:
> Op 30-4-2020 om 16:16 schreef Roelof Wobben:
>> nextIsOdd := true.
>>  aString reverseDo: [:digit |
>>  digit = Character space ifFalse: [
>>  nextIsOdd
>>  ifTrue:  [oddSum := ...]
>>  ifFalse: [evenSum := ...].
>>  nextIsOdd := nextIsOdd not]].
>
> hmm,
>
> Still no luck with this code :
>
> cardNumber := '4539 1488 0343 6467'.
> oddSum := 0.
> evenSum := 0.
> nextIsOdd := true.
>  cardNumber reverseDo: [:digit |
>  digit = Character space ifFalse: [
>  nextIsOdd
>  ifTrue:  [oddSum := oddSum + digit
asInteger ]
>  ifFalse: [evenSum := ((digit asInteger *
2) > 9)
>     ifTrue: [evenSum + ((digit asInteger * 2) - 9) ]
>     ifFalse: [ evenSum + (digit asInteger * 2) ]].
>         nextIsOdd := nextIsOdd not]].
> ^ oddSum + evenSum
  
  
  
  With the one change to use #digitValue instead of
#asInteger (which answers the code point), the above
example  works.

| evenSum oddSum |
cardNumber := '4539 1488 0343 6467'.
oddSum := evenSum := 0.
nextIsOdd := true.
cardNumber reverseDo: [:digit |
  

   digit = Character space
  ifFalse: [
  


  
 nextIsOdd

  


  

   ifTrue:  [oddSum :=
  oddSum + digit digitValue ]
 ifFalse:
  [evenSum := ((digit digitValue * 2) > 9)
  

  


  

   ifTrue: [evenSum +
  ((digit digitValue * 2) - 9) ]
 ifFalse: [
  evenSum + (digit digitValue * 2) ]].
  

  


  
 nextIsOdd :=
nextIsOdd not]].

  


  
  ^ oddSum + evenSum
  
  gives me 80 for the answer.
  
  
  A few (mostly) minor points:
  - I don't see why "reverse" is necessary. Of course if
you use #do:, nextIsOdd needs to start with true.
  - repeating the "digit digitValue" or other variations
adds clutter and reduces clarity
  - 
((digit digitValue * 2) - 9) is not unreasonable, but could
be simplified. What is the largest integer X such that
2xX<=9?
  
  
  
  
  

  



Reverse is necessary because  the  * 2  needs  to be all even index
from the right.

oke, can I then make a temp variable digit which holds the value and
rename digit to character because it holding a character instead of
a digit 

when x is 5 or larger then  2 times the value will be greater then
10. 

so the code will be then : 

|
evenSum oddSum |
cardNumber := '4539 1488 0343 6467'.
oddSum := evenSum := 0.
nextIsOdd := true.
cardNumber reverseDo: [:digit |
  

   
  character = 

Re: [Pharo-users] mentor question 4

2020-04-30 Thread Richard Sargent
See below.

On Thu, Apr 30, 2020 at 10:58 AM Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> and also not with this one :
>
> cardNumber := '4539 1488 0343 6467'.
> oddSum := 0.
> evenSum := 0.
> nextIsOdd := false.
>   cardNumber reverseDo: [:digit |
>   digit = Character space ifFalse: [
>   nextIsOdd
>   ifFalse:  [oddSum := oddSum + (digit asString asInteger ) ]
>   ifTrue: [(((digit asString asInteger) * 2) > 9)
>  ifTrue: [evenSum := evenSum + ((digit asString asInteger)  * 2) - 9 ]
>  ifFalse: [ evenSum := evenSum + (digit asString asInteger)  * 2 ]].
>  nextIsOdd := nextIsOdd not]].
> ^ evenSum
>
>
>
>
> Op 30-4-2020 om 18:30 schreef Roelof Wobben:
> > Op 30-4-2020 om 16:16 schreef Roelof Wobben:
> >> nextIsOdd := true.
> >>  aString reverseDo: [:digit |
> >>  digit = Character space ifFalse: [
> >>  nextIsOdd
> >>  ifTrue:  [oddSum := ...]
> >>  ifFalse: [evenSum := ...].
> >>  nextIsOdd := nextIsOdd not]].
> >
> > hmm,
> >
> > Still no luck with this code :
> >
> > cardNumber := '4539 1488 0343 6467'.
> > oddSum := 0.
> > evenSum := 0.
> > nextIsOdd := true.
> >  cardNumber reverseDo: [:digit |
> >  digit = Character space ifFalse: [
> >  nextIsOdd
> >  ifTrue:  [oddSum := oddSum + digit asInteger ]
> >  ifFalse: [evenSum := ((digit asInteger * 2) > 9)
> > ifTrue: [evenSum + ((digit asInteger * 2) - 9) ]
> > ifFalse: [ evenSum + (digit asInteger * 2) ]].
> > nextIsOdd := nextIsOdd not]].
> > ^ oddSum + evenSum
>

With the one change to use #digitValue instead of #asInteger (which answers
the code point), the above example  works.

| evenSum oddSum |
cardNumber := '4539 1488 0343 6467'.
oddSum := evenSum := 0.
nextIsOdd := true.
cardNumber reverseDo: [:digit |
digit = Character space ifFalse: [
nextIsOdd
ifTrue:  [oddSum := oddSum + digit digitValue ]
ifFalse: [evenSum := ((digit digitValue * 2) > 9)
ifTrue: [evenSum + ((digit digitValue * 2) - 9) ]
ifFalse: [ evenSum + (digit digitValue * 2) ]].
nextIsOdd := nextIsOdd not]].
^ oddSum + evenSum
gives me 80 for the answer.

A few (mostly) minor points:
- I don't see why "reverse" is necessary. Of course if you use #do:,
nextIsOdd needs to start with true.
- repeating the "digit digitValue" or other variations adds clutter and
reduces clarity
-  ((digit digitValue * 2) - 9) is not unreasonable, but could be
simplified. What is the largest integer X such that 2xX<=9?


>
> > the answer schould be 57 where I get 1157
> >
> > So some debugging to do
> >
>
>
>


Re: [Pharo-users] mentor question 4

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

and also not with this one :

cardNumber := '4539 1488 0343 6467'.
oddSum := 0.
evenSum := 0.
nextIsOdd := false.
 cardNumber reverseDo: [:digit |
 digit = Character space ifFalse: [
 nextIsOdd
 ifFalse:  [oddSum := oddSum + (digit asString asInteger ) ]
 ifTrue: [(((digit asString asInteger) * 2) > 9)
    ifTrue: [evenSum := evenSum + ((digit asString asInteger)  * 2) - 9 ]
    ifFalse: [ evenSum := evenSum + (digit asString asInteger)  * 2 ]].
    nextIsOdd := nextIsOdd not]].
^ evenSum




Op 30-4-2020 om 18:30 schreef Roelof Wobben:

Op 30-4-2020 om 16:16 schreef Roelof Wobben:

nextIsOdd := true.
 aString reverseDo: [:digit |
 digit = Character space ifFalse: [
 nextIsOdd
 ifTrue:  [oddSum := ...]
 ifFalse: [evenSum := ...].
 nextIsOdd := nextIsOdd not]].


hmm,

Still no luck with this code :

cardNumber := '4539 1488 0343 6467'.
oddSum := 0.
evenSum := 0.
nextIsOdd := true.
 cardNumber reverseDo: [:digit |
 digit = Character space ifFalse: [
 nextIsOdd
 ifTrue:  [oddSum := oddSum + digit asInteger ]
 ifFalse: [evenSum := ((digit asInteger * 2) > 9)
    ifTrue: [evenSum + ((digit asInteger * 2) - 9) ]
    ifFalse: [ evenSum + (digit asInteger * 2) ]].
        nextIsOdd := nextIsOdd not]].
^ oddSum + evenSum

the answer schould be 57 where I get 1157

So some debugging to do




--- End Message ---


Re: [Pharo-users] NeoCSV - how to parse a string with unescaped quoted string inside it?

2020-04-30 Thread Sven Van Caekenberghe
Hi Paul,

Quotes inside quoted fields must be doubled.

See #readQuotedField and #readEndOfQuotedField specifically.

See also #testEmbeddedQuotes

This is according to https://en.wikipedia.org/wiki/Comma-separated_values and 
https://tools.ietf.org/html/rfc4180

HTH,

Sven

BTW: the latest version can be found at https://github.com/svenvc/NeoCSV for 
more than 2 years, the older repositories are no longer maintained or kept up 
to date.

> On 30 Apr 2020, at 18:39, PAUL DEBRUICKER  wrote:
> 
> Hi - 
> 
> This is on Pharo 7 with Neo-CSV-Core-SvenVanCaekenberghe.28
> 
> 
> In a CSV file I have several instances  of records like
> 
> 
> "123","asdf", "one two "three" four five", "5678"
> 
> 
> When that line is parsed instead of an Array with 4 records
> 
> '123'
> 'asdf'
> 'one two "three" four five'
> '5678'
> 
> 
> I instead get 
> 
> '123'
> 'asdf'
> '"one two "three" four five"'
> '"5678"'
> 
> Is there a standard way or setting for handing strings with quoted strings in 
> them? 
> 
> I'd like to end up with the expected result rather than the result I'm 
> getting.  
> 
> 
> Thanks
> 
> Paul
> 
> 




[Pharo-users] NeoCSV - how to parse a string with unescaped quoted string inside it?

2020-04-30 Thread PAUL DEBRUICKER
Hi - 

This is on Pharo 7 with Neo-CSV-Core-SvenVanCaekenberghe.28


In a CSV file I have several instances  of records like


"123","asdf", "one two "three" four five", "5678"


When that line is parsed instead of an Array with 4 records

'123'
'asdf'
'one two "three" four five'
'5678'


I instead get 

'123'
'asdf'
'"one two "three" four five"'
'"5678"'

Is there a standard way or setting for handing strings with quoted strings in 
them? 

I'd like to end up with the expected result rather than the result I'm getting. 
 


Thanks

Paul




Re: [Pharo-users] mentor question 4

2020-04-30 Thread Pablo Navarro
I proposed as alternative to Luhn check. In special to validation problems
like credit card number validation.

El jue., 30 abr. 2020 11:11, Richard O'Keefe  escribió:

> I'm sure you could fit regular expressions into the Luhn check,
> but I am rather mystified as to what one could possibly gain by doing so.
> How do you do the equivalent of
>   #(0 2 4 6 8 1 3 5 7 9) at: char digitValue + 1
> in a regular expression and why would you want to?
>
> On Fri, 1 May 2020 at 01:31, Pablo Navarro  wrote:
> >
> > Hello!. For this problem is possible to use Regular Expressions too.
> >
> >
> https://ci.inria.fr/pharo-contribution/job/UpdatedPharoByExample/lastSuccessfulBuild/artifact/book-result/Regex/Regex.html
> >
> > Saludos, Pablo.
> > El 30 de abr. de 2020 10:11 -0300, Stéphane Ducasse <
> stephane.duca...@inria.fr>, escribió:
> >
> > It looks like a cool problem
> > from where did you take it?
> >
> > 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.
> >
> >
> >
> > I like people that are always thinking in string as if a collection of
> number would not make it :)
> >
> > 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
> >
> >
> >
> > you can also
> > 1 to: xx by: 2 do:
> >
> > 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
> >
> >
> >
> >
> >
> > 
> > Stéphane Ducasse
> > http://stephane.ducasse.free.fr / http://www.pharo.org
> > 03 59 35 87 52
> > Assistant: Julie Jonas
> > FAX 03 59 57 78 50
> > TEL 03 59 35 86 16
> > S. Ducasse - Inria
> > 40, avenue Halley,
> > Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
> > Villeneuve d'Ascq 59650
> > France
> >
>
>


Re: [Pharo-users] mentor question 4

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

Op 30-4-2020 om 16:16 schreef Roelof Wobben:

nextIsOdd := true.
 aString reverseDo: [:digit |
 digit = Character space ifFalse: [
 nextIsOdd
 ifTrue:  [oddSum := ...]
 ifFalse: [evenSum := ...].
 nextIsOdd := nextIsOdd not]].


hmm,

Still no luck with this code :

cardNumber := '4539 1488 0343 6467'.
oddSum := 0.
evenSum := 0.
nextIsOdd := true.
 cardNumber reverseDo: [:digit |
 digit = Character space ifFalse: [
 nextIsOdd
 ifTrue:  [oddSum := oddSum + digit asInteger ]
 ifFalse: [evenSum := ((digit asInteger * 2) > 9)
    ifTrue: [evenSum + ((digit asInteger * 2) - 9) ]
    ifFalse: [ evenSum + (digit asInteger * 2) ]].
        nextIsOdd := nextIsOdd not]].
^ oddSum + evenSum

the answer schould be 57 where I get 1157

So some debugging to do


--- End Message ---


Re: [Pharo-users] Automating release announcements

2020-04-30 Thread Gabriel Cotelli
I finally figure out how to make it work. For this mailing list you have to
send the e-mail in CC and not BCC, because if not it gets retained awaiting
for moderator approval.
In case anyone is interested here is the GitHub action making the magic:
https://github.com/ba-st-actions/email-release-notification

Regards,
Gabriel

On Thu, Apr 30, 2020 at 9:13 AM Stéphane Ducasse 
wrote:

> marcus probably knows.
>
> let us what he is saying.
>
> On 30 Apr 2020, at 13:44, Gabriel Cotelli  wrote:
>
> Hi!, Anyone here is moderator in the Pharo Users list?
>
> I want to automate the release announcement of projects in github/ba-st
> org. I'm using sendgrid to send the e-mails and I'm getting a returning
> mail saying: "Your message to Pharo-users awaits moderator approval".
>
> I subscribed previously to the list the e-mail account used as mail sender.
>
> Thank you for your help!
> Gabriel
>
>
> 
> Stéphane Ducasse
> http://stephane.ducasse.free.fr / http://www.pharo.org
> 03 59 35 87 52
> Assistant: Julie Jonas
> FAX 03 59 57 78 50
> TEL 03 59 35 86 16
> S. Ducasse - Inria
> 40, avenue Halley,
> Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
> Villeneuve d'Ascq 59650
> France
>
>


[Pharo-users] [ANN] RenoirSt v7.0.1 [v7.0.1] released!

2020-04-30 Thread Buenos Aires Smalltalk
RenoirSt, a dsl enabling programmatic cascading style sheet generation for pharo smalltalk reached it's v7.0.1 version.
Changelog

Updated Seaside version to v3.4.x
Renamed RenoirSt package to RenoirSt-Core to ease portability
Improved documentation
Use GitHub actions instead of TravisCI
Use CodeCov instead of Coveralls
Add automatic notification of new releases

Regards,
The Buenos Aires Smalltalk team



Re: [Pharo-users] mentor question 4

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

Op 30-4-2020 om 16:06 schreef Richard O'Keefe:

This sounds very much like the Luhn test task at RosettaCode.
https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
except that there it is described as working on the digits of an
integer.

(1) There are two approaches to traversing a sequence in reverse.
 (A) Reverse the sequence, then traverse the copy forward.
 aString reverse do: [:each | ...]
 (B) Just traverse the sequence in reverse
 aString reverseDo: [:each | ...]
 My taste is for the second.

(2) There are two approaches to deleting spaces.
 (A) Make a copy of the string without spaces.
 x := aString reject: [:each | each = Character space].
 x do: ...
 (B) Ignore spaces as you go:
 (i) aString do: [:each | each = Character space ifFalse: [...]]
 (ii) aString select: [:each | each ~= Character space] thenDo:
[:each | ...]

Combining (1A) and (2A) you get very obvious code:
 (aString reject: [:each | each = Character space]) reverse do:
[:digit } ...]
Combining (1B) and (2Bi) you get more efficient code:
 aString reverseDo: [:digit |
 digit = Character space ifFalse: [ ...]]

By the way, let's start by checking that the character in the string *are*
digits or spaces:
 (aString allSatisfy: [:each | each isDigit or: [each = Character s[ace]])
 ifFalse: [^false],

(3) There are two approaches to doubling the even digits.
 (A) Make a new string that starts as a copy and change every second
  digit from the right.
 (B) Simply *act* as if this has been done; keep track of whether the
 current digit position is even or odd and multiply by 1 or 2 as
 appropriate.
 nextIsOdd := true.
 aString reverseDo: [:digit |
 digit = Character space ifFalse: [
 nextIsOdd
 ifTrue:  [oddSum := ...]
 ifFalse: [evenSum := ...].
 nextIsOdd := nextIsOdd not]].

I *like* code that traverses a data structure exactly once and
allocates no intermediate garbage, so I'd be making (B) choices.




For me  , I use this to practice solving problems  and doing the "right" 
steps.

So I love it , that so many people share there way of solving it.
I can learn a lot from it
Expecially when they explain there thinking process so detailed.

I like this code also a lot.
Am  I correct for testing if it is a valid string by doing this ^ 
(oddSum + evenSum) dividedBy: 10


Roelof


--- End Message ---


Re: [Pharo-users] mentor question 4

2020-04-30 Thread Richard O'Keefe
I'm sure you could fit regular expressions into the Luhn check,
but I am rather mystified as to what one could possibly gain by doing so.
How do you do the equivalent of
  #(0 2 4 6 8 1 3 5 7 9) at: char digitValue + 1
in a regular expression and why would you want to?

On Fri, 1 May 2020 at 01:31, Pablo Navarro  wrote:
>
> Hello!. For this problem is possible to use Regular Expressions too.
>
> https://ci.inria.fr/pharo-contribution/job/UpdatedPharoByExample/lastSuccessfulBuild/artifact/book-result/Regex/Regex.html
>
> Saludos, Pablo.
> El 30 de abr. de 2020 10:11 -0300, Stéphane Ducasse 
> , escribió:
>
> It looks like a cool problem
> from where did you take it?
>
> 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.
>
>
>
> I like people that are always thinking in string as if a collection of number 
> would not make it :)
>
> 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
>
>
>
> you can also
> 1 to: xx by: 2 do:
>
> 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
>
>
>
>
>
> 
> Stéphane Ducasse
> http://stephane.ducasse.free.fr / http://www.pharo.org
> 03 59 35 87 52
> Assistant: Julie Jonas
> FAX 03 59 57 78 50
> TEL 03 59 35 86 16
> S. Ducasse - Inria
> 40, avenue Halley,
> Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
> Villeneuve d'Ascq 59650
> France
>



Re: [Pharo-users] mentor question 4

2020-04-30 Thread Richard O'Keefe
This sounds very much like the Luhn test task at RosettaCode.
https://rosettacode.org/wiki/Luhn_test_of_credit_card_numbers
except that there it is described as working on the digits of an
integer.

(1) There are two approaches to traversing a sequence in reverse.
(A) Reverse the sequence, then traverse the copy forward.
aString reverse do: [:each | ...]
(B) Just traverse the sequence in reverse
aString reverseDo: [:each | ...]
My taste is for the second.

(2) There are two approaches to deleting spaces.
(A) Make a copy of the string without spaces.
x := aString reject: [:each | each = Character space].
x do: ...
(B) Ignore spaces as you go:
(i) aString do: [:each | each = Character space ifFalse: [...]]
(ii) aString select: [:each | each ~= Character space] thenDo:
[:each | ...]

Combining (1A) and (2A) you get very obvious code:
(aString reject: [:each | each = Character space]) reverse do:
[:digit } ...]
Combining (1B) and (2Bi) you get more efficient code:
aString reverseDo: [:digit |
digit = Character space ifFalse: [ ...]]

By the way, let's start by checking that the character in the string *are*
digits or spaces:
(aString allSatisfy: [:each | each isDigit or: [each = Character s[ace]])
ifFalse: [^false],

(3) There are two approaches to doubling the even digits.
(A) Make a new string that starts as a copy and change every second
 digit from the right.
(B) Simply *act* as if this has been done; keep track of whether the
current digit position is even or odd and multiply by 1 or 2 as
appropriate.
nextIsOdd := true.
aString reverseDo: [:digit |
digit = Character space ifFalse: [
nextIsOdd
ifTrue:  [oddSum := ...]
ifFalse: [evenSum := ...].
nextIsOdd := nextIsOdd not]].

I *like* code that traverses a data structure exactly once and
allocates no intermediate garbage, so I'd be making (B) choices.

But I am not at all sure that it is right for *you* at this stage.
Your goal is to get practice in making code that is obviously
correct and lends itself to testing.

I think in your case it makes more sense to make the (A) choices.
You can write some code that reverses a string (using the built-in
method) and test that it does what you expect.

You can write some code that checks whether a string contains
only digits and spaces, and test that.

You can write some code that returns a space-less copy, and
test that.

You can write some code that returns the even (odd) elements
of a sequence, and test those.

SequenceableCollection>>
  withIndexSelect: aBlock
|index|
index := 0.
^self select: [:each |
aBlock value: each value: (index := index + 1)]

  evenElements
^self withIndexSelect: [:each :index | index even]

  oddElements
^self withIndexSelect: [:each :index | index odd]

The easiest way to convert a string to numbers and add up the numbers
is to use #detectSum, as in

aString oddElements detectSum: [:char | char digitValue]

How do you find #digitValue?  By looking in Character.
#detectSum:?  Collection enumeration methods.

Using the (A) approach will give you lots of little methods,
which you can comment and above all TEST, so that each
mistake will be in just one small method.  This is a typical
functional programming "lots of little functions" approach.

On Thu, 30 Apr 2020 at 18:33, Roelof Wobben via Pharo-users
 wrote:
>
> 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 

Re: [Pharo-users] mentor question 4

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

  
  
Op 30-4-2020 om 15:10 schreef Stéphane
  Ducasse:


  
  
It looks like a cool problem
from where did you take it?

  

   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.


  

  
  
  
  I like people that are always thinking in string as if a
  collection of number would not make it :)

  

  
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 

  

  
  
  
  
  
  you can also 
1 to: xx by: 2 do: 
  

  
 

  

  

  



Hmm, I looks like #do does not change the collection and I need to 

collection := '4539148803436467'.
1 to: (collection size + 1) by: 2 do: [:item | item asInteger * 2 ].
^ collection


  


--- End Message ---


Re: [Pharo-users] mentor question 4

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

  
  
Op 30-4-2020 om 15:10 schreef Stéphane
  Ducasse:


  
  
It looks like a cool problem
from where did you take it?

  

   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.


  

  
  
  
  I like people that are always thinking in string as if a
  collection of number would not make it :)

  

  
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 

  

  
  
  
  
  
  you can also 
1 to: xx by: 2 do: 
  

  
 
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


  
  
  

  



  

  

  

Stéphane Ducasse
http://stephane.ducasse.free.fr
  / http://www.pharo.org 
03 59 35 87 52
Assistant: Julie Jonas 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne,
  Bât.A, Park Plaza

  Villeneuve d'Ascq 59650
  France

  

  

  


  


The last problems I posted here are all from the exercism pharo
track. 


  


--- End Message ---


Re: [Pharo-users] mentor question 4

2020-04-30 Thread Pablo Navarro
Hello!. For this problem is possible to use Regular Expressions too.

https://ci.inria.fr/pharo-contribution/job/UpdatedPharoByExample/lastSuccessfulBuild/artifact/book-result/Regex/Regex.html

Saludos, Pablo.
El 30 de abr. de 2020 10:11 -0300, Stéphane Ducasse 
, escribió:
> It looks like a cool problem
> from where did you take it?
>
> > 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.
> >
>
> I like people that are always thinking in string as if a collection of number 
> would not make it :)
>
> > 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
>
>
> you can also
> 1 to: xx by: 2 do:
> > 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
> >
> >
> >
>
> 
> Stéphane Ducasse
> http://stephane.ducasse.free.fr / http://www.pharo.org
> 03 59 35 87 52
> Assistant: Julie Jonas
> FAX 03 59 57 78 50
> TEL 03 59 35 86 16
> S. Ducasse - Inria
> 40, avenue Halley,
> Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
> Villeneuve d'Ascq 59650
> France
>


Re: [Pharo-users] mentor question 4

2020-04-30 Thread Stéphane Ducasse
It looks like a cool problem
from where did you take it?

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

I like people that are always thinking in string as if a collection of number 
would not make it :)

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


you can also 
1 to: xx by: 2 do: 
> 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
> 
> 
> 
> 
> 


Stéphane Ducasse
http://stephane.ducasse.free.fr / http://www.pharo.org 
03 59 35 87 52
Assistant: Julie Jonas 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France



Re: [Pharo-users] Automating release announcements

2020-04-30 Thread Stéphane Ducasse
marcus probably knows.

let us what he is saying. 

> On 30 Apr 2020, at 13:44, Gabriel Cotelli  wrote:
> 
> Hi!, Anyone here is moderator in the Pharo Users list? 
> 
> I want to automate the release announcement of projects in github/ba-st org. 
> I'm using sendgrid to send the e-mails and I'm getting a returning mail 
> saying: "Your message to Pharo-users awaits moderator approval".
> 
> I subscribed previously to the list the e-mail account used as mail sender.
> 
> Thank you for your help!
> Gabriel


Stéphane Ducasse
http://stephane.ducasse.free.fr / http://www.pharo.org 
03 59 35 87 52
Assistant: Julie Jonas 
FAX 03 59 57 78 50
TEL 03 59 35 86 16
S. Ducasse - Inria
40, avenue Halley, 
Parc Scientifique de la Haute Borne, Bât.A, Park Plaza
Villeneuve d'Ascq 59650
France



[Pharo-users] Automating release announcements

2020-04-30 Thread Gabriel Cotelli
Hi!, Anyone here is moderator in the Pharo Users list?

I want to automate the release announcement of projects in github/ba-st
org. I'm using sendgrid to send the e-mails and I'm getting a returning
mail saying: "Your message to Pharo-users awaits moderator approval".

I subscribed previously to the list the e-mail account used as mail sender.

Thank you for your help!
Gabriel


Re: [Pharo-users] mentor question 4

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

  
  
Op 30-4-2020 om 10:57 schreef Ben
  Coman:


  
  




  On Thu, 30 Apr 2020 at
16:46, Roelof Wobben via Pharo-users 
wrote:
  
  Op 30-4-2020 om 10:31
schreef Ben Coman:
> collection := '8569 2478 0383 3437'.
> (collection reverse selectWithIndex: [:item :index |
(index % 2 == 0) 
> ifTrue: [item *2]] )  inspect


I see a error and the non-even numbers are nill now.

but after some figgeling this seems to work :
  
  
  
  Great that you worked it out for yourself !!!  
  "figgeling" is a very important part of programming. 
  
  
   
  

collection := '8569247803833437' asArray.
checkNumber := (collection reverse withIndexCollect: [:item
:index | 
(index % 2 == 0) ifTrue: [item asInteger *2] ifFalse: [item
asInteger]] 
) sum inspect
^ checkNumber isDivisibleBy: 10
  
  
  
  That looks like it would work, but the inspect is
redundant now you know how it works.
  An important take away from this is that when you are
confused by what is happening, 
  you need to LOOK at each spot you data is transformed,
where "inspect" is your friend, particular running it from
the debugger.   
  
  
  
  Now there is another path you might try, but first I want
to stress  
  that I didn't know this answer a minute ago - I only just
discovered it !! 
  So this answer is not based on something magic I knew
directly, but on my approach to guess, search and test
within the system.
  
  
  I was considering how you were dealing with "every second
digit" and considered a general description of this was
"pairs".
I _wondered_ whether Pharo had any methods dealing with
"pairs" ?
  
  
  Spotter is a good place to ask this question, so I...
       Pressed  
       Typed... pair #i
  
  to see if there were any implementors containing the word
"pair" and discovered
SequenceableCollection>>#pairsCollect:
  
  
  3. First time I've seen that method, I wonder how it
works?
  The method comment gives a clue that the block takes two
arguments.
  So lets try-the-simplest-possible-thing...
      ( '123456' pairsCollect: [  :a :b | a ] ) inspect
  ==> #($1 $3 $5)
  
  
  So that looks useful. 
  Try experimenting with it yourself. 
  
  
  cheers -ben

  




I will do and till  now I did not find a way to update the second
one with it and keep the first one.
It seems I will loose the first number and then I cannot use it to
calculate the sum.

Roelof

  


--- End Message ---


Re: [Pharo-users] mentor question 4

2020-04-30 Thread Ben Coman
On Thu, 30 Apr 2020 at 16:46, Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> Op 30-4-2020 om 10:31 schreef Ben Coman:
> > collection := '8569 2478 0383 3437'.
> > (collection reverse selectWithIndex: [:item :index | (index % 2 == 0)
> > ifTrue: [item *2]] )  inspect
>
>
> I see a error and the non-even numbers are nill now.
>
> but after some figgeling this seems to work :
>

Great that you worked it out for yourself !!!
"figgeling" is a very important part of programming.



>
> collection := '8569247803833437' asArray.
> checkNumber := (collection reverse withIndexCollect: [:item :index |
> (index % 2 == 0) ifTrue: [item asInteger *2] ifFalse: [item asInteger]]
> ) sum inspect
> ^ checkNumber isDivisibleBy: 10
>

That looks like it would work, but the inspect is redundant now you know
how it works.
An important take away from this is that when you are confused by what is
happening,
you need to LOOK at each spot you data is transformed, where "inspect" is
your friend, particular running it from the debugger.


Now there is another path you might try, but first I want to stress
that I didn't know this answer a minute ago - I only just discovered it !!
So this answer is not based on something magic I knew directly, but on my
approach to guess, search and test within the system.

I was considering how you were dealing with "every second digit" and
considered a general description of this was "pairs".
I _wondered_ whether Pharo had any methods dealing with "pairs" ?

Spotter is a good place to ask this question, so I...
 Pressed 
 Typed... pair #i
to see if there were any implementors containing the word "pair" and
discovered SequenceableCollection>>#pairsCollect:

3. First time I've seen that method, I wonder how it works?
The method comment gives a clue that the block takes two arguments.
So lets try-the-simplest-possible-thing...
( '123456' pairsCollect: [  :a :b | a ] ) inspect
==> #($1 $3 $5)

So that looks useful.
Try experimenting with it yourself.

cheers -ben


Re: [Pharo-users] mentor question 4

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

Op 30-4-2020 om 10:31 schreef Ben Coman:

collection := '8569 2478 0383 3437'.
(collection reverse selectWithIndex: [:item :index | (index % 2 == 0) 
ifTrue: [item *2]] )  inspect



I see a error and the non-even numbers are nill now.

but after some figgeling this seems to work :

collection := '8569247803833437'asArray.
checkNumber := (collection reverse withIndexCollect: [:item :index | 
(index % 2 == 0) ifTrue: [item asInteger *2] ifFalse: [item asInteger]] 
) sum inspect

^ checkNumber isDivisibleBy: 10




--- End Message ---


Re: [Pharo-users] mentor question 4

2020-04-30 Thread Ben Coman
On Thu, 30 Apr 2020 at 14:33, Roelof Wobben via Pharo-users <
pharo-users@lists.pharo.org> wrote:

> 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
>
I just want to check that before you are ask such questions, did you
actually run this code?
Because it would be fairly easy to see how close you are by evaluating it
without #sumNumbers.
i.e Please report what you see when evaluating this
```
collection := '8569 2478 0383 3437'.
(collection reverse selectWithIndex: [:item :index | (index % 2 == 0)
ifTrue: [item *2]] )  inspect
```

3) check if its a valid  number by doing this :
>
>
> ^ (checkNumber % 10 == 0)
>
That would work, but there is a more intention revealing method...
#isDivisibleBy:

cheers -ben


[Pharo-users] mentor question 4

2020-04-30 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 ---