+ 1

Finding the occurrence of a digit in python

I am trying to make a code that can find out if a number is a unique number or not(no recurrent digits).There,i came at a point where i needed to check if the recurrent variable belonged to the same index position,or was situated at a different index position.However,the .find() method only returns the first occurrence of that digit.Is there a way i can check the index position of 2 digits with the same value in an integer? https://code.sololearn.com/c4Lsv0c4RRHW/?ref=app

26th Mar 2019, 8:26 PM
Soham Roy
Soham Roy - avatar
8 Réponses
+ 4
Okay so this is the small change that will make your code work: if now == yo and i != c: You loop over the string twice. So if the digits are the same but the indexes are different, then the digit is repeated. https://code.sololearn.com/cvscclmzownn/?ref=app
27th Mar 2019, 5:50 AM
Tibor Santa
Tibor Santa - avatar
+ 4
Soham, here 2 other versions from me for your unique digit program. https://code.sololearn.com/cNuDfiSFZVIS/?ref=app https://code.sololearn.com/cHj6im3jvFxj/?ref=app
27th Mar 2019, 11:32 AM
Lothar
Lothar - avatar
+ 3
hi Soham, it would be great if you can place your current version of the code in the Code Playground. Then it’s much easier to check possible solutions for you. Thanks!
26th Mar 2019, 9:09 PM
Lothar
Lothar - avatar
+ 3
To me the most pythonic approach would be to make a dictionary comprehension on the count of digits. Like this: https://code.sololearn.com/cU5jC5HBo56k/?ref=app There can be many possible good solutions, would be happy to give tips on your own program if you actually post it.
26th Mar 2019, 9:14 PM
Tibor Santa
Tibor Santa - avatar
+ 2
Tibor Santa,please check this one. https://code.sololearn.com/c4Lsv0c4RRHW/?ref=app
27th Mar 2019, 5:35 AM
Soham Roy
Soham Roy - avatar
+ 2
Tibor Santa,thank you very much.Its ridiculous that I didnt notice this simple solution.
27th Mar 2019, 5:56 AM
Soham Roy
Soham Roy - avatar
+ 1
27th Mar 2019, 5:32 AM
Soham Roy
Soham Roy - avatar
+ 1
You're welcome, no worries. Discovering new patterns and best practices, is part of mastering the language. Keep up the good work ;)
27th Mar 2019, 5:58 AM
Tibor Santa
Tibor Santa - avatar