Create list and add 1 to list values from index [I,R].Print the Occurence of a specific number

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP





.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty margin-bottom:0;







up vote
1
down vote

favorite












def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number


For small values the program works but fails for large value.Suggest any faster method/algorithm.



Constraints:
1 <= N <= 100000



1 <= M <= 1000



1 <= l <= r <= N



1 <= Q <= 100000



1 <= K <= N







share|improve this question

















  • 1




    Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
    – Josiah
    Jul 18 at 6:46






  • 1




    fails for large values means Time Limit is Exceeded
    – AYUSH SINGHAL
    Jul 18 at 7:12






  • 1




    The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
    – Mathias Ettinger
    Jul 18 at 8:32






  • 1




    I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax.
    – Mathias Ettinger
    Jul 18 at 8:37










  • I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
    – Mast
    Jul 30 at 7:00
















up vote
1
down vote

favorite












def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number


For small values the program works but fails for large value.Suggest any faster method/algorithm.



Constraints:
1 <= N <= 100000



1 <= M <= 1000



1 <= l <= r <= N



1 <= Q <= 100000



1 <= K <= N







share|improve this question

















  • 1




    Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
    – Josiah
    Jul 18 at 6:46






  • 1




    fails for large values means Time Limit is Exceeded
    – AYUSH SINGHAL
    Jul 18 at 7:12






  • 1




    The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
    – Mathias Ettinger
    Jul 18 at 8:32






  • 1




    I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax.
    – Mathias Ettinger
    Jul 18 at 8:37










  • I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
    – Mast
    Jul 30 at 7:00












up vote
1
down vote

favorite









up vote
1
down vote

favorite











def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number


For small values the program works but fails for large value.Suggest any faster method/algorithm.



Constraints:
1 <= N <= 100000



1 <= M <= 1000



1 <= l <= r <= N



1 <= Q <= 100000



1 <= K <= N







share|improve this question













def add(a,b):
for i in xrange(a-1,b):
box[i]+=1
def count(k):
if box.count(k)!=0:
box.index(k)
return len(box)-box.index(k)
else:
return 0
from sys import stdin,stdout
box=[0]*int(stdin.readline()) #input the number of boxes of chocolate
for i in xrange(int(stdin.readline())): #loops for M days
a,b = stdin.readline().split() #gets the starting and ending index.
add(int(a),int(b)) #adding 1 each time to box for I,R
box.sort()
for i in xrange(int(stdin.readline())):
print count(int(stdin.readline())) #prints the number of boxes of chocolates greater than equal to the input number


For small values the program works but fails for large value.Suggest any faster method/algorithm.



Constraints:
1 <= N <= 100000



1 <= M <= 1000



1 <= l <= r <= N



1 <= Q <= 100000



1 <= K <= N









share|improve this question












share|improve this question




share|improve this question








edited Jul 30 at 6:58









Mast

7,30663483




7,30663483









asked Jul 18 at 6:05









user175190

61




61







  • 1




    Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
    – Josiah
    Jul 18 at 6:46






  • 1




    fails for large values means Time Limit is Exceeded
    – AYUSH SINGHAL
    Jul 18 at 7:12






  • 1




    The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
    – Mathias Ettinger
    Jul 18 at 8:32






  • 1




    I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax.
    – Mathias Ettinger
    Jul 18 at 8:37










  • I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
    – Mast
    Jul 30 at 7:00












  • 1




    Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
    – Josiah
    Jul 18 at 6:46






  • 1




    fails for large values means Time Limit is Exceeded
    – AYUSH SINGHAL
    Jul 18 at 7:12






  • 1




    The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
    – Mathias Ettinger
    Jul 18 at 8:32






  • 1




    I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax.
    – Mathias Ettinger
    Jul 18 at 8:37










  • I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
    – Mast
    Jul 30 at 7:00







1




1




Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
– Josiah
Jul 18 at 6:46




Hello, and welcome to code review. Could I please check what you mean by "fails for large values?" Is it giving the wrong answer, or just taking too long to give any answer at all?
– Josiah
Jul 18 at 6:46




1




1




fails for large values means Time Limit is Exceeded
– AYUSH SINGHAL
Jul 18 at 7:12




fails for large values means Time Limit is Exceeded
– AYUSH SINGHAL
Jul 18 at 7:12




1




1




The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
– Mathias Ettinger
Jul 18 at 8:32




The comments in the code feels like this is a coding challenge. Do you mind adding the description of the challenge into the question, please?
– Mathias Ettinger
Jul 18 at 8:32




1




1




I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax.
– Mathias Ettinger
Jul 18 at 8:37




I also removed the python-3.x tag as xrange doesn't exist anymore and print as a keyword is not valid syntax.
– Mathias Ettinger
Jul 18 at 8:37












I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
– Mast
Jul 30 at 7:00




I've removed the Python 2.6 tag. You'd initially tagged this question with all Python tags but the code is written in Python 2.7. If this code HAS to run on Python 2.6, please state so explicitly in the question. The 2.7 tag will eventually be renamed to 2.x.
– Mast
Jul 30 at 7:00















active

oldest

votes











Your Answer




StackExchange.ifUsing("editor", function ()
return StackExchange.using("mathjaxEditing", function ()
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
);
);
, "mathjax-editing");

StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "196"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
convertImagesToLinks: false,
noModals: false,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
bindNavPrevention: true,
postfix: "",
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);








 

draft saved


draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199724%2fcreate-list-and-add-1-to-list-values-from-index-i-r-print-the-occurence-of-a-s%23new-answer', 'question_page');

);

Post as a guest



































active

oldest

votes













active

oldest

votes









active

oldest

votes






active

oldest

votes










 

draft saved


draft discarded


























 


draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f199724%2fcreate-list-and-add-1-to-list-values-from-index-i-r-print-the-occurence-of-a-s%23new-answer', 'question_page');

);

Post as a guest













































































Popular posts from this blog

Python Lists

Aion

JavaScript Array Iteration Methods