4. Digit_sum solution


#1

https://www.codecademy.com/courses/learn-python/lessons/practice-makes-perfect/exercises/digitsum?action=lesson_resume&link_content_target=interstitial_undefined

I tried this exercise for a while…using for loops to convert the number into a string, iterate through it, and then turn the digits back into integers and add them up etc. I couldn’t get it to work. Eventually, I click get code, and code academy gave me this as the solution:

The description says, “digit_sum(1234) should return 10 which is 1 + 2 + 3 + 4.”

Ok. I call it:

digit_sum(1234)

and I get this:

123
12
1
0

So how is this a solution? It doesn’t seem to do what it is supposed to do (return the sum of the digits in the number). Am I misunderstanding this task?


def digit_sum(x):
    total = 0
    while x > 0:
        total += x % 10
        x = x // 10
        print x
    return total

#2

Not sure this is the solution a newbie will come up with, but there is nothing wrong with the code. Remove the print line and just go with the return.

print (digit_sum(1234))

I strongly encourage trying other possible solutions before walking away from this exercise. Practice makes perfect means first and foremost… Practice.


#3

Thanks! On that note, it seems like a lot of these practice exercises depend on knowledge (not concepts necessarily but maybe terms, functions, and operations etc.) that are not explicitly taught in the code academy course. It is best to just google stuff and figure out how to do it?


#4

Cannot say for sure that is the best route. There are enough tools in just the lessons to cobble together working solutions using only what has been introduced.

It is possible that the math operators have not been fully explored, particularly floor division, but I’m pretty sure we learn how to check divisibility using the remainder operator (modulo operation). Given that floor division may not have surfaced then it should not be expected in a working solution built from only the given instructions.

This should hint at the importance of fully absorbing those initial units leading up to this practice unit. It is through solving problems with the tools we have that we get in the best practice and ground ourselves in the basics.

For instance, we have completed a unit on strings, and some built-in (standard library) functions, so this exercise can be completed without using the math operators above.

def digit_sum(n):
    total = 0
    for x in str(n):
        total += int(x)
    return total

Not everything we find by searching is helpful, especially if we don’t really learn anything from it. When we have a good grasp of the fundamentals it is easier to spot the most useful information in the SERP, often without even clicking anything. That’s because we know what search phrases to use, and what to expect.

One cannot stress enough the imperativeness of reading documentation. Each new concept in the lessons can be correlated to a page or segment in the language docs. Side by side with a lesson or tutorial we can quickly compare specs with usage, and go to work practicing with variations on the concepts. Our brain begins to think intuitively and our fingers build musle memory for code patterns.

Practice, practice, practice; and, read, read, read. Rinse and repeat.

Happy coding!


#5

Thanks for the thoughtful response! Pardon my ignorance but “documentation?”. What is that?


#6

The specs as published by the language authors, as well as wiki’s and tutorials written by recognized authorities on the language.

docs.python.org

is a good place to start. Following is a SERP,

python documentation

Not reading the documentation may leave us in a vacuum, or with only a scant understanding of the concept. Take for instance Newton’s Second Law of Rectilinear Motion, from which we derive,

F = ma

where F is the Force, m is the mass, and a is the acceleration. If this is all we are told, and we do not research it further, we miss all the important details. First, we would not think of or know of F and a as being vectors.

F = m • a

which has an important bearing on the equation. And what would we know of the units? Do we know that mass is in kilograms? Or that acceleration is in meters per second per second? Do the units for Force just magically appear as kilogram-meters per second per second? Is there a specific unit that this can be equated to? How do we convert the units?

The above hints at a constant of variation, k, as in,

F = k • m • a

where k is,

N s**2
------
 kg m

Now we can do dimensional analysis to remove all the units and replace them with N in the result.

This is just the start, and not even the half of it. We still haven’t related this to time rate of change of momentum, which is fully implied in the relationship. Calculus, anyone?

Anyway, I don’t wish to harp or over burden you with this rhetoric. Just know that anything that is too simplistic is probably missing the most important aspects. We fill in those holes with our own reading, study and practice, which includes lots of experimentation and repetition. Learn how to cook an egg in the hundred ways that are known, and you can be a chef.


#7

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.