[Tutor] finding sum of digits in a string

dn PythonList at DancesWithMice.info
Mon Mar 14 22:28:13 EDT 2022


On 15/03/2022 14.13, Dennis Lee Bieber wrote:
> On Tue, 15 Mar 2022 09:53:58 +1100, Cameron Simpson <cs at cskk.id.au>
> declaimed the following:
> 
> 
>>>
>>> 1) sum(int(ele) for ele in st if ele in "123456789")
>>>
> 
>> And, in fact, your own code there _is_ incorrect, technically, though I 
>> suppose that adding "0" has no effect, so missing it out in the string 
>> still produces a correct result.
>>
> 	I'd missed that on my first glance at the code (and I should note that
> I've sort of started ignoring the OP -- 99% of their posts come down to
> some trivial example that is highly unlikely to be seen in any actual
> program, and the "question" is one of aesthetics/style, which is highly
> subjective to the experience of the reader).


Worse for his (declining) good-will: not responding when asked questions
or invited to give (at least some) feedback.


These questions remind me of the worst of educators asking 'tricky'
questions, or potential-employers and 'clever' pre-tests - most of which
seem designed to as an ego-trip (I'm better than you because THE answer
is...). They reflect abilities to write 'one-liners' and similar, but
don't answer the question: can I give this person a spec and expect
him/her to return with a completed program?

Although, might this characterisation reflect the education system used
in the OP's country/institution/employer?


I'm 'here', to take a break from struggling to find a realistic example
to use in a tutorial. As @Dennis says, it needs to be "trivial"; but
loses relevance and becomes somewhat distractive if "[not] actual". The
'real life' component adds relevance and therefore motivates learning!

At the very minimum I'll need a 'story' with which to introduce the
'problem', but "Take an example of..." wouldn't meet the standard.

We don't have time to have individual examples/tests for each little
fact, eg prove you can use isdigit(). We need to include several 'new'
constructs and/or concepts in each challenge!


An essential difficulty with each of these discussions, is that we are
considering the 'how' of coding, and only thereafter worrying about
which option is more/most readable.

The real concentration should be on the 'what' of the problem are we
trying to solve. The 'readability' of the solution comes after how we
document the spec and consequent sub-problems, have decided upon the
approach/tactics we'll take to solve that individual problem, and only
then, how we'll implement a 'readable' (and meaningful) solution (in
code) to accomplish it all.

Can we consider 'readability' at the code-level?
Of course!
Does 'readability' and thus 'understanding' of the code start at an
higher level?
Whither "context"?


Alternate solution:
The spec biases one's thinking by its use of the words: "the ...
digits". Instead of that fixation, could we consider something like
using translate() to replace any undesired characters with NULSTRING or
zero, and then adding the digits/all that remains?

-- 
Regards,
=dn


More information about the Tutor mailing list