qyliang/CodeNanoFix huggingface dataset:
Buggy code:
n = input(); a = 0; b = 0; c = 0
for i in range(len(n)):
    if n[i] != "9":
        if a == 0: a += 1; b = i; c = int(n[i])
        else: a += 1
if a == 0: print(9*len(n))
elif a == 1 and b == 0: print(c+9*(len(n)-1))
else: print(c-1+9*(len(n)-1))

Fixed code:
n = input(); a = 0; b = 0; c = int(n[0])
for i in range(len(n)):
    if n[i] != "9":
        if a == 0: a += 1; b = i
        else: a += 1
if a == 0: print(9*len(n))
elif a == 1 and b == 0: print(c+9*(len(n)-1))
else: print(c-1+9*(len(n)-1))
Buggy code:
N=list(str(input()))
ans=0
for i in range(1,N):
    if N[i]!='9':
        ans+=int(N[0])-1
        ans+=(len(N)-1)*9
        print(ans)
        exit()
for i in range(N):
    ans+=int(N[i])
print(ans)
        

Fixed code:
N=list(str(input()))
ans=0
if len(N)==1:
    print(int(N[0]))
    exit()
for i in range(1,len(N)):
    if N[i]!='9':
        ans+=int(N[0])-1
        ans+=(len(N)-1)*9
        print(ans)
        exit()
for i in range(len(N)):
    ans+=int(N[i])
print(ans)
Buggy code:
N = input()
N_row = len(N)
while True:
  if N_row == 1:
    print(int(N))
    break
  else:
    N = list(N)
    a = int(N[0])
    b = int(N[-1])
    for i in range(N_row):
      if not int(N[i]) == 9:
        sum = (a-1) + 9*(N_row-1)  
        print(sum)
        break
      else:
        sum = a + 9*(N_row-1)
        print(sum)
        break
    break

Fixed code:
N = input()
N_row = len(N)
while True:
  if N_row == 1:
    print(int(N))
    break
  else:
    N = list(N)
    a = int(N[0])
    b = int(N[-1])
    for i in range(1, N_row):
      if not int(N[i]) == 9:
        sum = (a-1) + 9*(N_row-1) 
        print(sum)
        break
    else:
      sum = a + 9*(N_row-1)
      print(sum)
      break
  break
Buggy code:
N = input()
N_list = list(N)
N = int(N)
K = len(N_list)

ans = int(N_list[0])+(K-1)*9
ans_list = [N_list[0]] + ['9' for _ in range(K-1)]
ans_cand = int(''.join(ans_list))
print(ans_cand)
if N < ans_cand:
    print(ans - 1)
else:
    print(ans)

Fixed code:
N = input()
N_list = list(N)
N = int(N)
K = len(N_list)

ans = int(N_list[0])+(K-1)*9
ans_list = [N_list[0]] + ['9' for _ in range(K-1)]
ans_cand = int(''.join(ans_list))
if N < ans_cand:
    print(ans - 1)
else:
    print(ans)
Buggy code:
n = input()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

tmp = ''
for i in range(1,len(n)):
    if n[i] == '9':
        tmp += n[i-1]
        print(tmp)
    else:
        n[i]!= '9'
        tmp += str(int(n[i-1])-1) + '9'*(len(n)-i)
        break
else:
    tmp += '9'

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)


Fixed code:
n = input()

if len(list(n)) == 1:
    print(int(n))
    exit()

if set(list(n)) == {'9'}:
    print(len(list(n))*9)
    exit()

tmp = ''
for i in range(1,len(n)):
    if n[i] == '9':
        tmp += n[i-1]
    else:
        tmp += str(int(n[i-1])-1) + '9'*(len(n)-i)
        break
else:
    tmp += '9'

ans=0
for i in range(len(tmp)):
    ans += int(tmp[i]) 

print(ans)
Buggy code:
X = list(map(int,input()))
print(X)
sn = []
for i in range(len(X)):
  sn.append(sum(X[0:i+1]))
ssn = []
for i in range(len(X)):
  ssn.append(sn[i]-1 + 9*(len(X)-1-i))
print(max(ssn))

Fixed code:
X = list(map(int,input()))
sn = []
for i in range(len(X)):
  sn.append(sum(X[0:i+1]))
ssn = []
for i in range(len(X)):
  if (sn[i] != 0):
    ssn.append(sn[i]-1 + 9*(len(X)-1-i))
ssn.append(sum(X))
print(max(ssn))
Buggy code:
n = list(input())

if len(n) == 1:
    print(n[0])
elif n[0] == '1':
    print(9 * (len(n) - 1))
elif n.count('9') == len(n):
    print(9 * len(n))
elif n.count('9') == len(n) - 1:
    if n[0] != 9:
        print((int(n[0])) + (9 * (len(n) - 1)))
    else:
        print(9 * (len(n) - 1) + 8)
else:
    print((int(n[0]) - 1 ) + (9 * (len(n) - 1))) 

Fixed code:
n = list(input())

if len(n) == 1:
    print(n[0])
elif n[0] == '1':
    if n.count('9') == len(n) - 1:
        print(1 + 9 * (len(n) - 1))
    else:
        print(9 * (len(n) - 1))
elif n.count('9') == len(n):
    print(9 * len(n))
elif n.count('9') == len(n) - 1:
    if n[0] != '9':
        print((int(n[0])) + (9 * (len(n) - 1)))
    else:
        print(9 * (len(n) - 1) + 8)
else:
    print((int(n[0]) - 1 ) + (9 * (len(n) - 1)))
Buggy code:
n = int(input())

sum = int(n[0]) + (len(n)-1) * 9
print(sum if set(n[1:]) == {"9"} else sum - 1)


Fixed code:
n = input()

sum = int(n[0]) + (len(n)-1) * 9

if len(n) == 1:
    print(n)
else:
    print(sum if set(n[1:]) == {"9"} else sum - 1)
Buggy code:
def maxf(n):
    a = list(map(int, list(n)))
    for i, item in enumerate(a):
        if i == 0 and item != 9:
            a[i] -=1
            a[i+1:] = [9]*(len(a)-1)
            break
        elif item != 9:
            a[i-1] -= 1
            a[i:] = [9]*(len(a)-i)
            break
    return sum(a)

print(maxf(input())


Fixed code:
def maxf(n):
    a = list(map(int, list(n)))
    for i, item in enumerate(a):
        if i == 0 and item != 9:
            if a[i+1:] == [9]*(len(a)-1):break
            a[i] -=1
            a[i+1:] = [9]*(len(a)-1)
            break
        elif item != 9:
            a[i-1] -= 1
            a[i:] = [9]*(len(a)-i)
            break
    return sum(a)

print(maxf(input()))
Buggy code:
N = int(input())
n = len(str(N))
NS = str(N)
ans0 = 0
for i in range(NS):
    ans0 += int(i)
ans1 = int(NS[0])-1 + 9 * (n-1)
ans2 = 9 * (n-1)

print(max(ans0, ans1, ans2))


Fixed code:
N = int(input())
n = len(str(N))
NS = str(N)
ans0 = 0
for i in NS:
    ans0 += int(i)
ans1 = int(NS[0])-1 + 9 * (n-1)
ans2 = 9 * (n-1)

print(max(ans0, ans1, ans2))
Buggy code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = [int(i) for i in list(input())]
length = len(N)

for l in N[:1]:
    if not l==9:
        ans = (length-1)*9+(N[0]-1)
        print(ans)
        break
else:
    print(sum(N))


Fixed code:
import sys
def input(): return sys.stdin.readline().strip()
def mapint(): return map(int, input().split())
sys.setrecursionlimit(10**9)

N = [int(i) for i in list(input())]
length = len(N)

for l in N[1:]:
    if not l==9:
        ans = (length-1)*9+(N[0]-1)
        print(ans)
        break
else:
    print(sum(N))
Buggy code:
a=list(input())
p=print
x=len(a)
y=int(a[0])
print(9*x-9+y if all(i=='9' for i in a[1:]) else y+9*(x-1))

Fixed code:
a=list(input())
p=print
x=len(a)-1
y=int(a[0])
print(9*x+y if all(i=='9' for i in a[1:]) else y-1+9*x)
Buggy code:
n=int(input())
if n<=9:
  print(n)
else:
  n=str(n)
  x=n[0]
  x=int(x)-1
  if n[1:].count('9')==len(n-1):
    print(x+1(len(n)-1)*9)
  else:
    print(x+(len(n)-1)*9)


Fixed code:
n=int(input())
if n<=9:
  print(n)
else:
  n=str(n)
  x=n[0]
  x=int(x)-1
  if n[1:].count('9')==len(n)-1:
    print(x+1+(len(n)-1)*9)
  else:
    print(x+(len(n)-1)*9)
Buggy code:
n = int(input())
k = len(str(n))
a = 9 * (k - 1)
b = 0
for i in range(k):
  b += int(str(n)[i])
c = 0
if k > 2:
  m = str(int(n))[:k - 2] + str(int(int(str(n)[-2]) - 1)) + '9'
  for i in range(k):
    c += int(m[i])
ans = max(a, b, c)
print(ans)

Fixed code:
n = input()
k = len(n)
a = 9 * (k - 1) + int(n[0]) - 1
b = 0
for i in range(k):
  b += int(n[i])
c = 0
m = ''
if k > 2 and n[-2] != '0':
  m = str(int(n))[:k - 2] + str(int(n[-2]) - 1) + '9'
elif k > 2 and n[-2] == '0':
  m = str(int(n) - (int(n[-1]) + 1))
for i in range(len(m)):
  c += int(m[i])
ans = max(a, b, c)
print(ans)
Buggy code:
Num = str(input())
L = len(Num)
if Num[1:] == '9'*(l-1):
  val = int(Num[0]) + 9*(l-1)
else:
  val = int(Num[0])-1 + 9*(l-1)
print(val)

Fixed code:
Num = str(input())
L = len(Num)
if Num[1:] == '9'*(L-1):
  val = int(Num[0]) + 9*(L-1)
else:
  val = int(Num[0])-1 + 9*(L-1)
print(val)
Buggy code:
N = int(input())

for i in range(17):
    if 10**i <=N <10**(i+1):
        m=i+1
        break

for j in range(1,10):
    if (10**(m-1))*j + 10**(m-1)-1 <=N< (j+1)*(10**(m-1))+10**(m-1)-1:
        a = j
        break

print(a+9*(m-1))

Fixed code:
N = int(input())

for i in range(17):
    if 10**i <=N <10**(i+1):
        m=i+1
        break

for j in range(0,10):
    if (10**(m-1))*j + 10**(m-1)-1 <=N< (j+1)*(10**(m-1))+10**(m-1)-1:
        a = j
        break

print(a+9*(m-1))
Buggy code:
N = int(input())
x = 9
order = 0
for i in range(16):
    if x > N:
        break
    x = x*10+9
    order = i+1
ans = x
for i in range(10):
    ans -= 10**order
    print(ans)
    if  ans < N:
        break
ret = 0
while True:
    if ans == 0:
        break
    ret += ans % 10
    ans = ans//10
print(ret)

Fixed code:
N = int(input())
x = 9
order = 0
for i in range(16):
    if x > N:
        break
    x = x*10+9
    order = i+1
ans = x
for i in range(10):
    ans -= 10**order
    if  ans <= N:
        break
ret = 0
while True:
    if ans == 0:
        break
    ret += ans % 10
    ans = ans//10
print(ret)
Buggy code:
s=input()
k=10**(len(s)-1)
print(sum(map(int,str(int(s)//k))) if len(s)!=1 else s)

Fixed code:
s=input()
k=10**(len(s)-1)
#print(s,str(int(s)//k*k-1))
print(sum(map(int,str((int(s)+1)//k*k-1))) if len(s)!=1 else s)
Buggy code:
import bisect
N = int(input())
if len(str(N)) == 1:
    print(N)
    exit()
L = len(list(str(N)))-1
A = [10**(L)*i+10**(L)-1 for i in range(10)]  
n = bisect.bisect_left(A,N)-1
if N in A:
    n += 1
ans = [int(list(str(A[n]))[i]) for i in range(len(list(str(A[n]))))]
print(sum(ans))
print(A)

Fixed code:
import bisect
N = int(input())
if len(str(N)) == 1:
    print(N)
    exit()
L = len(list(str(N)))-1
A = [10**(L)*i+10**(L)-1 for i in range(10)]  
n = bisect.bisect_left(A,N)-1
if N in A:
    n += 1
ans = [int(list(str(A[n]))[i]) for i in range(len(list(str(A[n]))))]
print(sum(ans))
Buggy code:
n=str(int(input())+1)
print(sum(map(int,list(n[0]+[0]*(len(n)-1)))))


Fixed code:
n=str(int(input())+1)
print(sum(map(int,list(str(int(n[0]+'0'*(len(n)-1))-1)))))
Buggy code:
X = int(input())
Y = list(map(int, str(X)))
if X >= 10:
  ans = max(X, Y[0]-1+9*(len(Y)-1))
else:
  ans = X
print(ans)


Fixed code:
X = int(input())
Y = list(map(int, str(X)))
if X >= 10:
  ans = max(sum(Y), Y[0]-1+9*(len(Y)-1))
else:
  ans = X
print(ans)
Buggy code:
n = list(map(int, list(input().strip())))
print(n)

total = sum(n)

for i in range(len(n)):
    subtotal = 0
    for j in range(i - 1):
        subtotal += n[j]
    subtotal += n[i] - 1
    for k in range(i + 1, len(n)):
        subtotal += 9

    total = max(total, subtotal)

print(total)


Fixed code:
n = list(map(int, list(input().strip())))

total = sum(n)

for i in range(len(n)):
    subtotal = 0
    for j in range(i - 1):
        subtotal += n[j]
    subtotal += n[i] - 1
    for k in range(i + 1, len(n)):
        subtotal += 9

    total = max(total, subtotal)

print(total)
Buggy code:
N = list(map(str, input().split()))
print(max(sum([int(i) for i in N]), 9*(len(N)-1)+int(N[0])-1))


Fixed code:
N = list(map(str, input().rstrip()))
print(max(sum([int(i) for i in N]), 9*(len(N)-1)+int(N[0])-1))
Buggy code:
s = list(input())
ans = [sum(map(int, s)), 9*(len(s)-1) + int(s[0]-1)]
print(max(ans))

Fixed code:
n = input()
ans = sum(map(int,list(n)))
ans = max(ans,int(n[0])-1+(len(n)-1)*9)
print(ans)
Buggy code:
import sys
input = sys.stdin.readline

# A - Digit Sum 2
def digit_sum(str_num):
	sum = 0	
	
	for i in range(len(str_num)):
		sum += int(str_num[i])
	
	return sum


N = input()
patterns = [N]

for i in range(len(N)-1):
	if N[i] != '0':
		left, right = '', ''  
				
		if i > 0:
			left = N[:i]
		
		n = int(N[i]) - 1
		
		if i < len(N) - 1:
			right = '9' * (len(N)-i-1) 
		
		patterns.append(left + str(n) + right)

ans = 0

for p in patterns:
	ans = max(ans, digit_sum(p))
	
print(ans)

Fixed code:
import sys
#input = sys.stdin.readline

# A - Digit Sum 2
def digit_sum(str_num):
	sum = 0	
	
	for i in range(len(str_num)):
		sum += int(str_num[i])
	
	return sum


N = input()
patterns = [N]

for i in range(len(N)-1):
	if N[i] != '0':
		left, right = '', ''  
				
		if i > 0:
			left = N[:i]
		
		n = int(N[i]) - 1
		
		if i < len(N) - 1:
			right = '9' * (len(N)-i-1) 
		
		patterns.append(left + str(n) + right)

ans = 0

for p in patterns:
	ans = max(ans, digit_sum(p))
	
print(ans)
Buggy code:
N = input()
Dig = len(N)
num = None
if Dig == 1:
    print(N)
else:
    for i in range(Dig-1):
        if N[i] != str(9) and N[i+1] != str(9):
            num = i
            print(num)
            break
        else:
            continue
    list_num = list(N)
    if num:
        target = int(list_num[num]) -1
        ans = 9*(num -1) + target + 9*(Dig)
        print(ans)
    else:
        array = list(map(int,N))
        print(sum(array))
    

Fixed code:
N = input()
Dig = len(N)
num = None
if Dig == 1:
    print(N)
else:
    for i in range(Dig-1):
        if N[i+1] != str(9):
            num = i
            break
        else:
            continue
    list_num = list(N)
    if not num == None:
        target = int(list_num[num]) -1
        array = list(map(int,N))
        ans = sum(array[:num]) + target + 9*(Dig-num-1)
        print(ans)
    else:
        array = list(map(int,N))
        print(sum(array))
Buggy code:
n = str(input().rstrip()+1)
ans = len(n)*9
ans += int(n[0])-1
print(ans)

Fixed code:
n = str(int(input().rstrip())+1)
ans = (len(n)-1)*9
ans += (int(n[0])-1)
print(ans)
Buggy code:
n=int(input())
k=list(str(n))
ans=0
print(max(sum(k),9*len(k)-9+k[0]-1))


Fixed code:
n = list(map(int, list(str(input()))))
ans = sum(n)
ans = max(ans, n[0] - 1 + 9 * (len(n) - 1))
print(ans)
Buggy code:
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N = input()
    M = [int(c) for c in N]

    lm=len(M)
    cnt=0
    if set(M[1:])=={9}:
        for i in M:
            cnt+=i
        print(cnt)
    else:
        print(9*(lm-1)+M[0]-1)

resolve()

Fixed code:
import sys
sys.setrecursionlimit(10 ** 5 + 10)
def input(): return sys.stdin.readline().strip()

def resolve():
    N = input()
    M = [int(c) for c in N]

    lm=len(M)
    cnt=0
    if lm==1:
        print(M[0])
    elif set(M[1:])=={9}:
        for i in M:
            cnt+=i
        print(cnt)
    else:
        print(9*(lm-1)+M[0]-1)

resolve()
Buggy code:
ans = ""
a = input()
num_len = len(a)
num = [k for k in a]
for i in range(1,num_len):
  if num[i] != "9":
    num[i-1]= chr(ord(num[i-1])-1)
    if num[i-1] == "0":
      num[i-1] = ""
    for j in range(i,num_len):
      num[j] = "9"
    break
for w in num:
  ans = ans + w
print(ans)

Fixed code:
ans = 0
a = input()
num_len = len(a)
num = [k for k in a]
for i in range(1,num_len):
  if num[i] != "9":
    num[i-1]= chr(ord(num[i-1])-1)
    for j in range(i,num_len):
      num[j] = "9"
    break
for w in num:
  ans = ans + int(w)
print(ans)
Buggy code:
N=input()
M=int(N[0:2])
L=len(N[2:])*9
K=0
for i in range(int(M+1)):
	j=str(i)
	if len(j)==1:
		K=max(K,(int(j)))
	else:
		K=max(K,(int(j[0])+int(j[1])))
print(K+L)

Fixed code:
N=input()
M=int(N[0:2])
L=len(N[2:])*9
K=0
sum=0
for i in range(len(N)):
	sum+=int(N[i])
K=0
if L>0:
	D=M
else:
	D=M+1
for i in range(D):
	j=str(i)
	if len(j)==1:
		K=max(K,(int(j)))
	else:
		K=max(K,(int(j[0])+int(j[1])))
print(max(sum,K+L))
Buggy code:
n=list(map(int,input()))
s=sum(n)
k=len(n)
print(max(n[0]-1)+9*(k-1),s)

Fixed code:
n=list(map(int,input()))
s=sum(n)
k=len(n)
print(max((n[0]-1)+9*(k-1),s))
Buggy code:
s = input()
if len(s) == 1: print(int(s[0])
elif s[1:] == "9"*(len(s)-1): print(int(s[0]+9*len(s)-9)
else: print(int(s[0]+9*len(s)-10)                                    

Fixed code:
s = input()
if len(s) == 1: print(int(s[0]))
elif s[1:] == "9"*(len(s)-1):
  print(int(s[0])+9*len(s)-9)
else: 
  print(int(s[0])+9*len(s)-10)
Buggy code:
n=input()
if len(n)!=n.count("9"): 
    print(9*len(n)) 
elif n[0]=="1" and len(n)-1==n.count("9"): 
    print(1+9*(len(n)-1)) 
else: 
    print(int(n[0])-1+(9*(len(n)-1))) 

Fixed code:
n=input()
if len(n)==1:
    print(int(n))
elif n[1:].count("9")==len(n)-1:
    print(int(n[0])+9*(len(n)-1))
else:
    print(int(n[0])-1+9*(len(n)-1))
Buggy code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    if str(n + 1)[0] != str(n)[0]:
        print(sum(map(int, list(str(n)))))
    else:
        print(int(str(n)[0]) + (len(str(n)) - 1) * 9)
    return


#main
if __name__ == '__main__':
    solve()


Fixed code:
#!/usr/bin/env python3
from collections import defaultdict,deque
from heapq import heappush, heappop
from bisect import bisect_left, bisect_right
import sys, random, itertools, math
sys.setrecursionlimit(10**5)
input = sys.stdin.readline
sqrt = math.sqrt
def LI(): return list(map(int, input().split()))
def LF(): return list(map(float, input().split()))
def LI_(): return list(map(lambda x: int(x)-1, input().split()))
def II(): return int(input())
def IF(): return float(input())
def LS(): return list(map(list, input().split()))
def S(): return list(input().rstrip())
def IR(n): return [II() for _ in range(n)]
def LIR(n): return [LI() for _ in range(n)]
def FR(n): return [IF() for _ in range(n)]
def LFR(n): return [LI() for _ in range(n)]
def LIR_(n): return [LI_() for _ in range(n)]
def SR(n): return [S() for _ in range(n)]
def LSR(n): return [LS() for _ in range(n)]
mod = 1000000007
inf = 1e10

#solve
def solve():
    n = II()
    if str(n + 1)[0] != str(n)[0]:
        print(sum(map(int, list(str(n)))))
    else:
        print(int(str(n)[0]) + (len(str(n)) - 1) * 9 - 1)
    return


#main
if __name__ == '__main__':
    solve()
Buggy code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N = INT()
	S = [int(s) for s in str(N)]
	# print(S)

	A = []
	smaller = False
	for i in range(len(S)-1):
		if smaller:
			A.append(9)
		else:
			if S[i] == 9 and S[i+1] == 9:
				A.append(9)
			elif S[i+1] == 9:
				A.append(S[i])
			else:
				smaller = True
				A.append(S[i]-1)

	if smaller: A.append(9)
	else: A.append(S[-1])

	print(A)
	print(sum(A))

if __name__ == '__main__':
	main()

Fixed code:
import sys
sys.setrecursionlimit(10**6)
if sys.platform in (['ios','darwin','win32']):
	sys.stdin=open('Untitled.txt')
input = sys.stdin.readline
def INT(): return int(input())
def MAP(): return [int(s) for s in input().split()]

def main():
	N = INT()
	S = [int(s) for s in str(N)]
	# print(S)

	A = []
	smaller = False
	for i in range(len(S)-1):
		if smaller:
			A.append(9)
		else:
			if S[i] == 9 and S[i+1] == 9:
				A.append(9)
			elif S[i+1] == 9:
				A.append(S[i])
			else:
				smaller = True
				A.append(S[i]-1)

	if smaller: A.append(9)
	else: A.append(S[-1])

	# print(A)
	print(sum(A))

if __name__ == '__main__':
	main()
Buggy code:
from sys import stdin

s = stdin.readline().rstrip()

if len(s) == 1:
    print(int(s))
    exit()

if s == "9"*len(s):
    print(9*len(s))
else:
    print(max(int(s[0])-1+9*(len(s)-1),int(s[0])+9*(len(s)-1)))

Fixed code:
from sys import stdin

s = stdin.readline().rstrip()

if len(s) == 1:
    print(int(s))
    exit()

if s == "9"*len(s):
    print(9*len(s))
else:
    if s[1:] == "9"*(len(s)-1):
        print(int(s[0])+9*(len(s)-1))
    else:
        print(int(s[0])-1+9*(len(s)-1))
Buggy code:
n = list(map(int,input()))

if len(n) == 1:
    print(n[0])
    exit()

if n[0] >= 2:
    if n.count(9) == len(n)-1:
        print(n[0] + (len(n)-1)*9)
    else:
        print(n[0]-1 + (len(n)-1)*9)
else:
    if n.count(9) == len(n)-1:
        print(n[0]-1 + (len(n)-1)*9)
    else:
        print((len(n)-1)*9)

Fixed code:
n = list(map(int,input()))

if len(n) == 1:
    print(n[0])
    exit()

if n[1:].count(9) == len(n) -1:
    print(n[0] + (len(n)-1)*9)
else:
    print(n[0]-1 + (len(n)-1)*9)
Buggy code:
def main():
    n = input()

    l = len(n)

    nines = "9" * (l - 1)
    num = int(nines)

    for i in range(1, 10):
        tmp = str(i) + nines
        if int(tmp) <= int(n):
            num = tmp

    ans = sum(map(int, num))
    print(ans)


if __name__ == "__main__":
    main()


Fixed code:
def main():
    n = input()

    l = len(n)

    nines = "0" + "9" * (l - 1)
    nines = int(nines)

    num = nines
    for i in range(1, 10):
        tmp = i * 10 ** (l - 1) + nines
        if tmp <= int(n):
            num = tmp

    ans = sum(map(int, str(num)))
    print(ans)


if __name__ == "__main__":
    main()
Buggy code:
N=input()

if len(N)==1:
    print(N)
    exit()

def calc(str):
    l=list(map(int,str))
    all=sum(l)
    
    return all

cand1=str(int(N)-int(N[-1])-1)
cand2=str(int(N[0])*10**(len(N)-1)-1)

print(cand1,cand2,N)
print(max(calc(cand1),calc(cand2),calc(N)))

Fixed code:
N=input()

if len(N)==1:
    print(N)
    exit()

def calc(str):
    l=list(map(int,str))
    all=sum(l)
    
    return all

cand1=str(int(N)-int(N[-1])-1)
cand2=str(int(N[0])*10**(len(N)-1)-1)

print(max(calc(cand1),calc(cand2),calc(N)))
Buggy code:
n=int(input())
print(int(str(n)[0])-1+9*(len(n)-1))

Fixed code:
n = int(input())
print(int(str(n+1)[0])-1 + 9 * (len(str(n+1))-1))
Buggy code:
n=int(input())
if n < 10 :
    print(n)
else:
    n=list(str(n))
    key=0
    for i in range(n):
        if n[1+i]!="9":
            key=1
    if key ==0:
        print((len(n)-1)*9+int(n[0]))
    else:
        print((len(n)-1)*9+int(n[0])-1)




Fixed code:
n=int(input())
if n < 10 :
    print(n)
else:
    n=list(str(n))
    key=0
    for i in range(len(n)-1):
        if n[1+i]!="9":
            key=1
    if key ==0:
        print((len(n)-1)*9+int(n[0]))
    else:
        print((len(n)-1)*9+int(n[0])-1)
Buggy code:
number = input()
length = len(number)
t = True
ans = 0
for i in range(length):
    if i > 0 and number[i] != '9':
        t = False
    ans += int(number[i])

if t:
    print(ans)
else:
    print(ans-1)

Fixed code:
number = input()
length = len(number)
t = True
ans = 0
for i in range(length):
    if i > 0 and number[i] != '9':
        t = False
    if i == 0:
        ans += int(number[i])
    else:
        ans += 9

if t:
    print(ans)
else:
    print(ans-1)
Buggy code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=S()

  if n=='3141592653589793':
    exit()

  f=False
  for i in range(1,len(n)):
    if n[i]!='9':
      f=True

  if f:
    return int(n[0])-1+9*(len(n)-1)
  else:
    return int(n[0])+9*(len(n)-1)

print(main())

Fixed code:
import math,string,itertools,fractions,heapq,collections,re,array,bisect,sys,random,time

sys.setrecursionlimit(10**7)
inf=10**20
mod=10**9+7

def LI(): return list(map(int,input().split()))
def I(): return int(input())
def LS(): return input().split()
def S(): return input()

def main():
  n=S()

  f=False
  for i in range(1,len(n)):
    if n[i]!='9':
      f=True

  if f:
    return int(n[0])-1+9*(len(n)-1)
  else:
    return int(n[0])+9*(len(n)-1)

print(main())
Buggy code:
def sol(n):
    #n=input()
    if len(set(n[1:]))<=1 and (n[-1]=='9' or len(n)==1):
      m=0
    else:
      m=1
    print(int(n[0])+9*len(n)-9-m)

n=input()
sol(str(i))

Fixed code:
def sol(n):
    #n=input()
    if len(set(n[1:]))<=1 and (n[-1]=='9' or len(n)==1):
      m=0
    else:
      m=1
    print(int(n[0])+9*len(n)-9-m)

n=input()
sol(str(n))