<html>
<head>
<style>
.hmmessage P
{
margin:0px;
padding:0px
}
body.hmmessage
{
font-size: 10pt;
font-family:Verdana
}
</style>
</head>
<body class='hmmessage'>
<BR>&nbsp;<BR>
&gt; Subject: Re: [Tutor] First code snipet<BR>&gt; From: andreas@kostyrka.org<BR>&gt; To: darthkaboda@msn.com<BR>&gt; CC: tutor@python.org<BR>&gt; Date: Sun, 26 Jul 2009 03:02:27 +0200<BR>&gt; <BR>&gt; Some things:<BR>&gt; <BR>&gt; 1) It's Python, not Phython.<BR>&gt; 2) Slightly shorter solution to your problem:<BR>&gt; <BR>&gt; import random<BR>&gt; <BR>&gt; input = range(53)<BR>&gt; random.shuffle(input)<BR>&gt; print input<BR>&gt; <BR>&gt; 3) The important part here is that reimplementing something that is in<BR>&gt; the standard library does not make sense usually.<BR>&gt; <BR>&gt; 4) A more sensible implementation of shuffle comes directly out of<BR>&gt; random.py:<BR>&gt; <BR>&gt; def shuffle(self, x, random=None, int=int):<BR>&gt; """x, random=random.random -&gt; shuffle list x in place; return<BR>&gt; None.<BR>&gt; <BR>&gt; Optional arg random is a 0-argument function returning a random<BR>&gt; float in [0.0, 1.0); by default, the standard random.random.<BR>&gt; """<BR>&gt; <BR>&gt; if random is None:<BR>&gt; random = self.random<BR>&gt; for i in reversed(xrange(1, len(x))):<BR>&gt; # pick an element in x[:i+1] with which to exchange x[i]<BR>&gt; j = int(random() * (i+1))<BR>&gt; x[i], x[j] = x[j], x[i]<BR>&gt; <BR>&gt; Notice that it is not exactly optimal, one could easily replace the<BR>&gt; reversed(xrange()) expression with a 3 parameter version of xrange:<BR>&gt; for i in xrange(len(x) - 1, 0, -1):<BR>&gt; <BR>&gt; Andreas<BR>&gt; <BR>&gt; <BR>&gt; <BR>&gt; Am Samstag, den 25.07.2009, 17:15 -0700 schrieb Darth Kaboda:<BR>&gt; &gt; I'm starting to learn Python as it seems to be adopted by many<BR>&gt; &gt; companies that I've been looking to apply to. I used the book Learning<BR>&gt; &gt; Phython to get the basics of the language and many of the gotcha's. I<BR>&gt; &gt; think I know enough of the basic features of the language to start<BR>&gt; &gt; playing around and can understand most code I've seen so far by<BR>&gt; &gt; looking at some of the posts here.<BR>&gt; &gt; <BR>&gt; &gt; The one worry I have is not coding things the Phython way as I've been<BR>&gt; &gt; a Visual Basic programmer for so long and a C++ programmer before<BR>&gt; &gt; that. So would like to have people look at a simplistic class<BR>&gt; &gt; (shuffles lists of objects wrote it for shuffling cards but<BR>&gt; &gt; with Phython could be used for any "list" type object) I wrote to give<BR>&gt; &gt; me some feedback on the style of it.<BR>&gt; &gt; <BR>&gt; &gt; Any feedback is appreciated.<BR>&gt; &gt; <BR>&gt; &gt; Thanks,<BR>&gt; &gt; Brian<BR>&gt; &gt; _______________________________________________<BR>&gt; &gt; Tutor maillist - Tutor@python.org<BR>&gt; &gt; <A href="http://mail.python.org/mailman/listinfo/tutor">http://mail.python.org/mailman/listinfo/tutor</A><BR>
&nbsp;<BR>
Thanks for the correction not sure why I typed it that way after typing it correctly in the first sentence,&nbsp;must have wondered off somewhere in my mind I guess. <BR>
&nbsp;<BR>
Oops I guess I should have also mentioned what I was trying to accomplish with this class. I wanted to mimic how a deck of cards get's shuffled in the real world. i.e. split the deck and shuffle the two halves together.&nbsp;<BR>
&nbsp;<BR>
Brian<BR>
<BR>&nbsp;<BR></body>
</html>