diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2024-09-18 19:56:42 -0400 |
| commit | f86f5e2fec16020c1d86f9028fb0f61cfeac106e (patch) | |
| tree | 0fd388a696b51ffde5a7bfe8519a74e1caf42461 /challenge-062 | |
| parent | ff8719c86653d5ad3121955e9494a0010527c2b9 (diff) | |
| parent | 0052ec63ca70eaa6d9ffb1926c294dbfd85f8c05 (diff) | |
| download | perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.gz perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.tar.bz2 perlweeklychallenge-club-f86f5e2fec16020c1d86f9028fb0f61cfeac106e.zip | |
Merge branch 'master' of https://github.com/manwar/perlweeklychallenge-club
Diffstat (limited to 'challenge-062')
| -rw-r--r-- | challenge-062/paulo-custodio/perl/ch-1.pl | 4 | ||||
| -rw-r--r-- | challenge-062/paulo-custodio/perl/ch-2.pl | 10 | ||||
| -rw-r--r-- | challenge-062/paulo-custodio/python/ch-1.py | 71 | ||||
| -rw-r--r-- | challenge-062/paulo-custodio/python/ch-2.py | 103 |
4 files changed, 181 insertions, 7 deletions
diff --git a/challenge-062/paulo-custodio/perl/ch-1.pl b/challenge-062/paulo-custodio/perl/ch-1.pl index e6cb2d6ece..538125b0d3 100644 --- a/challenge-062/paulo-custodio/perl/ch-1.pl +++ b/challenge-062/paulo-custodio/perl/ch-1.pl @@ -2,7 +2,7 @@ # Challenge 062 # -# TASK #1 › Sort Email Addresses +# TASK #1 > Sort Email Addresses # Submitted by: Neil Bowers # Reviewed by: Ryan Thompson # @@ -11,7 +11,7 @@ # 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 +# 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 diff --git a/challenge-062/paulo-custodio/perl/ch-2.pl b/challenge-062/paulo-custodio/perl/ch-2.pl index 2db5a3e870..2e2d9d2e35 100644 --- a/challenge-062/paulo-custodio/perl/ch-2.pl +++ b/challenge-062/paulo-custodio/perl/ch-2.pl @@ -2,10 +2,10 @@ # Challenge 062 # -# TASK #2 › N Queens +# TASK #2 > N Queens # Submitted by: Ryan Thompson # -# A standard 8×8 chessboard has 64 squares. The Queen is a chesspiece that can +# A standard 8x8 chessboard has 64 squares. The Queen is a chesspiece that can # attack in 8 directions, as shown by the green shaded squares below: # # @@ -15,15 +15,15 @@ # overlap). In fact, there are multiple ways to do so, and this is a favourite # undergraduate assignment in computer science. # -# But here at PWC, we’re going to take it into the next dimension! +# But here at PWC, we're going to take it into the next dimension! # # Your job is to write a script to work with a three dimensional chess cube, -# M×M×M in size, and find a solution that maximizes the number of queens that +# MxMxM in size, and find a solution that maximizes the number of queens that # can be placed in that cube without attacking each other. Output one possible # solution. # # Example -# A trivial 2×2×2 solution might look like this (1 = queen, 0 = empty): +# A trivial 2x2x2 solution might look like this (1 = queen, 0 = empty): # # [ # [[1,0], # Layer 1 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])) diff --git a/challenge-062/paulo-custodio/python/ch-2.py b/challenge-062/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..ca8a0f3c6a --- /dev/null +++ b/challenge-062/paulo-custodio/python/ch-2.py @@ -0,0 +1,103 @@ +#!/usr/bin/env python3 + +# Challenge 062 +# +# TASK #2 > N Queens +# Submitted by: Ryan Thompson +# +# A standard 8x8 chessboard has 64 squares. The Queen is a chesspiece that can +# attack in 8 directions, as shown by the green shaded squares below: +# +# +# +# It is possible to place 8 queens on to a single chessboard such that none of +# the queens can attack each other (i.e., their shaded squares would not +# overlap). In fact, there are multiple ways to do so, and this is a favourite +# undergraduate assignment in computer science. +# +# But here at PWC, we're going to take it into the next dimension! +# +# Your job is to write a script to work with a three dimensional chess cube, +# MxMxM in size, and find a solution that maximizes the number of queens that +# can be placed in that cube without attacking each other. Output one possible +# solution. +# +# Example +# A trivial 2x2x2 solution might look like this (1 = queen, 0 = empty): +# +# [ +# [[1,0], # Layer 1 +# [0,0]], +# +# [[0,0], # Layer 2 +# [0,0]], +# ] + +from copy import deepcopy +import re + +M = 2 + +board = [[[0 for _ in range(M)] for _ in range(M)] for _ in range(M)] +max_board = deepcopy(board) +max_queens = 0 + +def set_position(board, l, r, c): + if 0 <= l < M and 0 <= r < M and 0 <= c < M: + board[l][r][c] = 1 + +def place_queens(queens, board): + global max_queens, max_board + + # check for empty spaces + full = True + for l in range(M): + for r in range(M): + for c in range(M): + if board[l][r][c] == 0: + full = False + # empty, place queen + copy_board = deepcopy(board) + # fill attack positions + for i in range(M): + set_position(copy_board, i, r, c) + set_position(copy_board, l, i, c) + set_position(copy_board, l, r, i) + + set_position(copy_board, l, r-i, c-i) + set_position(copy_board, l, r-i, c+i) + set_position(copy_board, l, r+i, c-i) + set_position(copy_board, l, r+i, c+i) + + set_position(copy_board, l-i, r, c-i) + set_position(copy_board, l-i, r, c+i) + set_position(copy_board, l+i, r, c-i) + set_position(copy_board, l+i, r, c+i) + + set_position(copy_board, l-i, r-i, c) + set_position(copy_board, l-i, r+i, c) + set_position(copy_board, l+i, r-i, c) + set_position(copy_board, l+i, r+i, c) + + set_position(copy_board, l-i, r-i, c-i) + set_position(copy_board, l-i, r-i, c+i) + set_position(copy_board, l-i, r+i, c-i) + set_position(copy_board, l-i, r+i, c+i) + + set_position(copy_board, l+i, r-i, c-i) + set_position(copy_board, l+i, r-i, c+i) + set_position(copy_board, l+i, r+i, c-i) + set_position(copy_board, l+i, r+i, c+i) + # fill queen position + copy_board[l][r][c] = 'Q' + # recurse + place_queens(queens + 1, copy_board) + if full: + if queens > max_queens: + max_queens = queens + max_board = deepcopy(board) + +place_queens(0, board) +out = str(max_board) +out, _ = re.subn("'", '"', out) +print(out) |
