diff options
| author | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-08-17 19:03:25 -0400 |
|---|---|---|
| committer | Yitzchak Scott-Thoennes <sthoenna@gmail.com> | 2025-08-17 19:03:25 -0400 |
| commit | 10d8f8beccc598f1157a6b4314e151edc3ffcf91 (patch) | |
| tree | ad91b794e4080a38e45e9a461eaef1aa33446efe | |
| parent | 3f0c25ac7a8089ff3d921c6e4926ff88ebd806cd (diff) | |
| download | perlweeklychallenge-club-10d8f8beccc598f1157a6b4314e151edc3ffcf91.tar.gz perlweeklychallenge-club-10d8f8beccc598f1157a6b4314e151edc3ffcf91.tar.bz2 perlweeklychallenge-club-10d8f8beccc598f1157a6b4314e151edc3ffcf91.zip | |
weekly challenge 334 perl, python, and go solutions
| -rw-r--r-- | challenge-334/ysth/blog.txt | 1 | ||||
| -rw-r--r-- | challenge-334/ysth/go/ch-1.go | 42 | ||||
| -rw-r--r-- | challenge-334/ysth/go/ch-2.go | 54 | ||||
| -rw-r--r-- | challenge-334/ysth/perl/ch-1.pl | 45 | ||||
| -rw-r--r-- | challenge-334/ysth/perl/ch-2.pl | 67 | ||||
| -rw-r--r-- | challenge-334/ysth/python/ch-1.py | 45 | ||||
| -rw-r--r-- | challenge-334/ysth/python/ch-2.py | 65 |
7 files changed, 319 insertions, 0 deletions
diff --git a/challenge-334/ysth/blog.txt b/challenge-334/ysth/blog.txt new file mode 100644 index 0000000000..76311ab489 --- /dev/null +++ b/challenge-334/ysth/blog.txt @@ -0,0 +1 @@ +https://blog.ysth.info/weekly-challenge-334-divertissments/ diff --git a/challenge-334/ysth/go/ch-1.go b/challenge-334/ysth/go/ch-1.go new file mode 100644 index 0000000000..a808ee8a07 --- /dev/null +++ b/challenge-334/ysth/go/ch-1.go @@ -0,0 +1,42 @@ +package main + +import ( + "encoding/json" + "errors" + "fmt" + "os" +) + +func range_sum(ints []int, x int, y int) (int, error) { + sum := int(0) + if x < 0 || y < x || y >= len(ints) { + return sum, errors.New("index out of range") + } + for i := x; i <= y; i++ { + sum += ints[i] + } + return sum, nil +} + +func main() { + errors := false + for _, json_inputs := range os.Args[1:] { + var inputs struct { Ints []int; X int; Y int } + err := json.Unmarshal([]byte(json_inputs), &inputs) + if err != nil { + fmt.Printf("%-30s -> ERROR %v\n", json_inputs, err) + errors = true + } else { + result, err := range_sum(inputs.Ints, inputs.X, inputs.Y) + if err != nil { + fmt.Printf("%-30s -> ERROR %v\n", json_inputs, err) + errors = true + } else { + fmt.Printf("%-30s -> %v\n", json_inputs, result) + } + } + } + if errors { + fmt.Printf("Expected arguments like '{\"ints\":[1,2,3],\"x\":0,\"y\":1}'\n") + } +} diff --git a/challenge-334/ysth/go/ch-2.go b/challenge-334/ysth/go/ch-2.go new file mode 100644 index 0000000000..f2fc023114 --- /dev/null +++ b/challenge-334/ysth/go/ch-2.go @@ -0,0 +1,54 @@ +package main + +import ( + "encoding/json" + "fmt" + "os" + "math" +) + +func nearest_valid_point(x int, y int, points [][2]int) (int, error) { + nearest_index := -1 + distance := math.MaxInt + for i, point := range points { + var this_distance int + if point[0] == x { + this_distance = y - point[1] + } else if point[1] == y { + this_distance = x - point[0] + } else { + continue + } + if this_distance < 0 { + this_distance = -this_distance + } + if this_distance < distance { + nearest_index = i + distance = this_distance + } + } + return nearest_index, nil +} + +func main() { + errors := false + for _, json_inputs := range os.Args[1:] { + var inputs struct { X int; Y int; Points [][2]int } + err := json.Unmarshal([]byte(json_inputs), &inputs) + if err != nil { + fmt.Printf("%-30s -> ERROR %v\n", json_inputs, err) + errors = true + } else { + result, err := nearest_valid_point(inputs.X, inputs.Y, inputs.Points) + if err != nil { + fmt.Printf("%-30s -> ERROR %v\n", json_inputs, err) + errors = true + } else { + fmt.Printf("%-30s -> %v\n", json_inputs, result) + } + } + } + if errors { + fmt.Printf("Expected arguments like '{\"x\":3,\"y\":4,\"points\":[[1, 2],[3, 1], [2, 4], [2, 3]]}'\n") + } +} diff --git a/challenge-334/ysth/perl/ch-1.pl b/challenge-334/ysth/perl/ch-1.pl new file mode 100644 index 0000000000..c85c766c87 --- /dev/null +++ b/challenge-334/ysth/perl/ch-1.pl @@ -0,0 +1,45 @@ +use 5.036; +use Cpanel::JSON::XS; +use JSON::Schema::Modern; +use List::Util (); + +sub range_sum($ints, $x, $y) { + die "index out of range" if $x < 0 || $y < $x || $y > $#$ints; + + return List::Util::sum($ints->@[$x..$y]) +} + +sub main() { + my $json = Cpanel::JSON::XS->new; + my $validator = JSON::Schema::Modern->new( + 'specification_version' => 'draft2020-12', + 'output_format' => 'flag', + ); + my $schema = $json->decode('{ + "type": "object", + "properties": { + "x": {"type": "integer"}, + "y": {"type": "integer"}, + "points": { + "type": "array", + "items": { "type": "integer" } + } + }, + "required": ["ints","x","y"] + }'); + + for my $input_string (@ARGV) { + my $input_object = eval { $json->decode($input_string) }; + if (! $input_object || ! $validator->evaluate($input_object, $schema)->valid) { + say "Invalid input: ", $input_string; + next; + } + + my $result = range_sum(@$input_object{qw/ints x y/}); + printf "%-30s -> %s\n", $input_string, $result; + } + + return; +} + +main() unless caller; diff --git a/challenge-334/ysth/perl/ch-2.pl b/challenge-334/ysth/perl/ch-2.pl new file mode 100644 index 0000000000..b76ec3b486 --- /dev/null +++ b/challenge-334/ysth/perl/ch-2.pl @@ -0,0 +1,67 @@ +use 5.036; +use Cpanel::JSON::XS; +use JSON::Schema::Modern; +use List::Util (); + +sub nearest_valid_point($x, $y, $points) { + my $nearest_index = -1; + my $distance; + for my $i (0..$#$points) { + my $this_distance; + if ($points->[$i][0] == $x) { + $this_distance = $y - $points->[$i][1]; + } + elsif ($points->[$i][1] == $y) { + $this_distance = $x - $points->[$i][0]; + } + else { + next; + } + $this_distance = -$this_distance if $this_distance < 0; + if ($nearest_index < 0 or $this_distance < $distance) { + $distance = $this_distance; + $nearest_index = $i; + } + } + return $nearest_index; +} + +sub main() { + my $json = Cpanel::JSON::XS->new; + my $validator = JSON::Schema::Modern->new( + 'specification_version' => 'draft2020-12', + 'output_format' => 'flag', + ); + my $schema = $json->decode('{ + "type": "object", + "properties": { + "x": {"type": "integer"}, + "y": {"type": "integer"}, + "points": { + "type": "array", + "items": { + "type": "array", + "prefixItems": [{"type": "integer"},{"type": "integer"}], + "minItems": 2, + "items": false + } + } + }, + "required": ["x","y","points"] + }'); + + for my $input_string (@ARGV) { + my $input_object = eval { $json->decode($input_string) }; + if (! $input_object || ! $validator->evaluate($input_object, $schema)->valid) { + say "Invalid input: ", $input_string; + next; + } + + my $result = nearest_valid_point(@$input_object{qw/x y points/}); + printf "%-30s -> %s\n", $input_string, $result; + } + + return; +} + +main() unless caller; diff --git a/challenge-334/ysth/python/ch-1.py b/challenge-334/ysth/python/ch-1.py new file mode 100644 index 0000000000..9a9ba73d7e --- /dev/null +++ b/challenge-334/ysth/python/ch-1.py @@ -0,0 +1,45 @@ +import sys +import json +import jsonschema + +def range_sum(ints: list[int], x: int, y: int) -> int: + sum = 0 + if x < 0 or y < x or y >= len(ints): + raise Exception("index out of range") + for i in range(x,y+1): + sum += ints[i] + return sum + +def main() -> None: + validator = jsonschema.Draft202012Validator({ + "type": "object", + "properties": { + "x": {"type": "integer"}, + "y": {"type": "integer"}, + "points": { + "type": "array", + "items": { "type": "integer" }, + }, + }, + "required": {"ints","x","y"}, + }) + + inputs: list[str] = sys.argv[1:] + + for input_string in inputs: + try: + input_object = json.loads(input_string) + except Exception as inst: + print(inst) + continue + try: + validator.validate(input_object) + except jsonschema.exceptions.ValidationError as inst: + print(inst.message) + continue + + result = range_sum(**input_object) + print(f'{input_string:<30} -> {result}') + +if __name__ == '__main__': + main() diff --git a/challenge-334/ysth/python/ch-2.py b/challenge-334/ysth/python/ch-2.py new file mode 100644 index 0000000000..dad232a37d --- /dev/null +++ b/challenge-334/ysth/python/ch-2.py @@ -0,0 +1,65 @@ +import sys +import json +import jsonschema + +def nearest_valid_point(x: int, y: int, points: list[list[int]]) -> int: + nearest_index = -1 + distance: int + for i in range(len(points)): + this_distance: int + if points[i][0] == x: + #print("x matches") + this_distance = y - points[i][1] + elif points[i][1] == y: + #print("y matches") + this_distance = x - points[i][0] + else: + continue + #print(f'this distance {this_distance}') + if this_distance < 0: + this_distance = -this_distance + #print(f'this distance {this_distance} nearest_index {nearest_index}') + if nearest_index < 0 or this_distance < distance: + #print('setting') + distance = this_distance + nearest_index = i + return nearest_index + +def main() -> None: + validator = jsonschema.Draft202012Validator({ + "type": "object", + "properties": { + "x": {"type": "integer"}, + "y": {"type": "integer"}, + "points": { + "type": "array", + "items": { + "type": "array", + "prefixItems": [{"type": "integer"},{"type": "integer"}], + "minItems": 2, + "items": False, + }, + }, + }, + "required": {"x","y","points"}, + }) + + inputs: list[str] = sys.argv[1:] + + for input_string in inputs: + try: + input_object = json.loads(input_string) + except Exception as inst: + print(inst) + continue + try: + validator.validate(input_object) + except jsonschema.exceptions.ValidationError as inst: + print(inst.message) + continue + + result = nearest_valid_point(**input_object) + print(f'{input_string:<30} -> {result}') + +if __name__ == '__main__': + main() |
