Re: [Tutor] Hex to Str - still an open issue
Liam, Just looking at this - i = 456 s = '' while i: s = str(i % 2) + s i/=2 This works, far simpler than mine, which is always infuriating, but my question is, how exactly? This is the classic math treatment of how to calculate a binary number. Just keep dividing by two and take the remainder into the number. Almost any math textbook will cover this approach. The snag is that from a computing point of view its pretty slow so computer texts usually highlught a lookup approach 8instead. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hex to Str - still an open issue
Ah, yeah, gotta get me one of those textbooks. (Wait a minute, that would mean, my approach wasn't the textbook approach... /me salvages a little pride.) While I jest somewhat, that highlights a serious deficiency in my education that becomes more and more apparent, which is in maths. Sheesh, if I'd known I wanted to use maths for something I enjoyed, I would've paid attention in class. But the remainder thing - would this be why we read binary the way we do? 4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach we get 100. Regards, Liam Clarke On Sun, 6 Feb 2005 08:44:42 -, Alan Gauld [EMAIL PROTECTED] wrote: Liam, Just looking at this - i = 456 s = '' while i: s = str(i % 2) + s i/=2 This works, far simpler than mine, which is always infuriating, but my question is, how exactly? This is the classic math treatment of how to calculate a binary number. Just keep dividing by two and take the remainder into the number. Almost any math textbook will cover this approach. The snag is that from a computing point of view its pretty slow so computer texts usually highlught a lookup approach 8instead. Alan G. -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Are you allowed to shoot camels? [kinda OT]
Even more OT it would seem, but harking back to the original subject, Perl isn't looking too bad because I've been working through Java tonight. $j = STDIN; is relatively intuitive for a child of Unix, and it's also documented. BufferedReader keyboard = new BufferedReader(new InputStreamReader(System.in)); String j = keyboard.readline(); is not intuitive, and is hidden very well in the bowels of Sun's API's. Scary, when a language makes me think Perl would be nicer. : ) Heh. Oh, and whoever recommended Eclipse to me? Thank you very much. On Sat, 5 Feb 2005 08:59:30 -, Alan Gauld [EMAIL PROTECTED] wrote: Surely you jest, Alan. :-) Smiley noted but... Both perl and awk are turing complete, hence anything perl can do, awk can do as well. This is a popular misconception. Being Turing complete simply means you can implement any algorithm. But if the language doesn't provide I/O access for example it is impossible to write a device driver, or a comms stack, or any of a host of other low level programs. awk is non extendable (unless you have the source code!) so you can't do those things. Perl is not only extendable but actually comes wth a heap of those kinds of features that awk just doesn't have. And no amount of clever algorithms can compensate. Awk was designed for one task which it does spectacularly well but it was never intended for general purpose use. I/O is just one example, there are meny more... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hex to Str - still an open issue
On Feb 6, 2005, at 08:59, Liam Clarke wrote: Ah, yeah, gotta get me one of those textbooks. (Wait a minute, that would mean, my approach wasn't the textbook approach... /me salvages a little pride.) While I jest somewhat, that highlights a serious deficiency in my education that becomes more and more apparent, which is in maths. Sheesh, if I'd known I wanted to use maths for something I enjoyed, I would've paid attention in class. But the remainder thing - would this be why we read binary the way we do? 4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach we get 100. Regards, Liam Clarke Yes, it is 100. The most significant bit (i.e. the highest power of 2) is on the left, just as the most significant digit (matching the highest power of 10) is on the left when representing base-10 numbers: 415 is 4*10^2 + 1*10^1 + 5*10^0. -- Max maxnoel_fr at yahoo dot fr -- ICQ #85274019 Look at you hacker... A pathetic creature of meat and bone, panting and sweating as you run through my corridors... How can you challenge a perfect, immortal machine? ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hex to Str - still an open issue
Liam Clarke wrote: 4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach we get 100. ?? 4 (decimal) is 100 (binary). Not because of how the conversion algorithm works, but because that is how we write numbers. The least-significant digit is always the rightmost digit. 001 is 1 in every number base = 2. Actually, generating the digits from the right complicates the algorithm quite a bit. It's hidden in the Python version, but s = str(i % 2) + s is a relatively expensive operation here - it has to copy all of s to make room for the new digit. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hex to Str - still an open issue
On 6 Feb 2005, [EMAIL PROTECTED] wrote: Actually, generating the digits from the right complicates the algorithm quite a bit. It's hidden in the Python version, but s = str(i % 2) + s is a relatively expensive operation here - it has to copy all of s to make room for the new digit. Because of that the standard answer is to write: s = [] s.append(...) return ''.join(s) Karl -- Please do *not* send copies of replies to me. I read the list ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hex to Str - still an open issue
While I jest somewhat, that highlights a serious deficiency in my education that becomes more and more apparent, which is in maths. Yes, its a sad fact. Good programming beyond basics does require a modicum of maths. You can learnn enough to do useful things without math, but there reaches a point when math becomes essential. Its no coincidence that at university Computing was traditionally (up till the late 70's at least) a branch of mathematics. But the remainder thing - would this be why we read binary the way we do? 4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach we get 100. Not really. The reason we read 4 as 100 is the same reason we read 400 as 400 instead of 004 - we traditionally put the most significant part tothe left since we (in English at least) read from left to right. 400 = 4x10**2 + 0x10**1 + 0x10**0 110 = 1x2**2 + 0x2**1 + 0x2**0 But if we convert back again we can generate the number 400 from the value 400 by the same technique we saw for binary: 400/10 = 40 rem 0 40/10 = 4 rem 0 4/10 = 0rem 4 So reading remainders bottom up we get 400, which is the decimal representation of 400! :-) So the algorithm is identical, we can write a generic function to convert a value into a representation if we pass in the value and base. Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Percolation model in python
Title: Percolation model in python Hello, I have been working with some high school students to create a model of small pox transmission. I am somewhat new to python (my programming experience is in f77) so I have borrowed parts of Danny's code that he posted for the Game of Life. I have included the code that we are using below. I have two questions. Once a MxN world is generated how would you search for nearest neighbors (to see who is connected) and then color the '*' so it's easier to see who is connected and who isn't. For a definition of percolation theory- http://en.wikipedia.org/wiki/Percolation_theory or for the wolfram fans http://mathworld.wolfram.com/PercolationTheory.html Thanks, Ara CODE STARTS HERE: print Please pick your option: 1) Percolation model for Small Pox 2) 3) Instructions 4) Exit option = raw_input(Which option[1,2,3,4]? ) if option == '1': import random perc = raw_input(Please enter a thresold between 0-1. ) perc = float(perc) ### PERSON, EMPTY = '*', '.' ### ### def percolation(perc): randval = random.random() if randval perc: return EMPTY else: return PERSON def make_random_world(M, N): Constructs a new random game world of size MxN. world = {} for j in range(N): for i in range(M): world[i, j] = percolation(perc) world['dimensions'] = (M, N) return world def print_world(world): Prints out a string representation of a world. M, N = world['dimensions'] for j in range(N): for i in range(M): print world[i, j], print n = int(raw_input(Please enter a n dimension. )) m = int(raw_input(Please enter a m dimension. )) raw_input(Press return to make a world) print_world(make_random_world(n,m)) ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Hex to Str - still an open issue
Ah, thanks all. I wasn't thinking of base 2 numbers like base 10 - when you describe it like that, I get i. (100 = 10^2 + 0*10^1 + 0*10^0) I was thinking strictly in terms of a base 10 number described by flags for each power of 2, which (to me) would logically start from 2^0 and go right. And yeah, I intend to study computer science as I can, so it's definitely the maths papers first. I'm working through my little brother's textbook on matrix algebra at the moment. Ick. Regards, Liam Clarke On Sun, 6 Feb 2005 15:10:42 -, Alan Gauld [EMAIL PROTECTED] wrote: While I jest somewhat, that highlights a serious deficiency in my education that becomes more and more apparent, which is in maths. Yes, its a sad fact. Good programming beyond basics does require a modicum of maths. You can learnn enough to do useful things without math, but there reaches a point when math becomes essential. Its no coincidence that at university Computing was traditionally (up till the late 70's at least) a branch of mathematics. But the remainder thing - would this be why we read binary the way we do? 4 is 001 (on a continuum of 2^0 to 2^n), but using the above approach we get 100. Not really. The reason we read 4 as 100 is the same reason we read 400 as 400 instead of 004 - we traditionally put the most significant part tothe left since we (in English at least) read from left to right. 400 = 4x10**2 + 0x10**1 + 0x10**0 110 = 1x2**2 + 0x2**1 + 0x2**0 But if we convert back again we can generate the number 400 from the value 400 by the same technique we saw for binary: 400/10 = 40 rem 0 40/10 = 4 rem 0 4/10 = 0rem 4 So reading remainders bottom up we get 400, which is the decimal representation of 400! :-) So the algorithm is identical, we can write a generic function to convert a value into a representation if we pass in the value and base. Alan G. -- 'There is only one basic human right, and that is to do as you damn well please. And with it comes the only basic human duty, to take the consequences. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Are you allowed to shoot camels? [kinda OT]
Jacob S. wrote: aFuncList=[] def x(): print one aFuncList.append(x) def x(): print two aFuncList.append(x) def x(): print three aFuncList.append(x) for item in aFuncList: item() Okay, for this problem (it can be altered otherwise) def makefunct(stri): def x(): print stri return x aFuncList = [makefunct('one'),makefunct('two'),makefunct('three')] for item in aFuncList: item() It's shorter, it works and it looks cool. Thanks to Jeff Shannon for the backbone of this example. My intent in showing the above code was not to really print one two three, but to show that a function doesn't care what it's called. __ Do You Yahoo!? Tired of spam? Yahoo! Mail has the best spam protection around http://mail.yahoo.com ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] The Tkinter Text widget and .get()
As I understand, .get() has to get an index argument to get the text from the Text index... Thats true. The problem is that I dont realy understand what is this index thing Thats not surprising the Text widget index in Tk (its not really a Tkinter thing, its part of the underlying Tk toolkit...) is just a little bit weird! and what index do I need to give to the function so I'll get all the text in the widget. THe magic incantation (or one option, there are various ways) is: txt.get(1.0,END) Where 1.0 means first line, zeroth character (ie before the first!) is the starting position and END is the ending position. The index is a conceptual cursor that sits *between* characters. Thus for a line like: Here is a line If we only wanted the second word we'd use get(1.5, 1.7) This is explained in both the Tkinter documentation (but slightly vaguely), and,more precisely in the Tk documentation. AS with most things Python the best bet is to experiment at the prompt till you get it right! There are some examples of using the Text widget in my tutorial in both the Event Driven Programming and Case Study topics. Specifically the doReset() method inthe case study uses indexing to delete the text in a Text box and doAnalyze shown text being appended (inserted at END). HTH, Alan G Author of the Learn to Program web tutor http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] manipulating a file
I want to read the httpd-access.log and remove any oversized log records I quickly tossed this script together. I manually mv-ed log to log.bak and touched a new logfile. running the following with print i uncommented does print each line to stdout. but it doesn't write to the appropriate file... a) what am I missing? b) is there a less expensive way to do it? c) I originally wanted to delete lines over 2085 in length but couldn't find a way to do that... did I miss it? Thanks #!/usr/local/bin/python import os srcfile = open('/var/log/httpd-access.log.bak', 'r') dstfile = open('/var/log/httpd-access.log', 'w') while 1: lines = srcfile.readlines() if not lines: break #print lines for i in lines: if len(i) 2086: #print i dstfile.write(i) srcfile.close() dstfile.close() ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor