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 5 years ago.
Improve this question
While programming in python 3.6, I came across with some problem in list :
l = [2,2,3,3,3]
for n in range(len(l)):
print(l[n+1]) #increasing value of n by 1
This code will obviously give an error :
IndexError: list index out of range
But in case of c language :
#include<stdio.h>
int main() {
int i =0;
int arr[5] = {1,2,3,4,5};
for(i = 0 ; i<=4;i++){
printf("%d " , arr[i+1]);
}
return(0);
}
Gives the correct output(including garbage value):
2 3 4 5 32764
I wanted to know why are we not getting the value of elements of list in python by using the future values of n whereas in c it is just reverse?
In C, an "array" is mainly a pointer to a memory location (nb: this is an obvious simplification, you can read this for more details), and arr[x] will return N bytes from address_of_arr + (N * x) (where N is the size of the array data type). Like all memory access in C, there's absolutely NO check so you can read (and write) just any arbitrary memory location.
Note that, as Daniel Pryden mentions in a comment, the behaviour you observe with your C example is called an "undefined behaviour", which means the result is not defined by the C langage specs, depends on the implementation (your C compiler, platform and whatnots), and could actually yield just any result (including a core dump, erasing your hard drive or launching a nuclear missile). Luckily the implementation you tested it with just returns garbage. Now if you really feel lucky, for a bit of fun try writing at this memory location and find out what happens ;)
In Python, a list is an object which knows it's length and won't let you try to access items at indexes that don't exist. Where and how the effective list contents are stored in memory is handled by the implementation and is totally opaque to the Python code.
Disclaimer : this answer is based on C implementations most commonly found on today's operating systems - the C language spec doesn't specify anything about how an array should be implemented (just how it's supposed to work) so an implementation could as well choose to store array datas on clay tablets disposed on a rubber band and move that rubber band by N positions left or right, in which case my answer would be totally irrelevant...
Related
Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 2 months ago.
Improve this question
The question I was given requires me to get an output within 3 secs
The constraint given is 2<= len(a) <= 5000000 and 1 <= a[i] <= 10**9
Below is the code I have written. I belive that the Big O efficiency of this code is linear or O(n) as I am only using one for loop. Is that the case? If can it run the max input size within 3 secs?
line = input()
m = line.split()
m.sort()
smallest_diff = 10 ** 9
for i in range(len(m)-1):
diff = abs(int(m[i]) - int(m[i+1]))
if diff < smallest_diff:
smallest_diff = diff
else:
smallest_diff = smallest_diff
if i+1 > len(m):
break
print(smallest_diff)
Your runtime complexity is currently O(N log(N)), dominated by the sort call. You're correct that the main work loop takes only O(N) time. However, the input size of 5*10^6 will be close to the borderline for what Python can get done in 3 seconds; you
I actually suspect your input loading may be eating up a lot of time. That's not an asymptotics issue; input is just often pretty slow. Here are things I'd try:
Check whether the website you're submitting to allows you to use PyPy instead of regular Python. You could still submit the same code either way, but it'll probably run a lot faster in PyPy.
Replace your first 2 lines with m = map(int, input().split()). This stores a list of int instead of string. ints make your code less memory-intensive and ints are what you want to use anyway. There are some more advanced tricks you could try to make the input loading steps even faster, like using BytesIO, but I'm just mentioning for fun; you probably don't want to get into that right now.
You currently have a few other unnecessary lines of code that slow your program down slightly, but they won't affect the runtime much.
Bonus note: I don't know the exact problem you're trying to solve, but your code probably has a bug where you sort the data. Your m is a list of strings, not ints, so for example your code would consider this array to be already sorted: ['10', '3'] (because the string '10' comes before the string '3'). This bug would be fixed as a side effect of my suggestion (2) above, since if you store the data as ints then you'd instead get [3, 10] which I'm pretty sure is what you're intending.
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'm working on something in python and I have several variables that are calculated by very long formulas. I've done some searching on this site, and I found that python allows for implicit line breaking, which would be a solution for me as I can stretch the formula over multiple lines:
line-breaking
Now currently I have been working in a different way. Let me give you an example, if A would be given by the following formula:
A = b + c + d + e
I would be coding it as:
A = b + c
A += d + e
I was wondering what would be the preferred method. Is there a computational advantage to use implicit line-breaking over my current method?
We'd need more context to be sure, but here are some example cases.
For strings, as an implementation detail of CPython, the explicit split would be better (sometimes much better), because there is an optimization for an add (a = a + b) or inplace add (a += b) where the add/iadd instruction itself is immediately followed by a store to the left-hand side of the add. That said, for strings, you'll get more reliable performance regardless of operand size or the specific Python interpreter by doing A = ''.join((b, c, d, e)) rather than repeated concatenation.
For ints, floats and complex types, the single line approach is going to be ever so slightly more performant, solely because it avoids an unnecessary store and load instruction, but the difference is trivial; they're all immutable, scalar values, so no in-place manipulation of values occurs regardless, you'll have the same number of temporaries created during processing, the only question is whether they get (briefly) bound to a local name or not.
Frankly, if it's not a matter of concatenating strings or sequences (the former should be done with ''.join, the latter with itertools.chain to get O(n) performance), the preferred approach is not line continuation characters (which can easily get messed up by invisible trailing white space) but simple parentheses for grouping. If the real names of A = b + c + d + e were too long to fit on a line, my preferred approach would be:
A = (b + c +
d + e)
PEP8 prefers to break lines before the next operator, not after, so strictly PEP8 code would be:
A = (b + c
+ d + e)
This is the approach supported by PEP8 (the Python style guide), which says:
The preferred way of wrapping long lines is by using Python's implied line continuation inside parentheses, brackets and braces. Long lines can be broken over multiple lines by wrapping expressions in parentheses. These should be used in preference to using a backslash for line continuation.
The only real case for using backslashes instead is when parentheses aren't a legal part of the grammar (the link includes an example for with statements).
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 6 years ago.
Improve this question
For a particular reason I am trying to find (not exhaustively) the worst hash algorithm. I found this post analyzing some, and I targeted the Loselose algorithm. This page states the algorithm in C (I think) as:
unsigned long hash(unsigned char *str)
{
unsigned int hash = 0;
int c;
while (c = *str++)
hash += c;
return hash;
}
I'm not a programmer, and C (or C++ ?) of this block is killing me. Could you please give me a help and provide the equivalent in python?
PS.: For those asking "why the worst hash?",I intend to create set2, an "equivalent" to an original set1, but reduced in elements due to high algorithm hash collisions. I don't need to go back to the original set1. I just need to know if a hash is present in set2.
The C++ code that you have provided simply iterates over each character of the given string and adds its ASCII value to the variable hash and returns the hash.
We can achieve the same in Python 2.7 by doing this:
def custom_hash(s):
hsh = 0
for c in s: hsh += ord(c)
return hsh
Or you can always try to be Pythonic and do the same in one line :)
hsh = reduce(lambda x, y: ord(x) + ord(y), list(s))
Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 7 years ago.
Improve this question
How to do loop like: for(i = 0; i < 100; i++)?
This code is in PHP, i want create this loop like in Python 3x.
Thanks my brothers for any what you do for me.
Use the built-in range function which takes an optional step and an interval [a, b) like so:
for i in range(0, 100, 1):
# do something
A range based for loop in python is not equivalent to C like for loop that is prevalent in javascript, php or languages as such which supports them. Particularly, the distinction comes in
when the need comes to change the iterator variable inside the loop block
Condition is more complex beyond a limiting condition
Ideally the best alternative is to re-write the for loop to a while loop in the similar way as you would have written in languages that supports both. For example
for(<initializer>; <cond>; <increment>) {
// Loop Body
}
should equivalently be written as
<initializer>
while cond:
<Loop Body>
<increment>
so in your particular case, the equivalent of the php for(i = 0; i < 100; i++) construct in python would be
i = 0
while i < 100:
# Loop Body
i += 1
Using any other construct might have some surprising consequences.
for i in range(100):
# code here
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 9 years ago.
Improve this question
An pseudocode is required to input a series of numbers from the keyboard and to ensure that they are in ascending order even though they need not be consecutive.The number may be positive or negative and will end with a dummy value of 9999.
This is a python code I wrote and it works ok,
li=[]
num=int(raw_input("Enter number"))
while num!=9999:
li.append(num)
num=int(raw_input("Enter number"))
new=0
print "Entered sequence is "
print li
j=0
while j<len(li):
for i in range(len(li)-1):
value1=li[i]
value2=li[i+1]
if (value1>value2):
new=li[i]
li[i]=li[i+1]
li[i+1]=new
j+=1
print "ordered list is "
print li
But I have problem in writing this as an algorithm.
This is my tried algorithm:
Main
li list
num integer
new=0,j=0 integer
Begin
num=INPUT num.
while num<>9999.
append num to li.
num=INPUT num.
ENDWHILE.
DISSPLAY "Entered Sequence is".
OUTPUT li.
while j<(length of li).
FOR i=0 to (length of li-2).
value1=i th element of li
value2=(i+1) th element of li
if (value1>value2):
new=value1
value1=value2
value2=new
ENDIF
END FOR
j=j+1
ENDWHILE
DISPLAY "ORDERED LIST IS"
DISPLAY li
END
END
Can I use "list" in an algorithm because I think objects as "list" do not appear in every programming language.And shouldn't algorithm be a general code.Same way is it allowed to use arrays in writing an algorithm
And Is it okay to say "value1=ith element in li"?
And how to show that I am assigning the value entered from keyboard to the variable "num"
(This was originally a comment)
You seem to miss what the point of pseudo code is. Pseudo code is neither standardized nor somewhat defined. In general it is just a code-like representation of an algorithm, while maintaining a high level and readability. You can write pseudo code in whatever form you like. Even real Python code could be considered pseudo code. That being said, there are no thing disallowed in pseudo code; you can even write prose to explain something that happens. For example in the inner-most loop, you could just write “swap value1 and value2”.
This is approximately how I would transform your Python code into pseudo-code. I tend to leave out all language specific stuff and focus just on the actual algorithmic parts.
Input:
list: Array of input numbers
FOR j = 0 to length(list):
FOR i = 0 to length(list)-1:
if list[i] > list[i+1]:
Swap list[i] and list[i+1]
OUTPUT ordered list
So is it okay to use lists, tuples, dictionaries in a pseudocode, even if they are not common to all programming languages?
Absolutely! In more complex algorithms you will even find things like “Get minimum spanning tree for XY” which would be a whole different problem for which again multiple different solutions exist. Instead of specifying a specific solution you are keeping it open to the actual implementation which algorithm is to be used for that. It usually does not matter for the algorithm you are currently describing. Maybe later when you analyze your algorithm, you might mention things like “There are algorithms known for this who can do this in O(log n)” or something, so you just use that to continue.