2D List(2次元配列)を左に90度回転させるAppleScriptです。
回転させるにあたって、各行のアイテム数のうち最大のものを求め、最大アイテム数に足りない行については詰め物をして、2次元配列をなるべく正方形ないし長方形に整った形にしてから回転させます。
AppleScript名:2Dリストを左(counterclockwise)に90度回転 |
— Created 2017-10-02 by Takaaki Naganoya — 2017 Piyomaru Software use AppleScript version "2.4" use scripting additions use framework "Foundation" property NSArray : a reference to current application’s NSArray set origList to {{1, 2, 3, 4, 5, 6, 7}, {11, 12, 13, 14, 15, 16}, {21, 22, 23, 24, 25, 26, 27, 28}} set wList to rotateListCounterClockwise90(origList, 0) of me –> {{0, 0, 28}, {7, 0, 27}, {6, 16, 26}, {5, 15, 25}, {4, 14, 24}, {3, 13, 23}, {2, 12, 22}, {1, 11, 21}} set sList to rotateListCounterClockwise90(wList, 0) of me –> {{28, 27, 26, 25, 24, 23, 22, 21}, {0, 0, 16, 15, 14, 13, 12, 11}, {0, 7, 6, 5, 4, 3, 2, 1}} set eList to rotateListCounterClockwise90(sList, 0) of me –> {{21, 11, 1}, {22, 12, 2}, {23, 13, 3}, {24, 14, 4}, {25, 15, 5}, {26, 16, 6}, {27, 0, 7}, {28, 0, 0}} set nList to rotateListCounterClockwise90(eList, 0) of me –> {{1, 2, 3, 4, 5, 6, 7, 0}, {11, 12, 13, 14, 15, 16, 0, 0}, {21, 22, 23, 24, 25, 26, 27, 28}} on rotateListCounterClockwise90(aList, blankItem) set curMax to 0 set curLen to length of aList set curMax to getMaxItemCountFrom2DArray(aList) of me set tmpList to {} set twoDList to make2DBlankArray(curLen, curMax) of me set curY to 1 repeat with x from curMax to 1 by -1 set curX to 1 repeat with y from 1 to curLen by 1 set aCon to getItemByXY(x, y, aList, blankItem) of me set twoDList to setItemByXY(curX, curY, twoDList, aCon) of me set curX to curX + 1 end repeat set curY to curY + 1 end repeat return twoDList end rotateListCounterClockwise90 on getMaxItemCountFrom2DArray(curList) set anArray to NSArray’s arrayWithArray:curList set eRes to (anArray’s valueForKeyPath:"@unionOfObjects.@count")’s valueForKeyPath:"@max.self" return eRes as integer end getMaxItemCountFrom2DArray –2D Listに配列の添字的なアクセスを行なってデータを取得 on getItemByXY(aX, aY, aList, aBlankItem) –1 based index try set aContents to contents of (item aX of item aY of aList) on error set aContents to aBlankItem end try return aContents end getItemByXY –2D Listに配列の添字的なアクセスを行なってデータを設定 on setItemByXY(aX, aY, tmpList, aContents) –1 based index set (item aX of item aY of tmpList) to aContents return tmpList end setItemByXY –空白の2D Array を出力する on make2DBlankArray(curLen, curMax) set outArray to {} repeat curMax times set tmpList to {} repeat curLen times set the end of tmpList to "" end repeat set the end of outArray to tmpList end repeat return outArray end make2DBlankArray |
More from my site
(Visited 38 times, 1 visits today)