Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 9 years ago.
Improve this question
Perhaps this is not the correct place to ask this question, and part of me thinks that there is no real answer to it, but I'm interested to see what experienced Python users have to say on the subject:
For maximum readability, concision, and utility, what is a range for an optimal length of a Python function? (Assuming that this function will be used in combination with other functions to do something useful.)
I recognize that this is incredibly dependent on the task at hand, but as a Sophomore Comp. Sci. major, one of the most consistent instructions from professors is to write programs that are comprised of short functions so as to break them up into "simple", discrete tasks.
I've done a big of digging, including through the Python style guide, but I haven't come up with a good answer. If there are any experienced Python users that would like to weigh in on this subject, I would appreciate the insight. Thanks.
I'm sure a lot of people have strong opinions about this, but for new programmers a good rule of thumb is to try and keep it below 10-20 lines. A better rule of thumb is that a function should do one thing and do that one thing well. If it becomes really long, it is likely doing more than one thing and can be broken down into several functions.
Related
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 1 year ago.
Improve this question
I'm writing a z3 python program that is running a bit slow. The reason why it is running slow is because there is one part of the program where I am adding many harder constraints in nested for loops. My instructor told us that adding equality constraints would make the program run faster in terms of limiting the possibilities the program goes through when the line Solver().check() is reached.
I'm wondering whether I should be adding the equality constraints before the "harder" constraints to make it go faster, or whether the equality constraints should go after the "harder" constraints?
I would want the equality constraints to be checked first to limit possibilities of the harder constraints, so I would assume s.add(x == y), or something should be added first so that it is checked first?
These sorts of questions come often; and the honest answer is that the performance of the solver on any given question relies on many factors. Changing the order of constraints should in general have no effect, but in practice it usually does. (See https://github.com/Z3Prover/z3/issues/5559 as an example.) Even renaming variables (something you'd think that'd have no effect) can change performance. (See here, for instance: https://github.com/Z3Prover/z3/issues/5147)
If you're having performance problems, it's best to address it as a modeling issue: i.e., how can you formulate your problem so it's "easier" to solve; instead of thinking about how to "reorder" the constraints, which is a never-ending guessing game. I suggest you actually post your actual encoding and ask about specific advice regarding that problem. Your question is unanswerable in the sense that there's no single strategy that will work well in all cases.
Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 2 years ago.
Improve this question
Although this is a question of maths but I really wanted to figure out if something like this is possible or not by coding it .(preferably in C). I had the question posted in mathsstackexchange where I saw that there's a way to find out the cosets of D_12 in S_6 by using Python programming. Can someone just help me to figure out how are we doing this?
S_n is the group formed by all possible permutations of n-elements and D_2n is formed by the generators <r, s> where r =(123, n) and s=(1n)(2 n-1).
Here's the link to the answer
https://math.stackexchange.com/questions/3880306/find-the-cosets-of-d-2n-in-s-n
Also I am not accustomed to posting questions in stackoverflow, I really don't know how to add mathematical symbols.
Though the logic remains same, the implementation will vary from Python to c due to difference is data types and so on. So you should try learning Python, it’s very easy to learn and you can pick up writing code in Python within few days considering you already you know a few other languages. And writing such complex programs can be easy in Python due to the vast inbuilt libraries and readability. So it’s better if you learn Python and start implementing this in Python.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
I have been working on a program to solve a Rubik's cube, and I found myself unsure of a couple of things. Firstly, if I have a bunch of functions that have general applications (e.g., clearing the screen), and don't know whether or not I should change them to methods inside of a class. Part of my code is OOP and the other part is procedural. Does this violate PEP8/is it a bad practice? And I guess in broader terms, is it a bad idea to mix two styles of programming (OOP, functional, procedural, etc)?
I would say it's not bad at all, if the problem can be solved more easily that way and that your language supports it.
Programming paradigms are just different approaches to solving problems:
Procedural says "What are the steps that need to be performed to solve this problem?"
Functional says "What values should be transformed and how should they be transformed to solve this problem?"
OOP says "What objects need to interact with one another and what messages are needed to be sent between them to solve this problem?"
When you divide up your problem into smaller ones, you might find that some parts of the problem can be solved more easily in a functional way, other parts a procedural way. It's totally possible to have such a problem.
Python is mainly procedural and quite OOP, and has functional features (namely functools). It's not as functional as Haskell and not as OOP as C#, but it allows you to use those paradigms to some extent.
Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 6 years ago.
Improve this question
I learnt about DRY code and
Flat is better than nested
I also know that excessively DRY code is bad and I am not "religiously" DRY or anything. But which of these would be better? (I can't make out as the creator what might be less readable)
"""
Basically the variable "answer" is overwritten to equal "number" "+" or "-"
or "*" or "/" or "**" the existing value of answer. Here, operation is a
dictionary; and a value (such as to add, subtract etc.) is accessed based on
symbol the user has inputted (such as "+", "-" etc.)
"""
#Type 1:
number = float(input("Enter number: "))
answer = operation[symbol](answer, number)
#Type 2:
answer = operation[symbol](answer, (float(input("Enter number: "))))
Its greate that you think about software quality right from the start. The best answer is not always very clear, and especially if you consider a decision on a very low level of a single instruction call, as you do now.
I would vote for Type1, but Type2 is also entirely OK. This is dependent on person's habits that develop over time.
I think you can go with any of them for now, but still consider best practices and design patterns when thinking about the overall shape of your program. The goal is for software to be easily maintainable and (in case you create a library) easy to use by other programmers.
This example is not so complicated, so type 2 would be Okay (but meh) for me, but overall I would go for type 1 because I think it is more readable. Not just for me, but also for everyone else who will be working with my code later. I don't like the multiple actions in one line, I think it makes things unnecessarily complicated when you (or someone else) come back to it later
Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I am interested in learning the basics of scientific computing, but sadly in my daily life I don't encounter problems or situations in which I get to code something to solve a mathematical problem (Or any problem whatsoever, other than the basic programming things we are asked to do).
I know the basics of python and I'm not worried about it since I can always learn what I need on the go, but I need a resource (A book, website or mailing list will do) with problems and exercises I can use to start working with the kind of situations I could encounter if I decide to work in this in the future.
I've looked around but I haven't seen anything aimed for people knowing programming but not knowing the specific details of scientific computing, just advanced things I don't understand at the moment.
When I first started learning to program I used Project Euler. It is a series of challenging mathematical/computer programming problems that will require logical and mathematical insights to solve. Although mathematics will help you arrive at elegant and efficient methods, the use of a computer and programming skills will be required to solve most problems. The problems themselves are interesting, and they gradually increase in difficulty.
Be careful though - it can become quite addictive!
Here's the link: https://projecteuler.net
You might try [About Project Euler] (http://projecteuler.net/) and Problems
The problem are interesting and can be done using different languages. Since the problems have different complexities, and the different languages may allow better (or not as good) programming solutions, this would seem to be a good site to study.