Questions tagged [python]

Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability. Use the python tag for all Python related questions. If you believe your question may be even more specific, you can include a version specific tag such as python-3.x.

Filter by
Sorted by
Tagged with
2
votes
1answer
162 views

Shifting Paths problem (Code Jam)

I was trying, just for fun, to solve http://code.google.com/codejam/contest/dashboard?c=2075486#s=p4 I can easily solve the small set, but I struggle to solve the big one (my implementation would ...
3
votes
1answer
2k views

Baseball game state class in python looking for feedback

So far, this is just a class that tracks the state of a baseball game. Unfortunately, I can't tell you for sure what the next step is. It might be a an interactive playable baseball game, or a game ...
2
votes
1answer
137 views

Python: improve in elegant way (code saving) a function in order to avoid several statements

I wrote this function to read Las file and save a shapefile. The function creates a shapefile with 8 fields. What I wish insert a parse element in the function in order to select the fields I wish to ...
2
votes
2answers
70 views

Identifying equivalent lists

Title: Identifying equivalent lists I am using lists as keys, and therefore want to identify equivalent ones. For example, [0, 2, 2, 1, 1, 2] [4, 0, 0, 1, 1, 0] ...
5
votes
1answer
285 views

Sparse Bitarray Class in Python (or rather frequently having contiguous elements with the same value)

Sparse is probably the wrong word - this is for encoding arrays of booleans where contiguous values tend to be the same. It'd be great to know a proper name for this data structure so I could read ...
3
votes
1answer
95 views

Optimization of average calculation

I have a list of dictionaries, with keys 'a', 'n', 'o', 'u'. Is there a way to speed up this calculation, for instance with NumPy? There are tens of thousands of items in the list. The data is drawn ...
3
votes
1answer
511 views

Need an algorithm to optimize grouping python dictionaries in hierarchical form

Previously I asked a question on how to group dictionaries in a list in a hierarchical structure. Initially I wanted to group a list of dictionaries that looks like the following, using multiple keys:...
6
votes
1answer
281 views

How to improve my auto-reconnection Python script

I need some feedback here. Due to a weird issue on my server system, the network card disconnects from the network regularly. I'll fix this at a later date. But I wanted to know if the below script ...
3
votes
1answer
196 views

Python Optimizing/Speeding up the retrieval of values in a large string

I have a string containing around 1 million sets of float/int coords, i.e.: '5.06433685685, 0.32574574576, 2.3467345584, 1,,,' They are all in one large ...
5
votes
1answer
14k views

Using Python to model a single elimination tournament

I'm learning python and I want to model a single elimination tournament like they use in athletic events like tennis, basketball, etc. The idea would be to insert some meaningful metrics to determine ...
10
votes
1answer
952 views

Python __new__ and __init__ usage

I'm trying to write a Python module to handle matrices. (I know about numpy, this is just for fun) So far I have written a few classes, Matrix, ...
5
votes
3answers
901 views

Code review for python password generator

The following code is a simple random password generator that spits out a randomized password based on some input flags. ...
10
votes
4answers
630 views

Write a function to print numbers

I would like to write a python or c++ function to print numbers like the following examples (with n as input parameter): When n = 3, print: 1 2 3 8 0 4 7 6 5 ...
3
votes
3answers
4k views
3
votes
1answer
82 views

How to improve method of opening file in python then writing to another?

Very simple I want to edit a virtual host files and replace the tags I made with some actual data: ...
8
votes
2answers
888 views

LPTHW extra credit game

I quite like this extra credit assignment I did for Zed Shaw's Learn Python The Hard Way. Its a neat little text adventure game that uses stats. I would like to eventually expand this, but let me know ...
4
votes
1answer
3k views

How do I refine my code in Sikuli? Seems very long and redundant

I'm only just beginning in programming. The goal of the program is to check to make sure images are properly rendered within a program(within a VM) . Below is the code, and I am asking what I can ...
2
votes
2answers
525 views

creating a hash based sorting algorithm

For experimental and learning purposes. I was trying to create a sorting algorithm from a hash function that gives a value biased on alphabetical sequence of the string, it then would ideally place it ...
2
votes
2answers
201 views
6
votes
1answer
2k views

Intercept read access to a certain object attribute

I have a library that accepts a file-like object but assumes that it has a valid .name unless that name is None. This causes it ...
4
votes
1answer
3k views

Renaming Files and/or Directory

I'm quite new to Python and would love to gather feedback on my code. I've written a piece a script that allows me to search all file or directory names within a folder matching a particular pattern. ...
2
votes
2answers
924 views

A simple calculator to calculate the frequency of a recessive allele

It's based on the Hardy-Weinberg formula. I used the easygui module because I don't fully understand events in Tkinter yet. It's my first relatively useful script so any constructive criticism would ...
2
votes
1answer
420 views

OOP design of code representing units of measurement

I am developing a meta-data framework for monitoring systems such that I can easily create data adapters that can pull in data from different systems. One key component is the unit of measurement. To ...
2
votes
2answers
906 views

Python Code Review - Project Euler #12

I just figured out Project Euler problem #12. This the first one I've done in Python, so I'm putting my code out there to hopefully get some constructive criticism. If anyone is interested, would ...
11
votes
1answer
383 views

Help me make my Python code clearer

Here's my solution to Project Euler problem 40. ...
-1
votes
1answer
74 views

Is it a good idea to store GUI elements in a module on runtime? [closed]

I'm writing a program in wxpython, and to make GUI parts that are built in different modules I made a module called runtime, where all the GUI parts get stored on ...
5
votes
1answer
4k views

how to generate numbers for pagination?

I made a function that returns the number displayed for pagination. I want to get something like this (parentheses is the only show where the active site): if pages < 10 ...
2
votes
1answer
194 views

Aggregate by several keys

I have this rather verbose piece of code. How can I get it shorter and simpler? This function aggregate results of test by population and by different keys: ...
2
votes
3answers
6k views

pythonic longest repetition

I put together this code to for the Udacity cs101 exam. It passes the test, but I feel their must be a more elegant way to manage this problem. Some help from a module (like itertools). Question 8: ...
4
votes
1answer
211 views

Rapidly requesting a file from a server

Here is a Python script I wrote earlier for rapidly requesting a file from a server and timing each transaction. Can I improve this script? ...
2
votes
1answer
5k views

Working with sockets

Here is my code; ...
1
vote
2answers
5k views

Python: keeping track of info across classes by storing a variable to self

I wrote the following code to test a score-keeping class. The idea was to keep score of a game such that the score was kept across multiple classes and methods. I'm looking for any input on being ...
0
votes
1answer
118 views

Criticize my first attempt at using the itertools module

Here's the exercise: Given a string of X's and O's and a maximum number of swaps, determine the longest possible sequence of X's after swaps. Here's my solution using itertools. ...
1
vote
1answer
182 views

What specific optimizations can be made to this BitArray class, and how can they be implemented?

Here is the current code for the BitArray class needing optimization (implemented on big integers): ...
2
votes
2answers
185 views

Simple arithmetic in Python

As a beginner, I wrote the following python script that solves warrant 1 of this document - pp 436-438. My solution, although works, seems to me as poorly designed and highly unmaintanable. I was ...
2
votes
1answer
1k views

Random dungeon generator

I'm learning Python by converting a BASIC (BlitzPlus) program I wrote based on article at http://gamedeveloper.texterity.com/gamedeveloper/201002?pg=42#pg43 for random dungeon generation. I've ...
2
votes
1answer
563 views
2
votes
2answers
1k views

Python: flatten directories

Here is a simple Python script that I wrote with lots of help from the Python mailing list. I moves all the files of subdirectories to the top level directory. It works well for my use, but if there ...
3
votes
2answers
1k views

Is Event.wait necessary here? [closed]

When I read some code,I think the usage of Event is not necessary,is it right? ...
2
votes
2answers
179 views

Filtering logic

I have a FileAsset class. I have a set of 'filters' with which I determine if a FileAsset should be written to my database. ...
1
vote
1answer
148 views

Pythonic style guide [closed]

Which of these 3 python snippets is more pythonic? This 1 liner list comprehensions which is a little overcomplex ...
3
votes
1answer
521 views

Does this code use good Python practices for a context-free grammar implementation?

I'm completing exercises in Compilers: Principles, Techniques, and Tools (2nd Edition) in Python. The following code converts: ...
1
vote
1answer
82 views

Attributes with special behaviour in Python

What is the common practice to implement attributes that have side effects in Python? I wrote the following class which supposed to save singletons when a variable is going to get a certain value. The ...
3
votes
1answer
289 views

Isn't this dynamic programming?

I was solving problem 18 and 67 on Project Euler and came up with an algorithm that actually goes through almost all the possibilities to find the answer in O(n) time. However, ProjecEuler claims ...
2
votes
1answer
1k views

Median solving from Interview street in Python

Any suggestions to improve the performance of the following code? This code is exceeding the time limit for the last four cases. This is a solution to the problem https://www.interviewstreet.com/...
2
votes
1answer
182 views

How do I add memoization to this problem? [closed]

Problem Starting in the top left corner of a 22 grid, there are 6 routes (without backtracking) to the bottom right corner. How many routes are there through a 2020 grid? Solution Here is my ...
2
votes
2answers
3k views

Speed up two for-loops? [closed]

The code below is slow. Any thoughts on speeding up? ...
6
votes
1answer
25k views

Reading ints line from a file in Python

I have to list a integers from a text file separated by newlines into a python list. I ended up with the code above which works (for my case) but certainly is far form optimal. ...
3
votes
1answer
715 views

Speeding up an algorithm for finding the number of distinct lines

Is there a way to speed up the following python code: ...
6
votes
2answers
576 views

Euler 35 - python solution taking too long

Here is my solution for Project Euler 35. (Find the number of circular primes below 1,000,000. Circular meaning all rotations of the digits are prime, i.e. 197, <...