Re: [Tutor] Visual Studio Community 2017
Hi, you might want to look at things like Anaconda, Enthought, ... They come with lots of packages/libraries pre-installed and a complete programming environment. For small project, you could even look at Jupyter. I t allows you to have your program, data & results (IIRC even pics & vids) in something like a webpage. Have fun. On Apr 18, 2017 7:08 PM, "Rafael Knuth"wrote: > I wanted to start my first project using matplotlib (I have never > worked with libraries before). I am trying to get started with VS > Community 2017, and I am having trouble performing the most basic > tasks such as installing matplotlib. Anyone here using VS 2017? Or, > can anyone recommend an alternative to VS for Windows? Thanks! > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor > ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] trouble with beautiful soup
Hi, see below. On Thu, Dec 24, 2015 at 6:21 PM, marcus lütolfwrote: > dear pythonistas, > for hours I'am traying to use Beautiful Soup for scraping some websites as an > exercise. > But each time I run the code below: > import urllib from BeautifulSpoup import * > url = raw_input( 'Enter -') > html = urllib.urlopen(url).read() > soup = BeautifulSoup(html) print soup > > I am getting the following trace back without beeing prompted for an input: > > Traceback (most recent call last): > File "C:/Python27/Beautiful Soup_ex1.py", line 2, in > from beautifulspoup import * > ImportError: No module named beautifulspoup Change beautifulspoup to beautifulsoup It's just a typo... > > I have installed and unzipped etc. the latest file from www.crummy.com many > times. > If I click setup.py the command window appears only a fraction of a second. > ?? > > Happy holidays and thanks for help, Marcus. > HTH & nice holidays too! > > > > > --- > Diese E-Mail wurde von Avast Antivirus-Software auf Viren geprüft. > https://www.avast.com/antivirus > > ___ > Tutor maillist - Tutor@python.org > To unsubscribe or change subscription options: > https://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] create a copying tool
Hi, see comments below: On Tue, May 19, 2015 at 2:20 PM, Remco learningpyt...@hofs-bibo.nl wrote: Hello, snip import os import shutil target_dir = r'D:\target\' source_dir = r'D:\source\' source_file = r'D:\source\list.txt' you are not using this... missing_files = open(missings.txt,w) for line in open('list.txt'): source_file = os.path.normpath(source_dir +line) target_file = os.path.normpath(target_dir +line) if os.path.exists(target_file): don't you want to check if the source file is existing? shutil.copyfile(source_file,target_file) else: missing_files.write(line) missing_files.close() Can someone help my fixing my problem. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor HTH, ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: https://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Dictionaries and multiple keys/values
Hi, On Tue, Mar 26, 2013 at 5:36 AM, Robert Sjoblom robert.sjob...@gmail.comwrote: Hi again, Tutor List. I am trying to figure out a problem I've run into. Let me first say that this is an assignment, so please don't give me any answers, but just nudge me in the general direction. So the task is this: from a text file, populate three different dictionaries with various information. The text file is structured like so: Georgie Porgie 87% $$$ Canadian, Pub Food So name, rating, price range, and food offered. After food offered follows a blank line before the next restaurant is listed. The three dictionaries are: name_to_rating = {} price_to_names = {'$': [], '$$': [], '$$$': [], '': []} cuisine_to_names = {} Now I've poked at this for a while now, and one idea I had, which I worked on for quite a while, was that since the restaurants all start at index 0, 5, 10 and so on, I could structure a while loop like this: with open('textfile.txt') as mdf: file_length = len(mdf.readlines())-1 mdf.seek(0) data = mdf.readlines() No need to calculate the file length. Read yoiur data ask python for the length of data. You don't need nor want to read the file twice. i = 0 while file_length 0: name_to_rating[data[i]] = int(data[i+1][:2]) price_to_names[data[i+2].strip()].append(data[i].strip()) # here's the cuisine_to_names part i += 5 file_length -= 5 Another approach could be to process the file while reading it. Since the format is fixed, you knoow exactly what information the line contains. And while this works, for the two first dictionaries, it seems really cumbersome -- especially that second expression -- and very, very brittle. However, even if I was happy with that, I can't figure out what to do in the situation where: data[i+3] = 'Canadian, Pub Food' #should be two items, is currently a string. My problem is that I'm... stupid. I can split the entry into a list with two items, but even so I don't know how to add the key: value pair to the dictionary so that the value is a list, which I then later can append things to. You might want to read up on the dict.get() method. I'm sorry, this sounds terribly confused, I know. I had another idea to feed each line to a function, because no restaurant name has a comma in it, and food offered always has a comma in it if the restaurant offers more than one kind. But again, this seems really brittle. snip Much thanks in advance. -- best regards, Robert S. ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] BMI calc
Hi, On Wed, Mar 13, 2013 at 4:07 AM, Soliman, Yasmin ysoli...@uncc.edu wrote: I'm using python 2.7, where exactly does the return statment with the (return (weight/(height*height))*703 calculation get posted in the function? def calc_BMI(weight,height): if bmi =18.5: return 'underweight' elif bmi = 18.5 and bmi =24.9: return 'normal weight' elif bmi =25 and bmi =29.9: Following return is out of alignment. Should be at the same place in the line as the oters. return 'overweight' elif bmi =30: return 'obese' The user will need to input height ex) 5'3 and which will need to be split and stripped in order to convert to inches. Exactly. Do you have a problem with this? If so what? Dreetz ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] implementing rot 13 problems
On Tue, Mar 12, 2013 at 6:57 AM, RJ Ewing ewing...@gmail.com wrote: I am trying to implement rot 13 and am having troubles. My code is as follows: class Rot_13(webapp2.RequestHandler): def write_form(self, user=): self.response.out.write(rot_form % user) def get(self): self.write_form() def post(self): user = self.request.get(text) s = self.rot_text(user) print s s = .join(s) self.escape_html(s) self.write_form(s) You might want to read up on the translate() method. def rot_text(self, s): ls = [i for i in s] for i in ls: if i.isalpha(): if i.isupper(): if i = 'M': x = ls.index(i) ls[ls.index(i)] = chr(ord(i) + 13) else: x = ls.index(i) ls[ls.index(i)] = chr(ord(i) - 13) elif i.islower(): if i = 'm': x = ls.index(i) ls[x] = chr(ord(i) + 13) elif i 'm': x = ls.index(i) ls[x] = chr(ord(i) - 13) return ls def escape_html(self, s): return cgi.escape(s, quote = True) Now if I enter abc, I get nop. But if I enter abcdefghijklmnop, I get abcqrstuvwxyznop. I have included print statements to check if ls[x] is changing and it is, but something is going wrong when I return the ls, and I am not quite sure what it is. Me neither, how are you calling /returning it? Thanks ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] How to change the char in string for Python
On Sun, Feb 24, 2013 at 2:26 PM, Sudo Nohup sudo.no...@gmail.com wrote: Thanks very much!! I learnt a lot from you kind reply. Not only is it about the question itself, but also about how to ask a question in a mailing list.(Sorry that it is the first time for me to ask questions in a mailing list). The question comes from a riddle of the PythonChallenge website( http://www.pythonchallenge.com/pc/def/map.html). Now I write the code as the following, mystring = g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. temp = [ chr ( (ord(char)-ord('a')+2)%26 +ord('a')) if (ord(char)=ord('a') and ord(char)=ord('z')) else char for char in mystring ] result = .join(temp) print result OR mystring = g fmnc wms bgblr rpylqjyrc gr zw fylb. rfyrq ufyr amknsrcpq ypc dmp. bmgle gr gl zw fylb gq glcddgagclr ylb rfyr'q ufw rfgq rcvr gq qm jmle. sqgle qrpgle.kyicrpylq() gq pcamkkclbcb. lmu ynnjw ml rfc spj. result = [] for char in mystring: if(ord(char)=ord('a') and ord(char)=ord('z')): char = chr ( (ord(char)-ord('a')+2)%26 +ord('a')) result.append(char) result = .join(result) print result Thanks, James Hi James, for this riddle, look for the translate() method. Have fun nwith the challenges. Jos ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New User-Need-Help
Sorry, sent before finishing... If you changed raw_input, as asked. Now, it complains about print 'Game Over' Should become print('Game Over') On Sat, Feb 16, 2013 at 12:10 AM, Jos Kerc josk...@gmail.com wrote: No, not same. At lea On Fri, Feb 15, 2013 at 11:44 PM, Joel Goldstick joel.goldst...@gmail.com wrote: so copy the code and the error message here On Fri, Feb 15, 2013 at 5:42 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: I did what you said, nothing changed. same errors, same syntax message. I suggest you run it on your IDLE to see if it works. On Fri, Feb 15, 2013 at 3:31 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:29 PM, Joel Goldstick joel.goldst...@gmail.com wrote: Are you using python 2 or python 3? On Fri, Feb 15, 2013 at 5:28 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: Ok, so I made a shortcut to IDLE(GUI) and I open it up. click New Window and type in the code: print Game Over raw_input(\n\nPress Enter Key to exit) and save the file on my desktop. I double-click it and it opens a black window with gray text for a split second and if you look quickly enough then you can see Invalid Sytnax. If I do the same code in the Interactive Window then it highlights raw_input and says Invalid Syntax Are you using python 2 or python 3? Sorry, you said above python 3. In python 3 raw_input was changed to input. so change that and it will work for you. There are some differences between 2 and 3 that you will need to look out for. Go to the python.org site to learn about them. Your book was written for python 2 it seems On Fri, Feb 15, 2013 at 3:12 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:09 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: It Didn't work. First of all, reply to all. You are sending messages to me only, not to the group. Other's may be able to help you better than I can. Second. It didn't work is not a useful answer. So you have a file with two lines in it. The first has a print statement. The second has a raw_input statement. When you run it, what happens exactly? Do you get a traceback message telling you what the error is, and on what line? On Fri, Feb 15, 2013 at 3:05 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:03 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: On Fri, Feb 15, 2013 at 2:58 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 4:45 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: Hi, I am very new to Python, I am using the e-book Python Programming for the Absolute Beginner and am starting with a simple Game Over Program. This is the code:which is extremely simple! printGame Over raw_input(\n\nPress Enter Key to exit) welcome Nicholas One important thing about python is indentation is important. You have presented your code in a way that can't be. Can you actually copy your program and paste it into an email message. Also, Windows, Linux, Mac? That's it. It is supposed to bring up a window that says Game Over and at the bottom say Press enter Key to exit and when you press the enter key it is supposed to exit(big suprise). But all it does is highlight raw_input and says invalid syntax Now, if I just put print Game Over then it says Game Over UNDERNEATH the code I just printed! now I am following the book to the *pixel* and that is not what is supposed to happen! Please email me back as soon as you get this...(if you are not to busy). Thanks,Nicholas -- http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com Sorry, I am using Windows 7 Ultimate 32-bit with Python 3.3 Here is the code in exact form print Game Over raw_input(\n\nPress Enter Key to Exit) Thanks, Nicholas You need to unindent the raw_input like so: print Game Over raw_input(\n\nPress Enter Key to Exit) In python you indent code blocks (like for loops, if statements, function blocks, etc.). You can't just indent from one line to the next in sequential code or you will be told its a syntax error -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New User-Need-Help
No, not same. At lea On Fri, Feb 15, 2013 at 11:44 PM, Joel Goldstick joel.goldst...@gmail.comwrote: so copy the code and the error message here On Fri, Feb 15, 2013 at 5:42 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: I did what you said, nothing changed. same errors, same syntax message. I suggest you run it on your IDLE to see if it works. On Fri, Feb 15, 2013 at 3:31 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:29 PM, Joel Goldstick joel.goldst...@gmail.com wrote: Are you using python 2 or python 3? On Fri, Feb 15, 2013 at 5:28 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: Ok, so I made a shortcut to IDLE(GUI) and I open it up. click New Window and type in the code: print Game Over raw_input(\n\nPress Enter Key to exit) and save the file on my desktop. I double-click it and it opens a black window with gray text for a split second and if you look quickly enough then you can see Invalid Sytnax. If I do the same code in the Interactive Window then it highlights raw_input and says Invalid Syntax Are you using python 2 or python 3? Sorry, you said above python 3. In python 3 raw_input was changed to input. so change that and it will work for you. There are some differences between 2 and 3 that you will need to look out for. Go to the python.org site to learn about them. Your book was written for python 2 it seems On Fri, Feb 15, 2013 at 3:12 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:09 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: It Didn't work. First of all, reply to all. You are sending messages to me only, not to the group. Other's may be able to help you better than I can. Second. It didn't work is not a useful answer. So you have a file with two lines in it. The first has a print statement. The second has a raw_input statement. When you run it, what happens exactly? Do you get a traceback message telling you what the error is, and on what line? On Fri, Feb 15, 2013 at 3:05 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:03 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: On Fri, Feb 15, 2013 at 2:58 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 4:45 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: Hi, I am very new to Python, I am using the e-book Python Programming for the Absolute Beginner and am starting with a simple Game Over Program. This is the code:which is extremely simple! printGame Over raw_input(\n\nPress Enter Key to exit) welcome Nicholas One important thing about python is indentation is important. You have presented your code in a way that can't be. Can you actually copy your program and paste it into an email message. Also, Windows, Linux, Mac? That's it. It is supposed to bring up a window that says Game Over and at the bottom say Press enter Key to exit and when you press the enter key it is supposed to exit(big suprise). But all it does is highlight raw_input and says invalid syntax Now, if I just put print Game Over then it says Game Over UNDERNEATH the code I just printed! now I am following the book to the *pixel* and that is not what is supposed to happen! Please email me back as soon as you get this...(if you are not to busy). Thanks,Nicholas -- http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com Sorry, I am using Windows 7 Ultimate 32-bit with Python 3.3 Here is the code in exact form print Game Over raw_input(\n\nPress Enter Key to Exit) Thanks, Nicholas You need to unindent the raw_input like so: print Game Over raw_input(\n\nPress Enter Key to Exit) In python you indent code blocks (like for loops, if statements, function blocks, etc.). You can't just indent from one line to the next in sequential code or you will be told its a syntax error -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] New User-Need-Help
On Sat, Feb 16, 2013 at 12:20 AM, Deborah Piotrowski spiceninj...@gmail.com wrote: It works, but it doesn't open a window. It just says that stuff under the code. How do you open a window? Depends... How do you run the script? From Idle, command prompt? On Fri, Feb 15, 2013 at 4:14 PM, Jos Kerc josk...@gmail.com wrote: Sorry, sent before finishing... If you changed raw_input, as asked. Now, it complains about print 'Game Over' Should become print('Game Over') On Sat, Feb 16, 2013 at 12:10 AM, Jos Kerc josk...@gmail.com wrote: No, not same. At lea On Fri, Feb 15, 2013 at 11:44 PM, Joel Goldstick joel.goldst...@gmail.com wrote: so copy the code and the error message here On Fri, Feb 15, 2013 at 5:42 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: I did what you said, nothing changed. same errors, same syntax message. I suggest you run it on your IDLE to see if it works. On Fri, Feb 15, 2013 at 3:31 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:29 PM, Joel Goldstick joel.goldst...@gmail.com wrote: Are you using python 2 or python 3? On Fri, Feb 15, 2013 at 5:28 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: Ok, so I made a shortcut to IDLE(GUI) and I open it up. click New Window and type in the code: print Game Over raw_input(\n\nPress Enter Key to exit) and save the file on my desktop. I double-click it and it opens a black window with gray text for a split second and if you look quickly enough then you can see Invalid Sytnax. If I do the same code in the Interactive Window then it highlights raw_input and says Invalid Syntax Are you using python 2 or python 3? Sorry, you said above python 3. In python 3 raw_input was changed to input. so change that and it will work for you. There are some differences between 2 and 3 that you will need to look out for. Go to the python.org site to learn about them. Your book was written for python 2 it seems On Fri, Feb 15, 2013 at 3:12 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:09 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: It Didn't work. First of all, reply to all. You are sending messages to me only, not to the group. Other's may be able to help you better than I can. Second. It didn't work is not a useful answer. So you have a file with two lines in it. The first has a print statement. The second has a raw_input statement. When you run it, what happens exactly? Do you get a traceback message telling you what the error is, and on what line? On Fri, Feb 15, 2013 at 3:05 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 5:03 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: On Fri, Feb 15, 2013 at 2:58 PM, Joel Goldstick joel.goldst...@gmail.com wrote: On Fri, Feb 15, 2013 at 4:45 PM, Deborah Piotrowski spiceninj...@gmail.com wrote: Hi, I am very new to Python, I am using the e-book Python Programming for the Absolute Beginner and am starting with a simple Game Over Program. This is the code:which is extremely simple! printGame Over raw_input(\n\nPress Enter Key to exit) welcome Nicholas One important thing about python is indentation is important. You have presented your code in a way that can't be. Can you actually copy your program and paste it into an email message. Also, Windows, Linux, Mac? That's it. It is supposed to bring up a window that says Game Over and at the bottom say Press enter Key to exit and when you press the enter key it is supposed to exit(big suprise). But all it does is highlight raw_input and says invalid syntax Now, if I just put print Game Over then it says Game Over UNDERNEATH the code I just printed! now I am following the book to the *pixel* and that is not what is supposed to happen! Please email me back as soon as you get this...(if you are not to busy). Thanks,Nicholas -- http://mail.python.org/mailman/listinfo/python-list -- Joel Goldstick http://joelgoldstick.com Sorry, I am using Windows 7 Ultimate 32-bit with Python 3.3 Here is the code in exact form print Game Over raw_input(\n\nPress Enter Key to Exit) Thanks, Nicholas You need to unindent the raw_input like so: print Game Over raw_input(\n\nPress Enter Key to Exit) In python you indent code blocks (like for loops, if statements, function blocks, etc.). You can't just indent from one line to the next in sequential code or you will be told its a syntax error -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com -- Joel Goldstick http://joelgoldstick.com -- Nicholas J. Piotrowski -- Joel Goldstick http://joelgoldstick.com
Re: [Tutor] Help!
You are missing a multiplication sign. Near the end of your formula. On Fri, Jan 18, 2013 at 1:56 PM, Carpenter, Steven steven.carpen...@oakland.k12.mi.us wrote: To Whom it May Concern, I’m trying to get this code working. *Here’s my question:* Consider a triangle with sides of length 3, 7, and 9. The law of cosines states that given three sides of a triangle (a, b, and c) and angle C between sides a and b: Write Python code to calculate the three angles in the triangle. *Here’s my code: * # Calculate the angles in a triangle # Imports the Math data import math # Sets up the different angles with corresponding letters # The first angle is a a = 3 # The second angle is b b = 7 # The third angle is c c = 9 # Calculates angle C print(math.acos(((a**2)+(b**2)-(c**2))/(2(a*b *Here’s my output:* Traceback (most recent call last): File E:\Programming\Problem4.py, line 12, in module print(math.acos(((a**2)+(b**2)-(c**2))/(2(a*b TypeError: 'int' object is not callable ** ** *Steven Carpenter* ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor
Re: [Tutor] Recursion doubt
Hi, you are tackling 3 heavy subjects in just 1 go! graphs :a triving math society would approve your choice. But you might start with the *slightly* less difficult challenge: trees. I do not know your math/programming background, so the following link can perhaps enlighten you: http://www.brpreiss.com/books/opus7/ Trees, graphs, queues and what more implemented in several languages. Python included. Also, most CS starters have chapters devoted to (some of) them. (E.g. http://www.greenteapress.com/thinkpython/) Backtracking recursion will surely be topics too. Reading them will help getting a handle on it The 3 chosen topics are advanced or even rather advanced even for most of CS people. To return to your question... Basically, find_path takes a node connected to 'start', if not at the 'end', make the current node 'start' and see if find_path does return a path. As soon as a path is foud find_path quits searching. You can look at find_all as a master routine that keeps calling find_path until find_path gives up. If the search space has not been exhausted (here there are still other nodes connected to 'start') take a suitable candidate call find_path from there until no more paths from there can be found. Repeat until no more suitable candidates. As you have gone through the whole search space, all paths from 'start' to 'end' were found. Recursion makes backtracking easier: the program/routine will by itself keep track of the steps taken where to go back if a search fails. Backtracking is the powerful technique of remembering the steps already taken to retrace them till the first non-explored sidetrack when needed. Choosing the next non-explored sidetrack though can be complex involving other searches, ... It all depends on what you look for, how the data is represented. A related topic that will help you understand things is breadth-first depth-first searches on trees. (in this case find_path executes a depth-first search on the graph - represented as a dictionary find_all combines the 2 searches, first the depth search when it quits, the breadth search takes over.) Greetz On 4/15/08, Anshu Raval [EMAIL PROTECTED] wrote: Hi, At the url *http://www.python.org/doc/essays/graphs.html*http://www.python.org/doc/essays/graphs.htmlthere is some code by Guido Van Rossum for computing paths through a graph - I have pasted it below for reference - Let's write a simple function to determine a path between two nodes. It takes a graph and the start and end nodes as arguments. It will return a list of nodes (including the start and end nodes) comprising the path. When no path can be found, it returns None. The same node will not occur more than once on the path returned (i.e. it won't contain cycles). The algorithm uses an important technique called backtracking: it tries each possibility in turn until it finds a solution. def find_path(graph, start, end, path=[]): path = path + [start] if start == end: return path if not graph.has_key(start): return None for node in graph[start]: if node not in path: newpath = find_path(graph, node, end, path) if newpath: return newpath return None *** He then says It is simple to change this function to return a list of all paths (without cycles) instead of the first path it finds: def find_all_paths(graph, start, end, path=[]): path = path + [start] if start == end: return [path] if not graph.has_key(start): return [] paths = [] for node in graph[start]: if node not in path: newpaths = find_all_paths(graph, node, end, path) for newpath in newpaths: paths.append(newpath) return paths *** I couldn't understand how it was simple to change the function find paths to find all paths. How would you think about writing this second function recursively. Especially the part about if start==end: return [path]. I feel you would give square brackets around path here after first writing the inductive part ... for node in graph[start] and then by trial and error put square brackets around path in the Basis part. Can someone please explain how to write this code. Thanks! -- Planning marriage in 2008! Join Shaadi.com matrimony FREE! Try it now!http://ss1.richmedia.in/recurl.asp?pid=429 ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor ___ Tutor maillist - Tutor@python.org http://mail.python.org/mailman/listinfo/tutor