On 2019-01-31, Grant Edwards <grant.b.edwa...@gmail.com> wrote:
> On 2019-01-31, Terry Reedy <tjre...@udel.edu> wrote:
>> On 1/31/2019 11:19 AM, Ian Clark wrote:
>>> text = "The best day of my life!"
>>> output = ''
>>> 
>>> for i in text:
>>>   if i == ' ':
>>>    output +='\n'
>>>   else:
>>>    output += i
>>> 
>>> print(output)
>
>> But this is an awful, O(n*n) way to solve an inherently O(n) problem,
>
> How is it O(n^2)?
>
> It loops through the input sequence exactly once.  That looks like
> O(n) to me.

Doh!

The 'output +=' operation is also O(n), and it's executed n times.

-- 
Grant Edwards               grant.b.edwards        Yow! I hope something GOOD
                                  at               came in the mail today so
                              gmail.com            I have a REASON to live!!

-- 
https://mail.python.org/mailman/listinfo/python-list

Reply via email to