Problem p02761 (python)

Fixed submission:

from collections import defaultdict
N,M = (int(x) for x in input().split())
keta_num_str = [ (input().split()) for i in range(M) ]

#各桁をキー、数字を値の辞書にする
suuji = ""
num_str_dic = defaultdict(set)
kotae_dic ={ str(i):"0" for i in range(1,N +1) } #　各桁に初期値”０”を入力。一番右（１の位）は入力がなければ無し
if 1< N:
    kotae_dic["1"] = "1" # 2桁以上の時は左の桁は最低１
for i in keta_num_str:   
    num_str_dic[i[0]].add(i[1])


for i,v in num_str_dic.items():
    if 1 < len(v) : # 一つの桁に複数の値がある時はエラー
        kotae_dic ={}
        break
    
    else:
        kotae_dic[i] = list(v)[0]

# 表示
import math
if kotae_dic == {}: # 一つの桁に複数の値の時        
    print(-1)
else:
    kotae_dic_sorted = sorted(kotae_dic.items(),key=lambda x:int(x[0]))
    kotae_str = "".join( [i[1] for i in kotae_dic_sorted ] ) # 辞書の値を連結
    kotae_int =int(kotae_str)
    if kotae_int == 0 and 1< N: # "00"などはエラー、"0"はOK
        print(-1)
    elif kotae_int == 0 and N == 1:
        print("0")
    elif 10**(N-1) > kotae_int or kotae_int >= 10** N: # 桁があってない＝一番左の桁が”０”はエラー
        print(-1)
    elif kotae_dic["1"] == "0" and 1 < N: 
        kotae_dic["1"] ="1"
    elif 1000 <= kotae_int: # 答えが１０００以上はエラー
        print(-1)

    else:
        print(kotae_int)

Buggy submission (verdict: Runtime Error):
#初期入力
import sys
N,M = (int(x) for x in input().split())
if M==0:
    if N==1:
        print(0)
    elif N==2:
        print(10)
    elif N==3:
        print(100)
    sys.exit(0)
digit ={str(i):[] for i in range(1,N+1)}

for i in range(M):
    d,n =input().split()
    digit[d].append(n)

ans =""
for i,v in digit.items():
    if i =="1" and v[0] =="0" and (len(v) ==2 or len(v) ==3): #1桁目（一番左）は✖０、最小値「１」
            print(-1)
            sys.exit(0)
    elif len(v) ==2:
        if v[0] !=v[1]: #同じ桁なのに違う値は✖
            print(-1)
            sys.exit(0)
        else:
            ans +=v[0]
    elif len(v) ==3:
        if v[0] !=v[1] or v[2] !=v[1] or v[0] !=v[2]:
            print(-1)
            sys.exit(0)
        else:
            ans +=v[0]
    elif len(v) ==1:
        ans +=v[0]
    elif len(v) ==0:
        if i =="1":
            ans +="1" #1桁目（一番左）は✖０、最小値「１」
        else:
            ans +="0"

print(ans)

Buggy submission (verdict: Runtime Error):
#初期入力
import sys
N,M = (int(x) for x in input().split())
if N==1 and M==0:
    print(0)
    sys.exit(0)
digit ={str(i):[] for i in range(1,N+1)}

for i in range(M):
    d,n =input().split()
    digit[d].append(n)

ans =""
for i,v in digit.items():
    if i =="1" and v[0] =="0" and (len(v) ==2 or len(v) ==3): #1桁目（一番左）は✖０、最小値「１」
            print(-1)
            sys.exit(0)
    elif len(v) ==2:
        if v[0] !=v[1]: #同じ桁なのに違う値は✖
            print(-1)
            sys.exit(0)
        else:
            ans +=v[0]
    elif len(v) ==3:
        if v[0] !=v[1] or v[2] !=v[1] or v[0] !=v[2]:
            print(-1)
            sys.exit(0)
        else:
            ans +=v[0]
    elif len(v) ==1:
        ans +=v[0]
    elif len(v) ==0:
        if i =="1":
            ans +="1" #1桁目（一番左）は✖０、最小値「１」
        else:
            ans +="0"

print(ans)

Buggy submission (verdict: Runtime Error):
import math
if kotae_dic == {}: # 一つの桁に複数の値の時        
    print(-1)
else:
    kotae_dic_sorted = sorted(kotae_dic.items(),key=lambda x:int(x[0]))
    kotae_str = "".join( [i[1][0] for i in kotae_dic_sorted ] ) # 辞書の値を連結
    kotae_int =int(kotae_str)
    if kotae_int == 0 and 1< N: # "00"などはエラー、"0"はOK
        print(-1)
    elif kotae_int == 0 and N == 1:
        print("0")
    elif 10**(N-1) > kotae_int or kotae_int >= 10** N: # 桁があってない＝一番左の桁が”０”はエラー
        print(-1)
    else:
        print(kotae_int)