|
| 1 | +--- |
| 2 | +layout: post |
| 3 | +title: "We All Live In A Yellow Substring: Weekly Challenge #317" |
| 4 | +author: "Dave Jacoby" |
| 5 | +date: "2025-04-17 14:27:26 -0400" |
| 6 | +categories: "" |
| 7 | +--- |
| 8 | + |
| 9 | +### Task 1: Acronyms |
| 10 | + |
| 11 | +> Submitted by: Mohammad Sajid Anwar |
| 12 | +> You are given an array of words and a word. |
| 13 | +> |
| 14 | +> Write a script to return `true` if concatenating the first letter of each word in the given array matches the given word, return `false` otherwise. |
| 15 | +
|
| 16 | +#### Let's Talk About It! |
| 17 | + |
| 18 | +So, we want to deal with every element in an array. `map {...} @array` |
| 19 | + |
| 20 | +And we want to get the first character. `substr( $string, 0, 1 )` |
| 21 | + |
| 22 | +And we want to join everything into a string. `join '', @array` |
| 23 | + |
| 24 | +And we want to compare that string with another string. `$i eq $j` |
| 25 | + |
| 26 | +And we want to return `true` or `false`. `return $i eq $j ? 'true' : 'false'` |
| 27 | + |
| 28 | +#### Show Me The Code! |
| 29 | + |
| 30 | +```perl |
| 31 | +#!/usr/bin/env perl |
| 32 | + |
| 33 | +use strict; |
| 34 | +use warnings; |
| 35 | +use experimental qw{ say state postderef signatures }; |
| 36 | + |
| 37 | +my @examples = ( |
| 38 | + |
| 39 | + { |
| 40 | + array => [ "Perl", "Weekly", "Challenge" ], |
| 41 | + word => "PWC", |
| 42 | + }, |
| 43 | + { |
| 44 | + array => [ "Bob", "Charlie", "Joe" ], |
| 45 | + word => "BCJ", |
| 46 | + }, |
| 47 | + { |
| 48 | + array => [ "Morning", "Good" ], |
| 49 | + word => "MM", |
| 50 | + }, |
| 51 | +); |
| 52 | + |
| 53 | +for my $example (@examples) { |
| 54 | + my $array = join ', ', map { qq{"$_"} } $example->{array}->@*; |
| 55 | + my $word = $example->{word}; |
| 56 | + my $output = acronyms($example); |
| 57 | + say <<"END"; |
| 58 | + Input: \@array = ($array) |
| 59 | + \$word = "$word" |
| 60 | + Output: $output |
| 61 | +END |
| 62 | +} |
| 63 | + |
| 64 | +sub acronyms ($example) { |
| 65 | + my @array = $example->{array}->@*; |
| 66 | + my $word = $example->{word}; |
| 67 | + my $acronym = join '', map { substr $_, 0, 1 } $example->{array}->@*; |
| 68 | + return $word eq $acronym ? 'true' : 'false'; |
| 69 | +} |
| 70 | +``` |
| 71 | + |
| 72 | +```text |
| 73 | +$ ./ch-1.pl |
| 74 | + Input: @array = ("Perl", "Weekly", "Challenge") |
| 75 | + $word = "PWC" |
| 76 | + Output: true |
| 77 | +
|
| 78 | + Input: @array = ("Bob", "Charlie", "Joe") |
| 79 | + $word = "BCJ" |
| 80 | + Output: true |
| 81 | +
|
| 82 | + Input: @array = ("Morning", "Good") |
| 83 | + $word = "MM" |
| 84 | + Output: false |
| 85 | +``` |
| 86 | + |
| 87 | +### Task 2: Friendly Strings |
| 88 | + |
| 89 | +> Submitted by: Mohammad Sajid Anwar |
| 90 | +> You are given two strings. |
| 91 | +> |
| 92 | +> Write a script to return `true` if swapping any two letters in one string match the other string, return `false` otherwise. |
| 93 | +
|
| 94 | +#### Let's Talk About It! |
| 95 | + |
| 96 | +Did we really need to make this challenge PG-rated? |
| 97 | + |
| 98 | +I really thought about sorting both strings alphabetically, but that doesn't _really_ tell us anything. Consider `gins` and `sign`. So we have a two-loop solution where the second loop starts one after the first loop. I Think that's **O(nlogn)**. |
| 99 | + |
| 100 | +And I think we can talk here about [**lvalue**](https://perldoc.perl.org/perlsub#Lvalue-subroutines) functions, which I haven't done in a while. Most functions can assign (`$x = function($y)`) but with an lvalue function, you can assign to it (`function($y) = $x`). `substr` is my most-used lvalue function. We can read from `substr`, but we can also write to `substr`. That makes it possible to write `substr($string2,$i,1) = substr($string1,$j,1)`. There's not a lot of places where I use this, but it is good to have it available. |
| 101 | + |
| 102 | +And we get down to a design where we default to `false` while going through and finding the `true` case. |
| 103 | + |
| 104 | +#### Show Me The Code! |
| 105 | + |
| 106 | +```perl |
| 107 | +#!/usr/bin/env perl |
| 108 | + |
| 109 | +use strict; |
| 110 | +use warnings; |
| 111 | +use experimental qw{ say state postderef signatures }; |
| 112 | + |
| 113 | +my @examples = ( |
| 114 | + |
| 115 | + [qw{desc dsec}], |
| 116 | + [qw{fuck fcuk}], |
| 117 | + [qw{poo eop}], |
| 118 | + [qw{stripe sprite}], |
| 119 | +); |
| 120 | + |
| 121 | +for my $example (@examples) { |
| 122 | + my ( $str1, $str2 ) = $example->@*; |
| 123 | + my $output = friendly_strings($example); |
| 124 | + say <<"END"; |
| 125 | + Input: \$str1 = "$str1", |
| 126 | + \$str2 = "$str2" |
| 127 | + Output: $output |
| 128 | +END |
| 129 | +} |
| 130 | + |
| 131 | +sub friendly_strings ($example) { |
| 132 | + my ( $str1, $str2 ) = $example->@*; |
| 133 | + my $l = -1 + length $str1; |
| 134 | + for my $i ( 0 .. $l ) { |
| 135 | + for my $j ( $i + 1 .. $l ) { |
| 136 | + my $str1b = $str1; |
| 137 | + substr( $str1b, $i, 1 ) = substr( $str1, $j, 1 ); |
| 138 | + substr( $str1b, $j, 1 ) = substr( $str1, $i, 1 ); |
| 139 | + return 'true' if $str1b eq $str2; |
| 140 | + } |
| 141 | + } |
| 142 | + return 'false'; |
| 143 | +} |
| 144 | +``` |
| 145 | + |
| 146 | +```text |
| 147 | +$ ./ch-2.pl |
| 148 | + Input: $str1 = "desc", |
| 149 | + $str2 = "dsec" |
| 150 | + Output: true |
| 151 | +
|
| 152 | + Input: $str1 = "fuck", |
| 153 | + $str2 = "fcuk" |
| 154 | + Output: true |
| 155 | +
|
| 156 | + Input: $str1 = "poo", |
| 157 | + $str2 = "eop" |
| 158 | + Output: false |
| 159 | +
|
| 160 | + Input: $str1 = "stripe", |
| 161 | + $str2 = "sprite" |
| 162 | + Output: true |
| 163 | +``` |
| 164 | + |
| 165 | +#### 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