+ 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
8 odpowiedzi
+ 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
+ 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
+ 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!
+ 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.
+ 2
Tibor Santa,please check this one.
https://code.sololearn.com/c4Lsv0c4RRHW/?ref=app
+ 2
Tibor Santa,thank you very much.Its ridiculous that I didnt notice this simple solution.
+ 1
Lothar,please do check this.
https://code.sololearn.com/c4Lsv0c4RRHW/?ref=app
+ 1
You're welcome, no worries. Discovering new patterns and best practices, is part of mastering the language. Keep up the good work ;)