-2

Here is what I currently have:

unique = 0

while n > 0:    
    last, n = n % 10, n // 10    
        if not has_digit(n, last):    
            unique += 1    
    return unique

I'm trying to have it so for every unique number in an integer, it outputs how many unique numbers there are.

For Example:

unique_digits(123456) = 6    
unique_digits(112222) = 2

>>> unique_digits(8675309) # All are unique
    7
    >>> unique_digits(1313131) # 1 and 3
    2
    >>> unique_digits(13173131) # 1, 3, and 7
    3
    >>> unique_digits(10000) # 0 and 1
    2
    >>> unique_digits(101) # 0 and 1
    2
    >>> unique_digits(10) # 0 and 1
    2
    """
Friender0140
  • 7
  • 1
  • 3
  • It would probably be easier to turn the number into a String and count the characters. And what specifically are you asking? – Carcigenicate Jan 16 '18 at 05:53
  • @Carcigenicate I'm asking to make a function on how to count how many times a unique number appears in an integer. If it appears once, +1. If it appears 5 times, still only +1 – Friender0140 Jan 16 '18 at 05:56
  • 1
    This questions was answered here: https://stackoverflow.com/questions/48273921/how-do-i-return-the-number-of-unique-digits-in-a-positive-integer/48274012#48274012 moments ago. – pcgben Jan 16 '18 at 05:57
  • 1
    @Friender0140 I got that, but what specifically about that problem do you need help with? This is rather broad. – Carcigenicate Jan 16 '18 at 05:58
  • What's `has_digit`? Why do you have a `return` statement outside of a function? – PM 2Ring Jan 16 '18 at 06:02
  • @PM2Ring I'm trying to use the python built in function (has_digit) and its not outside the function, just a formatting error – Friender0140 Jan 16 '18 at 06:03
  • doesn't 56 count as a unique number? or 456? Do you mean unique digit? – Alexander Jan 16 '18 at 06:08
  • @Alexander meant digit – Friender0140 Jan 16 '18 at 06:13
  • I've never heard of a built-in `has_digit` function. Where did you find it? There is a `str` method `isdigit`. – PM 2Ring Jan 16 '18 at 06:24

2 Answers2

5

First make a string out of it, then get unique values by making a set and then count with len.

len(set(str(122226)))
# 3
Darkonaut
  • 20,186
  • 7
  • 54
  • 65
0

The easiest way would be to convert the numbers into string and pass it into set. This will only retain unique digits.

>>> len( set(str(num)) )

EDIT :

As a function,

def unique_digits(num): 
    return len( set(str(num)) )

#driver code :

>>> print(unique_digits(123456))
6
>>> print(unique_digits(112222))
2
Kaushik NP
  • 6,733
  • 9
  • 31
  • 60
  • I keep getting nothing in my doctest when I do it this way. – Friender0140 Jan 16 '18 at 06:02
  • @Friender0140 What doctest? Please include all relevant information in your question. – PM 2Ring Jan 16 '18 at 06:03
  • Um, you have to `print` the `unique_dig` – Kaushik NP Jan 16 '18 at 06:04
  • @KaushikNP I'm trying to do this: unique_digits = len( set(str(n)) ) print(unique_digits(28212)) and getting none – Friender0140 Jan 16 '18 at 06:06
  • Um no... You have to do just `unique_digits = len( set(str(28212)) )` `print(unique_digits)` – Kaushik NP Jan 16 '18 at 06:07
  • @KaushikNP How can I make it so any number I put as num would print the output im striving to get. Also I'm getting an error saying function unique_digits at 0x00000187c0541400 – Friender0140 Jan 16 '18 at 06:10
  • Ok. For better clarity, I'll write the whole function itself. – Kaushik NP Jan 16 '18 at 06:11
  • @Friender0140 : pls check the edit – Kaushik NP Jan 16 '18 at 06:14
  • @KaushikNP I have this right now: def unique_digits(n): return len(set(str(n)) print (unique_digits(123456)) print (unique_digits(112222)) Would this work? Not sure how to format it in Lines. – Friender0140 Jan 16 '18 at 06:21
  • @KaushikNP I'm using what u put in ur edit but its still giving me errors – Friender0140 Jan 16 '18 at 06:22
  • Can you pls mention what the error is that you are getting? – Kaushik NP Jan 16 '18 at 06:22
  • @KaushikNP These are my doctests: >>> unique_digits(8675309) # All are unique 7 >>> unique_digits(1313131) # 1 and 3 2 >>> unique_digits(13173131) # 1, 3, and 7 3 >>> unique_digits(10000) # 0 and 1 2 >>> unique_digits(101) # 0 and 1 2 >>> unique_digits(10) # 0 and 1 2 """ and im getting EOL while scanning string literal – Friender0140 Jan 16 '18 at 06:24
  • @KaushikNP how can i put my comments in lines so they are more readable? – Friender0140 Jan 16 '18 at 06:25
  • @Friender0140 Don't bury important information in the comments on an answer. Put it into your question, where it belongs. – PM 2Ring Jan 16 '18 at 06:27
  • As @PM2Ring mentioned, the info you are providing is not enough. What doctest? Pls include properly formatted info in the question as an Edit. – Kaushik NP Jan 16 '18 at 06:28
  • @KaushikNP Sorry about that, my doctests are edited in the question – Friender0140 Jan 16 '18 at 06:29
  • @Friender0140 : sorry buddy, but am not sure about Doctest. You'll have to cosult someone else on this. Maybe PM2Ring can help you out there. Continue this thread in the Question's comments section for better response. – Kaushik NP Jan 16 '18 at 06:32