From 48467961a3c23c3798480bb6090b27b8895a54bf Mon Sep 17 00:00:00 2001 From: Andrade Junior Date: Wed, 2 Oct 2019 01:33:51 -0300 Subject: [PATCH] Add is unique algorithm --- allalgorithms/string/__init__.py | 2 +- allalgorithms/string/is_unique.py | 16 ++++++++++++++ docs/string/is-unique.md | 36 +++++++++++++++++++++++++++++++ tests/test_string.py | 11 +++++++++- 4 files changed, 63 insertions(+), 2 deletions(-) create mode 100644 allalgorithms/string/is_unique.py create mode 100644 docs/string/is-unique.md diff --git a/allalgorithms/string/__init__.py b/allalgorithms/string/__init__.py index 6d8b504..301d40c 100644 --- a/allalgorithms/string/__init__.py +++ b/allalgorithms/string/__init__.py @@ -1,2 +1,2 @@ from .palindrome_check import * - +from .is_unique import * \ No newline at end of file diff --git a/allalgorithms/string/is_unique.py b/allalgorithms/string/is_unique.py new file mode 100644 index 0000000..986a30c --- /dev/null +++ b/allalgorithms/string/is_unique.py @@ -0,0 +1,16 @@ +# -*- coding: UTF-8 -*- +# +# Check if a string has all unique characters. +# The All ▲lgorithms library for python +# +# Contributed by: José E. Andrade Jr. +# Github: @andradejunior +# + +def is_unique(string_to_check): + character_set = set() + for character in string_to_check: + if character in character_set: + return False + character_set.add(character) + return True diff --git a/docs/string/is-unique.md b/docs/string/is-unique.md new file mode 100644 index 0000000..7dc439f --- /dev/null +++ b/docs/string/is-unique.md @@ -0,0 +1,36 @@ +# Is Unique + +This algorithms checks if a string has all unique characters in O(n) time. + +## Install + +``` +pip install allalgorithms +``` + +## Usage + +```py +from allalgorithms.string import is_unique + +str = "abcdefg" + +print(is_unique(str) +# -> True + +str = "test" + +print(is_unique(str) +# -> False +``` + +## API + +### is_unique(string) + +> Return True if string has all unique characters, False otherwise + +##### Params: + +- `string`: Input String + diff --git a/tests/test_string.py b/tests/test_string.py index 998daf0..3cca91c 100644 --- a/tests/test_string.py +++ b/tests/test_string.py @@ -1,6 +1,6 @@ import unittest -from allalgorithms.string import palindrome_check +from allalgorithms.string import palindrome_check, is_unique class TestSorting(unittest.TestCase): @@ -13,5 +13,14 @@ def test_palindrome_check(self): self.assertEqual(True, palindrome_check("Was it a car or a cat I saw?")) self.assertEqual(False, palindrome_check("How are you?")) + def test_is_unique(self): + self.assertEqual(True, is_unique("abcdefg")) + self.assertEqual(True, is_unique("1234567")) + self.assertEqual(True, is_unique("algorithms")) + self.assertEqual(False, is_unique("abcdefa")) + self.assertEqual(False, is_unique("abddefg")) + self.assertEqual(False, is_unique("12345567")) + + if __name__ == "__main__": unittest.main() pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy