A Collection of Code Snippets in as Many Programming Languages as Possible
This project is maintained by TheRenegadeCoder
Welcome to the Maximum Array Rotation in Tcl page! Here, you'll find the source code for this program as well as a description of how the program works.
proc usage {} {
puts stderr {Usage: please provide a list of integers (e.g. "8, 3, 1, 2")}
exit 1
}
proc parseList {s} {
set tokens [split [string trim $s] ","]
if {[llength $tokens] < 1} { usage }
set result {}
set result {}
foreach token $tokens {
set t [string trim $token]
if {$t eq "" || [catch {expr {int($t)}} val]} usage
lappend result $val
}
return $result
}
proc maximumRotationSum {numbers} {
set n [llength $numbers]
if {$n == 0} { usage }
set totalSum 0
set currentSum 0
for {set i 0} {$i < $n} {incr i} {
set val [lindex $numbers $i]
incr totalSum $val
incr currentSum [expr {$val * $i}]
}
set maxSum $currentSum
for {set i 1} {$i < $n} {incr i} {
set rotatedVal [lindex $numbers [expr {$n - $i}]]
set currentSum [expr {$currentSum + $totalSum - $n * $rotatedVal}]
if {$currentSum > $maxSum} { set maxSum $currentSum }
}
return $maxSum
}
if {$argc != 1} { usage }
set numbers [parseList [lindex $argv 0]]
puts [maximumRotationSum $numbers]
Maximum Array Rotation in Tcl was written by:
If you see anything you'd like to change or update, please consider contributing.
No 'How to Implement the Solution' section available. Please consider contributing.
No 'How to Run the Solution' section available. Please consider contributing.