I like that, Dave. It's another improvement. Safe - I absolutely agree. You get a lot of programmers who think everything needs to be a Class, without thinking it through. I never use a class just for the sake of it. I've found I sometimes get to a point where I choose to introduce one in order to restrict use of functions to instances of the thing I'm using, and to remove the need to pass the data structure in to the functions. If the data structure is an instance variable of an object and you call a method on the instance, you want it to perform the function on the instance's data structure. I know, kind of pointing out the obvious. But I'd rather make the decision to use a Class at this point than before I know what the code looks like.
A typical C# coder's solution of Life will involve several classes, for things like the Game, Grid, Cell, maybe even cell types like AliveCell and DeadCell which inherit Cell. Your point about abstraction for readability is also very good. I often abstract out as few as a couple of lines of code (sometimes just one) in to a separate method, if it's not too clear what those lines do. Last night at XP Manchester we had a language-off, where a few of us represented a language of their choice and we debated which was the best language. Robie represented Python (and won!) and I represented PHP, where I did surprisingly well coming third to Python and Io, beating Java, C#, Haskell, Smalltalk and Ruby! I managed to squash some of the common misconceptions about PHP and get people to value its free and open source nature. It was a really interesting debate and Robie did us proud :P Hope to see some of you on Tuesday at the Madlab Christmas party. Ben On Thursday, 13 December 2012 17:41:25 UTC, Dave Potts wrote: > > Hi Ben, > > I see that you can do the tennis score with just (server, receiver) pairs > mapping to what happens when the server wins the point. With a bit of code > you can then reverse the order of the lookup to work out what happens when > the receiver wins the point. However I'd be tempted to redundantly add > more data to make the code more expressive. What if the data structure > was a dictionary mapping to two tuples, one for the server winning and one > for the receiver wining? I'd argue this is easier to read. The a few > elements of the data structure would be: > > (0, 0) : ((15, 0), (0, 15)) > (15, 0) : ((30, 0), (15, 15)) > ... > (40, 40) : ((A, 40) , (40, A)) > (A , 40) : ((GAME), (40, 40)) > (40, A) : ((40, 40), (GAME)) > > What do you think? > > Dave. > > > > > On 13 December 2012 10:52, Ben Nuttall <[email protected] <javascript:> > > wrote: > >> Hi Safe >> >> The tennis scoring is interesting. Robie and I coded a simple >> implementation each since the last meet, using a dictionary of tuples >> mapping one score to the next (for player one scores) and reversing for >> player two. It removes the logic code you would otherwise need to write and >> test. >> >> Another example of good use of data structure we've found recently is in >> Conway's Game of Life. I've written a number of implementations of this, >> particularly at XP Man and the Code Retreat. When pairing with C# and Ruby >> types, they want to either set up multiple classes and introduce >> inheritance, or have a grid and store the 'alive' state as 0s and 1s in >> each position in the grid. I saw a video from PyCon recently (Stop Writing >> Classes [1]) where it was suggested that these usual approaches are >> overkill and that it can be simplified with a sensible data structure and >> by removing the logic using set theory: e.g. a set of tuples (only the live >> cells) therefore storing all the information you need in much less data. >> Your code become more readable too - (1, 2) in alive_cells returns whether >> or not that cell is alive, rather than having to loop over the values in a >> nested list. >> >> We could pair up and code one of these exercises in a Pythonic way, and >> compare outcomes? Or discuss data structures? I've coded with people >> recently who, it turns out, didn't know what things like dictionaries, >> tuples and sets are. I'd love to find out if there are any useful ones I >> don't know! >> >> I'll be at the Madlab Christmas Party - hope to see a few Pythons there! >> I've donated a Raspberry Pi as a raffle prize. If any of you would like to >> donate a prize, or some food for the party - that would be much appreciated >> by Madlab. I'm feeling particularly grateful for Madlab at the moment as >> the Sheffield GIST Lab (the organisation that eased me in to the tech >> community with Sheffield GeekUp) has just announced it is closing down, >> which is really sad. Thanks to Daley for his contribution to the GIST Lab. >> >> See you all next week >> >> Ben >> >> >> [1] Stop Writing Classes - http://www.youtube.com/watch?v=o9pEzgHorH0 >> >> >> >> On Wednesday, 12 December 2012 11:50:09 UTC, Safe wrote: >>> >>> Hi All, >>> >>> Python Northwest will be getting together again for a coding meeting at >>> 7pm on Thursday 20th December at MadLab. It would be great to get an idea >>> or two together in preparation for the meeting. As a starter for 10, >>> possibilities include: >>> >>> - On the way out of English Lounge last time, there was some discussion >>> about how you might go about coding a tennis scoring application. It might >>> be interesting to compare approaches, and with a bit of effort, perhaps >>> find point-by-point histories of classic games on the interweb, feed them >>> into our scorers and see how well they do. >>> >>> - In the past we've run a coding dojo style session where we make >>> suggestions of what we might code then take a vote and get cracking. Past >>> suggestions have included a crossword generator, a poker hand scorer and a >>> DVD database. >>> >>> Other ideas? >>> >>> fyi ... MadLab is also holding an End of Year party on Tuesday 18th >>> December to which we're cordially invited! Details here: >>> http://madlab.org.uk/**content/madlab-end-of-year-**party/<http://madlab.org.uk/content/madlab-end-of-year-party/> >>> >>> Best, >>> >>> Safe >>> >>> >>> Safe Hammad >>> http://safehammad.com >>> @safehammad >>> >> -- >> To post: [email protected] <javascript:> >> To unsubscribe: [email protected] <javascript:> >> Feeds: http://groups.google.com/group/python-north-west/feeds >> More options: http://groups.google.com/group/python-north-west >> > > -- To post: [email protected] To unsubscribe: [email protected] Feeds: http://groups.google.com/group/python-north-west/feeds More options: http://groups.google.com/group/python-north-west
