http://d.hatena.ne.jp/shindannin/20111202/1322833089 Yes, tonight's reference is from the link above
Problem 1 Count FizzBuzz How many integers from A to B that are divisible by 3 or 5? With that Reprinting the example of the other party is confusing, so just for me
Brute force using for loop.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import io
import re
import math
####Memory usage and operating time check preparation
from guppy import hpy
import time
h = hpy()
start = time.clock()
####Up to here
i=0
j=0
#ex1
for x in range(5, 11):
if x%3==0 or x%5==0:
i+=1
#ex2
for y in range(14, 17):
if y%3==0 or y%5==0:
j+=1
print 'ex1:'+str(i),'ex2:'+str(j)
####Memory usage and operating time output
end = time.clock()
print h.heap()
print end-start
Is range (5,11) so intuitive to turn from 5 to 10 with for? I don't think it is, but I didn't like this when I wrote an integer between A and B in a while statement.
When written in a while statement.py
i=0
a,b =5,10
#↓ will be specified to be executed between A and B
while a<=b:
#When it is divisible by 3 or 5, add 1 to the counter
if a%3==0 or a%5==0:
i+=1
#Add 1 by a before returning to the condition judgment of the execution of the while part.
a+=1
Hmmm, I think for is better in this case. And next
Problem 2 Count FizzBuzz (2) x is an integer from 0 to X, y is an integer from 0 to Y, and z is an integer from 0 to Z. How many combinations of (x, y, z) are there in which x + y * y + z * z * z * z is divisible by 3 or 5?
Brute force using multiple for loops
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import io
import re
import math
####Memory usage and operating time check preparation
from guppy import hpy
import time
h = hpy()
start = time.clock()
####Up to here
i=0
for x in range(0,101):
for y in range(0,101):
for z in range(0,101):
tmp=x + y*y + z*z*z*z
if tmp%3==0 or tmp%5==0:
i+=1
else:
pass
print i
####Memory usage and operating time output
end = time.clock()
print h.heap()
print end-start
For the time being, I wrote it while, but this is also not beautiful. .. .. And the execution speed was about 0.61 seconds for the for statement and 0.70 seconds for the while statement.
When using while.py
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import sys
import io
import re
import math
####Memory usage and operating time check preparation
from guppy import hpy
import time
h = hpy()
start = time.clock()
####Up to here
i=0
x=y=z=0
while x<=100:
while y<=100:
while z<=100:
tmp=x + y*y + z*z*z*z
if tmp%3==0 or tmp%5==0:
i+=1
else:
pass
z+=1
y+=1
z=0
x+=1
y=0
print i
####Memory usage and operating time output
end = time.clock()
print h.heap()
print end-start
I think I've seen somewhere that else: pass is unnecessary, useless, and shouldn't be written, but I often write it. .. .. Also, if you exit while once and then return in a loop again to execute the while statement that is a child, you can search for a method other than reassigning 0 to the counter at the exit. .. ..
from __future__ import print_function
Will be incorporated from tomorrow.
That's all for tonight.
Recommended Posts