1

真の定義プログラムを作成するために、これまでに行ったことを実装するのに問題があります。

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を解決する方法についての提案は大歓迎です! :)

4

3 に答える 3

16

CSコースの課題のように聞こえるので、これはOPではうまくいかないと思いますが、解決策を探している他の人は、次を使用してください。

from collections import deque

d = deque([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
d.rotate(3)  # to the right
d.rotate(-3)  # back to the left

*編集

コメントのフォローアップ ( deque docsから):

Deques は、スタックとキューを一般化したものです (名前は「デッキ」と発音され、「ダブルエンド キュー」の略です)。Deques は、deque の両側からのスレッドセーフでメモリ効率の高い追加とポップをサポートし、どちらの方向でもほぼ同じ O(1) パフォーマンスを実現します。

リスト オブジェクトは同様の操作をサポートしますが、高速な固定長操作用に最適化されており、基になるデータ表現のサイズと位置の両方を変更する pop(0) および insert(0, v) 操作の O(n) メモリ移動コストが発生します。 .

于 2013-10-15T03:31:17.680 に答える
4

これをチェックしてください:

>>> a
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]

4 循環シフト:

>>> b = a[4::] + a[:4:]
>>> b
[4, 5, 6, 7, 8, 9, 0, 1, 2, 3]

および 2 つの関数形式で:

def shiftLbyn(arr, n=0):
    return arr[n::] + arr[:n:]

def shiftRbyn(arr, n=0):
    return arr[n:len(arr):] + arr[0:n:]

それらを呼び出す:

print shiftLbyn([1,2,3,4,5,6,7,8], 3)
print shiftRbyn([1,2,3,4,5,6,7,8], 4)

出力が得られます:

[4, 5, 6, 7, 8, 1, 2, 3]
[5, 6, 7, 8, 1, 2, 3, 4]
于 2013-10-15T03:35:34.267 に答える
2

最初に関数を変更して、パラメーターを受け取り、結果を返します。例えば

def left(listL, k):
    right = listL[k::]
    left = listL[:k:]
    return right + left # is this the usual meaning of left and right?

# This is how you call the function
print(left([0, 1, 2, 3, 4, 5, 6, 7, 8, 9], 4))

これに気付き、最後の 3 行が同じであるleftとします。rightこのように組み合わせることができます

def shift(listL, k, direction):
    if direction == "right":
        k = len(listL) - k
    right = listL[k::]
    left = listL[:k:]
    return right + left

こんなmain感じなんだろうな

def main(listL):
    print(listL)
    print(shift(listL, 4, "left"))
    print(shift(listL, 4, "right"))
于 2013-10-15T03:33:02.780 に答える