Python Forum

Full Version: Printing a number sequence not working properly
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
This is not exactly homework. I am solving Python3 Practice problems in HackerRank to learn python coding. I am stuck on a mathematical logic part. In the program below, I describe the purpose of the program in the first comment block. In the second comment block, I explain the challenge I am encountering. I am not asking for a python code solution. I am asking for mathematical logic to overcome my problem.

#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Sat Oct 12 00:05:37 2019

@author: deep
"""

'''
Read an integer N.
Without using any string methods, try to prin the following:
123....N

Example: N=3
output: 123
'''

'''
Not working for N > 9. So N=10 should give an output 12345678910 instead my program gives 123456790. I understand why this is happening as far as coding is concerned but I cannot figure out what mathematical logic I need to employ to tackle to address the additional digit once N changes by a factor of 10
'''

# Creating function to reverse a list
def Reverse(lst)
    new_lst = lst[::-1]
    return new_lst

# Enter number
N= int(input('Enter a number: '))
Nabs= abs(N)
x=[]  # List of numbers starting from 0 to N-1
y=[]  # Reverse of list x[]
z=[]  # List of numbers from 1 to N+1

# Populating list x  by appendng with numbers from 0 to N-1.
for i in range(Nabs):
    x.append(i)
print(x)

# Length of list
lx=len(x)
print('length: ', lx)

# Creating reverse list by calling Reverse function for which function has been defined in the beginning of the program. This list will act as the place value calculator as a multiple of 10

y=Reverse(x)
print(y)

# Creating list raning from 1 to N+1 which will act as the list of coefficients that will be multiplied to the elements in the place value list
for j in range(1, Nabs+1):
    z.append(j)
print(z)

# Defining parameters: a is used to calculate the product of the coefficients and the place value. b is used to calculate the sum
a=0
b=0

for k in range(lx):
    a=z[k]*(10**y[k])
    b=b+a

# Negative sign check
if N<0:
    b=-b

print(b)
If you want to do this mathematically, I think it's simplest to multiply and add. Take the example of n = 3. You start with 1. Then you have 2, so you multiply 1 by 10 and add 2, getting 12. Then you have 3, so you multiply 12 by 10 and add 3, getting 123. But this doesn't work when you get to 10. If you multiply 123456789 by 10 and add 10, you get 1234567900. You need to multiply 123456789 by 100, to account for the two digits 10 has in the decimal representation. In general, you need to multiply by 10 ** n, where n is the number of decimal digits the next number has in it's decimal representation. How many digits does the number m have in it's decimal representation? math.floor(math.log10(m)) + 1.
Oh! Thank you so much for your help. Much appreciated.
Am I the only one that does not understand why all this math staff, reversed lists, etc. is used?
as you don't want code, I put in in hint tags

SPOILER:
It was my understanding that it was the constraint of the problem to use only numbers, not strings. Using print is converting things to strings.
(Oct-12-2019, 06:18 PM)ichabod801 Wrote: [ -> ]It was my understanding that it was the constraint of the problem to use only numbers, not strings. Using print is converting things to strings.
it says "Without using any string methods". After all they want "output", right? so at some point you will need to print somewhere... I think they just want to prevent converting to str and using ''.join()
Massive thanks to everyone for their input and help. I was able to take the suggestions from the responses and adjust my code, which works!