https://github.com/simpsonresearch/14uniquechars
Find the first 14 unique characters in a string. Two different algorithm implementations.
https://github.com/simpsonresearch/14uniquechars
algorithm python speed teach
Last synced: 3 months ago
JSON representation
Find the first 14 unique characters in a string. Two different algorithm implementations.
- Host: GitHub
- URL: https://github.com/simpsonresearch/14uniquechars
- Owner: simpsonresearch
- License: mit
- Created: 2023-05-23T00:17:01.000Z (over 2 years ago)
- Default Branch: main
- Last Pushed: 2023-05-23T00:26:51.000Z (over 2 years ago)
- Last Synced: 2025-08-11T15:30:00.993Z (4 months ago)
- Topics: algorithm, python, speed, teach
- Language: Python
- Homepage:
- Size: 4.88 KB
- Stars: 0
- Watchers: 0
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# 14uniquechars
Find the first 14 unique characters in a string. Two different algorithm implementations.
```py
import time
s: str = "abcdefgghijklmnopqrstuvwxyz"
b: bytes = bytes(s, 'ascii')
def one(start: int) -> str:
data: dict[str, int] = {}
tot: int = 0
if start >= len(s):
return ""
for i in range(len(s)):
if s[i] in data:
return one(start+1)
else:
data[s[i]] = 0
tot += 1
if tot == 14:
return s[start:tot+start]
return ""
def two(start: int) -> str:
arr: list[int] = [0] * 32
tot: int = 0
for i in range(len(b) - start):
d: int = b[start+i] % 32
if arr[d]:
return two(i+1)
arr[d] = 1
tot += 1
if tot == 14:
return s[start:tot+start]
return ""
st: float = time.time()
print(one(0))
t1: float = time.time()-st
st: float = time.time()
print(two(0))
t2: float = time.time()-st
print(t2)
print(t1/t2)
# two is 7x faster than one
```