aboutsummaryrefslogtreecommitdiff
path: root/challenge-062/paulo-custodio/python/ch-1.py
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-18 19:59:32 +0100
committerGitHub <noreply@github.com>2024-09-18 19:59:32 +0100
commit85073f31dae3decce780d1fd66cdcb79c82c7f65 (patch)
tree318d3bc02836f4cde215f7f15e9ec875c74a1490 /challenge-062/paulo-custodio/python/ch-1.py
parentbfe139d559d498bcf1ae48cc08520bfc99ee9b85 (diff)
parent51c035e04c5ae5f9f02555206f57eb675a737617 (diff)
downloadperlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.gz
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.tar.bz2
perlweeklychallenge-club-85073f31dae3decce780d1fd66cdcb79c82c7f65.zip
Merge pull request #10860 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-062/paulo-custodio/python/ch-1.py')
-rw-r--r--challenge-062/paulo-custodio/python/ch-1.py71
1 files changed, 71 insertions, 0 deletions
diff --git a/challenge-062/paulo-custodio/python/ch-1.py b/challenge-062/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..cc119d7a09
--- /dev/null
+++ b/challenge-062/paulo-custodio/python/ch-1.py
@@ -0,0 +1,71 @@
+#!/usr/bin/env python3
+
+# Challenge 062
+#
+# TASK #1 > Sort Email Addresses
+# Submitted by: Neil Bowers
+# Reviewed by: Ryan Thompson
+#
+# Write a script that takes a list of email addresses (one per line) and sorts
+# them first by the domain part of the email address, and then by the part to
+# the left of the @ (known as the mailbox).
+#
+# Note that the domain is case-insensitive, while the mailbox part is case
+# sensitive. (Some email providers choose to ignore case, but that's another
+# matter entirely.)
+#
+# If your script is invoked with arguments, it should treat them as file names
+# and read them in order, otherwise your script should read email addresses from
+# standard input.
+#
+# Bonus
+# Add a -u option which only includes unique email addresses in the output, just
+# like sort -u.
+#
+# Example
+# If given the following list:
+#
+# name@example.org
+# rjt@cpan.org
+# Name@example.org
+# rjt@CPAN.org
+# user@alpha.example.org
+# Your script (without -u) would return:
+#
+# user@alpha.example.org
+# rjt@cpan.org
+# rjt@CPAN.org
+# Name@example.org
+# name@example.org
+# With -u, the script would return:
+#
+# user@alpha.example.org
+# rjt@CPAN.org
+# Name@example.org
+# name@example.org
+
+import sys
+from getopt import getopt
+
+opt_u = False
+opts, args = getopt(sys.argv[1:], 'u')
+for o, a in opts:
+ if o == '-u':
+ opt_u = True
+
+mails = [line.strip() for line in sys.stdin]
+
+sorted_mails = sorted(
+ map(lambda x: [x] + x.split('@'), mails),
+ key=lambda x: (x[2].lower(), x[1])
+)
+
+if opt_u:
+ seen = set()
+ sorted_mails = [
+ [f"{mail[0]}"]
+ for mail in sorted_mails
+ if (key := f"{mail[1]}@{mail[2].lower()}") not in seen and not seen.add(key)
+ ]
+
+print("\n".join([x[0] for x in sorted_mails]))