首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >项目Euler 11 Haskell -网格中最大的产品

项目Euler 11 Haskell -网格中最大的产品
EN

Code Review用户
提问于 2022-07-16 14:21:50
回答 1查看 97关注 0票数 2

Intro

我一直在学习haskell和函数式编程使用随机的Project问题。目前,我已经解决了问题11

在20×20网格中,四个相邻数在同一方向(向上、向下、左、右或对角)的最大乘积是什么?

我的解决方案

我的解决方案由四个部分组成。

  1. 查找行、列和对角线
  2. 将它们压缩到一个列表中
  3. 查找4的子列表的乘积,将它们存储在列表中。
  4. 返回最大乘积
代码语言:javascript
复制
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)

我想要什么来复习

  1. 正如我说的,我对haskell并不熟悉,因此,如果有人能告诉我这段代码有多地道,以及如何改进它,我会同意的。
  2. 简洁-我在haskell见过一些绝对优雅的1线。有没有办法使我的diag函数更简洁?

性能

在我的系统(i5-6200u)上,这个特定的网格在使用0.00s编译时,用户会根据时间执行-O2。因此,我对此毫无顾虑,即使我能减少记忆的使用,我也很乐意接受。

EN

回答 1

Code Review用户

回答已采纳

发布于 2022-07-16 18:46:19

备注

  • 你的代码有效!软件是困难的,工作的软件值得庆祝。
  • 它很容易读懂。
  • 代码结构合理,您可以使用中等级别的特性(析构、where-clauses等)。

建议

没有特别的顺序

  • type Grid = [[Int]]应该是newtype Grid = Grid { rows :: [[Int]] }Grid在语义上不同于[[Int]],因此它应该有自己的类型。
  • 我发现你的一些函数名的选择令人困惑。我建议重命名:
    • diagsupperFallingDiags,为了清晰/精确
    • getDiagfallingDiag,为了清晰/精确
    • mirrorreflectHoriz,为了清晰/精确
    • diagdiags,因为它返回多个对角线,并匹配rowscols的命名方案。

  • 我可能是错的,但我不认为你的评论解释你的对角线获取算法的工作是正确的。实现的算法似乎与所描述的算法不同。我已经替换了编辑代码中的注释(如下所示)
  • 您编写的对角线获取算法返回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),所以您可以简单地将它们称为nubtranspose,而不是Data.List.nubData.List.transpose
  • 范围界定:我会将mirror (重命名为reflectHoriz)移到where-clause之外,因为它是一个非常通用的操作,不特定于对角线获取算法的实现。另外,我会将gridMirror的赋值移到内部,以传达它只用于where块的一小部分。
  • 缩进只有一个缩进级别的where-blocks。使用两种方法只是在浪费一个层次。(入学:这是我的首选;通常在哈斯克尔where-blocks使用两级)
  • 代码对角线[] = []对角线(xs : xss) \ xs = [] \x=.可以更清楚地写成对角[] = []对角([]:_) = []斜面网格=.

编辑代码

这段代码实现了我给出的所有建议以及少量其他更改

如果你能弄清楚fallingDiags是如何工作的,布朗尼就会指着你:-)

代码语言:javascript
复制
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是一种非常有特色的语言,而且几乎总是有更多的东西需要学习。

干杯!

票数 3
EN
页面原文内容由Code Review提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://codereview.stackexchange.com/questions/278125

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档