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


Messages In This Thread
Printing a number sequence not working properly - by deepsen - Oct-12-2019, 08:44 AM

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,983 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