diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-09-30 15:13:10 +0100 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-09-30 15:13:10 +0100 |
| commit | 063125ecee14fbb958ea602990a50de35954ff16 (patch) | |
| tree | f6f06acaf2d09754cdb4d85bb159f3b200e200f2 /challenge-289 | |
| parent | f5b66e522da469c1382a9d7826c26a33167c6743 (diff) | |
| parent | cc97c30adf5ca5c06280978360f4955d81fe7217 (diff) | |
| download | perlweeklychallenge-club-063125ecee14fbb958ea602990a50de35954ff16.tar.gz perlweeklychallenge-club-063125ecee14fbb958ea602990a50de35954ff16.tar.bz2 perlweeklychallenge-club-063125ecee14fbb958ea602990a50de35954ff16.zip | |
Merge pull request #10930 from pauloscustodio/master
Add Perl and Python solutions to challenge 289
Diffstat (limited to 'challenge-289')
| -rw-r--r-- | challenge-289/paulo-custodio/Makefile | 2 | ||||
| -rw-r--r-- | challenge-289/paulo-custodio/perl/ch-1.pl | 42 | ||||
| -rw-r--r-- | challenge-289/paulo-custodio/perl/ch-2.pl | 45 | ||||
| -rw-r--r-- | challenge-289/paulo-custodio/python/ch-1.py | 41 | ||||
| -rw-r--r-- | challenge-289/paulo-custodio/python/ch-2.py | 49 | ||||
| -rw-r--r-- | challenge-289/paulo-custodio/t/test-1.yaml | 15 | ||||
| -rw-r--r-- | challenge-289/paulo-custodio/t/test-2.yaml | 17 |
7 files changed, 211 insertions, 0 deletions
diff --git a/challenge-289/paulo-custodio/Makefile b/challenge-289/paulo-custodio/Makefile new file mode 100644 index 0000000000..c3c762d746 --- /dev/null +++ b/challenge-289/paulo-custodio/Makefile @@ -0,0 +1,2 @@ +all: + perl ../../challenge-001/paulo-custodio/test.pl diff --git a/challenge-289/paulo-custodio/perl/ch-1.pl b/challenge-289/paulo-custodio/perl/ch-1.pl new file mode 100644 index 0000000000..117adfc28f --- /dev/null +++ b/challenge-289/paulo-custodio/perl/ch-1.pl @@ -0,0 +1,42 @@ +#!/usr/bin/env perl + +# Challenge 289 +# +# Task 1: Third Maximum +# Submitted by: 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 +# maximum. +# +# 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. + +use Modern::Perl; +use List::Util 'uniq'; + +my @nums = sort {$b <=> $a} uniq @ARGV; +say @nums>2 ? $nums[2] : $nums[0]; diff --git a/challenge-289/paulo-custodio/perl/ch-2.pl b/challenge-289/paulo-custodio/perl/ch-2.pl new file mode 100644 index 0000000000..1719dc4c34 --- /dev/null +++ b/challenge-289/paulo-custodio/perl/ch-2.pl @@ -0,0 +1,45 @@ +#!/usr/bin/env perl + +# Challenge 289 +# +# Task 2: Jumbled Letters +# Submitted by: 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: +# +# The first and last letter of every word must stay the same +# The remaining letters in the word are scrambled in a random order +# (if that happens to be the original order, that is OK). +# Whitespace, punctuation, and capitalization must stay the same +# 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! + +use Modern::Perl; +use List::Util 'shuffle'; + +@ARGV and srand(shift//0); + +while (<>) { + s/(\w)(\w*)(\w)/$1 . join('', shuffle(split '', $2)) . $3/ge; + print; +} diff --git a/challenge-289/paulo-custodio/python/ch-1.py b/challenge-289/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..6814d59b6e --- /dev/null +++ b/challenge-289/paulo-custodio/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# Challenge 289 +# +# Task 1: Third Maximum +# Submitted by: 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 +# maximum. +# +# 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. + +import sys + +nums = sorted(set(map(int, sys.argv[1:])), reverse=True) +print(nums[2] if len(nums) > 2 else nums[0]) diff --git a/challenge-289/paulo-custodio/python/ch-2.py b/challenge-289/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..9defbf2b4a --- /dev/null +++ b/challenge-289/paulo-custodio/python/ch-2.py @@ -0,0 +1,49 @@ +#!/usr/bin/env python3 + +# Challenge 289 +# +# Task 2: Jumbled Letters +# Submitted by: 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: +# +# The first and last letter of every word must stay the same +# The remaining letters in the word are scrambled in a random order +# (if that happens to be the original order, that is OK). +# Whitespace, punctuation, and capitalization must stay the same +# 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! + +import sys +import random +import re + +if len(sys.argv) > 1: + random.seed(int(sys.argv[1])) + +for line in sys.stdin: + def shuffle_inner(match): + return match.group(1) + ''.join(random.sample(match.group(2), len(match.group(2)))) + match.group(3) + + line = re.sub(r'(\w)(\w*)(\w)', shuffle_inner, line) + print(line, end='') diff --git a/challenge-289/paulo-custodio/t/test-1.yaml b/challenge-289/paulo-custodio/t/test-1.yaml new file mode 100644 index 0000000000..5b1eb66a20 --- /dev/null +++ b/challenge-289/paulo-custodio/t/test-1.yaml @@ -0,0 +1,15 @@ +- setup: + cleanup: + args: 5 6 4 1 + input: + output: 4 +- setup: + cleanup: + args: 4 5 + input: + output: 5 +- setup: + cleanup: + args: 1 2 2 3 + input: + output: 1 diff --git a/challenge-289/paulo-custodio/t/test-2.yaml b/challenge-289/paulo-custodio/t/test-2.yaml new file mode 100644 index 0000000000..13f04d7a22 --- /dev/null +++ b/challenge-289/paulo-custodio/t/test-2.yaml @@ -0,0 +1,17 @@ +- setup: + cleanup: + args: 1 + input: | + |According to a research at Cambridge University, it doesn't matter + |in what order the letters in a word are, the only important thing + |is that the first and last letter be at the right place. The rest + |can be a total mess and you can still read it without problem. + |This is because the human mind does not read every letter by + |itself, but the word as a whole. + output: | + |Ainrcdocg to a reecarsh at Cbmgrdaie Uvtriensiy, it deosn't mtater + |in what odrer the ltteers in a wrod are, the only ipamtnrot thnig + |is that the frist and last letetr be at the rgiht pclae. The rset + |can be a ttoal mess and you can stlil read it whuoitt peroblm. + |This is bceusae the haumn mind deos not read erevy letetr by + |istlef, but the wrod as a whloe. |
