Re: [Tutor] Tutor Digest, Vol 53, Issue 99
On Tue, 2008-07-29 at 01:12 +0200, [EMAIL PROTECTED] wrote: Message: 3 Date: Mon, 28 Jul 2008 13:26:13 -0500 From: Daniel Sarmiento [EMAIL PROTECTED] Subject: Re: [Tutor] Memory error - how to manage large data sets? To: tutor@python.org Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; charset=ISO-8859-1 Hi I tried to run your code and checked (with top) the memory ussage and it uses more than 2 Gb of memory. I tried to modify the code a little bit to use less memory and came up with this: fib = {0:0,1:1} even = [] def fibonacci(x,y): return x+y for j in xrange (2,100): i = fib[j-1] + fib[j-2] if i % 2 == 0: even.append(i) fib = {j-1:fib[j-1], j:i} total = reduce(fibonacci,even) print total First, I replaced range with xrange. I figured that you only need the last two values in the fibonnaci series to calculate the next value, so I replaced the fib list with a dictionary to only store the last two values instead of the whole series. It looks like the progam still hangs and I did not notice any memory imrovements when running it with 1 000 000 Am I wrong thinking that the modifications I made help use less memory? I have realised that when you need to work with large numbers, lists are slow and tend to gobble up bytes. Another thing try to be as simple as possible :). This is how I would approach the problem, without using lists. It produces the result instantly, even for large numbers like a 100, 000, 000. def fibonacci() a = 0 b = 1 evenTotal = 0 while a 1: a,b = b,a+b if a%2 == 0: evenTotal += a print evenTotal Does this help? Kinuthia... ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Turtle problem: how to exit the .exe?
Dick Moores [EMAIL PROTECTED] wrote Very odd, I assume the python script when executed normally doesn't exhibit such odd behaviour? When I try to stop the script from running not by a Ctrl+Q on the console window, but on the Turtle window, the only way is to use the Task Manager. OK, let's eliminate some variables and go back to getting the python script working. I've used turtle many times and never seen this behaviour before. What does the code look like? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Tutor Digest, Vol 53, Issue 99
kinuthiA muchanE [EMAIL PROTECTED] wrote I have realised that when you need to work with large numbers, lists are slow and tend to gobble up bytes. The lists are not particularly slow (obviously if you want to traverse them they take longer for more members) and they don't take up much more memory that the sum of the sizes of whats in them. The problem here is that the data items themselves are huge. Around 20K per item. Thus a million times 20K is 20G! This is how I would approach the problem, without using lists. Yep, we all seem to be pretty much in agreement :-) It produces the result instantly, even for large numbers like a 100, 000, 000. Umm, not instantly on my PC... 1 million took 5 minutes, I've no idea how long 100 million would take! def fibonacci() a = 0 b = 1 evenTotal = 0 while a 1: a,b = b,a+b if a%2 == 0: evenTotal += a print evenTotal Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Turtle problem: how to exit the .exe?
At 12:38 AM 7/29/2008, Alan Gauld wrote: Dick Moores [EMAIL PROTECTED] wrote Very odd, I assume the python script when executed normally doesn't exhibit such odd behaviour? When I try to stop the script from running not by a Ctrl+Q on the console window, but on the Turtle window, the only way is to use the Task Manager. OK, let's eliminate some variables and go back to getting the python script working. Remember, it does work. I've used turtle many times and never seen this behaviour before. What does the code look like? http://py77.python.pastebin.com/f414c8ce4 Dick === Have you seen Kelie Feng's video introducing the terrific and free IDE, Ulipad? http://www.rcblue.com/u3/ Get Ulipad 3.9 from http://code.google.com/p/ulipad/downloads/list svn for the latest revision http://ulipad.googlecode.com/svn/trunk/ Mailing list for Ulipad: http://groups-beta.google.com/group/ulipad ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Turtle problem: how to exit the .exe?
On Mon, Jul 28, 2008 at 4:44 PM, Dick Moores [EMAIL PROTECTED] wrote: Win XP, Python 2.51 The script is at http://py77.python.pastebin.com/f414c8ce4 It calls the Windows console to show the printed data. I've successfully used PyInstaller to make an .exe for this. For informed users, exiting the program before it finishes is easily and cleanly done by a Ctrl+Q on the console window. But I'd like to make a version of the script that doesn't report any data. How can I write it so that the .exe can be easily exited by the user? If Tkinter instead of Turtle were involved, I could have an Exit button connected to command=root.quit . Take a look at the source for the turtle module (in Python/Lib/lib-tk/turtle.py I think). It has a global _root object which is the Tk instance. You could make your own Pen class, subclassing from RawPen, that makes whatever kind of window you would prefer. Then instead of using the module functions forward(), down(), etc - which just delegate to a global Pen instance - make an instance of your own pen and draw with that. Kent ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] newbie graphing question
Title: Re: [Tutor] newbie graphing question Thanks so much to everyone who helped me with my gui/drawing request. I appreciate the responses from Alan, bhaluu, James, and Pierre, and am exploring all the suggestions I received. At the moment, I have my immediate needs satisfied with Tkinter -- easiest because it was already present on my Mac, Windows, and Ubuntu installations. Alongside the recommendations, I came across the the Python Imaging Library, which I thought might also be worth mentioning in this thread. It's documentation is the best click with my brain; but I haven't tried it yet because installation on Mac Linux means I need to work from source; which I haven't been brave enough to try, yet. I am going to try my project with all the libraries suggested, and if results vary -- I'll post the results. Thanks again! Peter++ -- === Peter Petto [EMAIL PROTECTED] Bay Village, OH tel: 440.249.4289 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Memory error - how to manage large data sets?
The original post was a little ambiguous: I need to find the sum of all numbers at even positions in the Fibonacci series upto 2 million. But the project euler page (http://projecteuler.net/index.php?section=problemsid=2) is clear: Find the sum of all the even-valued terms in the sequence which do not exceed four million. Depending on which value you are after, it may not be necessary to enumerate a million terms of the fibonacci sequence. In fact, only about thirty are needed. Now if you want to do the harder problem, I suggest using GMP or some other Numerical extension, and not Python :) Or play some interesting math tricks. The wikipedia page on fibonaccis lists a lot of identities that could be exploited in intriguing ways. Cheers ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Memroy Error - how to manage large data sets?
On Tue, 2008-07-29 at 12:00 +0200, [EMAIL PROTECTED] wrote: Message: 2 Date: Tue, 29 Jul 2008 08:44:40 +0100 From: Alan Gauld [EMAIL PROTECTED] Subject: Re: [Tutor] Tutor Digest, Vol 53, Issue 99 To: tutor@python.org Message-ID: [EMAIL PROTECTED] Content-Type: text/plain; format=flowed; charset=iso-8859-1; reply-type=original kinuthiA muchanE [EMAIL PROTECTED] wrote I have realised that when you need to work with large numbers, lists are slow and tend to gobble up bytes. The lists are not particularly slow (obviously if you want to traverse them they take longer for more members) and they don't take up much more memory that the sum of the sizes of whats in them. The problem here is that the data items themselves are huge. Around 20K per item. Thus a million times 20K is 20G! This is how I would approach the problem, without using lists. Yep, we all seem to be pretty much in agreement :-) It produces the result instantly, even for large numbers like a 100, 000, 000. Umm, not instantly on my PC... 1 million took 5 minutes, I've no idea how long 100 million would take! I think the question is to calculate the sum of all even numbers in the Fibonacci series which do not exceed a million, not the millionth term. Number 25 on Project Euler which asks for first term in the Fibonacci series to contain 1000 digits, is the term-wise question ;) def fibonacci() a = 0 b = 1 evenTotal = 0 while a 1: a,b = b,a+b if a%2 == 0: evenTotal += a print evenTotal Alan G ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] (no subject)
Hello. I'm am very new to the whole programming sence. I am trying to catch on but as soon as i want to write a program i've been told to use like 'notepad' in windows XP and save it as a .py file i have gotten this far. Am i wrong so far? If i am right why is it that i can't run it in anyway besides it opening in 'notepad' i've tried opening with Python but it doesn't work. Thanks, Morgan -- This message has been scanned for viruses and dangerous content by the BCEC Security Gateway, and is believed to be clean. Brisbane Catholic Education however gives no warranties that this e-mail is free from computer viruses or other defects. Except for responsibilities implied by law that cannot be excluded, Brisbane Catholic Education, its employees and agents will not be responsible for any loss, damage or consequence arising from this e-mail. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Morgan Thorpe wrote: Hello. I'm am very new to the whole programming sence. Welcome. :) I am trying to catch on but as soon as i want to write a program i've been told to use like 'notepad' in windows XP and save it as a .py file i have gotten this far. Am i wrong so far? It's fine so far. Computer programs (especially ones in python) are text files which are formatted in a specific way which can be understood and executed by Python. When you write your program, you use an editor like notepad and save the program as a .py file. You're okay this far. The next stage is to ask Python to execute this program. You do that by typing python program.py and hitting enter. If i am right why is it that i can't run it in anyway besides it opening in 'notepad' i've tried opening with Python but it doesn't work. How have you tried opening it in python? Can you show us your program? P.S. It's a good idea to use a proper Subject line for your mail. :) -- ~noufal http://nibrahim.net.in/ ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Hi Morgan, Have you installed Python on your computer? If you are using Microsoft Windows, you can download and install Python from here: http://python.org/download/releases/2.5.2/ and select python-2.5.2.msi. Once it's installed, you should have a directory on your machine called C:\python25. If you save your program with a .py extension in that folder (for example, call it morgan.py), then all you have to do is open a command window (Start Run and enter cmd), go to the C:\python25 directory, and type: python morgan.py Personally, if you are just staring out to program, I would recommend using the Python shell that comes with the package I referred to. Alan's tutorial, found here: http://www.freenetpages.co.uk/hp/alan.gauld/ is very helpful, as is this site, which is what I used to start learning Python: http://hkn.eecs.berkeley.edu/~dyoo/python/idle_intro/ Hope that helps. Good luck! Samir On Tue, Jul 29, 2008 at 7:27 AM, Morgan Thorpe [EMAIL PROTECTED]wrote: Hello. I'm am very new to the whole programming sence. I am trying to catch on but as soon as i want to write a program i've been told to use like 'notepad' in windows XP and save it as a .py file i have gotten this far. Am i wrong so far? If i am right why is it that i can't run it in anyway besides it opening in 'notepad' i've tried opening with Python but it doesn't work. Thanks, Morgan -- This message has been scanned for viruses and dangerous content by the BCEC Security Gateway, and is believed to be clean. Brisbane Catholic Education however gives no warranties that this e-mail is free from computer viruses or other defects. Except for responsibilities implied by law that cannot be excluded, Brisbane Catholic Education, its employees and agents will not be responsible for any loss, damage or consequence arising from this e-mail. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] How to run a Python program under Windows.
Morgan Thorpe wrote: Hello. Hi. I'm am very new to the whole programming sence. Welcome. I am trying to catch on but as soon as i want to write a program i've been told to use like 'notepad' in windows XP and save it as a .py file i have gotten this far. Am i wrong so far? No. There are also IDEs such as IDLE and Python For Windows that make development easier (for many of us). If i am right why is it that i can't run it in anyway besides it opening in 'notepad' i've tried opening with Python but it doesn't work. Now comes the art of asking good (effective) questions. 1 - provide a subject (as I did) 2 - tell us what you tried (opening with Python is too vague) Example At the command prompt I entered c:\\python25\\python test.py 3 - tell us what happened. Example I got the message 'python' is not recognized as an internal or external command, operable program or batch file. 4 - reply to the list. Those are just examples. What did you do open with Python I'll bet you picked that from the context menu. If that is the case did you see a dos window flash ever so briefly? Try adding raw_input(press any key to exit) at the end of your program. Of course that will work only if there are no errors in the program. Best way for now is from the command prompt (like c:\\python25\\python test.py) -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Another thing you might want to check, since you're using Windows: Open up windows explorer, go the tools menu, folder options, then click the view tab, and then make sure the option hide extensions for known filetypes is unchecked. If it's not, uncheck it, and click OK. Then go and make sure your file has a filename like myfile.py, and not myfile.py.txt Hope this helps! -Wayne ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] (no subject)
Noufal Ibrahim wrote: Morgan Thorpe wrote: Hello. I'm am very new to the whole programming sence. Welcome. :) I am trying to catch on but as soon as i want to write a program i've been told to use like 'notepad' in windows XP and save it as a .py file i have gotten this far. Am i wrong so far? It's fine so far. Computer programs (especially ones in python) are text files which are formatted in a specific way which can be understood and executed by Python. When you write your program, you use an editor like notepad and save the program as a .py file. You're okay this far. The next stage is to ask Python to execute this program. You do that by typing python program.py and hitting enter. If i am right why is it that i can't run it in anyway besides it opening in 'notepad' i've tried opening with Python but it doesn't work. Have a look at this: http://www.python.org/doc/faq/windows/ and this: http://docs.python.org/dev/using/windows.html TJG ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Turtle problem: how to exit the .exe?
Dick Moores [EMAIL PROTECTED] wrote OK, let's eliminate some variables and go back to getting the python script working. Remember, it does work. Not if it doesn't close down cleanly. In my book that's broken! I've used turtle many times and never seen this behaviour before. What does the code look like? http://py77.python.pastebin.com/f414c8ce4 I can't see anything that would cause this(*) but it might be interesting to add some debugging print statements to see what happens to the loop counters etc when you try to close the window. (*) I see a few other things that raise my eyebrows but not that cause this... :-) Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Style help: long strings with formatting
Hi, I've been dinking around, making a program that will find out how different factors affect what you *really* save on gas, and at the end I have it output some information, but I'm not sure what the right style for formatting is. output = At an average weekly savings of $%.02f, your monthly savings will be $%.02f. \n Your annual savings will be $%.02f. % (diff, monthly_savings, annual_savings) print output. As you can see, it's very much longer than the 72 characters suggested in the PEP 8 found here: http://www.python.org/dev/peps/pep-0008/ I know it would be fairly trivial to split it up into several strings, and concatenating or printing each one. I've tried using the \ line continuation to break up the string, however that leaves the indentation inside the string. Breaking the string up with commas or even concatenation gives issues with the % operation. So my question is, what is the proper stylistic way to render my text? Also, I don't know if I just looked over it, or if it honestly didn't exist anywhere I looked for it, but I couldn't find any naming conventions for regular ol' variables. I found plenty for functions, classes, etc., so I'm not sure if my underscores are proper for my variables or if I should fix those. TIA, Wayne -- To be considered stupid and to be told so is more painful than being called gluttonous, mendacious, violent, lascivious, lazy, cowardly: every weakness, every vice, has found its defenders, its rhetoric, its ennoblement and exaltation, but stupidity hasn't. - Primo Levi ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Memroy Error - how to manage large data sets?
kinuthiA muchanE [EMAIL PROTECTED] wrote Umm, not instantly on my PC... 1 million took 5 minutes, I've no idea how long 100 million would take! I think the question is to calculate the sum of all even numbers in the Fibonacci series which do not exceed a million, not the millionth term. That's not what the OP requested. Number 25 on Project Euler which asks for first term in the Fibonacci series to contain 1000 digits, is the term-wise question ;) Assuming the OP is using that problerm set then it looks like he misinterpreted the problem. But the problem he asked us to solve was adding every other number up to 2 million entries... Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to run a Python program under Windows.
bob gailer [EMAIL PROTECTED] wrote Best way for now is from the command prompt (like c:\\python25\\python test.py) Since many newbies have no idea how to start a command prompt I'll add: go to Start-Run type CMD in the dialog and click OK A black window should appear, this is the command prompt. Typing pythonReturn at the prompt should get you a Python prompt. Typing Ctrl-ZReturn will exit it. But as mentioned elsewhere using IDLE (or Python GUI in the start menu) is a better option. Or if you got the ActiveState version of python better still use PythonWin. HTH, -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Style help: long strings with formatting
W W [EMAIL PROTECTED] wrote output = At an average weekly savings of $%.02f, your monthly savings will be $%.02f. \n Your annual savings will be $%.02f. % (diff, monthly_savings, annual_savings) print output. As you can see, it's very much longer than the 72 characters suggested in the PEP 8 found here: http://www.python.org/dev/peps/pep-0008/ Use a triple quoted string for multiline output output = At an average weekly savings of \t$%.02f, your monthly savings will be \t$%.02f. Your annual savings will be \t$%.02f. % (diff, monthly_savings, annual_savings) print output I know it would be fairly trivial to split it up into several strings, and concatenating or printing each one. Multiline strings are designed to save you having to do that. -- Alan Gauld Author of the Learn to Program web site http://www.freenetpages.co.uk/hp/alan.gauld ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Python Desktop Application for a Case Study
Hi list, I'm looking for a Python application with source code to analyse it with my small research project. The programm should have the following properties: - Using a GUI framework at best PyQt - Desktop application with small breaks. That means the software does nothing in that moments and is in a stationary phase and waits for the next user input. - Not to simple but also not to complex that means in detail: -- Only one process, however different threads are allowed -- Between 10-25 features would be appropiated - The example should be known from more than one person ;) I have a notion that a date management, cookbook application or something else are very appropriate. Thanks in advance! Kind regards, Michael ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
[Tutor] Obtaining various combinations of a given word
Hi,everyone. My name is Sai krishna, and I'm new to Python as well as Programming. I wanted to print out all the combinations of a given word. I am doing it this way: n='cat' def arrange(n): if len(n)==1: #length of word is 1 print n elif len(n)==2: # length of word is 2 print n[0]+n[1] print n[1]+n[0] elif len(n)==3: print n[0]+n[1]+n[2] print n[0]+n[2]+n[1] print n[1]+n[0]+n[2] print n[1]+n[2]+n[0] print n[2]+n[0]+n[1] print n[2]+n[1]+n[0] This process is quite lengthy, and I have managed to do this for word containing 5 letters,i,e.,120 combinations Is there a better way? Please help. -- cheers!!! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Python Desktop Application for a Case Study
Michael Perscheid [EMAIL PROTECTED] wrote I'm looking for a Python application with source code to analyse it with my small research project. The programm should have the following properties: Try Sourceforge. There is a drawing tool - think Visio - that is in Python, maybe that would do? Its called Dia and does UML diagrams. Its based on GTk. A simpler one is Skencil(sp?) but it has some bits in C for speed. www.skencil.org The documentation oddly refers to it as Sketch... it claims to be built on Tkinter Either might suit your needs? Alan G. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Obtaining various combinations of a given word
sai krishna wrote: Hi,everyone. My name is Sai krishna, and I'm new to Python as well as Programming. Welcome. I wanted to print out all the combinations of a given word. More precisely you want to print all the combination of the letters in a given word. I am doing it this way: n='cat' def arrange(n): if len(n)==1: #length of word is 1 print n elif len(n)==2: # length of word is 2 print n[0]+n[1] print n[1]+n[0] elif len(n)==3: print n[0]+n[1]+n[2] print n[0]+n[2]+n[1] print n[1]+n[0]+n[2] print n[1]+n[2]+n[0] print n[2]+n[0]+n[1] print n[2]+n[1]+n[0] This process is quite lengthy, and I have managed to do this for word containing 5 letters,i,e.,120 combinations Is there a better way? For starters see http://en.wikipedia.org/wiki/Combination and http://en.wikipedia.org/wiki/Combinadic. At the bottom of the latter is a link "The Art Of Computer Programming: Pre-Fascicle 3A, A DRAFT OF SECTION 7.2.1.3: GENERATING ALL COMBINATIONS" by Donald E. Knuth (compressed postscript file) I have not examined that but it seems to be what you want. -- Bob Gailer 919-636-4239 Chapel Hill, NC ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Obtaining various combinations of a given word
Hi Sai krishna, I recently wrote a program that does something similar to what you want. If you are familiar with the game text-twist, I wrote up a solver that will untwist any word by generating all possible combinations and then checking against a dictionary. To be honest, I had a little help from the internet for the permute function. Below is the code. To address your question, when you find yourself doing something like if 1, ..., if 2, ... ..., if 3, ... ... ..., you probably want to use recursion, which is what my permute function uses below. The whole point of programming is to make the computer do all the repetition, not you, the programmer in copy and pasting and changing! Hope this helps! Regards, Vivek #!/usr/bin/env python import sys import os import commands import random if len(sys.argv) == 1: print Usage: +sys.argv[0]+ word [len] exit() word = sys.argv[1].lower() if len(sys.argv) == 2: n = len(word) else: n = int(sys.argv[2]) a = [] for i in range(len(word)): a.append(word[i]) def permute(seq): if len(seq) = 1: yield seq else: for i in xrange(0,len(seq)): for tail in permute( seq[:i] + seq[i+1:] ): yield [ seq[i] ] + tail # Load dict in memory dict = {} infile = open(wordlist.txt, r) for t in infile.readlines(): dict[t.rstrip()] = 1 printed_words = [] for o in permute(a): possible_string = ''.join(o) possible_word = possible_string[:n] if dict.has_key(possible_word): if possible_word in printed_words: continue else: printed_words.append(possible_word) print possible_word On Jul 29, 2008, at 5:50 PM, sai krishna wrote: Hi,everyone. My name is Sai krishna, and I'm new to Python as well as Programming. I wanted to print out all the combinations of a given word. I am doing it this way: n='cat' def arrange(n): if len(n)==1: #length of word is 1 print n elif len(n)==2: # length of word is 2 print n[0]+n[1] print n[1]+n[0] elif len(n)==3: print n[0]+n[1]+n[2] print n[0]+n[2]+n[1] print n[1]+n[0]+n[2] print n[1]+n[2]+n[0] print n[2]+n[0]+n[1] print n[2]+n[1]+n[0] This process is quite lengthy, and I have managed to do this for word containing 5 letters,i,e.,120 combinations Is there a better way? Please help. -- cheers!!! ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Obtaining various combinations of a given word
sai krishna sparkignited at gmail.com writes: Hi,everyone.My name is Sai krishna, and I'm new to Python as well as Programming.I wanted to print out all the combinations of a given word.I am doing it this way: n='cat'def arrange(n):if len(n)==1: #length of word is 1 print nelif len(n)==2: # length of word is 2 print n[0]+n[1] print n[1]+n[0]elif len(n)==3: print n[0]+n[1]+n[2] print n[0]+n[2]+n[1] print n[1]+n[0]+n[2] print n[1]+n[2]+n[0] print n[2]+n[0]+n[1] print n[2]+n[1]+n[0]This process is quite lengthy, and I have managed to do this for word containing 5 letters,i,e.,120 combinations Is there a better way?Please help.-- cheers!!! Found the following link while googleing. It looks pretty nice and makes good use of Python's generators. http://www.daniweb.com/code/snippet553.html Cheers, - Brendan ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Obtaining various combinations of a given word
On 30/07/2008, sai krishna [EMAIL PROTECTED] wrote: I wanted to print out all the combinations of a given word. A thought for you: Let's say you want to find permutations of the string 'abcd'. Some of those permutations will start with 'a' -- how many? Can you list all the permutations starting with 'a'? Do you see any connection between that list and your solution for n == 3? (hint: the following is a function for computing factorials. Do you understand it? def fac(n): if n == 0: return 1 else: return n*fac(n-1) ) (also, another thought for you, once you've solved this problem: what answer would you expect for the string 'aaa'?) -- John. ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor