def count(inteval):
co = 0
for num in inteval:
if inteval.pop() <= time_of_del:
co = co + 1
else: pass
return co
internal
is [6, 5, 1, 1]
, and time_of_del
is 5
Why is the answer 3? I made it to get four...
python
1. If you are not sure, take a lot of logs first.
def count(inteval):
co = 0
for num in inteval:
print "----------"
print "--> inteval ", inteval
print "num: ", num
popped = inteval.pop()
print "popped: ", popped
if popped <= 5:
print "co + 1 !!"
co = co + 1
print "co: ", co
else:
print "pass"
print "<-- inteval ", inteval
print "----------"
print co
return co
count([6, 5, 1, 1, 1])
Here's the result.
----------
--> inteval [6, 5, 1, 1, 1]
num: 6
popped: 1
co + 1 !!
co: 1
<-- inteval [6, 5, 1, 1]
----------
--> inteval [6, 5, 1, 1]
num: 5
popped: 1
co + 1 !!
co: 2
<-- inteval [6, 5, 1]
----------
--> inteval [6, 5, 1]
num: 1
popped: 1
co + 1 !!
co: 3
<-- inteval [6, 5]
----------
3
I don't know about anything else, but one thing is definitely weird. What is it? Why is internal
getting shorter and shorter?
2. If you feel like what you know is being denied, open an official document (or Trustworthy course ) so that you don't miss a word of it and read it again.
Pop() returns the last element of the list and deletes the element .
So this code is working fine. The for
statement circulating after for 1 in [6, 5]:
but [6, 5] does not have
1
in [6, 5] how can this
for
for
block be executed? Therefore, this for
loop stops here. (You can see that the log was only recorded.) Therefore, 3
is the final result after processing inteval
.
3. Then, how can I make good use of pop()
? I think it's better to just change the approach. Because if you want to, it's just
If you enter an array and a specific integer, → Outputs the number of elements in the array that are not greater than the integer
As long as you're doing it... In fact, that's because in Python, it ends in a one-line code.
# Key Idea: Filter the list according to the condition.
def count(ints, the_int) :
return len(filter(lambda x: int(x) <= the_int, ints))
print count([6,5,1,1,1], 5)
# Output: 4
803 When building Fast API+Uvicorn environment with PyInstaller, console=False results in an error
557 rails db:create error: Could not find mysql2-0.5.4 in any of the sources
564 Uncaught (inpromise) Error on Electron: An object could not be cloned
573 GDB gets version error when attempting to debug with the Presense SDK (IDE)
© 2024 OneMinuteCode. All rights reserved.