为什么我的程序不能正常工作? (为什么停在中间)
why doesnt my program work as it should? (why does it stop in the middle)
所以我的作业是:
Post of Slovenia often gets a letter with unreadable ZIP code because people write such a way that some of the figures difficult to read.
Especially frequently make replacement
- Between 3 to 8 ,
- MED 1, 4 , 7, and 5
- Between 6
inscriptions subroutine Variants (k), which adopts a four-digit postcode K on the screen lists all the possible postcode can be obtained with these swaps digits. On the coating Variants (1035) on the screen appears the following zip codes, not necessarily against the order clean: 1035 , 1036 , 1085 , 1086 , 4035 , 4036 , 4085 , 4086 , 7035, 7036 , 7085 , 7086th
python:
def versions (k):
def Variante(x):
a=['3', '8'] #prva tabela
b=['1', '4', '7'] #druga tabela
c=['5', '6'] #treta tabela
v=[] #mozne variante
x=[x]
for s in x:
if s==a[1] or s==a[0]: #ce je stevilka(s) u prvih tabeli
v.append(a)
elif s==b[0] or s==[1] or s==[2]:#ce je stevilka(s) u drugi tabeli
v.append(b)
elif s==c[0] or c==[1]:#ce je stevilka(s) u treti tbabeli
v.append(c)
else:#ce stevilka(s) ni u nobeni skupini
v.append(x)
d=0
while d<len(v[0]):
e=0
print ("ratal")
while e<len(v[1]):
f=0
while f<len(v[2]):
g=0
while g<len(v[3]):
print(v[0][d], v[1][e], v[2][f], v[3][g])
g=g+1
f=f+1
e=e+1
d=d+1
x=input("vnesi postno stevilko: ")
Variante(x)
你的问题是你弄乱了索引。因此,例如,您有 s==[1]
而不是 s==b[1]
,c==[1]
而不是 s==c[1]
。
但是,分区还有其他问题,虽然都不是错误。您可以执行类似 s in b
的操作,而不是单独检查每个值。您可以使用 for
直接遍历列表的元素,而不是 while
遍历索引。您的代码的更惯用版本是:
for s in x:
if s in a:
v.append(a)
elif s in b:
v.append(b)
elif s in c:
v.append(c)
else:
v.append(x)
for vd in v[0]:
print("ratal")
for ve in v[1]:
for vf in v[2]:
for vg in v[3]:
print(vd, vem vf, vg)
只要您将单个字符映射到 aliases,您就可以将其视为获得 笛卡尔积别名列表
from itertools import product
def Variante(zip_code):
replace_tables = [
['3', '8'], #prva tabela
['1', '4', '7'], #druga tabela
['5', '6'], #treta tabela
]
# Convert to list of individual aliases
aliased_chars = []
for char in zip_code:
aliased = False
for table in replace_tables:
if char in table:
# Convert to possible aliases to this char
aliased_chars.append(table)
aliased = True
break
if not aliased:
# Otherwise convert to list for itertools.product
aliased_chars.append([char])
# Get the cartesian product of lists
return ["".join(l) for l in product(*aliased_chars)]
# x=input("vnesi postno stevilko: ")
print(Variante("1035"))
所以我的作业是:
Post of Slovenia often gets a letter with unreadable ZIP code because people write such a way that some of the figures difficult to read.
Especially frequently make replacement
- Between 3 to 8 ,
- MED 1, 4 , 7, and 5
- Between 6
inscriptions subroutine Variants (k), which adopts a four-digit postcode K on the screen lists all the possible postcode can be obtained with these swaps digits. On the coating Variants (1035) on the screen appears the following zip codes, not necessarily against the order clean: 1035 , 1036 , 1085 , 1086 , 4035 , 4036 , 4085 , 4086 , 7035, 7036 , 7085 , 7086th
python:
def versions (k):
def Variante(x):
a=['3', '8'] #prva tabela
b=['1', '4', '7'] #druga tabela
c=['5', '6'] #treta tabela
v=[] #mozne variante
x=[x]
for s in x:
if s==a[1] or s==a[0]: #ce je stevilka(s) u prvih tabeli
v.append(a)
elif s==b[0] or s==[1] or s==[2]:#ce je stevilka(s) u drugi tabeli
v.append(b)
elif s==c[0] or c==[1]:#ce je stevilka(s) u treti tbabeli
v.append(c)
else:#ce stevilka(s) ni u nobeni skupini
v.append(x)
d=0
while d<len(v[0]):
e=0
print ("ratal")
while e<len(v[1]):
f=0
while f<len(v[2]):
g=0
while g<len(v[3]):
print(v[0][d], v[1][e], v[2][f], v[3][g])
g=g+1
f=f+1
e=e+1
d=d+1
x=input("vnesi postno stevilko: ")
Variante(x)
你的问题是你弄乱了索引。因此,例如,您有 s==[1]
而不是 s==b[1]
,c==[1]
而不是 s==c[1]
。
但是,分区还有其他问题,虽然都不是错误。您可以执行类似 s in b
的操作,而不是单独检查每个值。您可以使用 for
直接遍历列表的元素,而不是 while
遍历索引。您的代码的更惯用版本是:
for s in x:
if s in a:
v.append(a)
elif s in b:
v.append(b)
elif s in c:
v.append(c)
else:
v.append(x)
for vd in v[0]:
print("ratal")
for ve in v[1]:
for vf in v[2]:
for vg in v[3]:
print(vd, vem vf, vg)
只要您将单个字符映射到 aliases,您就可以将其视为获得 笛卡尔积别名列表
from itertools import product
def Variante(zip_code):
replace_tables = [
['3', '8'], #prva tabela
['1', '4', '7'], #druga tabela
['5', '6'], #treta tabela
]
# Convert to list of individual aliases
aliased_chars = []
for char in zip_code:
aliased = False
for table in replace_tables:
if char in table:
# Convert to possible aliases to this char
aliased_chars.append(table)
aliased = True
break
if not aliased:
# Otherwise convert to list for itertools.product
aliased_chars.append([char])
# Get the cartesian product of lists
return ["".join(l) for l in product(*aliased_chars)]
# x=input("vnesi postno stevilko: ")
print(Variante("1035"))