Write a function shiftByTwo(*args) that takes in variable-length argument and returns a tuple with its elements shifted to the right by two indices. See samples given below.
大概意思就是写一个函数使元组里面的元素的索引都能往前面移动两步
>>> shiftByTwo(1,2,3,4,5,6) (5, 6, 1, 2, 3, 4) >>> shiftByTwo('a','b','c','d') ('c', 'd', 'a', 'b') >>> shiftByTwo('a','b') ('a', 'b') >>> shiftByTwo('b') ('b',)先找规律:在例子里面每一个元组里面的元素除了最后两个,其它的都是索引值加2,最后两个元素的索引值应该他们的索引值减去他们前面元素之和的长度len,即得到他们的索引值
def shiftByTwo(*args):
list1 = list(args)
list2 = []
list4 = []
list6 = []
for i,j in enumerate(list1):
list2.append((i+2,j)) #这里先把元组里面的所有索引值都加2
list3 = list2[-2:] #提取元组里面的最后两个元素
for k in list3:
list4.append(((k[0] - len(list1)),k[1])) #将提取出的两个元素的索引值减去元组的长度
list5 = sorted(list2[:-2] + list4) #将提取出来的两个元素与原来的元素合并到一起
for t in list5:
list6.append(t[1])
return list6
print(shiftByTwo(1,2,3,4,5,6))