Python Forum
Time limit exceed problem
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Time limit exceed problem
#1
I got the following question in my exercise:

"There is 2 integers, x and y and I need to classify the integer from 1 to y into 2 group. One group with integers that are multiple of y and the other is the remaining integer. Afterward, I need to calculate the mean value of each group."

I have written the following code and the direction should be correct, but there will be a time limit exceed problem when the size of data become very large.

x,y = [int(num) for num in input().split()]
A=[]
B=[]
for i in range(1,x+1):
    if i%y==0:
        A.append(i)
    else:
        B.append(i)
print(round((sum(A)/len(A)),1),round((sum(B)/len(B)),1))
I hope to find some suggestion to solve the problem, either modify my current code or give a new direction for me to write the code will be find. Thank you very much!
Reply
#2
Maybe an example input and the desired output would help.

Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#3
(Oct-06-2020, 07:27 AM)DPaul Wrote: Maybe an example input and the desired output would help.

Paul

example input : 100 16
desired output: 56.0 50.1
Reply
#4
Quote:"There is 2 integers, x and y and I need to classify the integer from 1 to y into 2 group. One group with integers that are multiple of y and the other is the remaining integer. Afterward, I need to calculate the mean value of each group."

Is this description of the problem 100% accurate ?
Does 1 need to be x ?
Does the last "integer" need to be "integers"?
It makes a big difference.
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#5
(Oct-06-2020, 07:53 AM)DPaul Wrote:
Quote:"There is 2 integers, x and y and I need to classify the integer from 1 to y into 2 group. One group with integers that are multiple of y and the other is the remaining integer. Afterward, I need to calculate the mean value of each group."

Is this description of the problem 100% accurate ?
Does 1 need to be x ?
Does the last "integer" need to be "integers"?
It makes a big difference.
Paul

Opps...sorry I have make a mistake. The description should be:
"There is 2 integers, x and y and I need to classify the integer from 1 to x into 2 group. One group with integers that are multiple of y and the other is the remaining integer. Afterward, I need to calculate the mean value of each group."

For example when the sample input is 100 16, I first need to separate the integer from 1 to 100 into 2 group. The first group is the integer that is multiple of 16 in 1 to 100 and the second group is the integers that is not the multiple of 16(i.e. the integer from 1 to 100 that is not in the first group.) Afterward, I need to calculate the mean of the two group.

I hope this will make you clearer, thanks!
Reply
#6
There is an input ? on line 1, that i made simpler
x,y = 100,16
I've written about the same program that you did,
and ran it with x = 10_000, no problem, instantaneous.
What would be your definition of "very large?"
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#7
(Oct-06-2020, 09:25 AM)DPaul Wrote: There is an input ? on line 1, that i made simpler
x,y = 100,16
I've written about the same program that you did,
and ran it with x = 10_000, no problem, instantaneous.
What would be your definition of "very large?"
Paul

Yup, it is ok when the program is run in idle. However when putting into the online judge system, which has a time limit of 1000MS and memory limit of 256MB, the TLE problem will appear. Thus, I am seeking way to further simplify my code.....
Reply
#8
OK, if that is your problem, maybe a suggestion:
You can make your vector of multiples of 16 , directly
[16 * i for i in range(1,int(100/16 + 1))]
You can do the same for 1 to 100.
Then with list comprehension make a new list 1-100 that does not contain the multiples of 16.

Whether that is faster, i have not timed. You are saving a lot of unnecessary divisions.
Probably there is plenty of room for other ideas Smile
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply
#9
(Oct-06-2020, 09:56 AM)DPaul Wrote: OK, if that is your problem, maybe a suggestion:
You can make your vector of multiples of 16 , directly
[16 * i for i in range(1,int(100/16 + 1))]
You can do the same for 1 to 100.
Then with list comprehension make a new list 1-100 that does not contain the multiples of 16.

Whether that is faster, i have not timed. You are saving a lot of unnecessary divisions.
Probably there is plenty of room for other ideas Smile
Paul

Thanks for your suggestion!
I successfully make the multiple of 16 directly in a list, however I encounter problem in the list does not contain the multiples of 16...

For example A=[list of multiple of 16], and I make the other list with the following list:
B=[j for j in range(1,100+1) if j!=[A]]
The list return still contain the multiple of 16... May I know what's wrong with my code. Thanks!
Reply
#10
Quote:
B=[j for j in range(1,100+1) if j!=[A]]
[A] is not what you need.A is a vector in its own right.
Try this:
.... if not in A.....
Paul
It is more important to do the right thing, than to do the thing right.(P.Drucker)
Better is the enemy of good. (Montesquieu) = French version for 'kiss'.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Help on Time Series problem Kishore_Bill 1 5,504 Feb-27-2020, 09:07 AM
Last Post: Kishore_Bill

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020