diff options
| author | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-07-23 17:15:21 -0400 |
|---|---|---|
| committer | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-07-23 17:15:21 -0400 |
| commit | 99c4e905c974e2e92e238c10cbfc771e89b6cc90 (patch) | |
| tree | 7b9b94dc1bb5ba419d0a9f383e60b785b2e99f20 /challenge-331 | |
| parent | fa6ac79755faeac2482b731861634bf63036ff3a (diff) | |
| download | perlweeklychallenge-club-99c4e905c974e2e92e238c10cbfc771e89b6cc90.tar.gz perlweeklychallenge-club-99c4e905c974e2e92e238c10cbfc771e89b6cc90.tar.bz2 perlweeklychallenge-club-99c4e905c974e2e92e238c10cbfc771e89b6cc90.zip | |
challenge 330 python, perl, and go solutions
Diffstat (limited to 'challenge-331')
| -rw-r--r-- | challenge-331/ysth/go/ch-1.go | 38 | ||||
| -rw-r--r-- | challenge-331/ysth/go/ch-2.go | 34 | ||||
| -rw-r--r-- | challenge-331/ysth/perl/ch-1.pl | 21 | ||||
| -rw-r--r-- | challenge-331/ysth/perl/ch-2.pl | 18 | ||||
| -rw-r--r-- | challenge-331/ysth/python/ch-1.py | 15 | ||||
| -rw-r--r-- | challenge-331/ysth/python/ch-2.py | 15 |
6 files changed, 141 insertions, 0 deletions
diff --git a/challenge-331/ysth/go/ch-1.go b/challenge-331/ysth/go/ch-1.go new file mode 100644 index 0000000000..209cebff64 --- /dev/null +++ b/challenge-331/ysth/go/ch-1.go @@ -0,0 +1,38 @@ +package main + +import ( + "fmt" + "os" + "regexp" + "github.com/clipperhouse/uax29/graphemes" +) + +func grapheme_length(word string) int { + var grapheme_length int = 0 + var seg = graphemes.NewSegmenter([]byte(word)) + for seg.Next() { + grapheme_length++ + } + return grapheme_length +} + +func last_match(r *regexp.Regexp, s string) string { + var out string + for offset, loc := 0, []int{0,0}; loc != nil; loc = r.FindStringIndex(s[offset:]) { + out = s[offset+loc[0]:offset+loc[1]] + offset += loc[1] + } + return out +} + +var word = regexp.MustCompile(`[\p{L}\p{N}\p{M}\p{Pc}]+`) + +func last_word_length(s string) int { + return grapheme_length(last_match(word, s)) +} + +func main() { + for _, string := range os.Args[1:] { + fmt.Printf("%-30s -> %d\n", string, last_word_length(string)) + } +} diff --git a/challenge-331/ysth/go/ch-2.go b/challenge-331/ysth/go/ch-2.go new file mode 100644 index 0000000000..421c5a8892 --- /dev/null +++ b/challenge-331/ysth/go/ch-2.go @@ -0,0 +1,34 @@ +package main + +import ( + "fmt" + "os" +) + +func buddy_strings(string1 string, string2 string) bool { + if (string1 == string2) { return false } + var runes1 = []rune(string1) + var runes2 = []rune(string2) + if len(runes1) != len(runes2) { return false } + var differences int = 0 + for i := range runes1 { + if runes1[i] != runes2[i] { + differences++ + if differences > 1 { return false } + } + } + return true +} + +func main() { + var inputs = os.Args[1:] + for arg := 0; arg < len(inputs) - 1; arg += 2 { + var buddies string + if buddy_strings(inputs[arg], inputs[arg+1]) { + buddies = "yes" + } else { + buddies = "no" + } + fmt.Printf("%-30s\n%-30s\n -> %s\n", inputs[arg], inputs[arg+1], buddies) + } +} diff --git a/challenge-331/ysth/perl/ch-1.pl b/challenge-331/ysth/perl/ch-1.pl new file mode 100644 index 0000000000..053c9b813b --- /dev/null +++ b/challenge-331/ysth/perl/ch-1.pl @@ -0,0 +1,21 @@ +use 5.036; + +use utf8::all; # utf8 @ARGV + +sub grapheme_length($string) { + scalar( () = $string =~ /\X/g ) +} + +sub last_word_length($string) { + # words are made up of letters, numbers, modifiers, and underscore-like punctuation + grapheme_length reverse($string) =~ /([\pL\pN\pM\p{pC}]+)/ ? scalar reverse $1 : '' +} + +sub main() { + my @inputs = @ARGV; + for my $string (@inputs) { + printf "%-30s -> %s\n", $string, last_word_length $string; + } +} + +main() unless caller; diff --git a/challenge-331/ysth/perl/ch-2.pl b/challenge-331/ysth/perl/ch-2.pl new file mode 100644 index 0000000000..2aebf3e46f --- /dev/null +++ b/challenge-331/ysth/perl/ch-2.pl @@ -0,0 +1,18 @@ +use 5.040; + +use utf8::all; # utf8 @ARGV +use Text::Fuzzy (); + +sub buddy_strings($string1, $string2) { + length $string1 == length $string2 + and Text::Fuzzy->new($string1, max => 1, no_exact => 1) == 1 +} + +sub main() { + my @inputs = @ARGV; + for my ($string1, $string2) (@inputs) { + printf "%-30s\n%-30s\n -> %s\n", $string1, $string2, (buddy_strings($string1, $string2) ? 'yes' : 'no'); + } +} + +main() unless caller; diff --git a/challenge-331/ysth/python/ch-1.py b/challenge-331/ysth/python/ch-1.py new file mode 100644 index 0000000000..815e1f4cfe --- /dev/null +++ b/challenge-331/ysth/python/ch-1.py @@ -0,0 +1,15 @@ +import sys +import grapheme +import regex + +def last_word_length(string: str) -> str: + return grapheme.length(regex.sub(r'^.*[^\pL\pN\pM\p{pC}](?=[\pL\pN\pM\p{pC}])|(?<=[\pL\pN\pM\p{pC}])[^\pL\pN\pM\p{pC}].*', '', string)) + +def main() -> None: + inputs: list[str] = sys.argv[1:] + + for string in inputs: + print(f'{string:<30} -> {last_word_length(string)}') + +if __name__ == '__main__': + main() diff --git a/challenge-331/ysth/python/ch-2.py b/challenge-331/ysth/python/ch-2.py new file mode 100644 index 0000000000..6d0f7c80e4 --- /dev/null +++ b/challenge-331/ysth/python/ch-2.py @@ -0,0 +1,15 @@ +import sys +import regex +from itertools import batched + +def buddy_strings(string1: str, string2: str) -> bool: + return True if regex.fullmatch(regex.escape(string1)+'{1<=s<=1}', string2) else False + +def main() -> None: + inputs: list[str] = sys.argv[1:] + + for string1, string2 in batched(inputs, 2): + print(f'{string1:<30}\n{string2:<30}\n -> {"yes" if buddy_strings(string1, string2) else "no"}') + +if __name__ == '__main__': + main() |
