38
\$\begingroup\$

Challenge:

Take a vector / list of integers as input, and output the largest number that's adjacent to a zero.

Specifications:

  • As always, optional input and output format
  • You may assume that there will be at least one zero, and at least one non-zero element.

Test cases:

1 4 3 6 0 3 7 0
7

9 4 9 0 9 0 9 15 -2
9

-4 -6 -2 0 -9
-2

-11 0 0 0 0 0 -12 10
0

0 20 
20

Good luck and happy golfing!

\$\endgroup\$
  • \$\begingroup\$ You should add a test case like the 4th one, but where the result is negative (there are positive numbers in the list). \$\endgroup\$ – mbomb007 Sep 21 '16 at 15:11
  • \$\begingroup\$ I was going to try this in Retina, but then I noticed there are negatives. Retina hates negatives. \$\endgroup\$ – mbomb007 Sep 21 '16 at 15:12
  • 2
    \$\begingroup\$ Don't let retina dictate what you can and cannot do. Take charge, you're the boss! \$\endgroup\$ – Stewie Griffin Sep 22 '16 at 10:26

31 Answers 31

1
2
0
\$\begingroup\$

SPSS Syntax (98 bytes)

Golfed solution:

CRE L=lead(A,1).
COMP R=MAX(lag(A),L).
EXE.
SEL IF A=0.
EXE.
AGG
/S=MAX(R).
LIST S
/CAS=1.

Ungolfed:

CREATE L=lead(A,1).
COMPUTE R=MAX(lag(A),L).
EXECUTE.
SELECT IF A=0.
EXECUTE.
AGGREGATE
/S=MAX(R).
LIST S
/CASES=1.

Explanation: Input values is ordered vertically in a column. The first line of code creates a column with the lead number, the number before any given number. MAX(lag,A) returns the lagging number, the number after any given number (in test series A). So the second line of code creates a column with the highest number of neighbourging numbers of any given number. We are only interested in the neighbours of zeros, so the forth line selects all the rows with zeros, which now also include the highest valued neighbours. The 6th and 7th line of code finds the max value of selected neighbours.

With data inputs:

* This syntax solves: Returning the greatest adjacent number bigger or equal to itself,
  for any number V.
* CodeGolf asks for the solution for the case V=0.
* Bytes=98.

DATA LIST LIST
/ Sample (A7) a b c d e f g h i j k l m .
BEGIN DATA.
"A" 1, 4, 3, 6, 0, 3, 7, 0
"B" 9, 4, 9, 0, 9, 0, 9, 15, -2
"C" -4, -6, -2, 0, -9
"D" -11, 0, 0, 0, 0, 0, -12, 10
"E"  0, 20
END DATA.
DATASET NAME Inputs WINDOW=FRONT.

FLIP VARIABLES=a b c d e f g h i
/NEWNAMES=Sample.
DATASET NAME CodeGolf WINDOW=FRONT.
DATASET CLOSE Inputs.

CRE L=lead(A,1).
COMP R=MAX(lag(A),L).
EXE.
SEL IF A=0.
EXE.
AGG
/S=MAX(R).
LIST S
/CAS=1.

Just change the sample references (A to E) for testing each sample.

| improve this answer | |
\$\endgroup\$
  • 1
    \$\begingroup\$ I'm assuming your indentation is a bit off. Otherwise it's 126 bytes =) \$\endgroup\$ – Stewie Griffin Sep 22 '16 at 10:58
  • \$\begingroup\$ Ok, thanks. Fixed that now. \$\endgroup\$ – kgbviz Sep 22 '16 at 11:15
  • \$\begingroup\$ What is SPSS syntax? Just curious. Also, welcome to PPCG! \$\endgroup\$ – clismique Sep 22 '16 at 12:09
  • \$\begingroup\$ @qwerp-derp Thank you for the welcome! :-) SPSS Syntax is a high-level data management programming language within the fourth generation of programming languages . It is used within IBM SPSS Statistics software package for statistical analysis. \$\endgroup\$ – kgbviz Sep 22 '16 at 14:01
1
2

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.