[Tutor] FUNCTIONS vs. CLASSES (early beginner questions)
Question: When should I use functions? When should I use classes? I wrote my program twice: as a function and as a class (I did so for educational purposes, to better understand both concepts). Both programs do exactly the same, and the work neatly. Can you advise when I should use functions and when it's appropriate to make use of classes? Any feedback rgrd. my code? Anything I can improve? Thanks! # FUNCTION def ManageFood(): shopping_list = [] prompt = ("Which foods would you like to purchase?\nEnter 'quit' to exit. ") food = input(prompt) while food != "quit": shopping_list.append(food) food = input(prompt) print("These are your foods on your shopping list: %s." % ", ".join(shopping_list)) eat_food = [] store_food = [] for food in shopping_list: print("You bought: %s. " % (food)) prompt = input("What would you like to do with it?\nEnter 'eat' or 'store'. ") if prompt == "eat": eat_food.append(food) elif prompt == "store": store_food.append(food) print("Food for instant consupmtion: %s." % ", " .join(eat_food)) print("Food for storage: %s." % ", " .join(store_food)) ManageFood() # CLASS class FoodManager: def __init__(self): self.shopping_list = [] self.user_input = ("Which foods would you like to purchase?\nEnter 'quit' to exit? ") self.eat_food = [] self.store_food = [] self.user_choice = ("What would you like to do with it?\nEnter 'eat' or 'store'. ") def purchase_food(self): get_food = input(self.user_input) while get_food != "quit": self.shopping_list.append(get_food) get_food = input(self.user_input) print("These are your foods on your shopping list: %s." % ", ".join(self.shopping_list)) def manage_food(self): for item in self.shopping_list: print("You bought: %s. " % (item)) eat_or_store = input(self.user_choice) if eat_or_store == "eat": self.eat_food.append(item) elif eat_or_store == "store": self.store_food.append(item) print("Food for instant consumption: %s." % ", ".join(self.eat_food)) print("Food for storage: %s." % ", ".join(self.store_food)) if __name__ == "__main__": my_food = FoodManager() my_food.purchase_food() my_food.manage_food() ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
[Tutor] Merge Sort Algorithm
Can someone pls explain this Merge Sort Algorithm, especially the Recursive bit of it. def mergeSort(alist): print("Splitting ",alist) if len(alist)>1: mid = len(alist)//2 lefthalf = alist[:mid] righthalf = alist[mid:] mergeSort(lefthalf) mergeSort(righthalf) i=0 j=0 k=0 while i < len(lefthalf) and j < len(righthalf): if lefthalf[i] < righthalf[j]: alist[k]=lefthalf[i] i=i+1 else: alist[k]=righthalf[j] j=j+1 k=k+1 while i < len(lefthalf): alist[k]=lefthalf[i] i=i+1 k=k+1 while j < len(righthalf): alist[k]=righthalf[j] j=j+1 k=k+1 print("Merging ",alist) alist = [54,26,93,17,77,31,44,55,20] mergeSort(alist) print(alist) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Merge Sort Algorithm
This is the Result form that piece of code: Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] Splitting [54, 26] Splitting [54] Merging [54] Splitting [26] Merging [26] Merging [26, 54] Splitting [93, 17] Splitting [93] Merging [93] Splitting [17] Merging [17] Merging [17, 93] Merging [17, 26, 54, 93] Splitting [77, 31, 44, 55, 20] Splitting [77, 31] Splitting [77] Merging [77] Splitting [31] Merging [31] Merging [31, 77] Splitting [44, 55, 20] Splitting [44] Merging [44] Splitting [55, 20] Splitting [55] Merging [55] Splitting [20] Merging [20] Merging [20, 55] Merging [20, 44, 55] Merging [20, 31, 44, 55, 77] Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] [17, 20, 26, 31, 44, 54, 55, 77, 93] I have never been more confused On Tue, Mar 28, 2017 at 3:56 PM, Elo Okonkwo wrote: > Can someone pls explain this Merge Sort Algorithm, especially the > Recursive bit of it. > > def mergeSort(alist): > print("Splitting ",alist) > if len(alist)>1: > mid = len(alist)//2 > lefthalf = alist[:mid] > righthalf = alist[mid:] > > mergeSort(lefthalf) > mergeSort(righthalf) > > i=0 > j=0 > k=0 > while i < len(lefthalf) and j < len(righthalf): > if lefthalf[i] < righthalf[j]: > alist[k]=lefthalf[i] > i=i+1 > else: > alist[k]=righthalf[j] > j=j+1 > k=k+1 > > while i < len(lefthalf): > alist[k]=lefthalf[i] > i=i+1 > k=k+1 > > while j < len(righthalf): > alist[k]=righthalf[j] > j=j+1 > k=k+1 > print("Merging ",alist) > > alist = [54,26,93,17,77,31,44,55,20] > mergeSort(alist) > print(alist) > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Merge Sort Algorithm
This is the Result of that of the Merge Sort Function: I have never been more confused!!, practically spent the whole day on this piece of code: Splitting [54, 26, 93, 17, 77, 31, 44, 55, 20] Splitting [54, 26, 93, 17] Splitting [54, 26] Splitting [54] Merging [54] Splitting [26] Merging [26] Merging [26, 54] Splitting [93, 17] Splitting [93] Merging [93] Splitting [17] Merging [17] Merging [17, 93] Merging [17, 26, 54, 93] Splitting [77, 31, 44, 55, 20] Splitting [77, 31] Splitting [77] Merging [77] Splitting [31] Merging [31] Merging [31, 77] Splitting [44, 55, 20] Splitting [44] Merging [44] Splitting [55, 20] Splitting [55] Merging [55] Splitting [20] Merging [20] Merging [20, 55] Merging [20, 44, 55] Merging [20, 31, 44, 55, 77] Merging [17, 20, 26, 31, 44, 54, 55, 77, 93] [17, 20, 26, 31, 44, 54, 55, 77, 93] On Tue, Mar 28, 2017 at 3:56 PM, Elo Okonkwo wrote: > Can someone pls explain this Merge Sort Algorithm, especially the Recursive > bit of it. > > def mergeSort(alist): > print("Splitting ",alist) > if len(alist)>1: > mid = len(alist)//2 > lefthalf = alist[:mid] > righthalf = alist[mid:] > > mergeSort(lefthalf) > mergeSort(righthalf) > > i=0 > j=0 > k=0 > while i < len(lefthalf) and j < len(righthalf): > if lefthalf[i] < righthalf[j]: > alist[k]=lefthalf[i] > i=i+1 > else: > alist[k]=righthalf[j] > j=j+1 > k=k+1 > > while i < len(lefthalf): > alist[k]=lefthalf[i] > i=i+1 > k=k+1 > > while j < len(righthalf): > alist[k]=righthalf[j] > j=j+1 > k=k+1 > print("Merging ",alist) > > alist = [54,26,93,17,77,31,44,55,20] > mergeSort(alist) > print(alist) ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] FUNCTIONS vs. CLASSES (early beginner questions)
On 28/03/17 17:45, Rafael Knuth wrote: > Question: When should I use functions? > When should I use classes? Thee is no definitive answer but here are some guidelines: 1) Many instances -> class 2) Many methods sharing the same data -> class 3) An algorithm that has no side effects -> a function Very often we start with a function then realize it would be better in a class. In general classes need more code but provide more maintainable and reusable solutions. so on a bigger project classes are likely to be a better option. Pythons module/package mechanism is quite powerful so often we don't need classes where other languages would use them. -- 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
Re: [Tutor] Merge Sort Algorithm
On 28/03/17 15:56, Elo Okonkwo wrote: > Can someone pls explain this Merge Sort Algorithm, You can try reading this generic explanation. It's not Python but the explanation seems fairly clear. http://www.personal.kent.edu/~rmuhamma/Algorithms/MyAlgorithms/Sorting/mergeSort.htm HTH -- 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
Re: [Tutor] Merge Sort Algorithm
On Tue, Mar 28, 2017 at 03:56:16PM +0100, Elo Okonkwo wrote: > Can someone pls explain this Merge Sort Algorithm, especially the Recursive > bit of it. Take a pack of cards and shuffle them. Now you want to sort the cards. Put the cards down in a pile in front of you and think about sorting it. There are 52 cards in a Western deck of cards, so that's a lot of cards to sort. Let's make the job easier: (1) Split the deck into two piles, half in each pile. Now you only have to sort 26 cards at a time, which is much easier. (2) After you sort the first pile of 26, and then sort the second pile of 26, then you merge the two piles, keeping the same order: Turn the piles upside down, turn over the top card of each pile so you can see it, and start moving cards from the two piles to a third. Pick whichever card is smaller, move it to pile number 3, then turn over the next card so you can see what it is. Repeat until all the cards from the two piles are merged into a single pile, which is sorted. Now comes the recursive step. (3) In step 1, I said that sorting 26 cards is easier than sorting 52 cards. This is true, but sorting 26 cards is still not that much fun. Let's make it easier: split the pile of 26 cards into two halves, and sort each half. Then merge the two halves together, and your pile of 26 cards is sorted. Recursive step: (4) In step 3 I said that sorting 13 cards is easier than sorting 26 cards. This is true, but sorting 13 cards is still not that much fun. Let's make it easier: split the pile of 13 cards into two (nearly) equal halves, and sort each half. Then merge the two halves together, and your pile of 13 cards is sorted. Recursive step: (5) In step 4 I said that sorting 7 cards is easier than sorting 13 cards. This is true, but sorting 7 cards is still not that much fun. Let's make it easier: split the pile of 7 cards into two (nearly) equal halves, and sort each half. Then merge the two halves together, and your pile of 7 cards is sorted. (6) In step 5 I said that sorting 4 cards is easier than sorting 7 cards. This is true, but sorting 4 cards is still not that much fun. Let's make it easier: split the pile of 4 cards into two (nearly) equal halves, and sort each half. Then merge the two halves together, and your pile of 4 cards is sorted. (7) In step 6 I said that sorting 2 cards is easier than sorting 4 cards. This is true, but sorting 2 cards is still not that much fun. Let's make it easier: split the pile of 2 cards into two (nearly) equal halves, and sort each half. Then merge the two halves together, and your pile of 2 cards is sorted. (8) In step 7 I said that sorting 1 card is easier than sorting 2 cards. This is true, because 1 card is automatically sorted, and I don't have to think about it at all. So merge sort takes a big job (sorting 52 cards) and repeatedly splits it into two smaller jobs, until the job is so simple even a computer can do it: sorting 1 card takes no brains at all. Then it merges 1 card and 1 card together, keeping them in order, to get two. Then it goes back to the other pair of 1 card piles, and merges them. Then it merges the two piles of two cards to get a pile of four cards, and so on, until it ends up merging two sorted piles of 26 cards, and then the job is done. You should actually try this as an exercise. Literally get a pile of cards and go through the steps until it makes sense. For a *person*, this is not an efficient way to sort cards, but for computers it is very efficient, especially if you had millions of cards to sort. -- Steve ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor