用while改写for j in range (4) for i in range (j+1): print ("* , end= ) print () for j in range (3) : for i in range (3-j) print (" ,)
时间: 2024-05-28 10:09:06 浏览: 95
以下是用while改写过的代码:
j = 0
while j < 4:
i = 0
while i < j+1:
print("*", end=" ")
i += 1
print()
j += 1
j = 0
while j < 3:
i = 0
while i < 3 - j:
print(" ", end=" ")
i += 1
print(",")
j += 1
希望能够帮到您!如果还有其他问题,请随时提出。
相关问题
for p in itertools.product(*(itertools.permutations(range(len(mf[i]))) for i in range(6))): for i in range(6): for j in range(len(mf[i])): print(p[i][j], end=" ") break将这段代码改写为,使mp为mf的排列
可以将for循环的第一行修改为以下代码:
```
for p in itertools.product(*[itertools.permutations(mf[i]) for i in range(6)]):
```
这样就能将mp改为mf的排列了。修改后的完整代码如下:
```
def guess_key1(cipher_text, words):
letter_frequency = get_letter_frequency(cipher_text.lower())
sorted_letters = sorted(letter_frequency.keys(), key=lambda x: letter_frequency[x], reverse=True) # paixu
most_common_letters_m = [sorted_letters[:8], sorted_letters[8:16], sorted_letters[-10:]] # high 8 middle 8 low 10
f1 = ['e'] # >10%
f2 = ['a', 'i', 'r', 't', 'o', 'n']
f3 = ['s', 'l', 'c']
f4 = ['u', 'p', 'm', 'd', 'h']
f5 = ['g', 'b', 'y', 'f', 'v']
f6 = ['w', 'k', 'x', 'z', 'q', 'j'] # <1%
mf = [f1, f2, f3, f4, f5, f6]
mp = [[iter(range(len(mf[i]))) for j in range(len(mf[i]))] for i in range(len(mf))]
flag = True
while(flag):
key = {}
for p in itertools.product(*[itertools.permutations(mf[i]) for i in range(6)]):
for i in range(6):
for j in range(len(mf[i])):
print(p[i][j], end=" ")
break
for i in range(6):
for j in range(len(mf[i])):
index = next(mp[i][j])
key.update({most_common_letters_m[i][index]: mf[i][j]})
decrypted_text = decrypt(cipher_text, key)
if is_plaintext(decrypted_text, words):
flag=False
return key
```
def split(params_str): p = [] start = 0 end = params_str.find(' ') while end != -1: p.append(int(params_str[start:end])) start = end + 1 end = params_str.find(' ', start) p.append(int(params_str[start:])) return p def split_str(params_str): p = [] start = 0 end = params_str.find(' ') while end != -1: p.append(params_str[start:end]) start = end + 1 end = params_str.find(' ', start) p.append(params_str[start:]) return p n = int(input()) tree = {} for _ in range(n): input_temp = input() temp = split_str(input_temp) a = temp[0] b = temp[1] if b not in tree: tree[b] = [] tree[b].append(a) target = input() childrens = tree.get(target, []) result = [] while childrens: node = childrens[0] childrens = childrens[1:] result.append(node) if node in tree: childrens.extend(tree[node]) result.sort() for res in result: print(res) 改写此代码
以下是对您提供的代码的改写版本:
```python
def split(params_str):
return list(map(int, params_str.split()))
def split_str(params_str):
return params_str.split()
n = int(input())
tree = {}
for _ in range(n):
input_temp = input()
temp = split_str(input_temp)
a, b = temp[0], temp[1]
tree.setdefault(b, []).append(a)
target = input()
childrens = tree.get(target, [])
result = []
while childrens:
node = childrens.pop(0)
result.append(node)
if node in tree:
childrens.extend(tree[node])
result.sort()
for res in result:
print(res)
```
在这个改写版本中,我使用了`split()`函数的`split()`方法替代了`find()`和循环,将字符串分割为整数列表。对于`split_str()`函数,我直接使用了`split()`方法将字符串分割为字符串列表。此外,使用了`setdefault()`方法来简化对树的构建过程,以及使用了`pop(0)`来替代了`childrens[0]`和`childrens[1:]`的操作。希望这样的改写能够更加简洁和高效地实现相同的功能。
阅读全文