Check If Number Has A Digit Multiple Times
Solution 1:
If your ultimate goal is simply detecting if there's a double, this function may help:
defhas_doubles(n):
returnlen(set(str(n))) < len(str(n))
Solution 2:
The best way I can think about is converting the number to a string and doing a Counter on it
from collections import Counter
a = 98
c = Counter(str(a))
ifany(value > 1for value in c.values()):
print"The number has repeating digits"
@Two-BitAlchemist thanks for the suggestion
Solution 3:
looks like you wanted to create your own algorithm probably researching or a student practice well you just have to understand the properties of numbers divided by 10 where 1/10 = 0.1 10/10 = 1 13/10 = 1 reminder 3 13013/10 = 1301 rem 3 hence we can create a function that stores the reminders in an array an check them against the reminder of next number here is the algorithm in python using recursion, you can achieve the same via loops
defcountNumber(foundDigits,number):
next_number = int(number/10);
reminder = number % 10;
if(next_number < 1):
for num in foundDigits:
if(num == number or num == reminder):
returnTruereturnFalse;
foundDigits.append(reminder);
return countNumber(foundDigits,next_number)
example in interpreter could be
digitsFound = list()
countNumber(digitsFound, 435229)
Solution 4:
Solved this! I didn't know pop executes based on position not value! remove is a better fit here.
arr = [[1,40],[1,10]]for i in arr:
l = list(range(i[0],i[1]))
for num in l:
if num < 11: continue
forcharin str(num):
if str(num).count(char) < 2: continue
l.remove(num)
breakprint(l)
Solution 5:
Here is my solution, its simple and works for 2 digit numbers.
nums = list(input().rstrip().split())
defhas_doubles(nums):
for number in nums:
if number[0] == number[1]:
print(number)
else:
continue
has_doubles(nums)
Post a Comment for "Check If Number Has A Digit Multiple Times"