1
num = int(input("Enter a number: "))

while num > 0:
    num2 = num
    dig = 0

    while num2 > 0:
        num2 = num2 //10
        dig = dig + 1

    x = num // 10**(dig-1)
    if dig == 1:
        print (x, end = " ")
    else:
        print (x, end = ", ")
    num = num % 10**(dig-1)

I wrote this code which outputs the number from left to right unless there is a zero at the middle or at the last of the number

For example: If input = 12345; output = 1, 2, 3, 4, 5

However, if input = 120 OR 102; output = 1, 2, OR 1, 2

Is there any workaround for this? I have been told not to use string indexing or lists to solve this.

Tasrif Rahman
  • 53
  • 1
  • 11
  • I think you can slice it and the iterate over it. Please check: https://stackoverflow.com/a/974956/5100770 – jquijano Feb 26 '22 at 04:23
  • Checkout: [How to extract digits from a number from left to right?](https://stackoverflow.com/questions/41321533/how-to-extract-digits-from-a-number-from-left-to-right) – DarrylG Feb 26 '22 at 04:23
  • @DarrylG I checked all these out. I could use them if I was coding for myself. Sadly this is an assignment and they have not taught functions, string indexing, lists etc so it is forbidden to use those methods. I am not sure about recursion so I'll ask my professor about that. Thanks a lot – Tasrif Rahman Feb 26 '22 at 04:41

3 Answers3

1

We can recursively iterate from right to left and then return the value we saw:

def get_digits(a: int):
    if a == 0:
        return ""
    if a > 9:
        return get_digits(a//10) + f", {a%10}" 
    return f"{a%10}"


    
print("100:", get_digits(100))
print("12345:", get_digits(12345))
print("120:", get_digits(120))
print("102:", get_digits(102))

The output will be:

100: 1, 0, 0
12345: 1, 2, 3, 4, 5
120: 1, 2, 0
102: 1, 0, 2
S4eed3sm
  • 1,398
  • 5
  • 20
  • I wonder if @Tasrif Rahman can confirm that strings are strictly forbidden as this answer uses an f string. If an f string is acceptable then there is an easier method than this to solve the problem. – oil_lamp Feb 26 '22 at 05:17
  • without using string index ! – S4eed3sm Feb 26 '22 at 05:24
  • I would accept this answer but sadly as I mentioned in another reply. "they have not taught functions, string indexing, lists etc so it is forbidden to use those methods" – Tasrif Rahman Feb 26 '22 at 13:29
1

Assuming strings are not strictly forbidden, here are some possible alternative solutions.

def get_digits(num):
    return [int(i) for i in str(num)]

Another solution [1]:

import math

def get_digits(num):
    return [(num//(10**i))%10 for i in range(math.ceil(math.log(num, 10))-1, -1, -1)]

This would be optimal if you just needed to print the digits directly.

print(", ".join([i for i in str(num)]))

If you're in a beginning Python class, your professor likely only wants to see something like this which doesn't use lists or string indexes:

num = 1203

for i, n in enumerate(str(num)):
    if i == 0:
        print(n, end="")
    else:
        print(', ' + n, end='')

[Out]: 1, 2, 0, 3
oil_lamp
  • 482
  • 7
  • 9
  • unfortunately functions are also forbidden. I mentioned it in another comment (Sorry for not mentioning this in the title too). However by seeing your code I could learn another approach for this problem – Tasrif Rahman Feb 26 '22 at 13:34
  • You can certainly use any of these without a function but I think the bottom one I provided is probably most like what your professor is looking for. – oil_lamp Feb 26 '22 at 15:24
1

Adjusted the code and now it is working for me. For now it gives me my desired results without using func, string indexing, etc.

num = int(input("Enter a number: "))
num2 = num
dig = 0

while num2 > 0:
        num2 = num2 //10
        dig = dig + 1

while dig > 0:
    x = num // 10**(dig - 1)
    if dig == 1:
        print (x, end = " ")
    else:
        print (x, end = ", ")
    num = num % 10**(dig - 1)
    dig = dig - 1
Tasrif Rahman
  • 53
  • 1
  • 11