Equivalent of Python in C++ operator [closed] - python

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 3 years ago.
Improve this question
Is there a way to do the same in C++?
Python
int.__add__(1, 1)
I can make something in C++, but it is still ugly and not very optimised and short.
I just want this : 1+1, but without + .
I absolutely want to use the int "class".
It's for matrix multiplication, addition, and subtraction...
I just want to have better-looking and shorter code.

I can make something in C++, but it is still ugly and not very optimised and short.
Last time I checked, int.__add__(1, 1) was way longer than 1+1. Just about anything is longer than 1+1. As for optimisations, you are not in a position to talk about what is more optimised, having not measured anything.
It's for matrix multiplication, addition, and subtraction
The same integer + operator is useful in lots of contexts. Matrix operations are among them. There is no need to single them out.
I just want this : 1+1, but without +
What you want is of secondary importance. Programming is a team sport. You do what everyone else does. Not only because it is likely to be tried and tested and the best thing after the sliced bread, but also because if everyone is doing something in a particular way and you are doing it differently, others will find it hard to understand what you mean.
This is not to say you cannot break conventions and introduce innovations. People do it all the time. But they are not asking anyone how to! If you need to ask, you are not in a position to lead the crowd.
I just want to have better-looking and shorter code.
Then absolutely positively use +. A no-brainer.

You want to use a function (or functor) for addition instead of the + operator. C++ has std::plus for that. I don't use C++ but I think one or both of these should work:
int a = std::plus(1, 1);
int a = std::plus<int>(1, 1);

Related

python shift operator uses [closed]

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 months ago.
Improve this question
Im just learning about pythons bitwise operator << and >>. as far as I see, it takes the binary version of an integer and shifts it n places left or right. that would mean that saying x<<y is equivalent to x*(2**y)
so my question is why is there an operator for this? as far as I know python doesnt like to give you more than 1 way of doing things to avoid confusion. is there a reason this operator is particularly useful or typical scenerios where its used? I know this is a pretty open ended question but when searching for this I only come across what this operator does, not why we would use it. thankyou in advance
The key is in your remark "it takes the binary version of an integer and shifts it n places left or right".
Ask yourself this: how does your computer represent integers at all? What are integers? Any integer is a sequence of bits, (typically a multiple of 8 bits, i.e. a byte) and your computer is built around memory positions, registers, addresses, etc. that hold these integer values.
So, it makes sense for a CPU to have an operation to shift such a value left or right by one bit, for an extremely fast multiplication or division by 2, more so since powers of two are very commonly needed because everything in your computer is binary.
Other operations can be composed from simple addition, subtraction, shift by n, etc. - Python exposes this operation to give you access to this very basic and quick operation, although Python integers aren't always (or even all that often) the same efficient integers you operate on directly in many other languages.
But bit-shifting has many applications, and as a standard operation of your computer, it only makes sense that Python would give you access to a tool that programmers are very used to, and have applications for in many common algorithms.

Pseudocode Python Concepts [closed]

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 3 years ago.
Improve this question
I know that pseudocode by its very nature is based more on natural language syntax and principles than anything else, but it is supposed to represent coding concepts. I am still a beginner , so I don't understand all of the concepts.
int string (char s[1..m], char t[1..n])
// d is a table with m+1 rows and n+1 columns
declare int d[0..m, 0..n]
I understand this except for the last line. Could somebody explain to this to me in python ?
This is declaring a 2D array of size mxn. With statically typed languages (and even with pseudocode), it's necessary/useful to state what the variable is before using it. If you've used C for example, we define variables before using them. With dynamic languages like Python, this is not necessary and you won't see it.
I guess the closest thing in Python would be like creating and empty list of lists holding integers and assigning it to d.

Can you generate new asm files based on old ones? And if so what is the most efficent way? [closed]

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 4 years ago.
Improve this question
Let's say I have a lot of .asm files on a python program (It can also be binary strings, hex strings or whatever you would like). How can I use those files to generate new files that function roughly the same (It's for an assembly game).
The thing is I have a lot of assembly players that were really good at the game and I wondered if I can somehow use natural selection to breed better assembly bots.
This sounds a lot like superoptimization (wikipedia).
e.g. STOKE starts with a sequence of asm instructions and stochastically modifies it looking for shorter / faster sequences that do the same thing.
(Or STOKE can start from scratch looking for an asm sequence that gives the desired result for a set of test-cases.)
It's open source, so have a look at the algorithms they use to modify asm and test-run the code. Of course it's possible if you have data structures that represent operands and opcodes.
See also Applying Genetic Programming to Bytecode and
Assembly, an academic paper from 2014.
I haven't read it, but hopefully it addresses ways to recombine code from different mutations and maybe get something useful more often than you get garbage that steps on the registers from the other code. (That's the major trick with random changes to code, especially in assembly where there are lots of non-useful instruction sequences.)

realization of a syntactic parser under python [closed]

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 4 years ago.
Improve this question
I am working on a python project, I have to realize a syntaxic parser complex numbers, I need to make a calculator for arithmetic operations. I want to know how to get started, can you help me please ?
You might get away with very little if you can accept individual operands one at a time. As you may well know, Python accepts 1+2j as a complex number, 5.36j as an imaginary number. Thus, most parsers are overkill for parsing single complex numbers, if you ask the user to input numbers in the conventional way (ie, 10.2+i6.57). Just do a little string processing to extract the real and imaginary parts and use the complex function.
I suggest:
Tk for the GUI
pyparsing, if you really want a parser but it's probably unnecessary
Python itself for doing the arithmetic
Possible exchange with user:
>>> x = 2+8j
>>> y = 3-7j
>>> x/y
(-0.8620689655172415+0.6551724137931035j)
>>> x*y
(62+10j)

Why does python use identation instead of braces and keywords? [closed]

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
One thing I was never able to wrap my head around is why python uses indentation unlike other scripting languages. Being dependent on indentation makes editing python codes sometimes very frustrating.
ps. I have a strong feeling that this question gets closed for not being constructive. As much as I think the answer would be very interesting to know.
I think the answer you want is well written in the doc, and I can't summarize this better than by quoting the text:
Why does Python use indentation for grouping of statements?
Guido van Rossum believes that using indentation for grouping is extremely elegant and contributes a lot to the clarity of the average Python program. Most people learn to love this feature after a while.
Since there are no begin/end brackets there cannot be a disagreement between grouping perceived by the parser and the human reader. Occasionally C programmers will encounter a fragment of code like this:
if (x <= y)
x++;
y--;
z++;
Only the x++ statement is executed if the condition is true, but the indentation leads you to believe otherwise. Even experienced C programmers will sometimes stare at it a long time wondering why y is being decremented even for x > y.
Because there are no begin/end brackets, Python is much less prone to coding-style conflicts. In C there are many different ways to place the braces. If you’re used to reading and writing code that uses one style, you will feel at least slightly uneasy when reading (or being required to write) another style.
Many coding styles place begin/end brackets on a line by themselves. This makes programs considerably longer and wastes valuable screen space, making it harder to get a good overview of a program. Ideally, a function should fit on one screen (say, 20-30 lines). 20 lines of Python can do a lot more work than 20 lines of C. This is not solely due to the lack of begin/end brackets – the lack of declarations and the high-level data types are also responsible – but the indentation-based syntax certainly helps.

Categories