- Strength to Increase Rep
- +8
- Strength to Decrease Rep
- -2
- Upvotes Received
- 29
- Posts with Upvotes
- 24
- Upvoting Members
- 16
- Downvotes Received
- 0
- Posts with Downvotes
- 0
- Downvoting Members
- 0
94 Posted Topics
Re: While Python offers flexible [I]container[/I] data structures, such as lists and dictionaries, it is occasionally helpful to create our own. Suppose, for example, that we wanted to create a data structure with two dictionaries: one for key-value pairs where len(key) is even, and one for key-value pairs where len(key) is … | |
Re: Write a simple calculator in Python. The program should accept an arithmetic expression from the command-line, then recursively parse it to produce a numerical value. Like many programming projects, this one can be done in phases: i) First, make the program parse only basic arithmetic. This means expressions that use … | |
Re: Hi EriCartman13, If you want to create a random number between 1 and 100, the conventional way of doing that in Python is:[CODE]import random x = random.randint(1,100)[/CODE]I would swap this code in and remove the for loop. The resulting program creates a random integer between 1 and 100, assigns that … | |
Re: You mean something like this?[CODE]import re f = dict() f["compile"] = re.compile f["findall"] = re.findall f["match"] = re.match pattern = f["compile"]("[\\w]+")[/CODE]This works for me. | |
Re: [I]...and that is .exe program that can be run on different machines.[/I] That's not necessarily true. Compiling a C/C++ program does not create a universal executable that can be installed on any machine. Whether the executable will work on your friend's machine depends on (a) what the program does, and … | |
Re: Hi ram.patil, A Hardy-Ramanujan number is a number which can be expressed as the sum of two positive cubes in exactly two different ways. For example, 1729 is equal to the sum: 1^3+12^3 - or alternatively, to the sum: 9^3+10^3 - but not to any other sums of positive cubes. … | |
Hi guys, I'm trying to learn the principles of artificial intelligence. I just finished coding up a belief net and a corresponding EM algorithm that learns the CPTs for latent nodes using training data (if you're unfamiliar with the lingo, you're probably going :eek: right now). While the program (written … | |
Here's something of mine that might actually be useful: a Python implementation of the K-means clustering algorithm. I wrote something similar last year in Java for a school project, and decided to rewrite it in Python this summer for practice. The purpose of the algorithm is to discover internal structure … | |
Re: Hi Ene Uran, There are two ways of doing this. The first way: use the textvariable option in the Label constructor to set the Label text to the value of a StringVar maintained elsewhere in your program. Then, in your callback function, change the value of the StringVar and the … | |
Re: Hi eleonora, Statements like this:[CODE]board[(x,y)] == "R"[/CODE]with a double equals sign (==) only test to see whether the value at (x,y) is "R"; that statement does not [I]assign[/I] "R" to board[(x,y)], which is what you want to do. Instead, use board[(x,y)] = "R" and it should remember the moves. Also … | |
Re: Hi Avner .H., I can't help you with your first problem, other than to say "why not put the Bash code in a Bash script?" which I guess is not an option for you, or else you would have done it. However, I can help with the second item: grabbing … | |
Hi all, Have you ever heard of "Petals Around the Rose?" It's a logic puzzle. The way it works is, I roll five dice, then tell you what the "score" is for this round. I can repeat this for you as many times as you want. It's your job to … | |
[URL="http://www.bitstorm.org/gameoflife/"]Life[/URL] is a "game" or cellular automaton - an evolving computational state system - developed by a Cambridge mathematician named John Conway. The idea is simple: start with a board of dimensions (x,y). Populate the board with an initial pattern of occupied and empty cells. In every turn, the rules … | |
Suppose you are a medical researcher studying diabetes. Your boss has given you a big chart of data from diabetes patients. Each row of the chart has information for one patient. Each column of the chart is a health-related statistic, such as height, weight, age, blood pressure, cholesterol level, etc. … | |
Here's a cute little encipher/decipher program with a Tkinter GUI I wrote a while back. It's an implementation of a derivative of the Vigenere algorithm; the algorithm is taken from Laurence Smith's Cryptography: The Science of Secret Writing, Amazon link here. It's a dated book (and the technique itself is … | |
Re: You could always make a .exe file that calls the Python script, then rename that to .scr, right? | |
Re: Hi jmroach, You're going to want to use an sqlite [URL="http://www.sqlite.org/pragma.html"]pragma statement[/URL]. A pragma statement lets you query database meta-data or in some cases alter the behavior of the database. The pragma that gives you column headers is called "table_info." In the example you gave below, you would use it … | |
Re: Hi gusbear, Are you using the Zelle graphics module? If you are, I think you should try the following strategy. Create a GraphWin, then in one of the corners make a button by drawing a rectangle, and inside it printing "Compute" or something like that. The API for the graphics … | |
Re: Hi rysin, Unless there's a high-level python module that takes care of chat-type stuff for you (and if there is, I don't know about it), you're going to have to learn how to use client and server sockets to send data from one computer to another. An example is given … | |
Re: Hi Edwards8, It turns out that PIL will let you read pieces of an image file in sequence without loading the entire thing into memory. Fred Lundh has a post about it on a python mailing list [URL="http://mail.python.org/pipermail/image-sig/1999-April/000713.html"]here[/URL]. I don't know if this will work for TIFs, but you can … | |
Re: Hi zachabesh, Can't you just use os.path.getsize()? You call it with the name of the file as an argument. | |
Re: Hi sidbizzle, So if I am understanding your code correctly, you are trying to set up a program that prints a welcome prompt with a menu, then allows the user to deposit or withdraw from an account. The amount of money in the account is stored in acctHist.data, along with … | |
Re: Hi linux, Yes, that is definitely possible. I've done something similar from within both pygame and Tkinter (I used Tkinter to design a GUI program for map editing). You would want to begin by creating a pygame Surface for each of the 10x10 image tiles, then stick all your Surfaces … | |
Hi all, I add a pygame implementation of the Game of Life to the Code Snippets section. I was actually kind of surprised that we didn't already have one in there! The program only has about 60 lines of code, but I buttressed that with a lot of comments and … | |
Re: Hi nish88, Search in this forum for "game of life." | |
Hi all, Are there any python modules which contain functions for getting the roots of a quartic (degree 4) polynomial? I don't think NumPy has anything like it, and I [I]really[/I] don't want to enter the equations in by hand. Thanks. | |
Re: Hi RMartins, This has less to do with Python and more to do with mathematics, but I'll say it anyway. As jrcagle said, 10**20 is a huge number of iterations to perform. Lucky for you, you won't need many more than 10**7. Actually, you'll only need to perform 10,015,005 computations, … | |
Re: Hi Carlo Gambino, The straightforward way of doing this is:[CODE=python]g = [ 1, 2, 3, 4 ] h = [] for i in range(len(g)+1)[1:]: h.append(g[i-1]+i) print h[/CODE]Alternatively, you could just use a list comprehension:[CODE=python]print [ g[i-1]+i for i in range(len(g)+1)[1:] ][/CODE]Hope this helps! | |
Re: Hi StepIre, You could use the pickle module, which serializes and saves data structures, effectively allowing you to skip the whole messy business of file parsing. Example:[CODE=python]import pickle # If you want to store a dictionary in a file... d = { "a":"aardvark", "b":"ball", "c":"centurion" } f = open("somefile.dat", "w") … | |
Re: Hi trihaitran, Use the "template method" software design pattern! Here's how it works: we're going to split that function of yours across two different functions in two different classes: a superclass with a "template method" and a subclass with a "hook method." The template method in the superclass contains all … | |
Hi all, I added a new script to the code snippets page. This time, it's a logic puzzle called "Petals Around the Rose." The goal is for you to infer what algorithm is being used to "score" rolls of five colored dice without peeking at the code. The snippet is … | |
Re: Hi nirmalarasu, I think [URL="http://wiki.python.org/moin/Freeze"]freeze[/URL] is what you're looking for. | |
Re: [QUOTE=gusbear;525524]i get to down as far as the tax equation and it won't run, any ideas??[/QUOTE]I have an idea, and that idea is "please wrap your program in code tags." | |
Re: Well, [URL="http://www.answers.com/firstly&r=67"]firstly...[/URL] (ducks) | |
Re: Hi gawain_, Why are you reading input using stdin? Why not use one of python's input commands? If you're set on using stdin, you could try this:[CODE=python]import sys inp = sys.stdin.readline() while inp.strip() != "": print inp # Do your calculations right here inp = sys.stdin.readline()[/CODE]Stolen from [URL="http://www.dalkescientific.com/writings/NBN/python_intro/io.html"]here[/URL]. It doesn't … | |
Re: Hi msaenz, Lardmeister is correct in that your check for the symbol token "UPS" is flubbed. His approach works fine if you want to treat the whole XML file as a string. However, if you want to do this using the Python xml module, please see my modifications to your … | |
Re: Hi nish88, I'm not sure I understand what you mean. Do you want to click on the canvas, then have an oval pop up where you clicked? It sounds like you need to bind mouseclicks on the canvas to a callback function. The way to do that in Tkinter is:[CODE=python]canvas.bind("<Button-1>", … | |
Re: Hi rysin, Can you open the file at all from within python? If you say[CODE=python]f = open("C:\\orb\\backroundorb.jpeg", "r")[/CODE]does that work? | |
Re: Hi Ilya, If you want to do interactive simulations, I think you might be better off checking out [URL="http://www.pygame.org"]pygame[/URL], which is designed for games but can easily be adapted for this kind of thing. I have written simple 2D epithelial tumor growth simulations in pygame - I can't imagine how … | |
Re: Hi John, If I understand things correctly, then first off, line 10 is wrong. Instead of saying[CODE=python]print "After year", n[/CODE]you ought to say[CODE=python]print "After year", i[/CODE]because the variable that represents the current year in the for-loop is i, not n, which is the total number of years. Also, are you … | |
Re: Hi seamus.boyle, The problem is in line 5:[CODE=python]im3 = Image.composite('im1','im2',"L")[/CODE]See, what you're doing is sending the [i]string literals[/i] "im1" and "im2" as arguments to the function Image.composite(). But Image.composite() doesn't want string literals - it wants actual image objects, the variables themselves! Get rid of the single quotes and you … | |
Re: Hi gtselvam, I hate to say this, but I think what you want to do is impossible due to operating system limitations. If memory serves, the os.system() function call creates a forked environment which is destroyed once the call returns. In other words, suppose you write a python script called … | |
Re: Hi balance, How are you running it? The error occurs because the "downloader" module, urllib, has a urlopen() function which doesn't recognize the URL being passed in. If you step backwards, you see that the error is in line 15 of UbuntuPackageGrabber.py, the main script. That line says:[CODE]source = urllib.urlopen(url).read()[/CODE]where … | |
Re: Hi eleonora, It's also easy to use the pickle module. Pickle dumps objects into a data file and reloads them without you having to worry about parsing text. So, if you have a variable named "board" and you want to dump it to a file called "board.dat" you can just … | |
Re: Hi eleonora, I am assuming that this is like a big game of tic-tac-toe, where the first player to get four in a row wins. You need to decide three things first: (i) are you going to do this using just the command prompt or are you going to use … | |
Re: Hi bumsfeld, I believe that the array data structure in the Numeric/NumPy package is both typed and non-resizable, just like C arrays (actually, I'm pretty sure that these data structures [I]are[/I] C arrays wearing a Python hat). Check out: [URL=http://numeric.scipy.org]http://numeric.scipy.org[/URL] | |
Re: Remotely connecting to other systems by invoking external bash commands is cumbersome, isn't it? You'd be better served by learning how to use the paramiko package, which lets you do the same thing but from within Python: [URL="http://www.lag.net/paramiko"]http://www.lag.net/paramiko[/URL] Check out the instructions that come with the package for short demos … | |
Re: Hi jrcagle, I would have said to give the ghost a memory of his last choice, then exclude the opposite of that choice as a possibility when deciding which way to go in the present decision. In other words, if the ghost just decided to move east, don't let him … | |
Re: Hi mattyd, Does that image represent a pygame game wrapped inside a Tkinter widget? Is that what you're trying to do? Running pygame and Tkinter together can be tricky, as both open new threads that listen for user input, and threading bugs are always the hardest to squash. Without seeing … | |
Re: Hi jimmypk, It might be easier if you maintained a 'parent' field in each Node, then used this to forcibly unlink subtrees from parent Nodes. In that case, deleting a Node would be similar to deleting a Node in a linked list - set the parent's child field to the … |
The End.