0027-编程打卡:实现表格的行列转换
本帖最后由 不二如是 于 2022-9-29 19:18 编辑一星答案:
def spreadsheet(s):
letter = ('ABCDEFGHIJKLMNOPQRSTUVWXYZ')
letter = list(letter)
s = list(s)
x = []
y = []
if(s == 'R' and s.isdigit() and 'C' in s):
while 'C' in s:
y.append(s.pop())
y.reverse()
temp = int(''.join(y))
while temp:
x_int= temp % 26
temp = temp // 26
if x_int == 0:
temp -= 1
x.append(letter)
x.reverse()
x_str = ''.join(x)
y_str = ''.join(s)
return(x_str+y_str)
else:
num = 0
y_int = 0
temp = s.copy()
for i in range(len(''.join(temp))):
if not temp.isdigit():
y.append(temp)
s.remove(temp)
while y != []:
temp = y.pop()
for i in range(26):
if letter == 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
else:
c = re.search(r'\D+', s).group()
r = re.search(r'\d+', s).group()
c_lst =
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), ''
while n:
n, r = divmod(n-1, 26)
cStr += chr(r + 65)
return "{}{}".format(cStr[::-1], nums)
else:
return "R{}C{}".format(nums, sum( 26**i * (ord(c)-64) for i,c in enumerate(re.sub(r'\d', '', s)[::-1])))
基础语法:
https://www.bilibili.com/video/BV1c4411e77t
算法讲解:
https://www.bilibili.com/video/BV1HT4y1K7DY
本帖最后由 hveagle 于 2022-10-1 19:37 编辑
@不二如是 有些编程打卡没加进淘贴,到这个淘贴补一下缺失的打卡(15,25)吧:
https://fishc.com.cn/forum.php?mod=collection&action=view&ctid=1998
内涵所有零基础入门学习Python【最新版】板块内容,所有Python奇技淫巧,所有编程打卡,所有Python高分笔记,一些作品。
页:
[1]