Python terminal printing an empty line during long repeats [closed] - python

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 4 years ago.
Improve this question
I've written a program calculating pi based on basel problem but this program includes a very big while repeat.
See:
import math
import decimal
sqrt=1.
sumn=0.
outerrepeat=1000000
while outerrepat>0:
repeat=10000
while repeat>0:
sumn+=1/(sqrt**2)
sqrt+=1
repeat-=1
outerrepeat-=1
print math.sqrt(sumn*6)
When I run this outputs an empty line like this without ">>>":
========================== RESTART: C:/Python/pi.py ==========================
What's the reason?
Edit: Sorry for different variables, I wrote this program in my own language and then translated the variables for you. I forgot some of them.
Second edit: I tried Luc's suggestion, also changed the place of the print math.sqrt(sumn*6) to the outer while so I could see if it was proccesing.
It did but after the 7th number of pi it stopped working and stuck at 3.14159264498

The short answer to your question, is that your while loop are too big. So when you execute your program, the time to go through the while loop is too long.
There are some typo in the code that you copy/pasted, but I will assume that there is none. You are trying to make 10 000 000 000 iterations. Do you really need that many?
I would start by simply going for 100 iterations, maximum. And then augment it if you have a need for more precision.
You algorithm would be easier to understand/debug, if you simply use a for-loop.
You can try this one for example:
import math
max_iteration = 100
sumn=0.
for n in range(1,max_iteration+1):
sumn+=1./(n**2)
print math.sqrt(sumn*6)
With max_iteration = 100 you should find 3.13207653181, and the precision is getting better if you increase your max_iteration. With 10 000 iterations your are already at 3.14149716395.

Related

What is the Big O efficiency of this code? [closed]

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.

what is the output of this python code? I got confused [closed]

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 2 years ago.
Improve this question
I'm a beginner in programming so I wish you be kind to my question :)
would you please tell me what is the output of this simple code?
It shows error but many say the output is " Learn PY 23 "
how is that possible?!
**def m(name,age=20)
print(name,age)
m("learnPY",23)**
By considering your code as below(removed **)
def m(name,age=20)
print(name,age)
m("learnPY",23)
In function m argument age is given default value, which is applied only if you don't pass 2nd argument while calling function.
In your code you have it with 23 so,
the output will be "LearnPY 23"
If you call m("learnPY") then
the output will be "LearnPY 20"
The code is not syntactically legal Python code.
Hence, the question "what is the output of this Python code" is non-sensical, because it isn't Python code.
Yes, it is. The thing is that the code is wrong formatted.
def m(name,age=20):
print(name,age)
m("learnPY",23)
If you run it correctly, it will work. This is because you're calling the function passing two arguments that will be printed.

Is it an array inside another array or are we looking for a certain value in the array? [closed]

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 4 years ago.
Improve this question
i know this might be a bit beginner level type of code but i am getting a bit confused by this code below. i don't know what is happening. may someone be kind enough to explain it to me? And what is the output of the code?
lest = [3, 1, -2]
print(lest[lest[-1]])
3
1
-1
-2
What really is happening here, especially with lst[lst[-1]]? i do not understand the concept being shown here. are we looking for a value in a multi-dimensional array or what?
lest = [3, 1, -2]
print(lest[lest[-1]])
Okay easier if you break it up.
First:
lest[-1] will give us the last value of list lest which is -2
Second:
lest[lest[-1]]
Since we know lest[-1] = -2 we know that this is equivalent to:
lest[-2]
Now your code is going to return the second to last digit of lest which is 1
Output
(xenial)vash#localhost:~/python$ python3.7 helpin.py
1

A better way to extract data in Python [closed]

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
I have some code that gets the lecture times from online courses I take. The resulting answer is in the form of [['02:29']]. I think I'd use the following code to get the number out:
time1=timeList[0]
time2=time1[0]
time3=(time2.contents[0])
(parts) = time3.split(":")
Is there a better way to do this in fewer steps? And what resources are there to help me in the future? Or is this one of those things you just learn by coding and then asking for help?
Is it a list in a list with just one item?
Then you could achieve the same with:
test = [['02:29']]
parts = test[0][0].split(':')
print parts # result: ['02', '29']
But I'am not sure if I got you right
Itertool is always available:
Itertools
And you can chain your list considering it's always in the same format:
import itertools
time1 = list(itertools.chain(*timeList))[0].split(":")[0]
This solution is very fast and it works very smooth. A bit complicated to understand but it makes the work very efficiently.
Note: it also works with more levels of subarrays, no matter if you have 1, 2 or 3 levels
Update
Due to you indicated you want to convert it to raw seconds the answer is:
time1 = list(itertools.chain(*timeList))[0].split(":")
seconds_total = (time1[0] * 60) + time1[1]

Biopython - String assigning error [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 8 years ago.
Improve this question
I tried to begin with Biopython. So that I can do my thesis in it. But this really makes me think twice. Show features missing, when I tried a integer value, it does not work and same is the case with string too. Kindly help. Thank you.
Link:
http://imgur.com/87Gw9E5
Biopython seems pretty robust to me, the errors are probably due to your inexperience with it.
You have several errors, one of them is that you forgot to end the strings with "". The following lines
print "location start, features[ftNum].location.start # note location.start"
print "feature qualifiers,features[ftNum].qualifiers"
should be corrected to
print "location start", features[ftNum].location.start # note location.start
print "feature qualifiers", features[ftNum].qualifiers
Furthermore, as Wooble pointed out the condition in your while loop is wrong. I'm guessing you meant to to invert the ">", that is, the number of features should be greater than zero.
Please add some example data and error messages.
The guys at Biopython actually made it easy to deal with the features. Your problem is string management (plain python). I've used format, but you can use the % operator.
Also in python you rarely have to keep the count when looping. Python is not C.
from Bio import SeqIO
for record in SeqIO.parse("NG_009616.gb", "genbank"):
# You don't have to take care of the number of features with a while
# Loop all of them.
for feature in record.features:
print "Attributes of feature"
print "Type {0}".format(feature.type)
print "Start {0}".format(feature.location.start)
print "End {0}".format(feature.location.end)
print "Qualifiers {0}".format(feature.qualifiers)
# This is the right way to extract the sequence:
print "Sequence {0}".format(feature.location.extract(record).seq)
print "Sub-features {0}".format(feature.sub_features)

Categories

Resources