From 6fbfd52c4d8d4fb596d8e08565630cce99b18b7b Mon Sep 17 00:00:00 2001 From: tikkanz Date: Sat, 15 May 2021 15:58:13 +1200 Subject: [PATCH 1/5] Add additional J solutions Solution 2 works on a pair of characters at a time (not very Jish) Solution 3 separates the string into a list of even indexes and a list of odd indexes and then processes those lists as a whole. --- P1844_Replace_All_Digits_with_Characters.ijs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/P1844_Replace_All_Digits_with_Characters.ijs b/P1844_Replace_All_Digits_with_Characters.ijs index 328396c..412ab23 100644 --- a/P1844_Replace_All_Digits_with_Characters.ijs +++ b/P1844_Replace_All_Digits_with_Characters.ijs @@ -2,3 +2,11 @@ NB. Problem Link: https://leetcode.com/contest/biweekly-contest-51/problems/repl isAlpha =. {{ 'y e. (97+i.26) { a. }} replaceDigits =. {{ ({. y), 2 {{ if. isAlpha {. y do. a. {~ (a. i. {. y) + ". {.|. y else. {.|.y end. }} \ y }} + +NB. Solution 2 (a pair at a time) +shift0=: 0&".@{: +&.(a:`(a.&i.)) {. +replaceDigits1=: [: ; _2&(<@(({. , shift0)^:(2 = #))\) + +NB. Solution 3 (whole array) +shift1=: +&.:(a:`(a.&i.)) NB. uses semidual available from >= J9.02 +replaceDigits2=: $ {. ,@((0 ". }."1) (,@] ,. shift1) {."1)@(_2 ]\ ]) From 8a4b83e64f4d4c685051219d02696a44e6374863 Mon Sep 17 00:00:00 2001 From: tikkanz Date: Sat, 15 May 2021 16:01:14 +1200 Subject: [PATCH 2/5] Remove errant quote from original soluiton --- P1844_Replace_All_Digits_with_Characters.ijs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/P1844_Replace_All_Digits_with_Characters.ijs b/P1844_Replace_All_Digits_with_Characters.ijs index 412ab23..fda3fe7 100644 --- a/P1844_Replace_All_Digits_with_Characters.ijs +++ b/P1844_Replace_All_Digits_with_Characters.ijs @@ -1,6 +1,6 @@ NB. Problem Link: https://leetcode.com/contest/biweekly-contest-51/problems/replace-all-digits-with-characters/ -isAlpha =. {{ 'y e. (97+i.26) { a. }} +isAlpha =. {{ y e. (97+i.26) { a. }} replaceDigits =. {{ ({. y), 2 {{ if. isAlpha {. y do. a. {~ (a. i. {. y) + ". {.|. y else. {.|.y end. }} \ y }} NB. Solution 2 (a pair at a time) From 95547aa71729a612037181cc41b33b792cc47888 Mon Sep 17 00:00:00 2001 From: tikkanz Date: Sat, 15 May 2021 16:34:36 +1200 Subject: [PATCH 3/5] Refactor last solution and tidy solution numbering Transposing the columns of even and odd indices means we can `insert` our verb between them. --- P1844_Replace_All_Digits_with_Characters.ijs | 11 +++++++---- 1 file changed, 7 insertions(+), 4 deletions(-) diff --git a/P1844_Replace_All_Digits_with_Characters.ijs b/P1844_Replace_All_Digits_with_Characters.ijs index fda3fe7..ec4f527 100644 --- a/P1844_Replace_All_Digits_with_Characters.ijs +++ b/P1844_Replace_All_Digits_with_Characters.ijs @@ -4,9 +4,12 @@ isAlpha =. {{ y e. (97+i.26) { a. }} replaceDigits =. {{ ({. y), 2 {{ if. isAlpha {. y do. a. {~ (a. i. {. y) + ". {.|. y else. {.|.y end. }} \ y }} NB. Solution 2 (a pair at a time) -shift0=: 0&".@{: +&.(a:`(a.&i.)) {. -replaceDigits1=: [: ; _2&(<@(({. , shift0)^:(2 = #))\) +shift=: +&.:(a:`(a.&i.)) NB. uses semidual available from >= J9.02 +NB. eg: 0 1 2 1 shift 'acdf' +replaceDigits2=: [: ; _2&(<@(({. , 0&".@{: shift {.)^:(2 = #))\) NB. Solution 3 (whole array) -shift1=: +&.:(a:`(a.&i.)) NB. uses semidual available from >= J9.02 -replaceDigits2=: $ {. ,@((0 ". }."1) (,@] ,. shift1) {."1)@(_2 ]\ ]) +replaceDigits3=: $ {. ,@((0 ". }."1) (,@] ,. shift) {."1)@(_2 ]\ ]) + +NB. Solution 4 (refactor 3 - J works with more naturally with rows than with columns) +replaceDigits4=: $ {. ,@(([ ,. shift~) 0 ". ,.)/@|:@(_2 ]\ ]) From 9b5ea35c5675c24c50d4c357d4e95a9e69188b92 Mon Sep 17 00:00:00 2001 From: tikkanz Date: Sat, 15 May 2021 16:54:10 +1200 Subject: [PATCH 4/5] Define initial solutions for global use This way they are available in the session if the definitions are loaded from a script. --- P1844_Replace_All_Digits_with_Characters.ijs | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/P1844_Replace_All_Digits_with_Characters.ijs b/P1844_Replace_All_Digits_with_Characters.ijs index ec4f527..0946e6c 100644 --- a/P1844_Replace_All_Digits_with_Characters.ijs +++ b/P1844_Replace_All_Digits_with_Characters.ijs @@ -1,7 +1,7 @@ NB. Problem Link: https://leetcode.com/contest/biweekly-contest-51/problems/replace-all-digits-with-characters/ -isAlpha =. {{ y e. (97+i.26) { a. }} -replaceDigits =. {{ ({. y), 2 {{ if. isAlpha {. y do. a. {~ (a. i. {. y) + ". {.|. y else. {.|.y end. }} \ y }} +isAlpha =: {{ y e. (97+i.26) { a. }} +replaceDigits =: {{ ({. y), 2 {{ if. isAlpha {. y do. a. {~ (a. i. {. y) + ". {.|. y else. {.|.y end. }} \ y }} NB. Solution 2 (a pair at a time) shift=: +&.:(a:`(a.&i.)) NB. uses semidual available from >= J9.02 From 9f062991593f111fc7ec0023ce41ec790cbedee2 Mon Sep 17 00:00:00 2001 From: tikkanz Date: Sat, 15 May 2021 16:56:18 +1200 Subject: [PATCH 5/5] Add Testing note Load the script to the J terminal in VSCode (Ctrl+L) Send lines in the Note to the terminal (Ctrl+Enter) --- P1844_Replace_All_Digits_with_Characters.ijs | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/P1844_Replace_All_Digits_with_Characters.ijs b/P1844_Replace_All_Digits_with_Characters.ijs index 0946e6c..92a5e4d 100644 --- a/P1844_Replace_All_Digits_with_Characters.ijs +++ b/P1844_Replace_All_Digits_with_Characters.ijs @@ -13,3 +13,13 @@ replaceDigits3=: $ {. ,@((0 ". }."1) (,@] ,. shift) {."1)@(_2 ]\ ]) NB. Solution 4 (refactor 3 - J works with more naturally with rows than with columns) replaceDigits4=: $ {. ,@(([ ,. shift~) 0 ". ,.)/@|:@(_2 ]\ ]) + +Note 'Testing' +inputs=: 'a1c1e1';'a1b2c3d4e' +outputs=: 'abcdef';'abbdcfdhe' +s=: 0 {:: inputs +replaceDigits4 s +replaceDigits4 &.> inputs +outputs -: replaceDigits4 &.> inputs +outputs (-:"1)&> (replaceDigits , replaceDigits2 , replaceDigits3 ,: replaceDigits4) &.> inputs +)