def N_lamps(n,k):
L = [0]*(n+1)
for i in range(1,k+1):
factor = 1
while(factor*i<=n):
L[factor*i] = 1-L[factor*i]
factor += 1
for k,i in enumerate (L):
if(i==1):print(k,' ',end = '')
def snake_matrix(n):
L = [[0]*n for i in range(n)]
dir = [(1,0),(0,-1),(-1,0),(0,1)]
i = 0;j = n-1;x = 1;
L[i][j] = 1;
while(x<n*n):
while(i<n-1 and (L[i+1][j]==0)):
L[i+1][j] = x+1;x+=1;i+=1
while(j>0 and (L[i][j-1]==0)):
L[i][j-1] = x+1;x+=1;j-=1
while(i>0 and (L[i-1][j]==0)):
L[i-1][j] = x+1;x+=1;i-=1
while(j<n-1 and (L[i][j+1]==0)):
L[i][j+1] = x+1;x+=1;j+=1
for row in L:
print(row)
number = 0
def vertical_form_de(abc,data):
def test(buf,data):
for each in buf:
if(int(each) not in data):return False
return True
global number
for d in data:
for e in data:
de = d*10+e
x = abc*d;y = abc*e;z = abc*de
buf = str(x)+str(y)+str(z)
if(test(buf,data)):
number+=1;
print('%5d\nX%4d\n-------\n%5d\n%4d\n------\n%5d\n'%(abc,de,x,y,z))
def vertical_form_abc(data):
for a in data:
for b in data:
for c in data:
vertical_form_de(a*100+b*10+c,data)
print('The number of solutions = %d'%number)
def toTex(buf):
left_right = 0
new_buf = ''
for each in buf:
if(each == '"'):
if(left_right == 0):each = '“'
else: each = '"'
left_right = 1 - left_right
new_buf+=each
print(new_buf)
def WERTYU(buf):
s = '`1234567890-=QWERTYUIOP[]\\ASDFGHJKL;ZXCVBNM,./'
for char in buf:
try:
pos = s.index(char)
except:
pos = -1
if(pos!=-1):print(s[pos-1],end = '')
else:print(char,end = '')
def palindromes(buf):
s = 'A 3 HIL JM O 2TUVWXY51SE Z 8 '
msg = ['is not a pal','is a regular pal','is a mirrored string','is a mirrored pal']
def rev(ch):
if(ch.isalpha()):return s[ord(ch)-ord('A')]
else :return s[ord(ch)-ord('0')+25]
print(buf,buf[::-1])
p = 1 if (buf[::-1]==buf) else 0
old_buf = list(buf)
buf = list(map(rev,buf))
m = 1 if (buf[::-1]==old_buf) else 0
print(m,p,buf,old_buf)
print (msg[2*m+p])
def master_mind(ans,guess):
def test(each):
tot = 0
wrong_occur = 0
mark = [0]*len(each)
for index in range(len(each)):
if(ans[index] == each[index]):tot+=1;mark[index]=1;
for index in range(0,len(each)):
if(mark[index]!=1):
pos = -1
while(True):
try:
pos = ans.index(each[index],pos+1)
if(mark[pos]!=1):wrong_occur += 1;break;
except:break
return tot,wrong_occur
def test1(each):
A = 0;B = 0;
for index in range(len(each)):
if(ans[index] == each[index]):A+=1;
for d in range(1,10):
c1 = 0;c2 = 0;
for index in range(len(each)):
if(ans[index]==d):c1+=1;
if(each[index]==d):c2+=1;
B += min(c1,c2);
return (A,B-A)
for each in guess:
print(test(each))
def mysum(x):
sum = x
while(x>0):
sum += x%10
x //= 10
return sum
def min_generator(n):
m = len(str(n))
for x in range(n-9*m,n):
if(mysum(x)==n):print(x);return
print(0)
def less(a,b):
a_l = len(a);b_l = len(b)
if(a_l > b_l): return less(b,a)
n = a_l if a_l < b_l else b_l
for i in range(n):
if(a[i]<b[i]):return True
elif(a[i]>b[i]):return False
if(b_l == a_l):return False
else: return True
def circular_s(s):
temp = s
for i in range(len(s)):
if(less(temp,s)):ans = temp
temp = temp[1:]+temp[0]
print(ans)
def score(buf):
curr_num = 0;tot = 0
for ch in buf:
if(ch=='O'):curr_num+=1;
else: curr_num = 0
tot += curr_num
print(tot)
def Molar_Mass(buf):
weight = {'C':12.01,'H':1.008,'O':16.00,'N':14.01}
sum = 0.0;i = 0
length = len(buf)
while(i+1<length):
if (buf[i+1].isdigit()):sum += weight[buf[i]]*int(buf[i+1]);i+=2
else: sum += weight[buf[i]];i+=1;
if(i==length-1):sum+= weight[buf[i]]
print(sum)
def get_zero(i,num):
tot = 0
u = 1;
for j in range(1,i+1):
x = j*u;
tot += x
u *= 10;
tot*=9
return tot - num*9
def digit_count(n):
k = 0;i = 0;
while(True):
if((k*10+9) <= n):k = k*10+9;i+=1;
else:break
gap = n - k
sum = [i*(k+1)//10]*10;
sum[0] = get_zero(i,sum[1])
for digit in range(k+1,n+1):
for ch in str(digit):
sum[ord(ch)-ord('0')]+=1
print(sum)
= 300
def perodic(buf):
def test(s):
k = len(s)
for i in range(len(buf)//k):
if(s!=buf[i*k:(i+1)*k]): return False
return True
beg = '';end = ''
length = len(buf)
for i in range(length//2 + 1):
beg += buf[i]
end = buf[length-i-1]+end
if(beg == end and test(beg)):print(len(beg));return
print(len(buf))
def get_pos(matrix):
for i,k in enumerate(matrix):
for j,item in enumerate(k):
if(item == ' '):return[i,j]
def Puzzle(matrix,command):
empty_pos = get_pos(matrix)
dir = {'A':[-1,0],'B':[1,0],'L':[0,-1],'R':[0,1]}
def move(instruction):
nonlocal empty_pos
old_i = empty_pos[0]
old_j = empty_pos[1]
if(instruction not in dir):return False
i = old_i + dir[instruction][0]
j = old_j + dir[instruction][1]
if(i<5 and i >0 and j<5 and j>0 ):
matrix[old_i][old_j],matrix[i][j] = matrix[i][j],matrix[old_i][old_j]
empty_pos = [i,j]
return True
else:return False
for ins in command:
if(not move(ins)):print('This puzzle has no final configuration');return
for i in matrix:
print(i)
A = [['T','R','G','S','J'],['X','D','O','K','I'],['M',' ','V','L','N'],['W','P','A','B','E'],['U','Q','H','C','F']]
Puzzle(A,'ARR')
简单的模拟而已,需要注意的是python3 nonlocal的用法可以用来修改外部的自由变量.