没有什么是这么复杂的算法

    科技2025-03-05  27

    I am not a Technical Blogger, and everyone who knows me or went to Flatiron with me knows that. But I love Algorithms, Puzzles and a good challenge.

    我不是技术博客,而且认识我或与我一起去过Flatiron的每个人都知道这一点。 但是我喜欢算法,难题和很好的挑战。

    There is nothing excites me more than a complicated array of arrays, or object of objects, or a mix!!! And I’d love to share my secret of how to solve any complicated problem and how I overcame my weaknesses when I started learning Algorithms.

    除了复杂的数组阵列,对象的对象或混合的东西之外,没有什么让我兴奋的!!! 我很想分享我的秘密:如何解决任何复杂的问题以及在我开始学习算法时如何克服自己的弱点。

    1- There is nothing called Hard or Easy Algorithm!

    1-没有什么叫做硬算法或简易算法!

    The word Algorithm isn’t just used to for engineers to fix problems, it could be anything from a recipe of your favorite dish, to the steps of Salsa dancing in the club. It’s simply a set of instructions to perform, fix or make something nice!

    算法一词不仅用于解决工程师问题,还可以是您喜欢的菜谱,也可以是俱乐部中萨尔萨舞的步骤。 这只是一组执行,修复或使事物变得更好的说明!

    2- For code challenges and Interviews

    2-对于代码挑战和面试

    Calm down, read and research anything in the instructions you are not familiar with. Remember you’re giving the instructions in English.

    冷静,阅读和研究您不熟悉的说明中的任何内容。 请记住,您使用的是英文说明。

    Also remember that the interviewer is looking for a team member, so consider him your teammate to help you with any clue or hints you may find helpful.

    还请记住,面试官正在寻找团队成员,因此请认为他是您的队友,可为您提供任何线索或暗示,以帮助您。

    “Put me in a Team and I’ll fix any problem ” — Mokhtar Ali :)

    “让我成为一个团队,我会解决所有问题”-Mokhtar Ali :)

    I will explain in This complicated challenge I got on CodeWars.com

    我将在CodeWars.com上遇到的这个复杂挑战中进行解释

    曼哈顿距离(Manhattan Distance)

    The distance formula can be used to find the distance between two points. What if we were trying to walk from point A to point B, but there were buildings in the way? We would need some other formula..but which?

    距离公式可用于查找两点之间的距离。 如果我们试图从A点步行到B点,但是路上有建筑物怎么办? 我们需要其他公式。但是,哪个?

    Manhattan distance is the distance between two points in a grid (like the grid-like street geography of the New York borough of Manhattan) calculated by only taking a vertical and/or horizontal path.

    曼哈顿距离是仅通过垂直和/或水平路径计算得出的网格中两个点之间的距离(例如,曼哈顿曼哈顿区的网格状街道地理)。

    Complete the function that accepts two points and returns the Manhattan Distance between the two points.

    完成接受两个点并返回两个点之间的曼哈顿距离的功能。

    The points are arrays containing the x and y coordinate in the grid. You can think of x as the row in the grid, and y as the column.

    这些点是在网格中包含x和y坐标的数组。 您可以将x视为网格中的行,并将y视为列。

    例子 (Examples)

    manhattanDistance( [1, 1], [1, 1] ) // => returns 0manhattanDistance( [5, 4], [3, 2] ) // => returns 4manhattanDistance( [1, 1], [0, 3] ) // => returns 3

    In this code challenge, I have found lots of things that I am not familiar with, such as Grids and Manhattan Geography.

    在此代码挑战中,我发现了许多我不熟悉的东西,例如Grids和Manhattan Geography。

    I quickly googled “Grid system for maps” and I got this helpful article:

    我Swift在Google搜索“ G rid system for maps ”上找到了这篇有用的文章:

    http://www.physicalgeography.net/fundamentals/2b.html http://www.physicalgeography.net/fundamentals/2b.html

    The map explains how many lines go from PointA to PointB using 2 arrays.

    该地图说明了使用2个数组从PointA到PointB的行数。

    PointA = [7,4] PointA = [7,4] If you look closer the blue star represents pointA which is [7, 4], where X is 7, and Y is 4.

    如果您仔细观察,则蓝星代表的pointA是[7,4],其中X是7,Y是4。 PointB is an array of [5,2], both points on the map will look like this, as the top blue star is the coordinate of PointA, and the bottom Black star is the coordinate of PointB.

    PointB是[5,2]的数组,地图上的两个点都将看起来像这样,因为顶部的蓝色星形是PointA的坐标,而底部的黑色星形是PointB的坐标。 PointB = [5,2] 点B = [5,2]

    Note:

    注意:

    Don’t hesitate to try any idea, even if you think it’s stupid or won’t fix it!!

    不要犹豫,尝试任何想法,即使您认为这很愚蠢或无法解决!!

    At this point I understood that from PointA to PointB I have to walk 4 blocks “lines”. But how can I put this in code?

    在这一点上,我知道从PointA到PointB我必须走4块“线”。 但是如何将其放入代码中? This the simple part, you have two arrays, and each array = [X,Y], if you deduct X of PointA from X of PointB and vice versa for Y, you’ll get 2 numbers.

    这是简单的部分,您有两个数组,每个数组= [X,Y],如果从PointB的X减去PointA的X,反之亦然,您将得到2个数字。 Add them together, and you’ll get the number of blocks between the 2 points!

    将它们加在一起,您将获得两点之间的块数! let x = PointA[X] - PointB[X]let y = PointA[Y] - PointB[Y]let distance = x + y

    Note:

    注意:

    Use Math.abs() when you add the distance because you may get a negative number which is a fair distance between 2 streets or points.

    添加距离时请使用Math.abs(),因为您可能会得到一个负数,它是2条街道或点之间的合理距离。

    Math.abs(-2) gives the absolute value of -2 which is 2.

    Math.abs(-2)给出-2的绝对值,即2。

    function manhattanDistance(pointA, pointB){ let x = PointA[0] - PointB[0]; let y = PointA[1] - PointB[1]; let distance = Math.abs(x) + Math.abs(y); return distance;}

    PointA[0] is the first number of the array which is X, and PointA[1] is the second number of the array which is Y.

    PointA [0]是数组的第一个数字X,而PointA [1]是数组的第二个数字Y。

    I hope this was helpful!

    我希望这可以帮到你!

    Mokhtar Ali, Full Stack Software Engineer | Designer | Marketing | Accounting | Blogger | World Traveler | NYC

    Mokhtar Ali,全栈软件工程师| 设计师| 市场营销会计学博客| 世界旅行者| 纽约市

    Check out my Traveling Blog:

    查看我的旅行博客:

    https://passportshub.website/

    https://passportshub.website/

    翻译自: https://medium.com/@joemokhtarali/nothing-is-such-a-complicated-algorithms-d7dce5980505

    Processed: 0.017, SQL: 8