Bottom Page

• 0 Vote(s) - 0 Average
• 1
• 2
• 3
• 4
• 5
 Printing a number sequence not working properly deepsen Unladen Swallow Posts: 4 Threads: 2 Joined: Oct 2019 Reputation: 0 Likes received: 0 #1 Oct-12-2019, 08:44 AM (This post was last modified: Oct-12-2019, 09:23 AM by Yoriz. Edited 2 times in total. Edit Reason: Added code tags ) 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)``` Yoriz wrote Oct-12-2019, 09:23 AM:Please post all code, output and errors (in it's entirety) between their respective tags. I did it for you this time, Here are instructions on how to do it yourself next time. ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 272 Likes received: 1264 #2 Oct-12-2019, 01:35 PM 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 deepsen Unladen Swallow Posts: 4 Threads: 2 Joined: Oct 2019 Reputation: 0 Likes received: 0 #3 Oct-12-2019, 03:52 PM Oh! Thank you so much for your help. Much appreciated. buran Posts: 6,020 Threads: 102 Joined: Sep 2016 Reputation: 462 Likes received: 1260 #4 Oct-12-2019, 04:09 PM (This post was last modified: Oct-12-2019, 04:15 PM by buran. Edited 3 times in total.) 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: Hide/Show ```n = int(input('n> ')) for k in range(1, n+1): print(k, end='') print() `````````Output:n> 12 123456789101112`````` ichabod801 Bunny Rabbit Posts: 4,231 Threads: 94 Joined: Sep 2016 Reputation: 272 Likes received: 1264 #5 Oct-12-2019, 06:18 PM 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 buran Posts: 6,020 Threads: 102 Joined: Sep 2016 Reputation: 462 Likes received: 1260 #6 Oct-12-2019, 06:37 PM (This post was last modified: Oct-12-2019, 06:37 PM by buran. Edited 1 time in total.) (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() deepsen Unladen Swallow Posts: 4 Threads: 2 Joined: Oct 2019 Reputation: 0 Likes received: 0 #7 Oct-12-2019, 07:43 PM 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! « Next Oldest | Next Newest »

Top Page

 Possibly Related Threads... Thread Author Replies Views Last Post Enigma Program not working properly npd29 3 163 May-01-2020, 10:37 AM Last Post: pyzyx3qwerty Why is this function printing the number of keys in a dictionary? mafr97 5 382 Sep-17-2019, 06:19 AM Last Post: perfringo Find not working properly fad3r 2 901 Feb-11-2018, 03:18 PM Last Post: fad3r

Forum Jump:

Users browsing this thread: 1 Guest(s)