您好,登錄后才能下訂單哦!
兩種不同的實現過程:算法-1,算法-2。
對比不同的算法實現的邏輯和效率差異,體會不同實現過程對測試方法、過程的影響。
軟件黑盒測試和白盒測試是相互融合、相互補充的兩種方法,完全相同的外部結果,其實現卻有著不同的方法和效率。
不同的設計思想、構造和實現方法,應有不同的測試策略和方法與之相對應。
軟件測試不僅要發現錯誤,更要預防錯誤,理解和分析是實現預測的基本能力和方法。
############################## 算法 - 1 ###################################
###########################################################################
# -*- coding: utf-8 -*-
#********************************************************************
# Copyright (c) 2014,LeiXun Studio
# All Rights Reserved.
#
# File Name: NumCompare.py
# Summary: Compare num W with num X
#
# Question:
# 帶通配符的數: 給定一個帶通配符問號的數W,問號可以代表任意一個一位數字。再給定一個整數X,和W具有同樣的長度。問有多少個整數符合W的形式并且比X大?
# 輸入格式 多組數據,每組數據兩行,第一行是W,第二行是X,它們長度相同。在[1..10]之間. 輸出格式 每行一個整數表示結果。
# 輸入樣例 36?1?8 236428 8?3 910 ? 5 輸出樣例 100 0 4
#
# @Version: 1.0
# @Author: Roy
# @Date: 04/25/2014
#
# @Update Version: 1.0
# @Author:
# @End Date:
#*********************************************************************
import re
#global ch
#ch = True
def checknum(num,n1):
try:
if num[0] == '0':
print 'The %s is wrong, please input again.' %num
return 0
for i in range(0,len(num)):
if (num[i] == n1) or (int(num[i]) in range(10)):
continue
else:
print 'The %s is wrong, please input again.' %num
return 0
return num
except:
print 'Inputting is wrong...'
return 0
def getnum():
#global ch
T = []
while 1:
W = raw_input('Please input the number W: ')
if W == 'quit':
#ch = False
break
return T
elif checknum(W,'?') == 0:
#ch = False
break
return 0
else:
T.append(W)
X = raw_input('Please input the number X: ')
if len(W) != len(X):
print 'Inputting is wrong, length not equal.'
#ch = False
break
return 0
elif checknum(X,'0') == 0:
#ch = False
break
return 0
else:
T.append(X)
return T
def comnum(w,x):
l = len(w)
c = w.count('?')
cn = 0
s = 0
tt = [t.start() for t in re.finditer('\?',w)] #'?' position in W
for i in range(len(tt)):
if i == 0:
s = 0
else:
s = tt[i - 1] + 1
if tt[i] == 0:
cn = cn + (9-int(x[i]))*(10**(c-i-1))
elif s == tt[i]:
cn = cn + (9-int(x[i]))*(10**(c-i-1))
elif int(w[s:tt[i]]) >= int(x[s:tt[i]]):
cn = cn + 10**(c-i)
else:
return cn
return cn
if __name__ == "__main__":
import doctest, NumCompare
G = getnum()
if len(G) != 0:
for i in range(len(G)//2):
print comnum(G[i*2],G[i*2+1])
doctest.testmod(NumCompare)
###########################################################################
############################## 算法 - 2 ###################################
###########################################################################
# -*- coding: utf-8 -*-
#********************************************************************
# Copyright (c) 2014,LeiXun Studio
# All Rights Reserved.
#
# File Name: NumCompare.py
# Summary: Compare num W with num X
#
# Question:
# 帶通配符的數: 給定一個帶通配符問號的數W,問號可以代表任意一個一位數字。再給定一個整數X,和W具有同樣的長度。問有多少個整數符合W的形式并且比X大?
# 輸入格式 多組數據,每組數據兩行,第一行是W,第二行是X,它們長度相同。在[1..10]之間. 輸出格式 每行一個整數表示結果。
# 輸入樣例 36?1?8 236428 8?3 910 ? 5 輸出樣例 100 0 4
#
# @Version: 1.0
# @Author: Roy
# @Date: 03/25/2014
#
# @Update Version: 1.0
# @Author:
# @End Date:
#*********************************************************************
global T,l,c,cn,tc,W,X,p, ml
T = [] #accepte and store varable input
W = [] #get W from T
X = [] #get X from T
l = 0 #length of inputting w
c = 0 #count of '?' in W
tc = 0 #element's position of W
cn = 0 #question's result
p = 0 #position of first '?' for W
def NumCopare(w,x):
global l,c,cn,tc,W,X,T,p, ml
if w != '?' and int(w) not in range(10):
print ("W3 is incorrect,Please input correct num!\n")
return
if int(x) not in range(10):
print ("X4 is incorrect,Please input correct num!\n")
return
if tc < l :
if w != '?' and c != 0:
if int(w) > int(x):
t = int(w) - int(x)
cn = 10**c*t
#cn = 10**c
return cn
elif int(w) < int(x):
cn = cn + 0
return cn
elif int(w) == int(x):
tc = tc + 1
NumCopare(W[tc],X[tc])
else:
if w == '?':
cn1 = 0
cn2 = 0
t = 9 - int(x)
cn1 = 10**(c-1)*t
tc = tc + 1
c = c -1
if tc < l-1:
cn2 = NumCopare(W[tc],X[tc])
cn = cn1 + cn2
return cn
def getnum():
global T
while 1:
# print("*********************** Compare input number ************************")
# ww = raw_input('Please input the number W: ')
ww = raw_input()
wl = len(ww)
wc = ww.count('?')
if ww == "quit":
break
elif wc == 0:
print("W1 is incorrect,Please input correct num!\n")
else: #judge inputting correction for W
for i in range(0,len(W)-1):
try:
int(ww[i])
except(ValueError):
if ww[i] != '?':
print ("W2 is incorrect,Please input correct num!\n")
return
T.append(ww)
# xx = raw_input('Please input the number X: ')
xx = raw_input()
xl = len(xx)
if xl < wl:
print ("X1 is incorrect,Please input correct num!\n")
break
elif xx.count('?') > 0:
print ("X2 is incorrect,Please input correct num!\n")
return
elif xx[0] == '0' and xl == wl and xl > 1:
print ("X3 is incorrect,Please input correct num!\n")
return
else:#judge inputting correction for xx
try:
int(xx)
except(ValueError):
print ("X4 is incorrect,Please input correct num!\n")
return
T.append(xx)
return T
def getcount(TT):
global l,c,W,X,T,p,cn, tc
if TT != None:
ll = len(TT)
else:
return
for i in range(ll//2):
tc = 0
cn = 0
W = []
W = TT[i*2]
l = len(W)
c = W.count('?')
p = W.find('?')
X = []
X = TT[i*2+1]
NumCopare(W[0],X[0])
#print "There is " + str(cn) + " W greater than X.\n"
print str(cn)
#print getnum()
if __name__ == "__main__":
import doctest, NumCompare
getcount(getnum())
doctest.testmod(NumCompare)
###########################################################################
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。