|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "Not Really Ready For Funny Titles: Weekly Challenge #315" |
| 4 | +author: "Dave Jacoby" |
| 5 | +date: "2025-04-03 21:46:04 -0400" |
| 6 | +categories: "" |
| 7 | +--- |
| 8 | + |
| 9 | +Welcome to [Weekly Challenge #315.](https://theweeklychallenge.org/blog/perl-weekly-challenge-315/) I try to make the introduction to each entry amusing and informational, but I just don't feel it this week. Sorry. |
| 10 | + |
| 11 | +### Task 1: Find Words |
| 12 | + |
| 13 | +> Submitted by: Mohammad Sajid Anwar |
| 14 | +> You are given a list of words and a character.> |
| 15 | +> |
| 16 | +> Write a script to return the index of word in the list where you find the given character. |
| 17 | +
|
| 18 | +#### Let's Talk About It |
| 19 | + |
| 20 | +This is the most simple, given the correct subset of Perl knowledge. Create an array of valid indexes of each array. Search each word, addressed by that array, for the character in question with a simple regular expression. If it passes, pass that value. I use `grep` for this. Without `grep`, it becomes more complex, but as is, there's pulling the values into variables and `return grep { $list[$_] =~ /$char/ } 0 .. $#list;` |
| 21 | + |
| 22 | +If anyone is looking for a Perl guy, contact me. |
| 23 | + |
| 24 | +#### Show Me The Code! |
| 25 | + |
| 26 | +```perl |
| 27 | +#!/usr/bin/env perl |
| 28 | + |
| 29 | +use strict; |
| 30 | +use warnings; |
| 31 | +use experimental qw{ say state postderef signatures }; |
| 32 | + |
| 33 | +my @examples = ( |
| 34 | + { |
| 35 | + list => [ "the", "weekly", "challenge" ], |
| 36 | + char => "e", |
| 37 | + }, |
| 38 | + { |
| 39 | + list => [ "perl", "raku", "python" ], |
| 40 | + char => "p", |
| 41 | + }, |
| 42 | + { |
| 43 | + list => [ "abc", "def", "bbb", "bcd" ], |
| 44 | + char => "b", |
| 45 | + }, |
| 46 | +); |
| 47 | + |
| 48 | +for my $example (@examples) { |
| 49 | + my $list = join ',', map { qq{"$_"} } $example->{list}->@*; |
| 50 | + my $char = $example->{char}; |
| 51 | + my @output = find_words($example); |
| 52 | + my $output = join ', ', @output; |
| 53 | + say <<"END"; |
| 54 | + Input: \@list = ($list) |
| 55 | + \$char = "$char" |
| 56 | + Output: ($output) |
| 57 | +END |
| 58 | +} |
| 59 | + |
| 60 | +sub find_words ($obj) { |
| 61 | + my @list = $obj->{list}->@*; |
| 62 | + my $char = $obj->{char}; |
| 63 | + return grep { $list[$_] =~ /$char/ } 0 .. $#list; |
| 64 | +} |
| 65 | +``` |
| 66 | + |
| 67 | +```text |
| 68 | +$ ./ch-1.pl |
| 69 | + Input: @list = ("the","weekly","challenge") |
| 70 | + $char = "e" |
| 71 | + Output: (0, 1, 2) |
| 72 | +
|
| 73 | + Input: @list = ("perl","raku","python") |
| 74 | + $char = "p" |
| 75 | + Output: (0, 2) |
| 76 | +
|
| 77 | + Input: @list = ("abc","def","bbb","bcd") |
| 78 | + $char = "b" |
| 79 | + Output: (0, 2, 3) |
| 80 | +``` |
| 81 | + |
| 82 | +### Task 2: Find Third |
| 83 | + |
| 84 | +> Submitted by: Mohammad Sajid Anwar |
| 85 | +> You are given a sentence and two words. |
| 86 | +> |
| 87 | +> Write a script to return all words in the given sentence that appear in sequence to the given two words. |
| 88 | +
|
| 89 | +#### Let's Talk About It |
| 90 | + |
| 91 | +Every sentence becomes an array, split on newlines. Come to words like `won't` and it can become a bit weird, but in the examples given, we can split on `not a word character`, or `\W`. |
| 92 | + |
| 93 | +From there, we're testing characters. `$array[$i]` should equal `$first`, `$array[$i+1]` should equal `$second` and `$array[$i+2]` should exist, so it can be pushed into the output array. |
| 94 | + |
| 95 | +#### Show Me The Code! |
| 96 | + |
| 97 | +```perl |
| 98 | +#!/usr/bin/env perl |
| 99 | + |
| 100 | +use strict; |
| 101 | +use warnings; |
| 102 | +use experimental qw{ say state postderef signatures }; |
| 103 | + |
| 104 | +my @examples = ( |
| 105 | + { |
| 106 | + sentence => |
| 107 | + "Perl is a my favourite language but Python is my favourite too.", |
| 108 | + first => "my", |
| 109 | + second => "favourite", |
| 110 | + }, |
| 111 | + { |
| 112 | + sentence => |
| 113 | + "Barbie is a beautiful doll also also a beautiful princess.", |
| 114 | + first => "a", |
| 115 | + second => "beautiful", |
| 116 | + }, |
| 117 | + { |
| 118 | + sentence => "we will we will rock you rock you.", |
| 119 | + first => "we", |
| 120 | + second => "will", |
| 121 | + } |
| 122 | +); |
| 123 | + |
| 124 | +for my $example (@examples) { |
| 125 | + my @output = find_third($example); |
| 126 | + my $output = join ', ', map { qq{"$_"} } @output; |
| 127 | + my ( $first, $second, $sentence ) = |
| 128 | + map { $example->{$_} } qw{ first second sentence }; |
| 129 | + say <<"END"; |
| 130 | + Input: \$sentence = "$sentence" |
| 131 | + \$first = "$first" |
| 132 | + \$second = "$second" |
| 133 | + Output: ($output) |
| 134 | +END |
| 135 | +} |
| 136 | + |
| 137 | +sub find_third ($obj) { |
| 138 | + my @output; |
| 139 | + my ( $first, $second, $sentence ) = |
| 140 | + map { $obj->{$_} } qw{ first second sentence }; |
| 141 | + my @sentence = split /\W+/, $sentence; |
| 142 | + for my $i ( 0 .. $#sentence ) { |
| 143 | + if ( $sentence[$i] eq $first |
| 144 | + && $sentence[ $i + 1 ] eq $second |
| 145 | + && defined $sentence[ $i + 2 ] ) |
| 146 | + { |
| 147 | + push @output, $sentence[ $i + 2 ]; |
| 148 | + } |
| 149 | + } |
| 150 | + return @output; |
| 151 | +} |
| 152 | +``` |
| 153 | + |
| 154 | +```text |
| 155 | +$ ./ch-2.pl |
| 156 | + Input: $sentence = "Perl is a my favourite language but Python is my favourite too." |
| 157 | + $first = "my" |
| 158 | + $second = "favourite" |
| 159 | + Output: ("language", "too") |
| 160 | +
|
| 161 | + Input: $sentence = "Barbie is a beautiful doll also also a beautiful princess." |
| 162 | + $first = "a" |
| 163 | + $second = "beautiful" |
| 164 | + Output: ("doll", "princess") |
| 165 | +
|
| 166 | + Input: $sentence = "we will we will rock you rock you." |
| 167 | + $first = "we" |
| 168 | + $second = "will" |
| 169 | + Output: ("we", "rock") |
| 170 | +``` |
| 171 | + |
| 172 | +#### 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