习题12:Reverse|习题12:Reverse or rotate?
【习题12:Reverse|习题12:Reverse or rotate?】是倒置呢还是错位
The input is a string str of digits. Cut the string into chunks of size sz (ignore the last chunk if its size is less than sz).If a chunk represents an integer such as the sum of the cubes of its digits is divisible by 2, reverse it;
otherwise rotate it to the left by one position. Put together these modified chunks and return the result as a string.
Examples:
revrot("123456987654", 6) --> "234561876549"
revrot("123456987653", 6) --> "234561356789"
revrot("66443875", 4) --> "44668753"
revrot("66443875", 8) --> "64438756"
revrot("664438769", 8) --> "67834466"
revrot("123456779", 8) --> "23456771"
revrot("", 8) --> ""
revrot("123456779", 0) --> ""
def revrot(strng, sz):
# your code
# slice to chunks e.g strng[(n-1)*sz: n*sz]
if sz <= 0:
return ''
r = ''
for k in range(len(strng)//sz):
chunk = strng[k*sz:(k+1)*sz]
if sum(int(i) for i in chunk)%2 == 0:
chunk = chunk[::-1]
else:
chunk = chunk[1:]+chunk[0]
r += chunk
return r
推荐阅读
- day03_3_流程控制练习题
- python|python篇 习题(循环语句)
- python循环语句选择题_python循环语句以及一些日常的练习题目
- python编程习题(循环语句)
- 笔记|利用canvas画布和rotate()方法让画的图形旋转起来
- 离散数学|-离散数学-期末练习题解析
- Java日常练习题|Java日常练习题,每天进步一点点(22)
- 编程入门|这Python100道练习题及答案送给你,学完直接上手做项目
- 免费python题库_128道Python练习题及答案送给你,学完直接上手做项目
- python考试题目及答案-这就是你需要的python99道练习题(附答案)