Python Coding Challenge - Unique String

Solution for Pass the Technical Interview - Python Coding Challenge - Unique String

INSTRUCTIONS:
Unique Characters in a String

Write a unique_characters() function that determines if any given string has all unique characters (i.e. no character in the string is duplicated). If the string has all unique characters, the function should return True. If the string does not have all unique characters, return False.

For example, unique_characters("apple") should return False.

def unique_characters(string_in): unique = [] text = list(string_in) print(string_in.split(' ')) #base case if len(string_in) < 1: print("Criteria not met for determination.") return "Error Message" for letter in text: if letter not in unique: unique.append(letter) if len(unique) < len(text): print("'{}' is not a Unique string".format(string_in)) return False elif len(unique) == len(text): print("'{}' is a Unique string,".format(string_in)) return True #Tests for all possibilities print(unique_characters("apple")) print(unique_characters("zebra")) print(unique_characters("")) print(unique_characters('o'))

Are you asking for help or clarification or constructive feedback?
I was wondering why you’re posting solutions to coding challenges here? Should it go under another category like Projects or something instead?

Don’t get me wrong, it’s great that you have completed them (hooray! :partying_face:), I just wonder if they should be here b/c others can copy your code and not really try to work out the challenges on their own.

See the community guidelines, specifically the “Do’s and Don’ts” section:
" * Don’t post full working code without explanation."
“Please do not post answers…”

Good job! Here’s a one-liner:

def solve(s):
    return len(s) == len(set(s)) if len(s) >= 1 else False


if __name__ == '__main__':
    print(solve('apple'))  # False
    print(solve('zebra'))  # True
    print(solve(''))       # False
    print(solve('o'))      # True

very readable, but doesn’t pass all tests.

Were there more test cases? This solution passes all test cases that you posted, no?

On the course work page if you run your function it says ‘4 out of 5 tests pass’.
Curiously, there are no additional files to have access to the criteria for the tests.
That seems to be the format for all these coding challenges.
I found by adding a loop with different conditional statements it eventually satisfies all the
requirements for passing all tests without actually knowing how/why specifically.

Link to Challenge: Unique Characters in a String

Instructions: Note that if the function is called with an empty string (“”), the program should return an error message.

Thank you for a link to the challenge. This should work:

def unique_characters(s):
   return 'Error message' if s == '' else len(s) == len(set(s)) if len(s) >= 1 else False