python表示除法的代码_找到您的最佳代码解决方案,理解大0表示法

python表示除法的代码

The building block for finding the “good”, the “better” and the “best” solution possible

寻找“最佳”,“最佳”和“最佳”解决方案的基础

Big 0 notation is an essential concept to understand for your developer’s career. First, it is asked a lot in coding interviews questions. Second, it’s the determining factor when it comes to choosing your most efficient code solution or algorithm.

Big 0表示法是开发人员的职业必不可少的基本概念。 首先,在编写采访访谈的问题时要问很多。 其次,这是选择最有效的代码解决方案或算法时的决定因素。

I already mentioned in another article how code changes when implemented on a large scale. Suppose you write a function that gets a series of strings and then parses out some word out of them.

我已经在另一篇文章中提到过,在大规模实施时代码将如何更改。 假设您编写了一个函数,该函数获取一系列字符串,然后从中解析出一些单词。

For most of you, such a program would be a no-brainer to implement in your language of choice. But what happens if the input strings start to become 10000? Or maybe a million, or even ten million.

对于大多数人来说,使用您选择的语言来实施这样的程序将是一件容易的事。 但是,如果输入字符串开始变为10000,会发生什么? 也许是一百万,甚至一千万。

You would still have 10 ways of doing it with all the tools that modern languages offer. But what’s the actual best way? What is the solution that takes up less runtime? Or in other words, what’s the most efficient algorithm?

使用现代语言提供的所有工具,您仍然会有10种方式来实现。 但是实际的最佳方法是什么? 什么是占用较少运行时间的解决方案? 换句话说,最有效的算法是什么?

That’s when Big 0 notation comes in handy.

这就是Big 0标记派上用场的时候。

Big 0 is your language of choice to express the complexity of your algorithms. Or, in simplified words, Big 0 is the way you will use to describe the runtime of your solution in terms of how quickly it grows relative to the input, as the input gets arbitrarily large.

Big 0是表示算法复杂性的首选语言。 或者,用简化的话来说,Big 0是用来描述解决方案的运行时间的方式,该解决方案的运行时间取决于输入相对于输入的增长速度(随着输入任意增大)。

It’s practical terms, it just boils down to some easy math formula, but without all the math hard stuff and with a lot of abstraction. In fact, Big 0 provides you with three different levels of abstraction:

这是实用的术语,它可以归结为一些简单的数学公式,但是却没有所有的数学难题,并且有很多抽象。 实际上,Big 0为您提供了三种不同的抽象级别:

  1. It’s hard to determine the exact runtime of an algorithm. Since this is tied to the processor running it, what else the computer is running and sometimes even the browser used can mislead the final result. Big 0 addresses this issue by describing just how quickly the runtime grows.

    很难确定算法的确切运行时间。 由于这与运行它的处理器有关,因此计算机在运行其他什么东西,有时甚至使用的浏览器也可能误导最终结果。 Big 0通过描述运行时的增长速度来解决此问题。

  2. Since we’re not using time constraints to measure the efficiency of our algorithms, Big 0 has to introduce another abstraction to describe the incremental runtime. The size of the input, called n, is used for this purpose. So you will often see stuff like: “This runtime grows in the order of the size of the input O(n)”.

    由于我们没有使用时间限制来衡量算法的效率,因此Big 0必须引入另一个抽象来描述增量运行时间。 为此使用称为n的输入大小。 因此,您经常会看到类似以下内容:“此运行时以输入O(n)的大小顺序增长”。

  3. You algorithms may have parts that could look expensive at first. But they will become totally irrelevant as n will grow. That’s why you will never see stuff like 0(n +1).

    您的算法一开始可能会有部分看起来很昂贵。 但是随着n的增长,它们将变得完全无关紧要。 这就是为什么您永远看不到0(n +1)之类的原因。

实际例子 (Practical Examples)

Constant Big 0 algorithm
Constant Big 0 algorithm
常数大0算法

In this function, you can see that we always perform the same operation: returning the first element of the passed-in array . It doesn’t matter how many items such array will contain. The runtime will be constant, or better expressed as O(1).

在此函数中,您可以看到我们总是执行相同的操作:返回传入array的第一个元素。 这样的数组包含多少个项目并不重要。 运行时间将是常量,或者更好地表示为O(1)。

Linear Big 0 notation example
Linear Big 0 notation algorithm
线性Big 0表示法

In this example, you can see how, as n grows, the runtime will grow. If you have 10 items, you will have to console.log 10 times, if n is 10000 you will have to log 10000 times and so on. So this function runs in what’s called linear time O(n).

在此示例中,您可以看到随着n的增长,运行时间将如何增长。 如果有10个项目,则必须console.log 10次​​,如果n为10000,则必须记录10000次,依此类推。 因此,此函数在所谓的线性时间O(n)中运行。

Quadratic Big 0 algorithm
Quadratic Big 0 algorithm
二次大0算法

This is an algorithm whose performance is directly proportional to the square of the size of the input data set: O(n²). This is pretty common with nested loops, which should generally be avoided as a rule of thumb. Note how more nested loops can also bring to a situation where you have 0(n³) 0(n⁴).

这是一种算法,其性能与输入数据集O(n²)的大小的平方成正比。 这在嵌套循环中很常见,通常应避免使用嵌套循环。 请注意,更多的嵌套循环还可以带来0(n³)0(n⁴)的情况。

简化大0表达式 (Simplifying Big 0 Expressions)

As mentioned above, never forget to simplify your big 0 expressions, as you just need to express the most important data. In fact, some steps of your algorithm may look important to you, but as n grows they will become totally useless to mention.

如上所述,不要忘记简化大的0表达式,因为您只需要表达最重要的数据即可。 事实上,你的算法的某些步骤可能看起来对你很重要,但随着n 增长后,它们将变得毫无用处。

Instead of expressing constants, always use O(1):

始终使用O(1)而不是表示常量:

0(500) becomes O(1)

For incredibly big numbers like 1000000, added numbers don’t matter, so:

对于不可思议的大数(例如1000000),加数并不重要,因此:

0(n + 1) becomes O(n); ------------ O(2n) becomes O(n) 

Think about it, is it really important to specify that 1 when you’re dealing with a giant input? Is that really meaningful information?

考虑一下,在处理大量输入时指定1是否真的很重要? 那真的是有意义的信息吗?

空间复杂度 (Space Complexity)

Sometimes, instead of calculating runtime, you may wish to calculate memory usage. Big 0 comes in handy also in this case. The usage is similar to that of runtime, you simply look the total size (relative to the size of the input) of any new variables you’re allocating.

有时,您可能希望计算内存使用量,而不是计算运行时间。 大0在这种情况下也派上用场。 用法与运行时类似,您只需查看要分配的任何新变量的总大小(相对于输入的大小)。

Linear Big 0 notation example
Constant memory usage O(1)
恒定内存使用量O(1)

In this case, since you’re just allocating one variable i, you have a constant memory usage, or better O(1).

在这种情况下,由于您只分配了一个变量i ,因此您具有恒定的内存使用率或更好的O(1)。

While in the following example, since the size of your array will scale with the number of elements passed as input n , we say that the memory usage is linear, or better O(n).

在下面的示例中,由于数组的大小将与作为输入n传递的元素数成比例,因此我们说内存使用情况是线性的,或者更好的是O(n)。

Linear memory usage O(1)
Linear memory usage O(1)
线性内存使用量O(1)

Keep in mind that when it comes to space complexity, space created by the passed in inputs is not taken into consideration.

请记住,当涉及到空间复杂性时,不会考虑传入的输入所创建的空间。

结论 (Conclusion)

Big O is one of those tools that every developer needs. And it’s something that true professionals have hardwired in their brains in order to find the best possible solution. So make Big O one of your best friends, and watch your code get better and better!

Big O是每个开发人员都需要的工具之一。 真正的专业人士已在脑海中扎根,以寻求最佳解决方案。 因此,让Big O成为您最好的朋友之一,并看着您的代码越来越好!

重要要点 (Key Takeaways)

  • Big 0 is an extremely important tool, both for acing code interview and building better algorithms.

    Big 0是一个非常重要的工具,它既可以加快代码访问速度,又可以构建更好的算法。
  • Big 0 is the way you will use to describe the runtime of your solution in terms of how quickly it grows relative to the input, as the input gets arbitrarily large.

    大0是用来描述解决方案的运行时间的方式,该解决方案是随着输入任意增大,它相对于输入增长的速度。
  • It can be used both to determine runtime and space complexity.

    它可以用于确定运行时和空间的复杂性。
  • True professionals know how to use Big O to find a balance between runtime, memory usage and complexity of a solution.

    真正的专业人员知道如何使用Big O在运行时,内存使用和解决方案的复杂性之间找到平衡。

资源资源 (Resources)

翻译自: https://medium.com/asayer/find-your-best-code-solution-understanding-big-0-notation-ce671c18fa62

python表示除法的代码

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值