Skip to content

Commit adf4f4f

Browse files
committed
DAJ 320 Blog
1 parent b395cf8 commit adf4f4f

File tree

1 file changed

+125
-0
lines changed

1 file changed

+125
-0
lines changed
Lines changed: 125 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
---
2+
layout: post
3+
title: "Lots and Lots of Integers!: Weekly Challenge #320"
4+
author: "Dave Jacoby"
5+
date: "2025-05-08 18:06:51 -0400"
6+
categories: ""
7+
---
8+
9+
Welcome to [**_Weekly Challenge #320!_**](https://theweeklychallenge.org/blog/perl-weekly-challenge-320/) This week's title is inspired by a video my child used to watch, [Lots and Lots of Jets and Planes!](https://www.youtube.com/watch?v=3fMGTHyYPTQ)
10+
11+
### Task 1: Maximum Count
12+
13+
> Submitted by: Mohammad Sajid Anwar
14+
> You are given an array of integers.
15+
>
16+
> Write a script to return the maximum between the number of positive and negative integers. Zero is neither positive nor negative.
17+
18+
#### Let's Talk About It
19+
20+
We get the count of positive and negative integers in a functional way. `grep` allows us to strip out the integers we want, and `scalar` gives us a count, rather than the values. Then I use a ternary operator to determinte whether the count of positive integers are highter than the count of negative integers, and `return` the higher count.
21+
22+
#### Show Me The Code!
23+
24+
```perl
25+
#!/usr/bin/env perl
26+
27+
use strict;
28+
use warnings;
29+
use experimental qw{ say state postderef signatures };
30+
31+
my @examples = (
32+
33+
[ -3, -2, -1, 1, 2, 3 ],
34+
[ -2, -1, 0, 0, 1 ],
35+
[ 1, 2, 3, 4 ],
36+
);
37+
38+
for my $example (@examples) {
39+
my $str = join ', ', $example->@*;
40+
my $output = max_count( $example->@* );
41+
say <<"END";
42+
Input: \$str = ($str)
43+
Output: $output
44+
END
45+
}
46+
47+
sub max_count(@ints) {
48+
my $pos = scalar grep { $_ > 0 } @ints;
49+
my $neg = scalar grep { $_ < 0 } @ints;
50+
return $pos > $neg ? $pos : $neg;
51+
}
52+
```
53+
54+
```text
55+
$ ./ch-1.pl
56+
Input: $str = (-3, -2, -1, 1, 2, 3)
57+
Output: 3
58+
59+
Input: $str = (-2, -1, 0, 0, 1)
60+
Output: 2
61+
62+
Input: $str = (1, 2, 3, 4)
63+
Output: 4
64+
```
65+
66+
# Task 2: Sum Difference
67+
68+
> Submitted by: Mohammad Sajid Anwar
69+
> You are given an array of positive integers.
70+
>
71+
> Write a script to return the absolute difference between digit sum and element sum of the given array.
72+
73+
#### Let's Talk About It
74+
75+
Because [List::Util](https://metacpan.org/pod/List::Util) gives us `sum0`, I use that. (Again, I agree with `sum0`'s behavior, not that it matters for most uses.) I use `map` and `split` to separate integers into separate digits, and then subtraction and `abs` to finish the math.
76+
77+
#### Show Me The Code!
78+
79+
```perl
80+
#!/usr/bin/env perl
81+
82+
use strict;
83+
use warnings;
84+
use experimental qw{ say state postderef signatures };
85+
86+
use List::Util qw{ sum0 };
87+
88+
my @examples = (
89+
90+
[ 1, 23, 4, 5 ],
91+
[ 1, 2, 3, 4, 5 ],
92+
[ 1, 2, 34 ],
93+
);
94+
95+
for my $example (@examples) {
96+
my $str = join ', ', $example->@*;
97+
my $output = sum_diff( $example->@* );
98+
say <<"END";
99+
Input: \$str = ($str)
100+
Output: $output
101+
END
102+
}
103+
104+
sub sum_diff (@ints) {
105+
my $digit_sum = sum0 @ints;
106+
my $element_sum = sum0 map { split //, $_ } @ints;
107+
return abs $digit_sum - $element_sum;
108+
}
109+
```
110+
111+
```text
112+
$ ./ch-2.pl
113+
Input: $str = (1, 23, 4, 5)
114+
Output: 18
115+
116+
Input: $str = (1, 2, 3, 4, 5)
117+
Output: 0
118+
119+
Input: $str = (1, 2, 34)
120+
Output: 27
121+
122+
jacoby  Bishop  ~  win  320  $ 
123+
```
124+
125+
#### If you have any questions or comments, I would be glad to hear it. Ask me on [Mastodon](https://mastodon.xyz/@jacobydave) or [make an issue on my blog repo.](https://github.com/jacoby/jacoby.github.io)

0 commit comments

Comments
 (0)