Maximum Array Rotation in Mathematica

Published on 28 April 2022 (Updated: 02 February 2023)

Maximum Array Rotation in Mathematica

Welcome to the Maximum Array Rotation in Mathematica page! Here, you’ll find the source code for this program as well as a description of how the program works.

Current Solution

(* Code *)

(* The actual maximum array rotation operating on Mathematica lists: *)

longestArrayRotation = a \[Function] Max @@
    Table[
     Dot[ (* inner product *)
      RotateRight[a, i], (* rotated input list *)
      Range[0, Length[a] - 1](* list of weights *)],
     {i, Length[a]}];

(* The outer function provides the 'user interface': *)

longestArrayRotationMain = l \[Function]
   Module[{e = "Usage: please provide a list of integers (e.g. \"8, 3, 1, 2\")"},
    Catch[
     longestArrayRotation @@ Map[
       (* convert string to integer, or throw *)
       s \[Function] If[StringMatchQ[s, DigitCharacter ..],
         FromDigits[s],
         Throw[e]],
       (* construct argument to array rotation *)
       {StringSplit[If[StringLength[l] > 0, l, Throw[e]], ", "]},
       {-1} (* at each leaf *)]]];


(* Valid Tests *)

Print /@ longestArrayRotationMain /@ {
    "3, 1, 2, 8",
    "1, 2, 8, 3",
    "8, 3, 1, 2"
    };


(* Invalid Tests *)

longestArrayRotationMain[""]

Maximum Array Rotation in Mathematica was written by:

If you see anything you’d like to change or update, please consider contributing.

How to Implement the Solution

No ‘How to Implement the Solution’ section available. Please consider contributing.

How to Run the Solution

No ‘How to Run the Solution’ section available. Please consider contributing.