List the coordinates of 8 squares adjacent to (0, 0)
in a 2d grid. Namely,
[(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1)]
Order does not matter. The goal is to store the points into a list. Shortest answer wins.
The most natural representation of a list of pairs is a list of boxes containing each pair.
}.4|.,{;~i:1
+---+----+---+---+-----+----+----+----+
|0 1|1 _1|1 0|1 1|_1 _1|_1 0|_1 1|0 _1|
+---+----+---+---+-----+----+----+----+
The magic happens in {
, called Catalogue. It is essentially a generalized Cartesian product. The }.4|.
part is for removing the 0 0
pair.
eval echo\ {-1..1},{-1..1}\;|grep 1
I know this doesn't beat the other bash answer but someone had to think of grep
(a=-1,1,0).product(a)[0,8]
Ruby has many nice builtin array operators, but some of them have ridiculously long names.
[*[-1,1,0].repeated_permutation(2)].take 8
(a=-1,1,0).product(a)[0,8]
is 26 chars.
\$\endgroup\$
Commented
Apr 8, 2014 at 20:33
l=zip([1,-1,0]*3,[-1,1]*3+[0]*2)
Inspired by @primo
9,4-{[.3/(\3%(]}%
Since the question calls for the points to be "listed in natural format", this leaves an array on the stack. To pretty-print it, append p
, as in the online demo.
c=[0,1,-1]
_:l=[(a,b)|a<-c,b<-c]
The list is l (L).
_:l=(,)<$>c<*>c
to save 6 characters, although it requires import Control.Applicative
.
\$\endgroup\$
Commented
Apr 9, 2014 at 4:45
++++++++[>+>++>+++>++++>+++++>++++++>+++++++>++++++++>+++++++++>++++++++++>+++++++++++>++++++++++++>+++++++++++++>++++++++++++++>+++++++++++++++>++++++++++++++++<<<<<<<<<<<<<<<<-]>>>>>>>>>>>+++.---<<<<<<<<<<<>>>>>.<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>.<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>.<<<<<<>>>>>>----.++++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>.<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>>----.++++<<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>----.++++<<<<<<>>>>>.<<<<<>>>>>>.<<<<<<>>>>>>----.++++<<<<<<>>>>>>---.+++<<<<<<>>>>>>+.-<<<<<<>>>>>+.-<<<<<>>>>>>>>>>>>---.+++<<<<<<<<<<<<.
++++++++++[>+++++++++>++++>+++++>++++<<<<-]>+.>.>-.>++++.<.<+.>>.<<-.>.>.<-.<+.>>.<<-.>+.>.+.-<.<+.>>.<<-.>-.>.<+.<+.>>.<<-.>-.>.+.<+.<+.>>-.<<-.>>+.<.>-.<.<+.>>.<<-.>>+.<.>-.<-.<+.>>.<<-.>>+.<+.>-.+.<.<+.<++.
Original by Benjamin - i gave it another shot as well and got to 209 characters as well.
for$x(-1..1){$x|$_&&push@r,[$x,$_]for-1..1}
The result is stored in array @r
. Explicitly it can be defined with 58 bytes:
@r=([-1,-1],[-1,0],[-1,1],[0,-1],[0,1],[1,-1],[1,0],[1,1])
}.4|.,<@,"0/~i:1
Usage:
}.4|.,<@,"0/~i:1
┌───┬────┬───┬───┬─────┬────┬────┬────┐
│0 1│1 _1│1 0│1 1│_1 _1│_1 0│_1 1│0 _1│
└───┴────┴───┴───┴─────┴────┴────┴────┘
Pretty ugly - especially dropping the (0, 0)
. There's probably a better way.
Most@Tuples[{-1,1,0},2]
(*
{{-1, -1}, {-1, 1}, {-1, 0}, {1, -1}, {1, 1}, {1, 0}, {0, -1}, {0, 1}}
*)
Tuples[{...},2]
with {...}~Tuples~2
to save another byte.
\$\endgroup\$
Commented
Apr 8, 2014 at 18:26
Most[]
(or I can't find a way around it). BTW I never saw you around mathematica.stackexchange.com Any particular reason?
\$\endgroup\$
Commented
Apr 8, 2014 at 18:31
~..~
had higher precedence than @
. I think that means I can save a byte in quite a few of my mathematica-answers :D. No particular reason why I haven't been around mathematica stackexchange, but I haven't used other SE sites other than SO much until recently. And I don't really consider myself an expert in Mathematica to be answering interesting questions. And I also don't use it for more than some casual stuff like here in code golf and the odd physics problem.
\$\endgroup\$
Commented
Apr 8, 2014 at 18:34
Tuples[{a,b,c ...}, n]
generates all possible lists of length n
containing {a,b,c ...}
as elements. Then Most[]
discards the last one (which is {0,0}
)
\$\endgroup\$
Commented
Apr 8, 2014 at 18:35
Only a bit worse than Java! 5x less than the other BF solution.
++++++++++[>+++++++++>++++>+++++>++++<<<<-]>+.>.>-.>++++.+.<.<+.>>-.<<-.>.>.<-.<+.>>.<<-.>+.>.<.<+.>>.<<-.>-.>.<+.<+.>>.<<-.>>+.<.>-.<.<+.>>.<<-.>>+.<.>-.<-.<+.>>.<<-.>>+.<+.>-.+.<.<+.>>-.<<-.>-.>.+.<+.<+.<++.
Outer[List,d=Range@3-2,d]~Flatten~1~Drop~{5}
Well. Generates all 9 coordinates, flattens the list and drops the centre.
l=[(a,b)for a in[1,0,-1]for b in[1,0,-1]];l.pop(4)
Storing the list of coordinates as a bash array a
:
a=({-1..1},{-1..1});a[4]=
Output:
$ a=({-1..1},{-1..1});a[4]= $ echo ${a[@]} -1,-1 -1,0 -1,1 0,-1 0,1 1,-1 1,0 1,1 $
[for i in 0..7->i*2/5-1,(i*5/4)%3-1]
Let's do this an interesting way. Hint: "2" is ASCII 50.
char x[]="11123332",y[8],i;for(i=8;i--;)y[(i+2)%8]=x[i]-=50;
char *x="11123332"
to save a char?
\$\endgroup\$
Commented
Apr 8, 2014 at 18:22
auto x="11123332"
to save another
\$\endgroup\$
Commented
Apr 8, 2014 at 18:36
char*x="11123332";for(i=8;i--;)y[(i+2)%8]=x[i]-=50;
saving 8 bytes. You have to include the type specifier char
so it knows what to store in *x.
\$\endgroup\$
Commented
Apr 8, 2014 at 18:42
char*x="11123332"
if you're not so comfortable with auto
.
\$\endgroup\$
Commented
Apr 8, 2014 at 18:42
i=8;while(i--)
I believe. Combine with char*x=
and you're looking at only 57 bytes.
\$\endgroup\$
r=range(-1,2);list({(i,j) for i in r for j in r}-{(0,0)})
Improved to
r=[-1,0,1];list({(i,j) for i in r for j in r}-{(0,0)})
with storage of the variable
r=[-1,0,1];l=list({(i,j) for i in r for j in r}-{(0,0)})
r=[-1,0,1]
is shorter!
\$\endgroup\$
Commented
Apr 8, 2014 at 17:56
l=
before list
at a price of 2 chars
\$\endgroup\$
Most simply:
$/=(-1..1 X -1..1)[^4,5..*]
Get the permutations of -1, 0, and 1 with itself, and hard-code skipping (0, 0). But that's no fun! I prefer things to be more complex: (50 characters)
$/=map {[.re,.im]},grep *.abs>0,(-1..1 X+ -i,0i,i)
a=[(i*2/5-1,-i%3-1)for i in range(8)]
An alternative at 40:
a=zip([0,1,-1]*3,[0]*3+[1]*3+[-1]*3)[1:]
And a 42 that's also interesting:
a=0,1,-1;a=zip(sum(zip(a,a,a),()),a*3)[1:]
1↓,∘.,⍨0 1 ¯1
Explanation:
1↓ ⍝ Drop the first element of
, ⍝ an array that comes from the matrix formed by
∘.,⍨ ⍝ concatenating each element with every other element
0 1 ¯1⍝ in this array [0,1,-1]
Way too much, I know, but its the best I could do right now...
I might think of something better...
class A{public static void main(String...a){int[][] l=new int[8][2];for(int x=-1,y,i=0;x<2;x++){for(y=-1;y<2;y++){if(x!=0||y!=0){l[i][0]=x;l[i][1]=y;System.out.print("("+l[i][0]+","+l[i][1]+")");i++;}}}}}
Sensibly:
class A {
public static void main(String...a) {
int[][] l = new int[8][2];
for (int x = -1, y, i = 0; x < 2; x++) {
for (y = -1; y < 2; y++) {
if (x != 0 || y != 0) {
l[i][0] = x;
l[i][1] = y;
System.out.print("("+l[i][0]+","+l[i][1]+")");
i++;
}
}
}
}
}
P(*ItPr((-1,0,1),repeat=2))
Nothing all that special, really.
In regular python:
from itertools import product
for i in product((-1,0,1),repeat=2): print i
(0, 0)
. I think this is not supposed to be included
\$\endgroup\$
Commented
Apr 8, 2014 at 17:29
main(i){for(i=0;i<9;i++)if(i-4)printf("%s(%d,%d)",i?",":"",i%3-1,i/3-1);}
This works most of the time ;)
Union[RandomInteger[2,{99,2}]-1]~Drop~{5}
Equivalently:
Drop[Union@RandomInteger[2,{99,2}]-1,{5}]
RandomInteger
.
\$\endgroup\$
Commented
Apr 11, 2014 at 11:17
Similar to my other answer:
unique(round(2*rand(99,2))-1,'rows');ans([1:4,6:9],:)
I couldn't index on the output of unique directly, and I couldn't find a way around that...
Too much for Haskell and definitely not as good as the other Haskell answer, but here's a different, albeit sucky, approach:
take 8 (zip (replicate 3 (-1) ++ replicate 3 1 ++ replicate 3 0) (concat (replicate 3 [-1,1,0])))
:D
Self-explanatory.
def x=List(-1,0,1);for{i<-x;j<-x;if i!=0|j!=0}yield(i,j)
Ungolfed:
def x=List(-1,0,1)
for{
i <- x
j <- x
if i!=0 | j!=0
} yield(i,j)
@r=map[split","],grep/1/,glob"{-1,0,1},"x2
Uses the shell-like glob functionality to generate the list of strings ("-1,-1","-1,0","-1,1","0,-1","0,0","0,1","1,-1","1,0","1,1")
, discards strings that don't contain the character "1"
(i.e. drop "0,0"
), then splits the strings on comma to get the desired data structure.
@r=map[split x],grep/1/,glob"{-1,0,1}x"x2
abuses barewords: the strings are ("-1x-1","-1x0"
, etc.)
and the split is done on the letter x
instead.
Fortran supports complex variables, so we just store the 8 values as a set of complex numbers and get ourselves a list of tuples.
complex x(8);x=reshape([(1,-1),(1,0),(1,1),(0,1),(-1,1),(-1,0),(-1,-1),(0,-1)],[8]);print*,x;end
Printing this to look nice might be a pain in the butt because printing the real
and aimag
components adds a lot of bytes. But we can deal with floats.
Old answer:
complex x(8);k=1;do i=-1,1;do j=-1,1;if((j==0).and.(i==0))cycle;x(k)=cmplx(i,j);k=k+1;enddo;enddo;end
This abuses implicit typing (as usual).
create table a(x int);insert into a values(-1),(0),(1);select a.x,b.x y from a,a b where not(a.x=0 and b.x=0)
if you want to retest it, you can prepend drop table if exists a;
(0, 0)
has to be omitted from the output? \$\endgroup\$(0,0)
, after all. \$\endgroup\$