--- Begin Message ---
Op 27-4-2020 om 13:16 schreef PBKResearch:

Roelof

 

You maybe have enough mentors already, but there are some important features of this problem which can be of use in future. Your code solves the problem, but it could be improved in two ways, which make it simpler and clearer.

 

  1. What is the purpose of the local variable ‘isValid’? It provides somewhere to remember the result of your validity test. But you have no need to remember it; all you want to do is evaluate it, use it to determine the course of the program and then forget it. If you write:

(self isValidBinary: aString)

         ifTrue: [….]

               ifFalse: [^nil]

you will achieve the same result without introducing the local variable. It probably reads more clearly, revealing the intention of the code. (You can simplify further by replacing the ifFalse: clause with an unconditional ^nil; if the code reaches this point, we know it’s false.) The general principle is: only introduce a named local variable if you need to remember something for later re-use.

 

  1. It is not necessary to reverse the string in order to evaluate it; it is in fact easier to evaluate it from left to right. You have used the withIndexDo: method to supply the index and know the appropriate power of two to use. Instead you could multiply by two as you process each digit, which will automatically use the right power of two without ever needing to know what it is. You could write:

result := 0.

aString do:

               [:digit| result := result * 2 + digit digitValue].

^result.

This is a special case of a very useful general procedure for evaluating a polynomial. The digits of a binary number are the coefficients of a polynomial in x which is evaluated at x=2; similarly, a decimal number is a polynomial evaluated at x=10.

 

I hope this is helpful.

 

Peter Kenny

 

From: Pharo-users <pharo-users-boun...@lists.pharo.org> On Behalf Of Roelof Wobben via Pharo-users
Sent: 26 April 2020 19:52
To: pharo-users@lists.pharo.org
Cc: Roelof Wobben <r.wob...@home.nl>
Subject: Re: [Pharo-users] mentor question 2.

 

Op 26-4-2020 om 20:17 schreef Sven Van Caekenberghe:

> 

>> On 26 Apr 2020, at 20:10, Gabriel Cotelli <g.cote...@gmail.com> wrote:

>> 

>> In the first method you aren't doing an assignment, are sending the message = to the temp isValid, if you change the method to:

>> decimalFromBinary: aString

>>       | result isValid |

>>       isValid := self isValidBinary: aString.

>>       isValid

>>           ifTrue: [ result := 0.

>>               aString reverse

>>                   withIndexDo:

>>                       [ :digit :index | result := result + (digit

>> digitValue * (2 raisedTo: index - 1)) ].

>>               ^ result ]

>>           ifFalse: [ ^ nil ]

>> it should work.

> There is a #reverseWithIndexDo: method

> 

> Also, #digitValue might be considered a builtin method that you are

> not allowed to use. Since you already did the #isValidBinary: test,

> you could say

> 

>    (digit charCode - $0 charCode)

> 

>> In the second one you're comparing characters with numbers, this will always return false because the number 0 is not the same as the character 0.

>> Use

>> isValidBinary: aString

>>       ^ aString allSatisfy: [ :c | c = $0 or: [ c = $1 ] ] or

>> something like

>> 

>> isValidBinary: aString

>>       ^ aString allSatisfy: [ :c | '01' includes: c ]

>> 

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

>> Hello,

>> 

>> I have to make some code that convert a binary to a decimal and im

>> not allowed to use the convert methods that Pharo has.

>> 

>> So I have written this :

>> 

>> 

>> decimalFromBinary: aString

>>       | result isValid |

>>       isValid = self isValidBinary: aString.

>>       isValid

>>           ifTrue: [ result := 0.

>>               aString reverse

>>                   withIndexDo:

>>                       [ :digit :index | result := result + (digit

>> digitValue * (2 raisedTo: index - 1)) ].

>>               ^ result ]

>>           ifFalse: [ ^ nil ]

>> 

>> isValidBinary: aString

>>       ^ aString allSatisfy: [ :c | c = 0 or: [ c = 1 ] ]

>> 

>> 

>> but on the first method I see a message that the temp variables are

>> read before written.

>> and the second one I see a message that I use a or instead of

>> searching literals.

>> 

>> Where did  I think wrong here ?

>> 

>> Roelof

>> 

>> 

> 

 

 

Thanks,  this problem is solved.

 

Roelof

 

 

 


Thanks for the feedback.
The parts of  polynomial I do not get but that is oke.
I think I needed a higher degree of maths then I have now.
Never reached a university level.

Roelof


--- End Message ---

Reply via email to