Python is an interpreted, general-purpose high-level programming language whose design philosophy emphasizes code readability.
1
vote
1answer
50 views
What parts of my Python code are adopting a poor style and why?
Here I have some simple code that worries me, look at the second to last line of code, I have to return spam in order to change spam in the global space. Also is it bad to be writing code in this ...
3
votes
4answers
114 views
Is there any downside to define a function that use one dictionary as an argument instead of several (+10) arguments?
I tried to refactoring someone else's code and I found that there is alot of this pattern
def train(a, b, c, d, e, f, g, h, i, j, k): #Real code has longer name and around 10-20 arguments
x = ...
2
votes
2answers
116 views
Pair Programming matrix: room for improvement?
At work, we have a "pair programming ladder" where you can keep track of who is pairing with whom (based on check-ins). The idea is to promote "promiscuous pairing" where each developer eventually ...
4
votes
2answers
126 views
Check_sudoku in Python
I have just started to learn programming with Python. I have been going through several online classes for the last month or two. Please bear with me if my questions are noobish.
One of the classes I ...
0
votes
3answers
84 views
How to shorten this lengthy Python code
I have been trying to make a simple "quiz" program on Python. So,what I plan to make is, say, a quiz of 3 rounds and each round having 3 questions. And at the end of the every round, the program will ...
12
votes
9answers
2k views
Asks the user to input 10 integers, and then prints the largest odd number
I have written a piece of python code in response to the following question and I need to know if it can be "tidied up" in any way. I am a beginner programmer and am starting a bachelor of computer ...
1
vote
1answer
97 views
Python code (repetitive in places)
I've developed the below Python code which can actually be run from the below URL so you can see the output:-
https://scraperwiki.com/scrapers/air_crash_data_1/edit/
The code does seem a bit ...
2
votes
2answers
142 views
Criticize my first Python module please
This is a Python module I have just finished writing which I plan to use at project Euler. Please let me know how I have done and what I could do to improve it.
# This constant is more or less an ...
2
votes
2answers
59 views
How should I present long string literals (URLs) in Python?
I have a web scraping application that contains long string literals for the URLs. What would be the best way to present them (keeping in mind that I would like to adhere to PEP-8.
URL = ...
2
votes
3answers
158 views
Scala inspired classes in Python?
I have to define a lot of values for a Python library, each of which will represent a statistical distribution (like the Normal distribution or Uniform distribution). They will contain describing ...
2
votes
1answer
71 views
Monitor filesystem for continuous integration and build
I've written a simple Python module that depends on watchdog to monitor for modified files, thens runs various integration and build processes.
I'm fairly new to Python, so I'd appreciate all ...
3
votes
1answer
95 views
Python/Django Script - Is it crap? Objects duplicated or passed?
I've written a script using the Django ORM to store 'configuration settings' and interacting with the Google Drive API and another API. The script creates a base project folder and subfolders in ...
3
votes
4answers
146 views
Can this string parse be improved?
My data is in this format:
龍舟 龙舟 [long2 zhou1] /dragon boat/imperial boat/\n
And I want to return:
('龍舟', '龙舟', 'long2 zhou1', '/dragon boat/imperial boat/')
In C I could do this in one line with ...
2
votes
1answer
115 views
Python 3: Finding common patterns in pairs of strings
I have written a piece of code that finds common patterns in two strings. These patterns have to be in the same order, so for example "I am a person" and "A person I am" would only match "person". The ...
0
votes
1answer
48 views
Instance of one class to contain arbitrary number of instances of another class in Python
I'm trying to see if there is a better way to design this. I have a class Animal that is inherited by Male and Female classes (Female class has an additional attribute). I also have a class called ...
2
votes
1answer
73 views
Largest Prime Factor
I'm trying to learn Python by working my way through problems on the Project Euler website. I managed to solve problem #3, which is
What is the largest prime factor of the number 600851475143 ?
...
1
vote
3answers
105 views
What is wrong with my Genetic Algorithm?
I am trying to understand how genetic algorithms work. As with everything, I learn by attempting to write something on my own;however, my knowledge is very limited and I am not sure if I am doing this ...
6
votes
2answers
235 views
Python terminal animation, better way than this?
Is there a better way of doing this terminal animation without having a load of if ... elif ...?
import sys, time
count = 100
i = 0
num = 0
def animation():
global num
if num == 0:
...
3
votes
3answers
158 views
Advice on program which calculates scores for predictions of Football scores
I am writing a program which calculates the scores for participants of a small "Football Score Prediction" game.
rules are:
if the match result(win/loss/draw) is predicted correctly: 1 point
if the ...
1
vote
1answer
43 views
Alternative to using sleep() to avoid a race condition in PyQt
I have a situation where I would like to use a single QThread to run two (or more) separate methods at different times. For example, I would like the QThread to run play() sometimes, and when I am ...
4
votes
2answers
207 views
Any better way to solve Project Euler problem # 5
So, here's my attempt on Problem # 5 of Project Euler, which is looking quite clumpsy when seen first time. Is there any better way to solve this? Or any built-in library that already does some part ...
1
vote
2answers
54 views
Looking for efficient python program for this following python script
I am looking for a memory efficient python script for the following script. The following script works well for smaller dimension but the dimension of the matrix is 5000X5000 in my actual calculation. ...
0
votes
1answer
34 views
Figuring out the structure of a shape tuple
I have some code that uses a multidimensional look-up table (LUT) to do interpolation. The typical application is a color space conversion, where 3D inputs (RGB) are converted to 4D (CMYK), but the ...
1
vote
2answers
90 views
Consolidate list of ranges that overlap in Python
I wanted to implemented an algorithm in Python 2.4 (so the odd construction for the conditional assignment) where given a list of ranges, the function returns another list with all ranges that overlap ...
6
votes
1answer
217 views
This Python script gets the job done, but what about it is not 'Pythonic'?
I get a lot done with Python scripts but always feel like I'm working with a messy desk when I'm using it. I assume this is because I am not coding in Python correctly. Coming from a C# and Ruby ...
-1
votes
2answers
64 views
Python: How do I add a “Final Grade” converter in my Grading System Calculator [closed]
I kind of have an idea what to do, but I can't figure it out. The program needs to be able to get the students name and three test scores, then get the average score (percentage) out of the three ...
2
votes
1answer
84 views
Python - Any suggestions for what i have so far
Alright, i put this together and it seems to work.
1channel.py -breakingbad 1
The Output (1-10):
(1) - Currently 3.80/5
(2) - Currently 3.50/5
(1 opens the link in browser)
if anyone has any ...
2
votes
1answer
87 views
Code review needed for my Prime factorization code
Below is the code for knowing all the prime factors of a number. I think there must be some better way of doing the same thing. Also, please tell me if there are some flaws in my code.
def ...
2
votes
3answers
39 views
Flexible multiple string comparision to determine variable value
I've a web form that allows users to create clusters of three sizes: small, medium and large. The form is sending a string of small, medium or large to a message queue, were job dispatcher determines ...
1
vote
1answer
27 views
Refactoring same code across 2 classes in python
I have two classes with similar first checking codes, but different behaviors. I supose there is a way to refactor the code, so I dont need to retype it every time.
It is possible to refactor this, ...
1
vote
3answers
70 views
How to improve this functional python fast exponentiation routine?
I have the following python functions for exponentiation by squaring :
def rep_square(b,exp):
return reduce(lambda sq,i: sq + [sq[-1]*sq[-1]],xrange(len(radix(exp,2))),[b])
def ...
1
vote
1answer
32 views
How to improve this functional python trial division routine?
The following functional program factors an integer by trial division. I am not interested in improving the efficiency (but not in decreasing it either), I am interested how it can be made better or ...
4
votes
1answer
67 views
Nicely tabulate a list of strings for printing
I have a list of strings of variable lengths, and I want to pretty-print them so they are lining up in columns. I have the following code, which works as I want it to currently, but I feel it is a ...
1
vote
1answer
26 views
Most concise Python radix function using functional constructions?
The problem with computing a radix using a non-imperative style of programming is you need to calculate a list of successive quotients, each new entry being the quotient of the last entry divided by ...
2
votes
1answer
38 views
Improving my code of Margrabe Formula in Python
I am new to Python and I want to see how can I improve my code to make it faster and cleaner. Below you can see the code of Margrabe's Formula for pricing Exchange Options.
I think the part where I ...
2
votes
1answer
117 views
Iterating X times (where X > Y) over Y element dictionary - is there a nicer way of doing this?
I need to write a procedure, that will take a dictionary consisting of server names and their available slots and a number of slots required, and redispatch it over the servers, so that every server ...
2
votes
7answers
368 views
What could I do differently to slim down my Python code?
Could I code differently to slim down the point of this Python source code? The point of the program is too get the users total amount and add it too the shipping cost. The shipping cost is determined ...
1
vote
1answer
97 views
How to make function more pythonic?
Is there a way to accomplish this function in less lines of code? I am learning python and want to make sure I am baking in good habits...I repeat myself twice which I don't like and it seems ...
3
votes
2answers
62 views
Refactorize my python code for GET and POST
I'm newbe with Python and GAE, but years of procedural programming. I have this code, but I know that there must be some better solution to avoid retyping code.
The first and last section of code are ...
2
votes
1answer
97 views
How to make BWT inverse faster, and how to understand why the optimizations work?
I think understanding basic data compression and pattern finding / entropy reduction primitives is really important. Mostly the basic algorithms (before the many tweaks and optimizations are made) are ...
1
vote
2answers
120 views
Flatten dictionary in Python (functional style)
I'm trying to learn how to write functional code with Python and have found some tutorials online. Please note that I know Python is not a promoter for functional programming. I just want to try it ...
2
votes
3answers
86 views
Python any way to convert this carry-add loop to a map or reduce one liner?
I want to perform standard additive carry on a vector. The base is a power of 2, so we can swap modulus for bitwise AND. In
def carry(z,direction='left',word_size=8):
v = z[:]
mod = ...
2
votes
1answer
99 views
Evaluating longest path
Here is a program which keeps track of the longest path in a graph. I think it can be written much better.
from Tkinter import *
'''
This program tries to compute the longest path (largest number of
...
3
votes
3answers
123 views
Python how to make this reduce() fibonacci generator better?
Recently discoverer how cool reduce could be. Want to do this:
>>> a = [1, 1] + [0] * 11
>>> count = 1
>>> def fib(x,n):
... global count
... r = x + n
... if ...
3
votes
1answer
67 views
Python Reverse the binary equivalent of input and output the integer equivalent of the reverse binary
Am supposed to capture user input as an integer, convert to a binary, reverse the binary equivalent and convert it to an integer.Am getting the right output but someone says the solution is wrong. ...
3
votes
1answer
247 views
The Observer design pattern in Python in a more pythonic way (plus unit testing best practices)
I'm continuing to work on the Head First Design Patterns book in an effort to become a more efficient and better Python programmer. Code review for the Strategy pattern in Chapter 1 is here with ...
3
votes
2answers
465 views
How can I improve the performance of this code?
I was solving the Find the Min problem on facebook hackercup.
The code below works fine for the sample inputs given there, but for input size as big as 10^9 this takes hours to return the solution.
...
2
votes
1answer
48 views
Readability: literal dictionary lookup vs if-else
I'm having concerns about readability of this piece of code:
messages_dict = {'error':errors, 'warning':warnings}[severity]
messages_dict[field_key] = message
and I'm to use this instead:
if ...
2
votes
3answers
142 views
A pythonic way of de-interleaving a list (i.e. data from a generator), into multiple lists
I've recently discovered the wonders of the Python world, and am quickly learning. Coming from Windows/C#/.NET, I find it refreshing working in Python on Linux. A day you've learned something new is ...
3
votes
3answers
95 views
How can this function be faster? Solving for a row of Pascal's triangle
I saw a posting on Hacker News this morning that was ranting about people not being able to solve an interview question. I thought I would give it a shot, but I would like to know how my attempt could ...



