马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 不二如是 于 2022-9-29 19:18 编辑
一星答案:
def spreadsheet(s):
letter = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
letter = list(letter)
s = list(s)
x = []
y = []
if(s[0] == 'R' and s[1].isdigit() and 'C' in s[2:]):
while 'C' in s:
y.append(s.pop())
y.reverse()
temp = int(''.join(y[1:]))
while temp:
x_int = temp % 26
temp = temp // 26
if x_int == 0:
temp -= 1
x.append(letter[x_int-1])
x.reverse()
x_str = ''.join(x)
y_str = ''.join(s[1:])
return(x_str+y_str)
else:
num = 0
y_int = 0
temp = s.copy()
for i in range(len(''.join(temp))):
if not temp[i].isdigit():
y.append(temp[i])
s.remove(temp[i])
while y != []:
temp = y.pop()
for i in range(26):
if letter[i] == temp:
y_int += (i+1) * (26 ** num)
num += 1
return('R'+''.join(s)+'C'+str(y_int))
二星答案:
def spreadsheet(s):
import re, functools
if re.search(r'\dC', s):
r, c = s.split('C')
c = int(c)
column = ''
while c:
c -= 1
column = chr(c % 26 + 65) + column
c //= 26
return column + r[1:]
else:
c = re.search(r'\D+', s).group()
r = re.search(r'\d+', s).group()
c_lst =[ord(i) - 64 for i in c]
c = functools.reduce(lambda x, y: x * 26 + y, c_lst)
return 'R' + r + 'C' + str(c)
三星答案:
import re
def spreadsheet(s):
nums = re.findall(r'(\d+)', s)
if len(nums) == 2:
n, cStr = int(nums[1]), ''
while n:
n, r = divmod(n-1, 26)
cStr += chr(r + 65)
return "{}{}".format(cStr[::-1], nums[0])
else:
return "R{}C{}".format(nums[0], sum( 26**i * (ord(c)-64) for i,c in enumerate(re.sub(r'\d', '', s)[::-1])))
基础语法:
算法讲解:
|