import re text='''Suppose my Phone No.is 0535-1234567, yours is 010-12345678, his is 025-87654321.''' matchResult=re.findall(r'(\d{3,4})-(\d{7,8})',text) #注意大括号内逗号后不能有空格 for item in matchResult: print(item[0],item[1],sep='-')
时间: 2023-12-02 10:02:49 浏览: 18
以上代码的输出结果是:
```
0535-1234567
010-12345678
025-87654321
```
相关问题
程序中两处有错误,请给出错误行的编号、错误的原因和改正后的整行代码。 import re telNumber = '''Suppose my Phone No. is 0535-1234567, yours is 010-12345678, his is 025-87654321.''' pattern = re.compile(r'(\d)-(\d{7,8})') index = 0 while True: matchResult = pattern.findall(telNumber, index) if not matchResult: break print('-'*30) print('Success:') for i in range(3): print('Searched content:', matchResult.group(i),\ ' Start from:', matchResult.start(i), 'End at:', matchResult.end(i),\ ' Its span is:', matchResult.span(i)) index = matchResult.end(2)
错误行的编号是第9行。原因是在print语句中使用了反斜杠续行,但是没有加上引号。应该修改为:
```python
print('Searched content:', matchResult.group(i), ' Start from:', matchResult.start(i), 'End at:', matchResult.end(i), ' Its span is:', matchResult.span(i))
```
另外,第7行应该将pattern.findall()的参数改为telNumber[index:],表示从telNumber的第index个字符开始查找。修改后的代码如下:
```python
import re
telNumber = '''Suppose my Phone No. is 0535-1234567, yours is 010-12345678, his is 025-87654321.'''
pattern = re.compile(r'(\d)-(\d{7,8})')
index = 0
while True:
matchResult = pattern.findall(telNumber[index:])
if not matchResult:
break
print('-'*30)
print('Success:')
for i in range(3):
print('Searched content:', matchResult[i][0]+'-'+matchResult[i][1], ' Start from:', matchResult[i].start(), 'End at:', matchResult[i].end(), ' Its span is:', (matchResult[i].start()+index, matchResult[i].end()+index))
index += matchResult[-1].end()
```
改正后的代码在输出结果方面也做了一些修改,使其符合预期。
Consider the 5-bit generator, G = 10011, and suppose that D has the value1010101010. What is the value of R?
To calculate the value of R, we need to perform polynomial long division of D by G. Here are the steps:
1. Append four 0s to the end of D to create a dividend with 14 bits: 10101010100000.
2. The first 5 bits of the dividend are divided by G=10011 to get the first bit of the quotient and the remainder. The first bit of the quotient is 1 and the remainder is 1110.
3. The next bit of the dividend (which is a 0) is brought down to the remainder to create a new 5-bit sequence: 11100.
4. This new sequence is divided by G to get the next bit of the quotient and remainder. The next bit of the quotient is 0 and the remainder is 1010.
5. Steps 3 and 4 are repeated until we have a quotient of 10 bits.
6. The final remainder is R. In this case, R=1010.
Therefore, the value of R is 1010.