Recently, I had a very, very intense discussion about the complexity of executing a super simple algorithm with my colleague. In the end, we both agreed to disagree, but as I thought about it, it challenged my basic understanding of the basics of computer science, and so I therefore need to get more information on this.
Given the following python, what is the difficulty of executing Big-O:
for c in "How are you today?": print c
Now I immediately called that it was just of the order of O (n) aka linear. It depends on the length of the string, so this loop will grow linearly as the length of the string increases.
Then my colleague said: “No, it is permanent, because we know that for the set of all the lines that we deal with (in our case), the maximum line always has a length of 255 characters (in our case), so it should be permanent. " He further said: "Since we have a maximum upper bound on the length of the string character, this results in O (255), which boils down to O (1)."
In any case, we returned and the fourth, and after 45 minutes we both drew the sketches, which we both locked in this matter.
My question is, in which world or in which mathematical system is the cycle higher than the cycle with constant time? If we knew that our upper bound corresponds to 1,000,000 characters, and the set of all lines can be anywhere from 0 to 1,000,000, this cycle will obviously show linear runtime depending on the size of the line.
I also asked him if he thinks the following code is O (1) if the size n above is known. We are sure that this code will work only with the maximum upper bound, for example, 255 characters:
s = "How are you today?" for c in s: for d in s: print c+d
He said that it is also a constant time .... even after I explained that it is an O (n ^ 2) algorithm and demonstrated that the following code will create a quadratic curve.
So, I missed some theoretical concept, where any of the above is true depending on how the theory goes? To be clear, his understanding is that I am right if n is unknown. If the upper bound of n is always known, he claims that both algorithms in this post are like constant execution complexity.
I just want to keep my sanity, but maybe, if I'm wrong, then, of course, you can get additional training. My good, good colleague was very convincing. In addition, if someone has additional links or materials on a subject related to this issue, add to the comments.