我一直在学习haskell和函数式编程使用随机的Project问题。目前,我已经解决了问题11。
在20×20网格中,四个相邻数在同一方向(向上、向下、左、右或对角)的最大乘积是什么?
我的解决方案由四个部分组成。
module Main where
import Data.List (nub, transpose)
-- | Grid is just a 2-D List.
type Grid = [[Int]]
grid :: Grid
grid =
[ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
]
main :: IO ()
main = do
print $ maximum products
where
seqs = concatMap ($ grid) [rows, cols, diag]
products = subLists $ concat seqs
where
-- Function to operate on sublists of four
subLists :: [Int] -> [Int]
subLists xs
| null xs = []
| otherwise = (product . take 4 $ xs) : subLists (tail xs)
rows, cols, diag :: Grid -> Grid
-- | Rows returns all rows of Grid.
rows = id
-- | Columns can be defined as the transposition of rows
cols = Data.List.transpose
-- | Diagnoals of a Grid. (All directions: Up & Right, Down & Right, Up & Left, Down & Left)
diag grid =
Data.List.nub allDiags -- Deduplicate list of diagonals to reduce computation of products.
where
-- Concatenate all 4 Directions
allDiags =
(diags . rows) grid
++ (diags . cols) grid
++ (diags . rows) gridMirror
++ (diags . cols) gridMirror
gridMirror = mirror grid
-- Mirror of a grid just reflects it about its columns.
mirror :: Grid -> Grid
mirror = reverse . Data.List.transpose
-- Main logic to get Diagonals of a grid.
-- How this works is basically explained here: https://stackoverflow.com/a/2792547
-- Imagine this grid.
-- [X . . . . .]
-- [. X . . . .]
-- [. . X . . .]
-- [. . . X . .]
-- [. . . . X .]
-- [. . . . . X]
-- When you drop 0 elems from row 0, 1 elem from row 1 ...: You get:
-- [X . . . . .]
-- [X . . . .]
-- [X . . . ]
-- [X . .]
-- [X .]
-- [X]
-- Each of the columns is a diagonal. Repeat this for mirror, tranpose of mirror and transpose and you got all diagonals from all mirrors.
diags :: Grid -> Grid
diags [] = []
diags (xs : xss)
| null xs = []
| otherwise = getDiag (xs : xss) : diags (map (drop 1) (xs : xss))
getDiag :: Grid -> [Int]
getDiag [] = []
getDiag xss
| null $ head xss = []
| otherwise = (head . head) xss : getDiag ((map (drop 1) . drop 1) xss)diag函数更简洁?在我的系统(i5-6200u)上,这个特定的网格在使用0.00s编译时,用户会根据时间执行-O2。因此,我对此毫无顾虑,即使我能减少记忆的使用,我也很乐意接受。
发布于 2022-07-16 18:46:19
where-clauses等)。没有特别的顺序
type Grid = [[Int]]应该是newtype Grid = Grid { rows :: [[Int]] }。Grid在语义上不同于[[Int]],因此它应该有自己的类型。diags到upperFallingDiags,为了清晰/精确getDiag到fallingDiag,为了清晰/精确mirror到reflectHoriz,为了清晰/精确diag到diags,因为它返回多个对角线,并匹配rows和cols的命名方案。all directions: up & right, ...。我觉得这是误导性的。当应用于网格0 1 2 4 5 6 7 8 9时,您的算法不会同时生成[1, 6]和[6, 1],而只产生其中的一种。更准确的是,您的算法生成了上、左/下-右方向(下降的对角线)和上-右/下-左方向(上升的对角线)。Data.List.nub的应用是值得的。我只希望重复两条对角线(最长的两条对角线),我认为这不值得使用O(n^2)运行时的nub。不过,我可能错了。subLists可以实现为fmap产品。过滤器(长度>>> (== 4))。fmap (采取4)。尾巴或者只是fmap产品。fmap (采取4)。因为所有的数字都是正数(所以filter不影响最终的maximum)Data.List (nub, transpose),所以您可以简单地将它们称为nub和transpose,而不是Data.List.nub和Data.List.transposemirror (重命名为reflectHoriz)移到where-clause之外,因为它是一个非常通用的操作,不特定于对角线获取算法的实现。另外,我会将gridMirror的赋值移到内部,以传达它只用于where块的一小部分。where-blocks。使用两种方法只是在浪费一个层次。(入学:这是我的首选;通常在哈斯克尔where-blocks使用两级)这段代码实现了我给出的所有建议以及少量其他更改
如果你能弄清楚fallingDiags是如何工作的,布朗尼就会指着你:-)
module Main where
import Data.List (nub, tails)
import qualified Data.List as List
newtype Grid = Grid { rows :: [[Int]] }
reflectHoriz :: Grid -> Grid
reflectHoriz = Grid . reverse . List.transpose . rows
-- | Columns can be defined as the transposition of rows
transpose :: Grid -> Grid
transpose = Grid . List.transpose . rows
cols :: Grid -> [[Int]]
cols = rows . transpose
-- | Rising and falling diagnoals of a Grid
diags :: Grid -> [[Int]]
diags = \grid -> risingDiags grid ++ fallingDiags grid
where
risingDiags :: Grid -> [[Int]]
risingDiags = fallingDiags . reflectHoriz
fallingDiags :: Grid -> [[Int]]
fallingDiags = upperFallingDiags <> (upperFallingDiags . transpose)
-- Main logic to get Diagonals of a grid.
-- Imagine this grid.
-- [X . . . . .]
-- [. X . . . .]
-- [. . X . . .]
-- [. . . X . .]
-- [. . . . X .]
-- [. . . . . X]
-- When you drop 1 elem from all rows, the diagonal shifts:
-- [ X . . . .]
-- [ . X . . .]
-- [ . . X . .]
-- [ . . . X .]
-- [ . . . . X]
-- [ . . . . .]
-- Repeat to produce every upper falling diagonal of the grid
upperFallingDiags :: Grid -> [[Int]]
upperFallingDiags (Grid []) = []
upperFallingDiags (Grid ([] : _)) = []
upperFallingDiags grid = fallingDiag grid : upperFallingDiags (Grid $ map (drop 1) (rows grid))
fallingDiag :: Grid -> [Int]
fallingDiag (Grid []) = []
fallingDiag (Grid ([] : _)) = []
fallingDiag (Grid xss) = (head . head) xss : fallingDiag (Grid $ (map (drop 1) . drop 1) xss)
main :: IO ()
main = do
print $ maximum products
print $ maximum products == 70600674
where
seqs = concatMap ($ grid) [rows, cols, diags]
products = fmap product . fmap (take 4) . tails $ concat seqs
grid :: Grid
grid = Grid
[ [8, 2, 22, 97, 38, 15, 0, 40, 0, 75, 4, 5, 7, 78, 52, 12, 50, 77, 91, 8],
[49, 49, 99, 40, 17, 81, 18, 57, 60, 87, 17, 40, 98, 43, 69, 48, 4, 56, 62, 0],
[81, 49, 31, 73, 55, 79, 14, 29, 93, 71, 40, 67, 53, 88, 30, 3, 49, 13, 36, 65],
[52, 70, 95, 23, 4, 60, 11, 42, 69, 24, 68, 56, 1, 32, 56, 71, 37, 2, 36, 91],
[22, 31, 16, 71, 51, 67, 63, 89, 41, 92, 36, 54, 22, 40, 40, 28, 66, 33, 13, 80],
[24, 47, 32, 60, 99, 3, 45, 2, 44, 75, 33, 53, 78, 36, 84, 20, 35, 17, 12, 50],
[32, 98, 81, 28, 64, 23, 67, 10, 26, 38, 40, 67, 59, 54, 70, 66, 18, 38, 64, 70],
[67, 26, 20, 68, 2, 62, 12, 20, 95, 63, 94, 39, 63, 8, 40, 91, 66, 49, 94, 21],
[24, 55, 58, 5, 66, 73, 99, 26, 97, 17, 78, 78, 96, 83, 14, 88, 34, 89, 63, 72],
[21, 36, 23, 9, 75, 0, 76, 44, 20, 45, 35, 14, 0, 61, 33, 97, 34, 31, 33, 95],
[78, 17, 53, 28, 22, 75, 31, 67, 15, 94, 3, 80, 4, 62, 16, 14, 9, 53, 56, 92],
[16, 39, 5, 42, 96, 35, 31, 47, 55, 58, 88, 24, 0, 17, 54, 24, 36, 29, 85, 57],
[86, 56, 0, 48, 35, 71, 89, 7, 5, 44, 44, 37, 44, 60, 21, 58, 51, 54, 17, 58],
[19, 80, 81, 68, 5, 94, 47, 69, 28, 73, 92, 13, 86, 52, 17, 77, 4, 89, 55, 40],
[4, 52, 8, 83, 97, 35, 99, 16, 7, 97, 57, 32, 16, 26, 26, 79, 33, 27, 98, 66],
[88, 36, 68, 87, 57, 62, 20, 72, 3, 46, 33, 67, 46, 55, 12, 32, 63, 93, 53, 69],
[4, 42, 16, 73, 38, 25, 39, 11, 24, 94, 72, 18, 8, 46, 29, 32, 40, 62, 76, 36],
[20, 69, 36, 41, 72, 30, 23, 88, 34, 62, 99, 69, 82, 67, 59, 85, 74, 4, 36, 16],
[20, 73, 35, 29, 78, 31, 90, 1, 74, 31, 49, 71, 48, 86, 81, 16, 23, 57, 5, 54],
[1, 70, 54, 71, 83, 51, 54, 69, 16, 92, 33, 48, 61, 43, 52, 1, 89, 19, 67, 48]
]我有很多建议,因为您的代码有很多需要改进的地方。这并不是说它是坏的。不是的!给我留下深刻印象。但是Haskell是一种非常有特色的语言,而且几乎总是有更多的东西需要学习。
干杯!
https://codereview.stackexchange.com/questions/278125
复制相似问题