Re: [Tutor] how to calculate execution time and complexity
On 10/28/2011 01:38 AM, Praveen Singh wrote: splitWord('google', 2) ['go', 'og', 'le'] >>> splitWord('google', 3) ['goo', 'gle'] >>> splitWord('apple', 1) ['a', 'p', 'p', 'l', 'e'] >>> splitWord('apple', 4) ['appl', 'e'] def splitWord(word, number): length=len(word) list1=[] x=0 increment=number while number<=length+increment: list1.append(word[x:number]) x=x+increment number=number+increment for d in list1: if d=='': list1.remove('') return list1 I am getting the desired output and this code is working fine..but i think it is quite bulky for this small operation. qus.1-- can you guys suggest me some better solution?? qus 2-- i know writing just a piece of code is not going to help me. i have to write efficient code.i want to know how to calculate execution time of my code and can you guys suggest me some links so that i can learn how to find complexity of code?? Thanks in advance... Use the grouper() recipe, shown on the intertools page of the docs: http://docs.python.org/library/itertools.html -- DaveA ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to calculate execution time and complexity
. > Thanks Christian for your links and code > > > ___ > Tutor maillist - Tutor@python.org > > To unsubscribe or change subscription > options:http://mail.python.org/mailman/listinfo/tutor > > > Below [1] is how I would write it, which is simply a re-factoring of your > code so it's cleaner and more compact. For calculating execution time you > can use the `timeit` module [2] and for more in-depth analysis you can look > at `profile` [3] and further to the bare-bones `dis` [4]. > > [1] > >>> def splitWord(word, number): > ... x = [] > ... for y in xrange(0, len(word), number): > ... x.append(word[y:y+number]) > ... return x > ... > >>> splitWord('google', 1) > ['g', 'o', 'o', 'g', 'l', 'e'] > > >>> splitWord('google', 2) > ['go', 'og', 'le'] > >>> splitWord('google', 3) > ['goo', 'gle'] > >>> splitWord('google', 4) > ['goog', 'le'] > >>> splitWord('google', 5) > ['googl', 'e'] > >>> splitWord('google', 6) > ['google'] > >>> splitWord('google', 7) > ['google'] > > [2] http://www.doughellmann.com/PyMOTW/timeit/ > [3] http://www.doughellmann.com/PyMOTW/profile/index.html#module-profile > [4] http://www.doughellmann.com/PyMOTW/dis/ > > -- > > Christian Witts > Python Developer > > ** > -- www.tricksfind.blogspot.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to calculate execution time and complexity
On 2011/10/28 07:38 AM, Praveen Singh wrote: >>> splitWord('google', 2) ['go', 'og', 'le'] >>> splitWord('google', 3) ['goo', 'gle'] >>> splitWord('apple', 1) ['a', 'p', 'p', 'l', 'e'] >>> splitWord('apple', 4) ['appl', 'e'] def splitWord(word, number): length=len(word) list1=[] x=0 increment=number while number<=length+increment: list1.append(word[x:number]) x=x+increment number=number+increment for d in list1: if d=='': list1.remove('') return list1 I am getting the desired output and this code is working fine..but i think it is quite bulky for this small operation. qus.1-- can you guys suggest me some better solution?? qus 2-- i know writing just a piece of code is not going to help me. i have to write efficient code.i want to know how to calculate execution time of my code and can you guys suggest me some links so that i can learn how to find complexity of code?? Thanks in advance... ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor Below [1] is how I would write it, which is simply a re-factoring of your code so it's cleaner and more compact. For calculating execution time you can use the `timeit` module [2] and for more in-depth analysis you can look at `profile` [3] and further to the bare-bones `dis` [4]. [1] >>> def splitWord(word, number): ... x = [] ... for y in xrange(0, len(word), number): ... x.append(word[y:y+number]) ... return x ... >>> splitWord('google', 1) ['g', 'o', 'o', 'g', 'l', 'e'] >>> splitWord('google', 2) ['go', 'og', 'le'] >>> splitWord('google', 3) ['goo', 'gle'] >>> splitWord('google', 4) ['goog', 'le'] >>> splitWord('google', 5) ['googl', 'e'] >>> splitWord('google', 6) ['google'] >>> splitWord('google', 7) ['google'] [2] http://www.doughellmann.com/PyMOTW/timeit/ [3] http://www.doughellmann.com/PyMOTW/profile/index.html#module-profile [4] http://www.doughellmann.com/PyMOTW/dis/ -- Christian Witts Python Developer // ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] how to calculate execution time and complexity
Hi Praveen I am still new to the language but here is what I would do. Sorry I can't comment on how to best check for efficiency. my_str='google' split_by= 2 [ my_str[i:i+split_by] for i in range(0, len(my_str), split_by) ] Just using a list comprehension. best, -Abhi On Thu, Oct 27, 2011 at 10:38 PM, Praveen Singh wrote: > >>> splitWord('google', 2) > ['go', 'og', 'le'] > > > >>> splitWord('google', 3) > ['goo', 'gle'] > > > >>> splitWord('apple', 1) > ['a', 'p', 'p', 'l', 'e'] > > > >>> splitWord('apple', 4) > ['appl', 'e'] > > > > def splitWord(word, number): > length=len(word) > list1=[] > x=0 > increment=number > while number<=length+increment: > list1.append(word[x:number]) > x=x+increment > > > number=number+increment > > for d in list1: > if d=='': > list1.remove('') > return list1 > > I am getting the desired output and this code is working fine..but i think it > is quite bulky for this small operation. > > > qus.1-- can you guys suggest me some better solution?? > qus 2-- i know writing just a piece of code is not going to help me. i have > to write efficient code.i want to know how to calculate execution time of my > code and > > > can you guys suggest me some links so that i can learn how to find > complexity of code?? > > Thanks in advance... > > > > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > http://mail.python.org/mailman/listinfo/tutor > > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor