diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 2ad95510..256798c8 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -65,7 +65,7 @@ If your function doesn't return anything just show how to use it. If the result To ensure your snippet isn’t refused, consider these questions: - **Does the standard library of my language provide an easy way of doing this ?** -- **Does that snippet have a real, and practical use case ?** +- **Does that snippet not have a real, and practical use case ?** - **Could it be split into separate parts to be better understood ?** If any answer is yes, then your snippet will most likely get rejected. diff --git a/public/consolidated/python.json b/public/consolidated/python.json index 8acadcfc..801424a5 100644 --- a/public/consolidated/python.json +++ b/public/consolidated/python.json @@ -535,16 +535,17 @@ "code": "def snake_to_camel(s):\n parts = s.split('_')\n return parts[0] + ''.join(word.capitalize() for word in parts[1:])\n\n# Usage:\nsnake_to_camel('hello_world') # Returns: 'helloWorld'\n" }, { - "title": "Convert String to ASCII", - "description": "Converts a string into its ASCII representation.", + "title": "Convert String to Unicode", + "description": "Converts a string into its Unicode representation.", "author": "axorax", "tags": [ "string", "ascii", + "unicode", "convert" ], "contributors": [], - "code": "def string_to_ascii(s):\n return [ord(char) for char in s]\n\n# Usage:\nstring_to_ascii('hello') # Returns: [104, 101, 108, 108, 111]\n" + "code": "def string_to_unicode(s):\n return [ord(char) for char in s]\n\n# Usage:\nstring_to_unicode('hello') # Returns: [104, 101, 108, 108, 111]\n" }, { "title": "Count Character Frequency", @@ -626,6 +627,18 @@ "contributors": [], "code": "import random\nimport string\n\ndef random_string(length):\n letters_and_digits = string.ascii_letters + string.digits\n return ''.join(random.choice(letters_and_digits) for _ in range(length))\n\n# Usage:\nrandom_string(10) # Results: Random 10-character string\n" }, + { + "title": "Remove Characters", + "description": "Removes specific characters from a string.", + "author": "axorax", + "tags": [ + "string", + "remove", + "characters" + ], + "contributors": [], + "code": "def remove_chars(s, chars):\n return ''.join(c for c in s if c not in chars)\n\n# Usage:\nremove_chars('hello world', 'eo') # Returns: 'hll wrld'\n" + }, { "title": "Remove Duplicate Characters", "description": "Removes duplicate characters from a string while maintaining the order.", @@ -650,18 +663,6 @@ "contributors": [], "code": "import string\n\ndef remove_punctuation(s):\n return s.translate(str.maketrans('', '', string.punctuation))\n\n# Usage:\nremove_punctuation('Hello, World!') # Returns: 'Hello World'\n" }, - { - "title": "Remove Specific Characters", - "description": "Removes specific characters from a string.", - "author": "axorax", - "tags": [ - "string", - "remove", - "characters" - ], - "contributors": [], - "code": "def remove_chars(s, chars):\n return ''.join(c for c in s if c not in chars)\n\n# Usage:\nremove_chars('hello world', 'eo') # Returns: 'hll wrld'\n" - }, { "title": "Remove Whitespace", "description": "Removes all whitespace from a string.", @@ -683,7 +684,7 @@ "reverse" ], "contributors": [], - "code": "def reverse_string(s):\n return s[::-1]\n\n# Usage:\nreverse_string('hello') # Returns: 'olleh'\n" + "code": "def reverse_string(s:str) -> str:\n return s[::-1]\n\n# Usage:\nreverse_string('hello') # Returns: 'olleh'\n" }, { "title": "Split Camel Case", @@ -698,15 +699,17 @@ "code": "import re\n\ndef split_camel_case(s):\n return ' '.join(re.findall(r'[A-Z][a-z]*|[a-z]+', s))\n\n# Usage:\nsplit_camel_case('camelCaseString') # Returns: 'camel Case String'\n" }, { - "title": "Truncate String", - "description": "Truncates a string to a specified length and adds an ellipsis.", + "title": "Truncate", + "description": "Truncates a string to a specified length and a toggleable truncation notation.", "author": "axorax", "tags": [ "string", "truncate" ], - "contributors": [], - "code": "def truncate_string(s, length):\n return s[:length] + '...' if len(s) > length else s\n\n# Usage:\ntruncate_string('This is a long string', 10) # Returns: 'This is a ...'\n" + "contributors": [ + "MinerMinerMods" + ], + "code": "def truncate(s:str, length:int, suffix:bool = True) -> str :\n return (s[:length] + (\"...\" if suffix else \"\")) if len(s) > length else s\n\n# Usage:\ntruncate('This is a long string', 10) # Returns: 'This is a ...'\ntruncate('This is a long string', 10, False) # Returns: 'This is a '\n" } ] } diff --git a/snippets/python/string-manipulation/convert-string-to-ascii.md b/snippets/python/string-manipulation/convert-string-to-ascii.md deleted file mode 100644 index 61e830ba..00000000 --- a/snippets/python/string-manipulation/convert-string-to-ascii.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -title: Convert String to ASCII -description: Converts a string into its ASCII representation. -author: axorax -tags: string,ascii,convert ---- - -```py -def string_to_ascii(s): - return [ord(char) for char in s] - -# Usage: -string_to_ascii('hello') # Returns: [104, 101, 108, 108, 111] -``` diff --git a/snippets/python/string-manipulation/convert-string-to-unicode.md b/snippets/python/string-manipulation/convert-string-to-unicode.md new file mode 100644 index 00000000..28d3f676 --- /dev/null +++ b/snippets/python/string-manipulation/convert-string-to-unicode.md @@ -0,0 +1,14 @@ +--- +title: Convert String to Unicode +description: Converts a string into its Unicode representation. +author: axorax +tags: string,ascii,unicode,convert +--- + +```py +def string_to_unicode(s): + return [ord(char) for char in s] + +# Usage: +string_to_unicode('hello') # Returns: [104, 101, 108, 108, 111] +``` diff --git a/snippets/python/string-manipulation/remove-specific-characters.md b/snippets/python/string-manipulation/remove-characters.md similarity index 88% rename from snippets/python/string-manipulation/remove-specific-characters.md rename to snippets/python/string-manipulation/remove-characters.md index 3965ae9b..b4a5366a 100644 --- a/snippets/python/string-manipulation/remove-specific-characters.md +++ b/snippets/python/string-manipulation/remove-characters.md @@ -1,5 +1,5 @@ --- -title: Remove Specific Characters +title: Remove Characters description: Removes specific characters from a string. author: axorax tags: string,remove,characters diff --git a/snippets/python/string-manipulation/reverse-string.md b/snippets/python/string-manipulation/reverse-string.md index fa045607..1334a775 100644 --- a/snippets/python/string-manipulation/reverse-string.md +++ b/snippets/python/string-manipulation/reverse-string.md @@ -6,7 +6,7 @@ tags: string,reverse --- ```py -def reverse_string(s): +def reverse_string(s:str) -> str: return s[::-1] # Usage: diff --git a/snippets/python/string-manipulation/truncate-string.md b/snippets/python/string-manipulation/truncate-string.md deleted file mode 100644 index 9788ac3e..00000000 --- a/snippets/python/string-manipulation/truncate-string.md +++ /dev/null @@ -1,14 +0,0 @@ ---- -title: Truncate String -description: Truncates a string to a specified length and adds an ellipsis. -author: axorax -tags: string,truncate ---- - -```py -def truncate_string(s, length): - return s[:length] + '...' if len(s) > length else s - -# Usage: -truncate_string('This is a long string', 10) # Returns: 'This is a ...' -``` diff --git a/snippets/python/string-manipulation/truncate.md b/snippets/python/string-manipulation/truncate.md new file mode 100644 index 00000000..5238f53b --- /dev/null +++ b/snippets/python/string-manipulation/truncate.md @@ -0,0 +1,16 @@ +--- +title: Truncate +description: Truncates a string to a specified length and a toggleable truncation notation. +author: axorax +contributors: MinerMinerMods +tags: string,truncate +--- + +```py +def truncate(s:str, length:int, suffix:bool = True) -> str : + return (s[:length] + ("..." if suffix else "")) if len(s) > length else s + +# Usage: +truncate('This is a long string', 10) # Returns: 'This is a ...' +truncate('This is a long string', 10, False) # Returns: 'This is a ' +```