1

I'm currently trying to learn python. Suppose there was a a number n = 12345. How would one go about changing every digit starting from the first spot and iterating it between (1-9) and every other spot after (0-9). I'm sadly currently learning python so I apologize for all the syntax error that might follow. Here's my last few attempts/idea for skeleton of the code.

define the function
turn n into string
start with a for loop that for i in n range(0,9) for i[1]
else range(10)

Basically how does one fix a number while changing the others? Please don't give solution just hints I enjoy the thinking process.

For example if n =29 the program could check
19,39,49,59,69,79,89,99
and
21,22,23,24,25,26,27,28
Neil
  • 14,063
  • 3
  • 30
  • 51
user415853
  • 13
  • 2
  • Can you be more specific? This isn't a place for "hints", it's a question and answer site. – juanpa.arrivillaga Mar 17 '17 at 07:17
  • Hi thanks for responding, basically is it possible for a function to hold one digit of a large number fixed while changing the rest. For example is there a function that would check if 298 changing the first digit from 1-9 the second and third from 0-9. and between each iteration checking to see it the number was even (I know it's silly since you only need to check the last number, but suppose) Thanks for the edit nfn, and I can see how I was vague I could learn from the answer I suppose – user415853 Mar 17 '17 at 07:22

1 Answers1

0

Although you are new, the process seems far easy than you think.

You want to make that change to every digit of the number (let's say n=7382). But you cannot iterate over numbers (not even changing specific digits of it as you want to): only over iterables (like lists). A string is an iterable. If you get the way to do a int-str conversion, you could iterate over every number and then print the new number.

But how do you change only the digit you are iterating to? Again, the way is repeating the conversion (saving it into a var before the loop would make great DRY) and getting a substring that gets all numbers except the one you are. There are two ways of doing this:

  1. You search for that specific value and get its index (bad).
  2. You enumerate the loop (good).

Why 2 is good? Because you have the real position of the actual number being change (think that doing an index in 75487 with 7 as the actual one changing would not work well when you get to the last one). Search for a way to iterate over items in a loop to get its actual index.

The easiest way to get a substring in Python is slicing. You slice two times: one to get all numbers before the actual one, and other to get all after it. Then you just join those two str with the actual variable number and you did it.

I hope I didn't put it easy for you, but is hard for a simple task as that.

RompePC
  • 815
  • 1
  • 8
  • 17
  • Thanks @RompePC I wasn't aware of splicing but it seems to be an easier way of doing what I've been attempting (brace yourself)counter = 0 str(n) a = n for n,i in enumerate(a): if i == 1: a[n] =2 if i == 2: a[n] = 3 if i == 3: a[n] =4 if i == 4: a[n] = 5 Do you mind doing a small splicing example – user415853 Mar 17 '17 at 07:30
  • 1
    Although is great you posted the code, would be better if you edit the post and put it in, because here isn't readable xDD. Anyway, here is a general hint: when you don't know how to do such a thing (like this number changing), `str` conversion is normally the way to go. Mastering it will make life easier. – RompePC Mar 17 '17 at 07:34
  • Thanks so much I think I am on the right track now! Much Appreciated! – user415853 Mar 17 '17 at 07:36
  • I just saw you want an slicing [example](http://stackoverflow.com/questions/509211/explain-pythons-slice-notation): `my_string[:3]` would get the first 3 chars of `my_string` (exclusive). – RompePC Mar 17 '17 at 07:41