簡體   English   中英

來自 Hackerearth 的帶有運行時錯誤的基本編程 Python3 練習題

[英]Basic Programming Python3 practice question with runtime error from Hackerearth

我正在嘗試解決一個問題,我應該打印連續重復的特定符號的最大數量,但我必須為每一行手動輸入。 如果我復制並粘貼測試用例,它會返回錯誤。 下面是完整的代碼和測試用例,之后給出了輸出。

注意:如果不完全理解問題,請訪問https://www.hackerearth.com/practice/basic-programming/input-output/basics-of-input-output/practice-problems/algorithm/maximum-border-9767e14c/

def maxborder(a):
    maxi=0
    for i in range(len(a)):
        count=0
        for j in range(len(a[0])):
            if(a[i][j]=='#'):
                count+=1
                if count> maxi:
                    maxi=count
    return maxi

no=int(input())
while(no!=0):
    m=int(input())
    n=int(input())
    a=[]
    for i in range(m):
        r=[]
        e=str(input())
        if len(e)!=0:
            for x in range(len(e)):
                r.append(e[x])
        a.append(r)
    res=maxborder(a)
    no-=1

print(res)

測試用例和 output,(完整的測試用例在下面)

TESTCASE:
2 #loop_count(no)
2 15 #row and column(m and n)

.....####......
.....#.........

7 9 
...###...
...###...
..#......
.####....
..#......
...#####.
.........

EXPECTED OUTPUT:
4
5

我得到什么:

Execution failed.
ValueError: invalid literal for int() with base 10 : '2 15'

Stack Trace:
Traceback (most recent call last):
File "something.py3",  line 26, in <module>
m=int(input())
ValueError: invalid literal for int() with base 10: '2 15'

完整的測試用例:

10
2 15
.....####......
.....#.........
7 9
...###...
...###...
..#......
.####....
..#......
...#####.
.........
18 11
.#########.
########...
.........#.
####.......
.....#####.
.....##....
....#####..
.....####..
..###......
......#....
....#####..
...####....
##.........
#####......
....#####..
....##.....
.#######...
.#.........
1 15
.....######....
5 11
..#####....
.#######...
......#....
....#####..
...#####...
8 13
.....######..
......##.....
########.....
...#.........
.............
#######......
..######.....
####.........
7 5
.....
..##.
###..
..##.
.....
..#..
.#...
14 2
..
#.
..
#.
..
#.
..
..
#.
..
..
..
#.
..
7 15
.###########...
##############.
...####........
...##########..
.......#.......
.....#########.
.#######.......
12 6
#####.
###...
#.....
##....
###...
......
.##...
..##..
...#..
..#...
#####.
####..

上述測試用例的預期 output:

4
5
9
6
7
8
3
1
14
5

嘗試這個:

def maxborder(a):
    maxi=0
    for i in range(len(a)):
        count=0
        for j in range(len(a[0])):
            if(a[i][j]=='#'):
                count+=1
                if count> maxi:
                    maxi=count
    return maxi

no=int(input())
while(no!=0):
    t=input().split(" ")
    m=int(t[0])
    n=int(t[1])
    a=[]
    for i in range(m):
        r=[]
        e=str(input())
        if len(e)!=0:
            for x in range(len(e)):
                r.append(e[x])
        a.append(r)
    res=maxborder(a)
    no-=1

print(res)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM