11

I am supposed to find the number of unique characters in a string.

Here's the catch, no arrays or even while loops (although for loop can accomplish this). I can't seem to do it without writing a bunch of ridiculous if-else statements under a for loop checking each char value against each other.

mkrieger1
  • 19,194
  • 5
  • 54
  • 65
Son Gohan
  • 111
  • 1
  • 1
  • 3
  • 3
    show your attempts. We are not here to write code for you. – Avinash Raj Feb 21 '15 at 04:26
  • I haven't made a physical attempt yet, just trying to come up with a plan. I don't want code to be written for me, I just want to see if there is even a way to do it without a huge body of ifel statements given the constraints. – Son Gohan Feb 21 '15 at 04:33
  • 5
    If this is homework, use `len(set(the_string))` – Christian Tapia Feb 21 '15 at 04:34
  • Not homework, I don't think we can use the set function either since it hasn't been taught yet. – Son Gohan Feb 21 '15 at 04:37
  • Not allowed to use lists or tuples. – Son Gohan Feb 21 '15 at 04:37
  • Christian's answer is the best way to do it. As a TA for intro programming courses, it never hurts to learn ahead. – Adam Hughes Feb 21 '15 at 04:38
  • I'm sure it's an efficient method of solving the problem but we're not supposed to use functions that have not been formally introduced. If that were the case I could create an array and check for uniqueness that way. I would like to know, conceptually, how to do this using only for loops, ifel conditionals, and string methods. If possible, thanks for the help so far though. – Son Gohan Feb 21 '15 at 04:41

5 Answers5

20

Use this:

len(set("stackoverflow"))  # returns 12
Vlad Havriuk
  • 1,291
  • 17
  • 29
5

Since you said you are not allowed to use set and thus the solution Christian recommended: len(set('aabbcd'))

You can solve it using a for loop like you wanted to like so:

string = 'aabbcd'
unique = []
for char in string[::]:
    if char not in unique:
        unique.append(char)
print(len(unique))

Output: 4

Please do upload your attempts next time though and let us try and help you with why those did not work. This website isn't for doing homework your assignments for you.

Edit: Replaced list() that you also said you couldn't use.

Vale
  • 1,003
  • 1
  • 9
  • 22
2

Count the number of unique characters in a string Python using only for loops and ifel operations

To count the number of unique ASCII letters in a string s:

import string

s = 'AabC'
s = s.lower()
print(sum(1 for c in string.ascii_lowercase if s.count(c) == 1))

If you want to count upper/lower case letters separately then remove s.lower() line and use string.ascii_letters instead of string.ascii_lowercase. The result may differ from len(set(s)) if there could be other symbols in the string such as punctuations e.g., ':!'

jfs
  • 399,953
  • 195
  • 994
  • 1,670
2

So not allowed are; 'while loops', 'sets', 'arrays' (and lists I assume)..

You can create a string with all unique characters and loop through those with a for loop, this way you don't need a list, array array or while loop.

letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz,.?*&^%$#@!etc"
def unique_count(some_string):
    letter_count = 0
    for letter in letters:
        if letter in some_string:
        letter_count += 1
    return letter_count

print(unique_count("whatEver@")

// this will print 9 (because E != e )

Perhaps not as elegant because of the long string to match against, but it meets the requirement of not using lists, arrays or while loop.

Steven B. Peutz
  • 141
  • 1
  • 8
1

You can do this way by using only for loop and by applying len() function on list.

def unique_count(word):
  k=list()
  for x in word:
    if x not in k:
      k.append(x)
  return len(k)

print(unique_count("stackoverflow is goOd"))

//prints 17 because case sensitive and count whitespace

you can also make a function and call it whenever you want just by passing the argument.

def unique_count(word):
  k=list()
  for x in word:
    if x not in k:
      k.append(x)
  return len(k)

print(unique_count("stackoverflow is goOd"))

//prints 17
Dharman
  • 30,962
  • 25
  • 85
  • 135