由逗号分隔的数字字符串列表的数据转换给我带来了多个错误,并且我无法获得正确的输出
Section 1 Example A: Input of "1,2,3,4,5,6,7,8,9,10" should output 1 2 3 4 5 6 7 8 9 10
Section 1 Example B: Input of "1,2,6,4,8" should output 1 2 6 4 8
Section 2 Example C: Input of "1-3,5-9,12,14-15" should output 1 2 3 5 6 7 8 9 12 14 15
Section 2 Example D: Input of "2-4,3-7,10" should output 2 3 4 3 4 5 6 7 10
Section 3 Example E: Input of "3-1,5-9,12,14-15" should output 1 2 3 5 6 7 8 9 12 14 15
Section 3 Example F: Input of "2-4,7-3,10" should output 2 3 4 3 4 5 6 7 10
我尝试解决这个问题,但发现自己遇到了诸如 EOF 之类的错误,或者我将解决第 1 A 部分,但无法在循环中同时执行第 1 b 部分。他们应该是相互补充的。我不允许进行用户输入,所以我对如何解决这些问题感到非常困惑。
这是我现在的代码,它没有得到正确的输出并给我一个 EOF 错误。我应该采取哪些步骤来修复这些错误。
def PrintNumbers(s):
a=s.split(',') #split() function returns a list where text between the character ',' becomes a list element
i=0
#It is said that negative numbers need not to be accounted so let's replace the negative numbers with 0.
while i<len(a):
c=a[i].count('-')
if c==3: #it means that both the elements in the range are negative possible string representation will be of "-a--b" so we can remove the element from list
a.remove(a[i])
i=i-1
elif c==2: #it means that one of the element is negative possible string will be either "-a-b" or "a--b" so we remo
if a[i][0]=='-':#if the first element is'-' it means that the first number is negative(possible string is '-a-b') and second one is positive so lets change to first number to '0'
a[i]=a[i][1:] #taking the whole remaining string and finding the position of next '-' and adding 0 before it
pos=a[i].find('-')
a[i]='0'+a[i][pos:]
else:
pos=a[i].find('-') #possible string will be of form "a--b" replace the whole value after first '-' with 0
a[i]=a[i][0:pos+1]+'0'
else: #it means that the list may be a range like "1-3" or a negative number "-1"
if a[i][0]=='-': #if first character is negative number then we can change the whole string to '0'
a.remove(a[i])
i=i-1
i+=1
#It is said that ranges are also given so we need to split the list based on the seperator '-'
for i in range(len(a)):
a[i]=a[i].split('-') #We perform split operation to each array index and add the returned list to the same index.
#If seperator character is not present in the string the whole string returns as a list element
for i in range(len(a)):
for j in range(len(a[i])):
a[i][j]=a[i][j].strip() #strip() is used to remove white spaces at the starting and ending of the string if they are present
m=-1 #m is used to find the maximum value given in the ranges it is initialized to -1
for i in range(len(a)):
for j in range(len(a[i])):
if a[i][j].isdigit(): #check if the element is digit or not if digit convert it into integer
a[i][j]=int(a[i][j])
else:
print('Error : Given string contains alphanumeric values ')
return
m=max(m,a[i][j]) #updating the value of m to maximum value
for i in range(len(a)):
if len(a[i])==2: #if at an index the number of elements are 2 that means it represents range
if a[i][0]>a[i][1]: #It is said that while representing range the ranges are represented backward so if the second element in range is less than the first element then we swap them
a[i][0],a[i][1]=a[i][1],a[i][0]
#We are told to print the numbers
#Printing the numbers:
for i in range(len(a)):
if len(a[i])==1:
print(a[i][0],end=' ')
else:
for j in range(a[i][0],a[i][1]+1): #printing all numbers in range
print(j,end=' ')
s=input('Enter input : ')
PrintNumbers(s)
Section 1 Example A: Input of "1,2,3,4,5,6,7,8,9,10" should output 1 2 3 4 5 6 7 8 9 10
Section 1 Example B: Input of "1,2,6,4,8" should output 1 2 6 4 8
Section 2 Example C: Input of "1-3,5-9,12,14-15" should output 1 2 3 5 6 7 8 9 12 14 15
Section 2 Example D: Input of "2-4,3-7,10" should output 2 3 4 3 4 5 6 7 10
Section 3 Example E: Input of "3-1,5-9,12,14-15" should output 1 2 3 5 6 7 8 9 12 14 15
Section 3 Example F: Input of "2-4,7-3,10" should output 2 3 4 3 4 5 6 7 10
I have tried solving this problem and find myself getting either errors such as EOF or I will solve section 1 A but cannot do section 1 b at the same time in a loop. They are supposed to be building off of each other. I am not allowed to do user input so I am very confused on how to solve these problems.
This is my code as of now, which does not get the right outputs and gives me an EOF error. What are some steps i should take to fix these errors.
def PrintNumbers(s):
a=s.split(',') #split() function returns a list where text between the character ',' becomes a list element
i=0
#It is said that negative numbers need not to be accounted so let's replace the negative numbers with 0.
while i<len(a):
c=a[i].count('-')
if c==3: #it means that both the elements in the range are negative possible string representation will be of "-a--b" so we can remove the element from list
a.remove(a[i])
i=i-1
elif c==2: #it means that one of the element is negative possible string will be either "-a-b" or "a--b" so we remo
if a[i][0]=='-':#if the first element is'-' it means that the first number is negative(possible string is '-a-b') and second one is positive so lets change to first number to '0'
a[i]=a[i][1:] #taking the whole remaining string and finding the position of next '-' and adding 0 before it
pos=a[i].find('-')
a[i]='0'+a[i][pos:]
else:
pos=a[i].find('-') #possible string will be of form "a--b" replace the whole value after first '-' with 0
a[i]=a[i][0:pos+1]+'0'
else: #it means that the list may be a range like "1-3" or a negative number "-1"
if a[i][0]=='-': #if first character is negative number then we can change the whole string to '0'
a.remove(a[i])
i=i-1
i+=1
#It is said that ranges are also given so we need to split the list based on the seperator '-'
for i in range(len(a)):
a[i]=a[i].split('-') #We perform split operation to each array index and add the returned list to the same index.
#If seperator character is not present in the string the whole string returns as a list element
for i in range(len(a)):
for j in range(len(a[i])):
a[i][j]=a[i][j].strip() #strip() is used to remove white spaces at the starting and ending of the string if they are present
m=-1 #m is used to find the maximum value given in the ranges it is initialized to -1
for i in range(len(a)):
for j in range(len(a[i])):
if a[i][j].isdigit(): #check if the element is digit or not if digit convert it into integer
a[i][j]=int(a[i][j])
else:
print('Error : Given string contains alphanumeric values ')
return
m=max(m,a[i][j]) #updating the value of m to maximum value
for i in range(len(a)):
if len(a[i])==2: #if at an index the number of elements are 2 that means it represents range
if a[i][0]>a[i][1]: #It is said that while representing range the ranges are represented backward so if the second element in range is less than the first element then we swap them
a[i][0],a[i][1]=a[i][1],a[i][0]
#We are told to print the numbers
#Printing the numbers:
for i in range(len(a)):
if len(a[i])==1:
print(a[i][0],end=' ')
else:
for j in range(a[i][0],a[i][1]+1): #printing all numbers in range
print(j,end=' ')
s=input('Enter input : ')
PrintNumbers(s)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您可以使用正则表达式更好地处理 ab 案例
You can process the a-b cases with regex much better