Skip to content

Commit c11933c

Browse files
committed
License Key Formatter (#157)
1 parent 23b5528 commit c11933c

3 files changed

Lines changed: 56 additions & 0 deletions

File tree

‎strings/README.md‎

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
* [Crop String](#crop-string)
55
* [First Unique Character](#first-unique-character-in-a-string)
66
* [Jewels and Stones](#jewels-and-stones)
7+
* [License Key Formatter](#license-key-formatter)
78
* [Longest Common Prefix](#longest-common-prefix)
89
* [Longest Substring Without Duplicates](#longest-substring-without-duplicates)
910
* [Multiply Strings](#multiply-strings)
@@ -174,6 +175,37 @@ def jewels_and_stones(jewels: str, stones: str) -> int:
174175
return count
175176
```
176177

178+
## License Key Formatter
179+
180+
Given a license string which consists only alphanumeric character and dashes.
181+
The string is separated into `N+1` groups by `N` dashes.
182+
183+
Given a `group_length` reformat the strings such that each group contains the exact number of characters,
184+
except for the first which could be shorter than `group_length`, but still must contain at least one character.
185+
There must be a dash inserted between groups and all lowercase letters should be converted to uppercase.
186+
187+
Example:
188+
189+
Input: `5F3Z-2e-9-w`, 3
190+
Output: `5F-3Z2-E9W`
191+
192+
```python
193+
def license_key_formatter(license_str: str, group_length: int) -> str:
194+
license_keys = license_str.replace('-', '').upper()
195+
remainder = len(license_keys) % group_length
196+
197+
start = 0
198+
end = remainder if remainder > 0 else group_length
199+
license_groups = []
200+
201+
while end <= len(license_keys):
202+
license_groups.append(license_keys[start:end])
203+
start = end
204+
end += group_length
205+
206+
return '-'.join(license_groups)
207+
```
208+
177209
## Longest Common Prefix
178210

179211
Write a function to find the longest common prefix string amongst an array of strings.

‎strings/license_key_formatter.py‎

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
2+
3+
def license_key_formatter(license_str: str, group_length: int) -> str:
4+
license_keys = license_str.replace('-', '').upper()
5+
remainder = len(license_keys) % group_length
6+
7+
start = 0
8+
end = remainder if remainder > 0 else group_length
9+
license_groups = []
10+
11+
while end <= len(license_keys):
12+
license_groups.append(license_keys[start:end])
13+
start = end
14+
end += group_length
15+
16+
return '-'.join(license_groups)
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
from strings.license_key_formatter import license_key_formatter
2+
3+
4+
def test_license_key_formatter():
5+
assert license_key_formatter('5F3Z-2e-9-w', 4) == '5F3Z-2E9W'
6+
assert license_key_formatter('5F3Z-2e-9-w', 3) == '5F-3Z2-E9W'
7+
assert license_key_formatter('TES-T', 4) == 'TEST'
8+
assert license_key_formatter('t-est', 2) == 'TE-ST'

0 commit comments

Comments
 (0)