pubfnadjacent_product(nums:&[usize])->Option<usize>{nums.windows(2).map(|slice|matchslice{[a,b]=>a*b,_=>unreachable!(),// this could panic at runtime if it could actually be reached}).max()}
which produce identical assembly to each other in release mode (meaning the compiler proves different sized slices unreachable without your help)
Coding since 11yo, that makes it over 30 years now ~~~
Have a PhD in Comp Sci ~~~
Love to go on bike tours ~~~
I try to stay as generalist as I can in this crazy wide place coding is at now.
I'm Calin Baenen – AKA KattyTheEnby – a programmer born October 30th, 2006.
I love programming, it has been my passion since I was a kid, and will forever be my passion.
adjacentProduct=lambdaarr:max(arr[i]*arr[i+1]foriinrange(len(arr)-1))cases=[([1,2,3],6),([3,4,5],20),([3,7,9],None),([-3,-4,15],None),([-4,-1,-10],None),]forarg,ansincases:print(f"adjacentProduct({arg}) = {adjacentProduct(arg)}{f'; answer should be: {ans}' if ans else ''}.")ifall(adjacentProduct(arg)==ansforarg,ansincasesifans):print('All test cases are correct')else:print('NOT all test cases are correct')
Rust
on stable you have the options of instead doing:
or
which produce identical assembly to each other in release mode (meaning the compiler proves different sized slices unreachable without your help)
Generic:
Haha square meters go brr
Golang solution
JS
Here is the simple solution in PHP:
A KISS version in typescript...
C
Perl
What counts as adjacent? Neighboring?
Like the tests, I'm gonna assume that this only takes 3 inputs.
But, Java:
Python 3 oneliner with testcases and TIO link:
Try it online!
JS
JS