真の定義プログラムを作成するために、これまでに行ったことを実装するのに問題があります。
def left():
listL = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
k=4
right = listL[k::]
left = listL[:k:]
print(right + left)
def right():
listL = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
k=len(listL)-4
right = listL[k::]
left = listL[:k:]
print(right + left)
私のコードは、k、この場合は 4 だけ左または右に移動することに基づいて、元の listL を再作成する場所を claculates します。しかし、私の練習問題は尋ねます...
Given a list of N numbers, write a function to shift the numbers circularly by some integer k (where k < N). The function should take the list and k as arguments and return the shifted list.
a) Write a function that assumes the shifting is to the left. It should not print anything.
b) Write a function that takes a third argument that specifies shifting left or right. It should not print anything. Perform whatever error-checking you consider necessary.
c) Write a main() function that calls the above functions. It should print the lists both before and after shifting. Perform whatever error-checking you consider necessary.
私はパート A を満足しましたが、問題の質問を完全に再現するためにパート B と C をどのように構築するかについて混乱しています。
ソリューション サンプルの実行:
Sample run
>>> ================================ RESTART ================================
>>>
original list: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
shifted by 4, to the left: [4, 5, 6, 7, 8, 9, 0, 1, 2, 3]
shifted by 4, to the right: [6, 7, 8, 9, 0, 1, 2, 3, 4, 5]
パートbとcを解決する方法についての提案は大歓迎です! :)