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.
Related
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.
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.
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 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 know that the last element of a list say li can be accessed by li[-1] but how exactly does it work in the background? Is it same as li[len(li) - 1]
Is there any other way of getting the last element of a list without actually knowing the length of the list? I am looking at this question in terms of efficiency so please suggest any other alternate solutions with less complexity if it exists.
You probably want to read this:
https://docs.python.org/2/faq/design.html#how-are-lists-implemented-in-cpython
It said,
CPython’s lists are really variable-length arrays, not Lisp-style
linked lists. The implementation uses a contiguous array of references
to other objects, and keeps a pointer to this array and the array’s
length in a list head structure.
That's why li[-1] is recommended and most efficient.
li[-1] is the fastest and more Pythonic way to do it.
You can do li[-n] to get the nth element starting from the end, it is just an access by index and it is in fact faster than li[len(li) - 1].
If you have a list in python the length is given so li[1] works the same way as li[-1].
From docs
The formal syntax makes no special provision for negative indices in
sequences; however, built-in sequences all provide a getitem()
method that interprets negative indices by adding the length of the
sequence to the index (so that x[-1] selects the last item of x).
So yes, internally it DOES work as -1 + len(li)
However, here is the kicker. len is a constant time/O(1) operation, and does not depend on the length of the list. So, negative indexing is really efficient, and really IS the right way you should be accessing elements. That is why it is there after all.
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 8 years ago.
Improve this question
What are some of the most basic things that a programmer who is used to coding in MATLAB needs to be cautious about, when beginning to code in Python? I am not asking for an entire list of differences between MATLAB and Python, just a few very basic things that can save a beginner a few hours of debugging.
An example would be how someone used to MATLAB's 1:N may make a mistake in python by using range(1,N)
Differences in concepts would be more helpful than absolute differences in specific commands.
The NumPy site has an excellent list, which I won't reproduce in full here. NumPy provides basically all the basic functionality of MATLAB for Python users, so it is highly recommended if you're coming to Python from MATLAB.
Link
Some highlights:
INDEXING: MATLAB® uses one based indexing, so the initial element of a sequence has index 1. Python uses zero based indexing, so the initial element of a sequence has index 0. Confusion and flamewars arise because each has advantages and disadvantages. One based indexing is consistent with common human language usage, where the "first" element of a sequence has index 1. Zero based indexing simplifies indexing. See also a text by prof.dr. Edsger W. Dijkstra.
LOGICOPS: & or | in Numpy is bitwise AND/OR, while in Matlab & and | are logical AND/OR. The difference should be clear to anyone with significant programming experience. The two can appear to work the same, but there are important differences. If you would have used Matlab's & or | operators, you should use the Numpy ufuncs logical_and/logical_or.