由逗号分隔的数字字符串列表的数据转换给我带来了多个错误,并且我无法获得正确的输出

发布于 2025-01-12 06:46:03 字数 4030 浏览 1 评论 0原文

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

戏舞 2025-01-19 06:46:03

您可以使用正则表达式更好地处理 ab 案例

import re
def process_input(input: str)->None:
    to_print = []
    elements = input.split(',')
    for element in elements:
        try:
            num = int(element)
            to_print.append(element)
            continue
        except ValueError:
            pass
        numbers = [int(i) for i in re.match(r'(-?\d*)-(-?\d*)',element).group(1,2)]
        for x in range(min(numbers),max(numbers)):
            to_print.append(str(x))
    print(' '.join(to_print))
        
if __name__ == __main__:
    str_input = input('Enter input: ')
    process_input(str_input)         

You can process the a-b cases with regex much better

import re
def process_input(input: str)->None:
    to_print = []
    elements = input.split(',')
    for element in elements:
        try:
            num = int(element)
            to_print.append(element)
            continue
        except ValueError:
            pass
        numbers = [int(i) for i in re.match(r'(-?\d*)-(-?\d*)',element).group(1,2)]
        for x in range(min(numbers),max(numbers)):
            to_print.append(str(x))
    print(' '.join(to_print))
        
if __name__ == __main__:
    str_input = input('Enter input: ')
    process_input(str_input)         
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文