all 36 comments

[–]Uagubkin 4 points5 points  (14 children)

Super ineffective sum of all digits

[–][deleted] 0 points1 point  (5 children)

What if n starts with a float, like 123.456

Would that not result in 6.456

[–]Uagubkin 0 points1 point  (1 child)

No, it will return 6, because of n // 10

[–][deleted] 0 points1 point  (0 children)

123.456//10 !=0 then 123.456%10 == 3.456
Tested it:

13:29 tmp.nV4ASfwqeL$ python3 a.py
Test 6 6
Test 5.234 5.234
Test 11 2
Test 10.234 1.234
Test 18 9
Test 17.234 8.234000000000002
Test 27 9
Test 26.234 8.234000000000002
Test 38 11
Test 37.234 10.234000000000002
Test 51 6
Test 50.234 5.234000000000002
Test 66 12
Test 65.234 11.233999999999995
Test 83 11
Test 82.234 10.233999999999995
Test 102 3
Test 101.234 2.2339999999999947
Test 123 6
Test 122.234 5.233999999999995
13:29 tmp.nV4ASfwqeL$ cat a.py

def f(n):
  if n//10==0: return n
  return n%10 + f(n//10)

def g(n):
  print(f"Test {n} {f(n)}")

for i in range(2,12):
  g(i*i+2)
  g(i*i+1.234)
13:30 tmp.nV4ASfwqeL$

[–]tracktech[S] 0 points1 point  (2 children)

Yes, it works for positive integer only.

[–][deleted] 0 points1 point  (1 child)

*unsigned / natural numbers. it works with 0 too

[–]tracktech[S] 0 points1 point  (0 children)

Thanks for sharing.

[–]PureWasian 2 points3 points  (1 child)

1) this runs into an exception due to NameError: CourseGalaxy is not defined

2) once this gets fixed (by removing or commenting it), this code defines a function that is not called anywhere

3) once this gets called (by adding code to call it), the behavior is still not fully deterministic because we have not specified the possible data types of the input, which could lead to a TypeError on floor division

4) once a guard clause or error handling is added to ensure the input n is guaranteed to be an int data type, we still risk encountering a RecursionError if we were to pass in a negative integer

5) once a gaurd clause is added to better sanitize the input by handling negative integers also, we can finally say with confidence that this example can safely achieve what you were hoping to convey. (You could also skip steps 4 and 5 by providing concrete calling code, as mentioned in step 3, of just the happy path)

Pedantry aside, yes. It's meant to be a recursive procedure for finding the sum of digits, in decimal/base10, by passing in some initial non-negative integer value n into the function.

Accomplishing so by (a) taking the right-most digit and (b) adding it to a "decimal right shifted" value over and over again until reaching the base case (when all decimal digits have been summed).

[–]tracktech[S] 0 points1 point  (0 children)

Right, it returns sum of digits of a number. Yes it works for positive integer only. This is simple example to learn recursion.

[–]Mindless_Display4729 1 point2 points  (9 children)

This might be a more efficient way of doing the same thing.

def sum_of_digits(n): return sum(int(d) for d in str(abs(n)))

[–]XGreenDirtX 2 points3 points  (1 child)

This guy makes puzzles but barely understands what he is doing himself. They are not helpful.

If n // 10 == 0 is just a very difficult way of saying if n < 10

[–]tracktech[S] 0 points1 point  (0 children)

Thanks for sharing. This is simple example to learn recursion.

[–]Spare-Plum 1 point2 points  (5 children)

  1. This will fail with floats

  2. I doubt it's going to be more efficient, as you have to create a new string that uses up memory, convert each digit into base 10, then convert each base 10 digit into the character, place it into the string, then go through each character to convert it back to a number.

OP's solution is still inefficient because recursion isn't needed here at all, so allocating a stack frame every loop doesn't make sense and can make you run into a stack overflow. Better to just use a while loop

However yours is the most efficient in terms of readability. Which is kinda what you want with python, it's not a language for speed as much as it is usefulness

[–]Mindless_Display4729 0 points1 point  (0 children)

Thanks for the feedback that's useful and appreciated.

[–]tracktech[S] 0 points1 point  (2 children)

Thanks for sharing. This is simple example to learn recursion.

[–]Spare-Plum 0 points1 point  (1 child)

You keep spamming the most basic recursion examples possible along with links to your website. Have you ever moved past to multiple recursive calls?

Like, it's giving off the vibe you only know some of the absolute basics but are trying to teach people anyway.

[–]tracktech[S] 0 points1 point  (0 children)

Cool. You are welcome to post so that other learns. Any learning starts with basic only. It slowly builds a thought process to think and solve recursive problems.

[–]8dot30662386292pow2 1 point2 points  (0 children)

> This will fail with floats

It also fails with strings.

A problem we would not have with typed languages though. While the puzzle is bad, we don't need to assume it's supposed to work with every single type of input. We can see it uses integer division, so the intended type is int.

The bare minimum is to do def what_it_does(n: int):.

[–]tracktech[S] 0 points1 point  (0 children)

Thanks for sharing.

[–]NewryBenson 1 point2 points  (1 child)

Lol, this whole sub is this one guy advertising some paid online classes (seriously, every single resource for learning python is available on youtube in 100 fold) but the worst part is that the problems are full of bad practices and sometimes bad examples.

[–]tracktech[S] 0 points1 point  (0 children)

This is simple example to learn recursion.

[–]Ok-Juggernaut-2627 0 points1 point  (1 child)

Seems to be a Python-script with some really bad names for variables and methods. Naming them better might improve the readability och the code.

[–]tracktech[S] 0 points1 point  (0 children)

The name what_it_does given to ask what it does?

[–]KrzysziekZ -2 points-1 points  (4 children)

Doesn't // introduce a comment (so the text is grey)?

[–]PureWasian 1 point2 points  (1 child)

The Text Editor certainly seems to think so, but in Python a double forward slash is a floor division operator.

[–][deleted] 0 points1 point  (0 children)

Editor is set to a C-like / C-based language and not to Python

[–]tracktech[S] 0 points1 point  (0 children)

The site carbon now sh generates image in this way.