Last active
August 29, 2015 14:18
-
-
Save adrian17/16e6c9ee9edcdaabde62 to your computer and use it in GitHub Desktop.
sequence search in J (https://github.com/paralax/ColossalOpera/blob/master/easy/integer_sequence_search_1.md)
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
1!:44 ;}:<;.1> (4!:4<'thisfile'){(4!:3) thisfile=.'' NB. boilerplate to set the working directory | |
input =: cutopen 0 : 0 | |
9 6 | |
A000055 1, 1, 1, 1, 2, 3, 6, 11, 23, 47 | |
A000045 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 | |
A050278 1029, 1028, 1029, 1027, 1028, 1027, 1029, 1028, 1029, 1026 | |
A000010 1, 1, 2, 2, 4, 2, 6, 4, 6, 4 | |
A194508 _1, 1, 0, 2, 1, 0, 2, 1, 3, 2 | |
A000111 1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936 | |
A233586 1, 6, 12, 19, 63, 263, 856, 2632, 7714, 9683 | |
A000391 1, 6, 21, 71, 216, 672, 1982, 5817, 16582, 46633 | |
A000713 1, 3, 8, 18, 38, 74, 139, 249, 434, 734 | |
1, 3, 8, 18 | |
263, 856, 2632, 7714, 9683 | |
1, 1, 2, 3, 5, 8 | |
1, 6, 12, 19, 63 | |
20, 22, 24, 28, 30, 34 | |
434, 734, 12115, 1967, 3132, 4902 | |
) | |
func =: ] (([: |: 7 ({. ; }.)&> [ {. }.@]) (>@] , ' ' , (] ([: {. [: (1 {.!.0 >:)&> [ ([: < [ ([: I. (+./@E.~ +. [: +./ [ ([ (] -: [: |. #@] {. |.@[) >@])"1 0 <\@])"1) >@])"2 1 ".@>@])~ ".@>@{:@[) { 'NOMATCH' , >@{.@[)"2 0 [ }. }.@])~ ".@{.@>@{. | |
NB. code golf mode: | |
func =: ](([:|:7({.;}.)&>[{.}.@])(>@],' ',(]([:{.[:(1{.!.0>:)&>[([:<[([:I.(+./@E.~+.[:+./[([(]-:[:|.#@]{.|.@[)>@])"1 0<\@])"1)>@])"2 1".@>@])~".@>@{:@[){'NOMATCH',>@{.@[)"2 0[}.}.@])~".@{.@>@{. | |
func input | |
NB. true oneliner: | |
(](([:|:7({.;}.)&>[{.}.@])(>@],' ',(]([:{.[:(1{.!.0>:)&>[([:<[([:I.(+./@E.~+.[:+./[([(]-:[:|.#@]{.|.@[)>@])"1 0<\@])"1)>@])"2 1".@>@])~".@>@{:@[){'NOMATCH',>@{.@[)"2 0[}.}.@])~".@{.@>@{.) cutopen 1!:1 <'input.txt' | |
0 : 0 NB. displays; | |
1, 3, 8, 18 A000713 | |
263, 856, 2632, 7714, 9683 A233586 | |
1, 1, 2, 3, 5, 8 A000045 | |
1, 6, 12, 19, 63 A233586 | |
20, 22, 24, 28, 30, 34 NOMATCH | |
434, 734, 12115, 1967, 3132, 4902 A000713 | |
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
9 6 | |
A000055 1, 1, 1, 1, 2, 3, 6, 11, 23, 47 | |
A000045 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 | |
A050278 1029, 1028, 1029, 1027, 1028, 1027, 1029, 1028, 1029, 1026 | |
A000010 1, 1, 2, 2, 4, 2, 6, 4, 6, 4 | |
A194508 _1, 1, 0, 2, 1, 0, 2, 1, 3, 2 | |
A000111 1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936 | |
A233586 1, 6, 12, 19, 63, 263, 856, 2632, 7714, 9683 | |
A000391 1, 6, 21, 71, 216, 672, 1982, 5817, 16582, 46633 | |
A000713 1, 3, 8, 18, 38, 74, 139, 249, 434, 734 | |
1, 3, 8, 18 | |
263, 856, 2632, 7714, 9683 | |
1, 1, 2, 3, 5, 8 | |
1, 6, 12, 19, 63 | |
20, 22, 24, 28, 30, 34 | |
434, 734, 12115, 1967, 3132, 4902 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
load 'cheq.ijs' | |
NB. check if it's a simple subsequence | |
simple =: 13 : '+./ x E.~ y' | |
simple =: +./@E.~ | |
cheq 1 2 3 4 5 simple 2 3 4 | |
1 | |
) | |
NB. check if it only matches at the end | |
end =: 13 : '(|.(#y){.|.x) -: y' | |
end =: ] -: [: |. (#@]) {. |.@[ | |
cheq 1 2 3 4 5 end 3 4 5 | |
1 | |
) | |
NB. for all inits of y, check if its end matches | |
test_end =: 13 : '+./ x ([end[:>])"(1 0) <\ y' | |
test_end =: [: +./ [ ([ end >@])"1 0 <\@] | |
cheq 1 2 3 4 5 6 test_end 4 5 6 7 | |
1 | |
) | |
cheq 1 2 3 4 5 6 test_end 4 5 7 7 | |
0 | |
) | |
NB. it matches if it's a simple subsequence or if one of inits matches | |
seq =: simple +. test_end | |
seq =: +./@E.~ +. [: +./ [ ([ (] -: [:|.#@] {. |.@[) >@])"1 0 <\@] | |
cheq 1 2 3 4 5 6 seq 1 2 | |
1 | |
) | |
cheq 1 2 3 4 5 6 seq 1 2 4 | |
0 | |
) | |
cheq 1 2 3 4 5 6 seq 4 5 6 7 | |
1 | |
) | |
cheq 1 2 3 4 5 6 seq 4 5 7 7 | |
0 | |
) | |
NB. wraps seq to find index of sequence matching with query | |
NB. returns empty box when no match | |
find =: 13 : '< x ([: I. seq"1) >y' | |
find =: [: < [ ([: I. seq"1) >@] | |
input =: cutopen 0 : 0 | |
9 6 | |
A000055 1, 1, 1, 1, 2, 3, 6, 11, 23, 47 | |
A000045 0, 1, 1, 2, 3, 5, 8, 13, 21, 34 | |
A050278 1029, 1028, 1029, 1027, 1028, 1027, 1029, 1028, 1029, 1026 | |
A000010 1, 1, 2, 2, 4, 2, 6, 4, 6, 4 | |
A194508 _1, 1, 0, 2, 1, 0, 2, 1, 3, 2 | |
A000111 1, 1, 1, 2, 5, 16, 61, 272, 1385, 7936 | |
A233586 1, 6, 12, 19, 63, 263, 856, 2632, 7714, 9683 | |
A000391 1, 6, 21, 71, 216, 672, 1982, 5817, 16582, 46633 | |
A000713 1, 3, 8, 18, 38, 74, 139, 249, 434, 734 | |
1, 3, 8, 18 | |
263, 856, 2632, 7714, 9683 | |
1, 1, 2, 3, 5, 8 | |
1, 6, 12, 19, 63 | |
20, 22, 24, 28, 30, 34 | |
434, 734, 12115, 1967, 3132, 4902 | |
) | |
n =: ". {. > {. input | |
NB. split into data and queries | |
data =: n {. }. input | |
query =: n }. }. input | |
NB. splits into name row and numers row | |
data =: |: 7 ({.;}.)&> data | |
names =: 'NOMATCH', >{. data | |
nums =: ". >{: data | |
NB. wraps find to take query as string | |
NB. and return match_index+1 on match, 0 on no match | |
indices =: 13 : '{. (1{.!.0 >:)&> x find"2 1 ". > y' | |
indices =: [: {. [: (1 {.!.0 >:)&> [ find"2 1 ".@>@] | |
NB. displays the result | |
func =: (13 : '(>y), '' '', (nums indices y) { names')"0 | |
NB. we can call it now: | |
NB. func query | |
NB. Let's inline creation of nums and names: | |
func =: 13 : '(>y), '' '', ((".>{:x) indices y) { (''NOMATCH'',>{.x)'"2 0 | |
NB. now inline everything into a tacit definition: | |
func =: ((>@]), ' ', (] ([: {. [: (1 {.!.0 >:)&> [ ([: < [ ([: I. (+./@E.~ +. [: +./ [ ([ (] -: [: |. #@] {. |.@[) >@])"1 0 <\@])"1) >@])"2 1 ".@>@])~ ".@>@{:@[) { 'NOMATCH', >@{.@[)"2 0 | |
NB. and remove whitespace because... dunno, golf | |
func =: ((>@]),' ',(]([:{.[:(1{.!.0>:)&>[([:<[([:I.(+./@E.~+.[:+./[([(]-:[:|.#@]{.|.@[)>@])"1 0<\@])"1)>@])"2 1".@>@])~".@>@{:@[){'NOMATCH',>@{.@[)"2 0 | |
NB. and call it: | |
data func query | |
NB. now let's also obtain data and query in the same line. | |
driver =: 13 : '(|: 7 ({.;}.)&> x {. }. y) func (x }. }. y)' | |
driver =: ([: |: 7 ({.;}.)&> [ {. }.@]) func [ }.}.@] | |
n driver input -> same output | |
NB. and finally inline obtaining value of n: | |
solution =: 13 : '(". {. > {. y) driver y' | |
solution =: ] driver~ ".@{.@>@{. | |
solution input | |
NB. we can also read it from file: | |
NB. solution cutopen 1!:1 <'input.txt' | |
NB. and finally, fully tacit version of solution: | |
(](([:|:7({.;}.)&>[{.}.@])(>@],' ',(]([:{.[:(1{.!.0>:)&>[([:<[([:I.(+./@E.~+.[:+./[([(]-:[:|.#@]{.|.@[)>@])"1 0<\@])"1)>@])"2 1".@>@])~".@>@{:@[){'NOMATCH',>@{.@[)"2 0[}.}.@])~".@{.@>@{.) input |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment