Python Challenge - Unique Characters in a String

def unique_characters(string_in):
  new_list = []
  if string_in is "":
    return "Please enter a string"
  else:
      for char in string_in:
        if char in new_list:
          return False
        else:
          new_list.append(char)
  return True



print(unique_characters("apple"))

def unique_characters(string_in):
remove_duplicate=set(string_in)
sorted_duplicate=sorted(remove_duplicate)
sorted_original=sorted(string_in)
if string_in==’’:
return “ERROR”
if sorted_original==sorted_duplicate:
return True
else:
return False

print(unique_characters(“apple”))

def unique_characters(string_in):
set_list = set(string_in)
if len(set_list) != len(string_in):
return False
elif len(string_in) == 0:
return “error message”
else:
return True

print(unique_characters(“apple”))

def unique_characters(string_in): if len(string_in) == 0: return "error" for i in range(0,len(string_in)): cont = 0 for j in range(0,len(string_in)): if string_in[j] == string_in [i]: cont+=1 if cont >1: return False return True print(unique_characters(""))
def unique_characters(string):
  string = sorted(string)

  if string == "" :
    return "error message"
  else :
    for val in range(0,len(string)) :
      counts = string.count(string[val])
      if counts >= 2:
        return False
      elif counts == 1:
        return True

print(unique_characters("apple"))

def unique_characters(string_in): if string_in == "": print("Error") return letters = [] for s in string_in: if s in letters: return False else: letters.append(s) return True print(unique_characters("apple"))

Very easy once I remembered the set() method…

def unique_characters(string_in):
letters = set(string_in)
if string_in ==“”:
print(“Error”)
elif len(letters) < len(string_in):
return False
else:
return True

print(unique_characters(“apple”))

Not the most elegant solution but it’s fairly simple and works

def unique_characters(string_in): char = [] if string_in == "": return 'Error' for i in string_in: if i in char: return False else: char.append(i) return True print(unique_characters("apple"))
def unique_characters(string_in): #the attempted approach is not optimal since it operates in O(n²) #however it is an intuitive solution which doesn't require vast knowledge of python functions l = [] #empty list for char in string_in: #iteration over the input string l.append(char) #add every character in string to list if len(l) > 0: #in case we are given an empty string for i in string_in: #second iteration over the input string count = 0 #def of count variable to count the matches of each i for j in l: #iteration over the items in the list if i == j: #comparison between every item in the list and the respective character i count += 1 #add number matches to count if count >= 2: #if we have more than one match... return False #... we don't have a unique_characters() string else: #if we don't have any double+ matching characters or an empty string... return True #... we got a unique_characters() string print(unique_characters("Terry"))
def unique_characters(string_in): if len(string_in) > 0: chars = [] for e in string_in: chars.append(e) for c in chars: if chars.count(c) > 1: return False return True else: return ValueError("String must be longer than 0") print(unique_characters("apple"))

This was my solution:

def unique_characters(string_in):
letter_list =
if string_in == “”:
return “error”
else:
for i in string_in:
letter_list.append(i)
for t in letter_list:
nn = letter_list.count(t)
if nn != 1:
return False
else:
return True

print(unique_characters(“”))

def unique_characters(string_in):
for i in range(len(string_in)):
if (i==len(string_in)-1):
return True
for j in range(i+1,len(string_in)):
print(i,j)
if string_in[i]==string_in[j]:
return False

print(unique_characters(“apleb”))

def unique_characters(string_in):
seen = set()

if string_in == “”:
return “Error”

for letter in string_in:
if letter in seen:
return False
seen.add(letter)

return True

print(unique_characters(“apple”))

def unique_characters(string_in): if string_in is "": return "Enter a string" else: if(len(set(string_in))==len(string_in)): return True else: return False print(unique_characters("apple"))

In order for all the test cases to pass, we also need to consider the empty “” string case. We verify if we have only unique characters in our string if the length of the string is equal to the one of the set. If they are equal, the function returns True, since the set presents the unique values of the string.

1 Like
def unique_characters(string_in): if len(string_in) == 0: return None edit = string_in.lower() lister = [] for i in range(0, len(edit)): if edit[i] not in lister: lister.append(edit[i]) if len(lister) == len(edit): return True return False print(unique_characters(""))

Did not print error message, can try to print error message before returning None.

Nice challenge!
I struggled on how to iterate through the remaining string characters given the character choice of the first iteration. Done! :muscle:

def unique_characters(string_in): if string_in == "": print ('Error: empty string') else: for i in range(len(string_in)): for j in range(i+1,len(string_in)): if string_in.find(string_in[i], j) != -1: return False break return True print(unique_characters("apple"))

This was a really cleaver solution. Congrats! :clap:

1 Like

@tbgoes thank you! :grin: