From 9e56f497ae79225e5a6e041a963741885335d0d0 Mon Sep 17 00:00:00 2001 From: Mohammad Sajid Anwar Date: Tue, 24 Sep 2024 17:06:27 +0100 Subject: - Added solutions by Conor Hoekstra. --- challenge-288/conor-hoekstra/bqn/ch-1.bqn | 25 ++++++++++++++++++++++ challenge-288/conor-hoekstra/bqn/ch-2.bqn | 33 +++++++++++++++++++++++++++++ challenge-288/conor-hoekstra/ch-1.bqn | 25 ---------------------- challenge-288/conor-hoekstra/ch-1.py | 30 -------------------------- challenge-288/conor-hoekstra/ch-2.bqn | 33 ----------------------------- challenge-288/conor-hoekstra/python/ch-1.py | 30 ++++++++++++++++++++++++++ 6 files changed, 88 insertions(+), 88 deletions(-) create mode 100644 challenge-288/conor-hoekstra/bqn/ch-1.bqn create mode 100644 challenge-288/conor-hoekstra/bqn/ch-2.bqn delete mode 100644 challenge-288/conor-hoekstra/ch-1.bqn delete mode 100644 challenge-288/conor-hoekstra/ch-1.py delete mode 100644 challenge-288/conor-hoekstra/ch-2.bqn create mode 100644 challenge-288/conor-hoekstra/python/ch-1.py diff --git a/challenge-288/conor-hoekstra/bqn/ch-1.bqn b/challenge-288/conor-hoekstra/bqn/ch-1.bqn new file mode 100644 index 0000000000..893baf3412 --- /dev/null +++ b/challenge-288/conor-hoekstra/bqn/ch-1.bqn @@ -0,0 +1,25 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/288-1.bqn + +# Slow One-Line Solution +ClosestPalindrome ← {⊑(⌽⊸≡•Fmt)¨⊸/∾<˘⍉>((•BQN𝕩)(-⋈+)(1+↕))10⋆≠𝕩} + +# Faster Solution +Find ← { 𝕩 < 0 ? ¯∞ ; ⌽⊸≡•Fmt 𝕩 ? 𝕩 ; 𝕨 𝕊 𝕨 + 𝕩 } +ClosestPalindrome2 ← { ⊑(⌊´⊸=·|t-⊢)⊸/¯1 ⋈○(Find⟜(+⟜(t←•BQN𝕩))) 1 } + +# Tests +•Show ClosestPalindrome "123" # "121" +•Show ClosestPalindrome "2" #"1" +•Show ClosestPalindrome "1400" # "1441" +•Show ClosestPalindrome "1001" # "999" +•Show ClosestPalindrome "999" # "10001" +•Show ClosestPalindrome "0" # "1" + +•Show ClosestPalindrome2 "123" # "121" +•Show ClosestPalindrome2 "2" #"1" +•Show ClosestPalindrome2 "1400" # "1441" +•Show ClosestPalindrome2 "1001" # "999" +•Show ClosestPalindrome2 "999" # "10001" +•Show ClosestPalindrome2 "0" # "1" +•Show ClosestPalindrome2 "1231234" # "12311321" diff --git a/challenge-288/conor-hoekstra/bqn/ch-2.bqn b/challenge-288/conor-hoekstra/bqn/ch-2.bqn new file mode 100644 index 0000000000..0a450ff6a6 --- /dev/null +++ b/challenge-288/conor-hoekstra/bqn/ch-2.bqn @@ -0,0 +1,33 @@ +# For up to date code: +# https://github.com/codereport/bqn-code/blob/main/pwc/288-2.bqn + +deltas ← ⌽¨⊸∾0⋈¨1‿¯1 + +MaxContiguousBlocks ← { 𝕊orig: + empty ← 0⥊˜≢orig + IsValid ← ∧´≥⟜0‿0∾(≢orig)>⊢ + FloodFill ← { 𝕊sym‿todo‿mat: + { 0=≠todo ? +´⥊mat ; + pos ← ⊑todo + new ← IsValid¨⊸/pos⊸+¨deltas + FloodFill { (sym=pos⊑orig)∧¬pos⊑mat + ? sym‿(new∾˜1↓todo)‿(1⊸+⌾(pos⊸⊑)mat) + ; sym‿(1↓todo)‿mat }} + } + ⌈´FloodFill¨{(𝕩⊑orig)‿⟨𝕩⟩‿empty}¨⥊↕≢orig +} + +# Tests +•Show MaxContiguousBlocks >⟨"xxxxo", + "xoooo", + "xoooo", + "xxxoo"⟩ # 11 + +•Show MaxContiguousBlocks >⟨"xxxxx", + "xoooo", + "xxxxo", + "xoooo"⟩ # 11 +•Show MaxContiguousBlocks >⟨"xxxoo", + "oooxx", + "oxxoo", + "oooxx"⟩ # 7 diff --git a/challenge-288/conor-hoekstra/ch-1.bqn b/challenge-288/conor-hoekstra/ch-1.bqn deleted file mode 100644 index 893baf3412..0000000000 --- a/challenge-288/conor-hoekstra/ch-1.bqn +++ /dev/null @@ -1,25 +0,0 @@ -# For up to date code: -# https://github.com/codereport/bqn-code/blob/main/pwc/288-1.bqn - -# Slow One-Line Solution -ClosestPalindrome ← {⊑(⌽⊸≡•Fmt)¨⊸/∾<˘⍉>((•BQN𝕩)(-⋈+)(1+↕))10⋆≠𝕩} - -# Faster Solution -Find ← { 𝕩 < 0 ? ¯∞ ; ⌽⊸≡•Fmt 𝕩 ? 𝕩 ; 𝕨 𝕊 𝕨 + 𝕩 } -ClosestPalindrome2 ← { ⊑(⌊´⊸=·|t-⊢)⊸/¯1 ⋈○(Find⟜(+⟜(t←•BQN𝕩))) 1 } - -# Tests -•Show ClosestPalindrome "123" # "121" -•Show ClosestPalindrome "2" #"1" -•Show ClosestPalindrome "1400" # "1441" -•Show ClosestPalindrome "1001" # "999" -•Show ClosestPalindrome "999" # "10001" -•Show ClosestPalindrome "0" # "1" - -•Show ClosestPalindrome2 "123" # "121" -•Show ClosestPalindrome2 "2" #"1" -•Show ClosestPalindrome2 "1400" # "1441" -•Show ClosestPalindrome2 "1001" # "999" -•Show ClosestPalindrome2 "999" # "10001" -•Show ClosestPalindrome2 "0" # "1" -•Show ClosestPalindrome2 "1231234" # "12311321" diff --git a/challenge-288/conor-hoekstra/ch-1.py b/challenge-288/conor-hoekstra/ch-1.py deleted file mode 100644 index 98587b5bad..0000000000 --- a/challenge-288/conor-hoekstra/ch-1.py +++ /dev/null @@ -1,30 +0,0 @@ -# Link: https://theweeklychallenge.org/blog/perl-weekly-challenge-288 - -def is_palindrome(n): - return str(n) == str(n)[::-1] - -def find(start: int, delta: int) -> int: - while not is_palindrome(start): - start += delta - print(start) - if start < 0: - return None - return start - -def closest_palindrome(n: str) -> str: - i = int(n) - a = find(i - 1, -1) - b = find(i + 1, 1) - print(a, b) - if a is not None and i - a <= b - i: - return str(a) - return str(b) - -# Tests -print(closest_palindrome("123")) # "121" -print(closest_palindrome("2")) # "1" -print(closest_palindrome("1400")) # "1441" -print(closest_palindrome("1001")) # "999" -print(closest_palindrome("999")) # "10001" -print(closest_palindrome("12312345")) # "12311321" -print(closest_palindrome("0")) # "1" diff --git a/challenge-288/conor-hoekstra/ch-2.bqn b/challenge-288/conor-hoekstra/ch-2.bqn deleted file mode 100644 index 0a450ff6a6..0000000000 --- a/challenge-288/conor-hoekstra/ch-2.bqn +++ /dev/null @@ -1,33 +0,0 @@ -# For up to date code: -# https://github.com/codereport/bqn-code/blob/main/pwc/288-2.bqn - -deltas ← ⌽¨⊸∾0⋈¨1‿¯1 - -MaxContiguousBlocks ← { 𝕊orig: - empty ← 0⥊˜≢orig - IsValid ← ∧´≥⟜0‿0∾(≢orig)>⊢ - FloodFill ← { 𝕊sym‿todo‿mat: - { 0=≠todo ? +´⥊mat ; - pos ← ⊑todo - new ← IsValid¨⊸/pos⊸+¨deltas - FloodFill { (sym=pos⊑orig)∧¬pos⊑mat - ? sym‿(new∾˜1↓todo)‿(1⊸+⌾(pos⊸⊑)mat) - ; sym‿(1↓todo)‿mat }} - } - ⌈´FloodFill¨{(𝕩⊑orig)‿⟨𝕩⟩‿empty}¨⥊↕≢orig -} - -# Tests -•Show MaxContiguousBlocks >⟨"xxxxo", - "xoooo", - "xoooo", - "xxxoo"⟩ # 11 - -•Show MaxContiguousBlocks >⟨"xxxxx", - "xoooo", - "xxxxo", - "xoooo"⟩ # 11 -•Show MaxContiguousBlocks >⟨"xxxoo", - "oooxx", - "oxxoo", - "oooxx"⟩ # 7 diff --git a/challenge-288/conor-hoekstra/python/ch-1.py b/challenge-288/conor-hoekstra/python/ch-1.py new file mode 100644 index 0000000000..98587b5bad --- /dev/null +++ b/challenge-288/conor-hoekstra/python/ch-1.py @@ -0,0 +1,30 @@ +# Link: https://theweeklychallenge.org/blog/perl-weekly-challenge-288 + +def is_palindrome(n): + return str(n) == str(n)[::-1] + +def find(start: int, delta: int) -> int: + while not is_palindrome(start): + start += delta + print(start) + if start < 0: + return None + return start + +def closest_palindrome(n: str) -> str: + i = int(n) + a = find(i - 1, -1) + b = find(i + 1, 1) + print(a, b) + if a is not None and i - a <= b - i: + return str(a) + return str(b) + +# Tests +print(closest_palindrome("123")) # "121" +print(closest_palindrome("2")) # "1" +print(closest_palindrome("1400")) # "1441" +print(closest_palindrome("1001")) # "999" +print(closest_palindrome("999")) # "10001" +print(closest_palindrome("12312345")) # "12311321" +print(closest_palindrome("0")) # "1" -- cgit