Re: [Tutor] Messy - Very Messy string manipulation.
Thanks Meenu. str.translate. Worked like a charm for python 3.5. And thanks Alan Gauld for the 2.7 version. Appreciate the help guys. You guys are awesome. Regards Vusa On Wed, Oct 28, 2015 at 6:23 PM, meenu ravi wrote: > Hi Vusa, > > I was not able to reply through mail list due to some issue. So just > replying through email. > > We can make use of string translate method for more pythonic way. If you > are using python 2.x, the following itself should work for you: > > *** > import string > def anti_vowel(str): > word = str.translate(None, 'aeiouAEIOU') > return word > > print anti_vowel('The cow moos louder than the frog') > *** > > And if you are using python 3.x, "None" inside the str.translate method > doesn't work. So instead, you can use in this way: > > *** > import string > def anti_vowel(str): > word = str.translate(str.maketrans("","","aeiouAEIOU")) > return(word) > > print(anti_vowel("The cow moos louder than the frog")) > > > *** > > The above code should work with python 2.x as well with python 2 syntax as > follows: > > import string > def anti_vowel(str): > word = str.translate(string.maketrans('', ''), 'aeiouAEIOU') > return word > > print anti_vowel('The cow moos louder than the frog') > > > If you want to know more about translate method, please follow the link, > https://docs.python.org/2/library/string.html#string-functions > > I hope you will get much more options through mailing list. > > Happy python:) > > Thanks, > Meenakshi > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Messy - Very Messy string manipulation.
On 28/10/2015 18:06, Alan Gauld wrote: On 28/10/15 17:35, Peter Otten wrote: Alan Gauld wrote: On 28/10/15 16:37, Peter Otten wrote: 'The cow moos louder than the frog'.translate(str.maketrans("", "", "aeiouAEIOU")) 'Th cw ms ldr thn th frg' Even easier, forget the maketrans stuff and just use 'The cow moos louder than the frog'.translate(None,'aeiouAEIOU') This only works for byte strings, not unicode. Aha, I tried it in Python 2.7 which worked, but I didn't think about v3... Seems like as good a place as any to point out that in Python 3 all of the following also exist. static bytes.maketrans(from, to) bytes.translate(table[, delete]) static bytearray.maketrans(from, to) bytearray.translate(table[, delete]) -- My fellow Pythonistas, ask not what our language can do for you, ask what you can do for our language. Mark Lawrence ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] counter not working in Quick Sort script
Mac OS 10.10 Python 3.4.3 I self-study Python and am using it for a Coursera algorithm class. Hi! My script sorts correctly on all my test arrays. My accumulator variable to count the number of comparisons returns nonsense. I'm counting the (length - one) of each sublist that will be sorted in place, not the individual comparisons. I put in a print statement for the count variable, and the expected values print, but the function seems to be returning the first increment of the count variable instead of the final value. I reread on the difference between print and return, but I haven't figured this out yet. I think I'm doing something language-specific wrong would appreciate another set of eyes. def partition(A, start, stop): p = A[start] # make pivot first element in partition i = start + 1 for j in range(i, stop): # swap elements if A[j] bigger than pivot if A[j] < p: A[j], A[i] = A[i], A[j] i += 1 # swap pivot into sorted index A[start], A[i-1] = A[i-1], A[start] return i def quick_sort(A, start, stop, count): if start < stop: # increment count by length of partition less the pivot count += (stop - start - 1) print(count) split = partition(A, start, stop) # recursively sort lower partition quick_sort(A, start, split-1, count) # recursively count upper partition quick_sort(A, split, stop, count) return count def main(): unsorted = [ 1, 2, 3, 4, 5, 6, 7, 8 ] count = quick_sort(unsorted, 0, len(unsorted), 0) print(unsorted) print("There are {} comparisons.".format(count)) main() This code is giving me this output: ➜ algorithms python3 quick_sort_first.py 7 13 18 22 25 27 28 28 [1, 2, 3, 4, 5, 6, 7, 8] There are 7 comparisons. Thank you, Patricia ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] counter not working in Quick Sort script
On 29/10/15 19:11, Patti Scott via Tutor wrote: Caveat: I didn't check the algorithms for correctness, I'll just take your word for that. My accumulator variable to count the number of comparisons returns nonsense. def quick_sort(A, start, stop, count): if start < stop: # increment count by length of partition less the pivot count += (stop - start - 1) print(count) split = partition(A, start, stop) # recursively sort lower partition quick_sort(A, start, split-1, count) # recursively count upper partition quick_sort(A, split, stop, count) return count Notice that you only set count once at the top of the function. What the recursive instances of the function do is irrelevant because you don't use their return values. So the return value of this function is always (count + stop - start -1) for the initial invocation value of count. I suspect you really want to do something to count based on the returns from the recursive calls too. def main(): unsorted = [ 1, 2, 3, 4, 5, 6, 7, 8 ] This looks very sorted to me? Is that correct? count = quick_sort(unsorted, 0, len(unsorted), 0) count should return 0+len()-0-1 -> len-1 = 7 print(unsorted) print("There are {} comparisons.".format(count)) main() This code is giving me this output: ➜ algorithms python3 quick_sort_first.py 7 This is the outer functions count 13 18 22 25 27 28 28 These are the recursive values of count which are invisible to the outer invocation [1, 2, 3, 4, 5, 6, 7, 8] This is the sorted result There are 7 comparisons. And this reflects the outer value of count again. Your code does exactly what you told it to do. Your problem is that you are not using the returned counts from the recursive calls. -- Alan G Author of the Learn to Program web site http://www.alan-g.me.uk/ http://www.amazon.com/author/alan_gauld Follow my photo-blog on Flickr at: http://www.flickr.com/photos/alangauldphotos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor