Skip to content

Commit 01146f3

Browse files
committed
Merge branch 'main' of github.com:dostonnabotov/quicksnip
2 parents 87f2076 + 52e7c9c commit 01146f3

12 files changed

+181
-51
lines changed

CONTRIBUTING.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ If your function doesn't return anything just show how to use it. If the result
6565

6666
To ensure your snippet isn’t refused, consider these questions:
6767
- **Does the standard library of my language provide an easy way of doing this ?**
68-
- **Does that snippet have a real, and practical use case ?**
68+
- **Does that snippet not have a real, and practical use case ?**
6969
- **Could it be split into separate parts to be better understood ?**
7070

7171
If any answer is yes, then your snippet will most likely get rejected.

public/consolidated/python.json

Lines changed: 23 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -535,16 +535,17 @@
535535
"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"
536536
},
537537
{
538-
"title": "Convert String to ASCII",
539-
"description": "Converts a string into its ASCII representation.",
538+
"title": "Convert String to Unicode",
539+
"description": "Converts a string into its Unicode representation.",
540540
"author": "axorax",
541541
"tags": [
542542
"string",
543543
"ascii",
544+
"unicode",
544545
"convert"
545546
],
546547
"contributors": [],
547-
"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"
548+
"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"
548549
},
549550
{
550551
"title": "Count Character Frequency",
@@ -626,6 +627,18 @@
626627
"contributors": [],
627628
"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"
628629
},
630+
{
631+
"title": "Remove Characters",
632+
"description": "Removes specific characters from a string.",
633+
"author": "axorax",
634+
"tags": [
635+
"string",
636+
"remove",
637+
"characters"
638+
],
639+
"contributors": [],
640+
"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"
641+
},
629642
{
630643
"title": "Remove Duplicate Characters",
631644
"description": "Removes duplicate characters from a string while maintaining the order.",
@@ -650,18 +663,6 @@
650663
"contributors": [],
651664
"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"
652665
},
653-
{
654-
"title": "Remove Specific Characters",
655-
"description": "Removes specific characters from a string.",
656-
"author": "axorax",
657-
"tags": [
658-
"string",
659-
"remove",
660-
"characters"
661-
],
662-
"contributors": [],
663-
"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"
664-
},
665666
{
666667
"title": "Remove Whitespace",
667668
"description": "Removes all whitespace from a string.",
@@ -683,7 +684,7 @@
683684
"reverse"
684685
],
685686
"contributors": [],
686-
"code": "def reverse_string(s):\n return s[::-1]\n\n# Usage:\nreverse_string('hello') # Returns: 'olleh'\n"
687+
"code": "def reverse_string(s:str) -> str:\n return s[::-1]\n\n# Usage:\nreverse_string('hello') # Returns: 'olleh'\n"
687688
},
688689
{
689690
"title": "Split Camel Case",
@@ -698,15 +699,17 @@
698699
"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"
699700
},
700701
{
701-
"title": "Truncate String",
702-
"description": "Truncates a string to a specified length and adds an ellipsis.",
702+
"title": "Truncate",
703+
"description": "Truncates a string to a specified length and a toggleable truncation notation.",
703704
"author": "axorax",
704705
"tags": [
705706
"string",
706707
"truncate"
707708
],
708-
"contributors": [],
709-
"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"
709+
"contributors": [
710+
"MinerMinerMods"
711+
],
712+
"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"
710713
}
711714
]
712715
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Date time formatting american
3+
description: Formats a timestamp to a human-readable date-time string in the format "MM/dd/yyyy hh:mm:ss a"
4+
author: Mcbencrafter
5+
tags: date,time,date-time,formatting,american
6+
---
7+
8+
```java
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
12+
import java.util.concurrent.TimeUnit;
13+
14+
// using the system default time zone
15+
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit) {
16+
return formatDateTimeAmerican(time, timeUnit, ZoneId.systemDefault());
17+
}
18+
19+
public static String formatDateTimeAmerican(long time, TimeUnit timeUnit, ZoneId timeZone) {
20+
return DateTimeFormatter.ofPattern("MM/dd/yyyy hh:mm:ss a")
21+
.withZone(
22+
timeZone != null ? timeZone : ZoneId.systemDefault()
23+
)
24+
.format(Instant.ofEpochSecond(
25+
timeUnit.toSeconds(time)
26+
));
27+
}
28+
29+
// Usage:
30+
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS)); // "12/31/2024 | 11:59:59 PM" for GMT+0000
31+
System.out.println(formatDateTimeAmerican(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "12/31/2024 | 11:59:59 PM"
32+
```
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
---
2+
title: Date time formatting european
3+
description: Formats a timestamp to a human-readable date-time string in the format "dd.MM.yyyy HH:mm:ss"
4+
author: Mcbencrafter
5+
tags: date,time,date-time,formatting,european
6+
---
7+
8+
```java
9+
import java.time.Instant;
10+
import java.time.ZoneId;
11+
import java.time.format.DateTimeFormatter;
12+
import java.util.concurrent.TimeUnit;
13+
14+
// using the system default time zone
15+
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit) {
16+
return formatDateTimeEuropean(time, timeUnit, ZoneId.systemDefault());
17+
}
18+
19+
public static String formatDateTimeEuropean(long time, TimeUnit timeUnit, ZoneId timeZone) {
20+
return DateTimeFormatter.ofPattern("dd.MM.yyyy HH:mm:ss")
21+
.withZone(
22+
timeZone != null ? timeZone : ZoneId.systemDefault()
23+
)
24+
.format(Instant.ofEpochSecond(
25+
timeUnit.toSeconds(time)
26+
));
27+
}
28+
29+
// Usage:
30+
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS)); // "31.12.2024 | 23:59:59" for GMT+0000
31+
System.out.println(formatDateTimeEuropean(1735689599, TimeUnit.SECONDS, ZoneId.of("GMT+0000"))); // "31.12.2024 | 23:59:59"
32+
```
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
---
2+
title: Duration formatting hours minutes seconds
3+
description: Converts a given time duration to a human-readable string in the format "hh:mm(:ss)"
4+
author: Mcbencrafter
5+
tags: time,formatting,hours,minutes,seconds
6+
---
7+
8+
```java
9+
import java.util.concurrent.TimeUnit;
10+
11+
public static String formatDurationToHoursMinutesAndSeconds(int time, TimeUnit timeUnit, boolean showSeconds) {
12+
long totalSeconds = timeUnit.toSeconds(time);
13+
14+
if (totalSeconds < 0)
15+
throw new IllegalArgumentException("Duration must be a non-negative value.");
16+
17+
// These variables can be directly used in the return statement,
18+
// but are kept as separate variables here for better readability.
19+
long hours = totalSeconds / 3600;
20+
long minutes = (totalSeconds % 3600) / 60;
21+
long seconds = totalSeconds % 60;
22+
23+
if (showSeconds) {
24+
return String.format("%02d:%02d:%02d", hours, minutes, seconds);
25+
} else {
26+
return String.format("%02d:%02d", hours, minutes);
27+
}
28+
}
29+
30+
// Usage:
31+
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, true)); // "01:03:30"
32+
System.out.println(formatDurationToHoursMinutesAndSeconds(3810, TimeUnit.SECONDS, false)); // "01:03"
33+
```
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
---
2+
title: Duration formatting minutes seconds
3+
description: Converts a given time duration to a human-readable string in the format "mm:ss"
4+
author: Mcbencrafter
5+
tags: time,formatting,minutes,seconds
6+
---
7+
8+
```java
9+
import java.util.concurrent.TimeUnit;
10+
11+
public static String formatDurationToMinutesAndSeconds(int time, TimeUnit timeUnit) {
12+
long totalSeconds = timeUnit.toSeconds(time);
13+
14+
if (totalSeconds < 0)
15+
throw new IllegalArgumentException("Duration must be a non-negative value.");
16+
17+
// These variables can be directly used in the return statement,
18+
// but are kept here as separate variables for better readability.
19+
long minutes = totalSeconds / 60;
20+
long seconds = totalSeconds % 60;
21+
22+
return String.format("%02d:%02d", minutes, seconds);
23+
}
24+
25+
// Usage:
26+
System.out.println(formatDurationToMinutesAndSeconds(120, TimeUnit.SECONDS)); // "02:00"
27+
System.out.println(formatDurationToMinutesAndSeconds(75, TimeUnit.SECONDS)); // "01:15"
28+
```

snippets/python/string-manipulation/convert-string-to-ascii.md

Lines changed: 0 additions & 14 deletions
This file was deleted.
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
---
2+
title: Convert String to Unicode
3+
description: Converts a string into its Unicode representation.
4+
author: axorax
5+
tags: string,ascii,unicode,convert
6+
---
7+
8+
```py
9+
def string_to_unicode(s):
10+
return [ord(char) for char in s]
11+
12+
# Usage:
13+
string_to_unicode('hello') # Returns: [104, 101, 108, 108, 111]
14+
```

snippets/python/string-manipulation/remove-specific-characters.md renamed to snippets/python/string-manipulation/remove-characters.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
---
2-
title: Remove Specific Characters
2+
title: Remove Characters
33
description: Removes specific characters from a string.
44
author: axorax
55
tags: string,remove,characters

snippets/python/string-manipulation/reverse-string.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ tags: string,reverse
66
---
77

88
```py
9-
def reverse_string(s):
9+
def reverse_string(s:str) -> str:
1010
return s[::-1]
1111

1212
# Usage:

0 commit comments

Comments
 (0)