diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-10-02 21:12:33 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-10-02 21:12:33 +0100 |
| commit | 8680eda7724469e5e1d4853be9e4a80728eb20b0 (patch) | |
| tree | 236ca6e3f57629fa88a21cd9c460b4b021270be4 | |
| parent | 7763e20084f24f8f8e8e06b2092ed7718a699c4a (diff) | |
| parent | bfc0c746e3ac19af956f57f9f4c34de84332da06 (diff) | |
| download | perlweeklychallenge-club-8680eda7724469e5e1d4853be9e4a80728eb20b0.tar.gz perlweeklychallenge-club-8680eda7724469e5e1d4853be9e4a80728eb20b0.tar.bz2 perlweeklychallenge-club-8680eda7724469e5e1d4853be9e4a80728eb20b0.zip | |
Merge pull request #10949 from pokgopun/pwc289
Pwc289
| -rw-r--r-- | challenge-289/pokgopun/go/ch-1.go | 78 | ||||
| -rw-r--r-- | challenge-289/pokgopun/go/ch-2.go | 87 | ||||
| -rw-r--r-- | challenge-289/pokgopun/python/ch-1.py | 63 | ||||
| -rw-r--r-- | challenge-289/pokgopun/python/ch-2.py | 64 |
4 files changed, 292 insertions, 0 deletions
diff --git a/challenge-289/pokgopun/go/ch-1.go b/challenge-289/pokgopun/go/ch-1.go new file mode 100644 index 0000000000..70166d1686 --- /dev/null +++ b/challenge-289/pokgopun/go/ch-1.go @@ -0,0 +1,78 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-289/ +/*# + +Task 1: Third Maximum + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to find the third distinct maximum in the given array. + If third maximum doesn’t exist then return the maximum number. + +Example 1 + +Input: @ints = (5, 6, 4, 1) +Output: 4 + +The first distinct maximum is 6. +The second distinct maximum is 5. +The third distinct maximum is 4. + +Example 2 + +Input: @ints = (4, 5) +Output: 5 + +In the given array, the third maximum doesn't exist therefore returns the maximu +m. + +Example 3 + +Input: @ints = (1, 2, 2, 3) +Output: 1 + +The first distinct maximum is 3. +The second distinct maximum is 2. +The third distinct maximum is 1. + +Task 2: Jumbled Letters +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "io" + "os" + "slices" + + "github.com/google/go-cmp/cmp" +) + +type ints []int + +func (is ints) thirdMax() int { + slices.Sort(is) + is = slices.Compact(is) + //fmt.Println(is) + l := len(is) + if l > 2 { + return is[l-3] + } + return is[l-1] +} + +func main() { + for _, data := range []struct { + input ints + output int + }{ + {ints{5, 6, 4, 1}, 4}, + {ints{4, 5}, 5}, + {ints{1, 2, 2, 3}, 1}, + } { + io.WriteString(os.Stdout, cmp.Diff(data.input.thirdMax(), data.output)) // blank if ok, otherwise show the differences + } +} diff --git a/challenge-289/pokgopun/go/ch-2.go b/challenge-289/pokgopun/go/ch-2.go new file mode 100644 index 0000000000..9a6475883b --- /dev/null +++ b/challenge-289/pokgopun/go/ch-2.go @@ -0,0 +1,87 @@ +//# https://theweeklychallenge.org/blog/perl-weekly-challenge-289/ +/*# + +Task 2: Jumbled Letters + +Submitted by: [51]Ryan Thompson + __________________________________________________________________ + + An Internet legend dating back to at least 2001 goes something like + this: + + Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer + in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is + taht the frist and lsat ltteer be at the rghit pclae. The rset can + be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is + bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the + wrod as a wlohe. + + This supposed Cambridge research is unfortunately an urban legend. + However, the effect has been studied. For example—and with a title that + probably made the journal’s editor a little nervous—Raeding wrods with + jubmled lettres: there is a cost by Rayner, White, et. al. looked at + reading speed and comprehension of jumbled text. + + Your task is to write a program that takes English text as its input + and outputs a jumbled version as follows: + 1. The first and last letter of every word must stay the same + 2. The remaining letters in the word are scrambled in a random order + (if that happens to be the original order, that is OK). + 3. Whitespace, punctuation, and capitalization must stay the same + 4. The order of words does not change, only the letters inside the + word + + So, for example, “Perl” could become “Prel”, or stay as “Perl,” but it + could not become “Pelr” or “lreP”. + + I don’t know if this effect has been studied in other languages besides + English, but please consider sharing your results if you try! + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 6th October + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +#*/ +//# solution by pokgopun@gmail.com + +package main + +import ( + "bytes" + "io" + "math/rand" + "os" + "regexp" +) + +func scramble(bs []byte) []byte { + l := len(bs) + if l < 4 { + return bs + } + r := make([]byte, l) + r[0] = bs[0] + r[l-1] = bs[l-1] + bs = bs[1 : l-1] + l -= 2 + for l > 1 { + n := rand.Intn(l) + r[l] = bs[n] + copy(bs[n:], bs[n+1:]) + l-- + } + r[1] = bs[0] + return r +} + +func main() { + content := []byte(`Mechanical Sympathy +When you understand how a system is designed to be used, you can align with the design to gain optimal performance. For example, if you know that a certain type of memory is more efficient when addresses are multiples of a factor, you can optimize your performance by using data structure alignment. + +`) + io.Copy(os.Stdout, bytes.NewReader(content)) + pattern := regexp.MustCompile(`\w+`) + io.Copy(os.Stdout, bytes.NewReader(pattern.ReplaceAllFunc(content, scramble))) +} diff --git a/challenge-289/pokgopun/python/ch-1.py b/challenge-289/pokgopun/python/ch-1.py new file mode 100644 index 0000000000..0ddb137fac --- /dev/null +++ b/challenge-289/pokgopun/python/ch-1.py @@ -0,0 +1,63 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-289/ +""" + +Task 1: Third Maximum + +Submitted by: [50]Mohammad Sajid Anwar + __________________________________________________________________ + + You are given an array of integers, @ints. + + Write a script to find the third distinct maximum in the given array. + If third maximum doesn’t exist then return the maximum number. + +Example 1 + +Input: @ints = (5, 6, 4, 1) +Output: 4 + +The first distinct maximum is 6. +The second distinct maximum is 5. +The third distinct maximum is 4. + +Example 2 + +Input: @ints = (4, 5) +Output: 5 + +In the given array, the third maximum doesn't exist therefore returns the maximu +m. + +Example 3 + +Input: @ints = (1, 2, 2, 3) +Output: 1 + +The first distinct maximum is 3. +The second distinct maximum is 2. +The third distinct maximum is 1. + +Task 2: Jumbled Letters +""" +### solution by pokgopun@gmail.com + +def thirdMax(ints: tuple): + mx = sorted(set(ints),reverse=True) + if len(mx) > 2: + return mx[2] + return mx[0] + +import unittest + +class TestThirdMax(unittest.TestCase): + def test(self): + for inpt, otpt in { + (5, 6, 4, 1): 4, + (4, 5): 5, + (1, 2, 2, 3): 1, + }.items(): + self.assertEqual(thirdMax(inpt),otpt) + +unittest.main() + + diff --git a/challenge-289/pokgopun/python/ch-2.py b/challenge-289/pokgopun/python/ch-2.py new file mode 100644 index 0000000000..4d90d19419 --- /dev/null +++ b/challenge-289/pokgopun/python/ch-2.py @@ -0,0 +1,64 @@ +### https://theweeklychallenge.org/blog/perl-weekly-challenge-289/ +""" + +Task 2: Jumbled Letters + +Submitted by: [51]Ryan Thompson + __________________________________________________________________ + + An Internet legend dating back to at least 2001 goes something like + this: + + Aoccdrnig to a rscheearch at Cmabrigde Uinervtisy, it deosn’t mttaer + in waht oredr the ltteers in a wrod are, the olny iprmoetnt tihng is + taht the frist and lsat ltteer be at the rghit pclae. The rset can + be a toatl mses and you can sitll raed it wouthit porbelm. Tihs is + bcuseae the huamn mnid deos not raed ervey lteter by istlef, but the + wrod as a wlohe. + + This supposed Cambridge research is unfortunately an urban legend. + However, the effect has been studied. For example—and with a title that + probably made the journal’s editor a little nervous—Raeding wrods with + jubmled lettres: there is a cost by Rayner, White, et. al. looked at + reading speed and comprehension of jumbled text. + + Your task is to write a program that takes English text as its input + and outputs a jumbled version as follows: + 1. The first and last letter of every word must stay the same + 2. The remaining letters in the word are scrambled in a random order + (if that happens to be the original order, that is OK). + 3. Whitespace, punctuation, and capitalization must stay the same + 4. The order of words does not change, only the letters inside the + word + + So, for example, “Perl” could become “Prel”, or stay as “Perl,” but it + could not become “Pelr” or “lreP”. + + I don’t know if this effect has been studied in other languages besides + English, but please consider sharing your results if you try! + __________________________________________________________________ + + Last date to submit the solution 23:59 (UK Time) Sunday 6th October + 2024. + __________________________________________________________________ + +SO WHAT DO YOU THINK ? +""" +### solution by pokgopun@gmail.com + +# https://docs.python.org/3/library/re.html#text-munging + +import re +import random + +def repl(m): + inner_word = list(m.group(2)) + random.shuffle(inner_word) + return m.group(1) + "".join(inner_word) + m.group(3) + +text = '''Mechanical Sympathy +When you understand how a system is designed to be used, you can align with the design to gain optimal performance. For example, if you know that a certain type of memory is more efficient when addresses are multiples of a factor, you can optimize your performance by using data structure alignment. + +''' +print(text) +print(re.sub(r"(\w)(\w+)(\w)", repl, text)) |
