Python Forum
Printing a number sequence not working properly
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Printing a number sequence not working properly
#1
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)
Reply
#2
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#3
Oh! Thank you so much for your help. Much appreciated.
Reply
#4
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:
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#5
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.
Craig "Ichabod" O'Brien - xenomind.com
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Reply
#6
(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()
If you can't explain it to a six year old, you don't understand it yourself, Albert Einstein
How to Ask Questions The Smart Way: link and another link
Create MCV example
Debug small programs

Reply
#7
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!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Create sum clusters of a number sequence BramQBIC 7 932 Nov-24-2023, 12:09 PM
Last Post: Pedroski55
  Printing sequence of numbers kam_uk 1 2,797 Sep-27-2020, 01:50 PM
Last Post: perfringo
  Enigma Program not working properly npd29 3 2,037 May-01-2020, 10:37 AM
Last Post: pyzyx3qwerty
  Why is this function printing the number of keys in a dictionary? mafr97 5 2,980 Sep-17-2019, 06:19 AM
Last Post: perfringo
  Find not working properly fad3r 2 2,440 Feb-11-2018, 03:18 PM
Last Post: fad3r

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020