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.
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 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);
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 wondering what I should do for the purpose of my project.
I am gonna operate on about 100 000 rows, every time.
what I wanted to do is to create an object "{}" and then, if I need to search for a value, just call it , for example
data['2018']['09']['Marketing']['AccountName']
the second option is to pull everyting into an array "[]" and in case I need to pull value, I will create a function to go through the array and sum numbers for specific parameters.
But don't know which method is faster.
Will be thankful if you can shed some light on this
Thanks in advance,
If performance (speed) is an issue, Python might not be the ideal choice...
Otherwise:
Might I suggest the use of a proper database, such as SQLLite (which comes shipped with Python).
And maybe SQLAlchemy as an abstraction layer. (https://docs.sqlalchemy.org/en/latest/orm/tutorial.html)
After all, they were made exactly for this kind of tasks.
If that seems overkill: Have a look at Pandas.
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
I want to get < from a string that contains the character.
So what I want to do is:
magic_function('<') = <
in order to make. For example:
1 magic_function('<') 3
I expect, of course, that the program returns True
This isn’t possible with specifically what you want. A function returns an object, not an operator. I’m not sure what your main intention is here but you might want to rethink your architecture.
What you can do (but what I don’t recommend) is using eval() like so:
eval(“1 < 3”)
But there are many resources online about why eval is “evil”: to sum them up, if you don’t know what your data source is, then you could be performing unexpected operations which you might not want.
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
I'm studying a-level computer science and my text shows array pseudocode declarations, some starting at 1 and others 0. Can anyone tell me why this is the case. Please note that I am studying Python.
DECLARE List1 : ARRAY[1:3] OF STRING // 3 elements in this list
DECLARE List2 : ARRAY[0:5] OF INTEGER // 6 elements in this list
DECLARE List3 : ARRAY[1:100] OF INTEGER // 100 elements in this list
DECLARE List4 : ARRAY[0:25] OF STRING // 26 elements in this list
There are languages that use either or both, and some algorithms are easier to express with one or the other. For instance, a textbook heap uses 1-based indexing. C and Python use 0 based indexing, Pascal and Ada let you choose, Lua and Matlab use 1 based. In practical terms, you mostly need to be aware which is used in the language you write.
https://en.wikipedia.org/wiki/Comparison_of_programming_languages_(array) lists the conventions used in a few languages. One bit of trivia that's not in there is the C way to find the number of elements in an array: sizeof(array)/sizeof(array[0]). It's rarely used because C's calling conventions strip size information by demoting arrays to pointers, anyway.
In languages like for example Pascal you actually decide what is the lower and upper bound for indexing. This is the notation the text is using.
In Python and most other programming languages instead the first element is always at index 0 and when declaring an array you only say what is the number N of elements.
In a few badly designed programming languages the first element is instead at index 1.
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)