English 中文(简体)
诽谤问题是否正确?
原标题:Dilemma in a swapping question - whose logic is correct?

Define a function SWAP(SCORES) to swap all those values in the list of SCORES, which are alternate elements. For example: If the SCORES contain [200, 456, 300, 100, 234, 678], the output should be displayed as [300, 200, 100, 456, 678, 234].

现在的安排模式是特殊的,因为如果[1,2,3,4,5,6]是命令,那么结果就是命令[3,1,4,2,6,5]。

while True:
    l=eval(input( List:  ))
    x=len(l)
    n=x//4
    t=4*n
    l1=l[:t]
    print(l1)
    l2=l[t:]
    print(l2)
    for i in range((len(l1)//2)):
        l1[i],l1[i+2]=l1[i+2],l1[i]
        #l1 shuffling over
    if len(l2)==1:
        l1+=l2
        print( Shuffled list: ,l1)
    elif len(l2)>=2:
        l2=l2[::-1]
        l1+=l2
        print( Shuffled list: ,l1)
    elif len(l2)==0:
        print( Shuffled list: ,l1)
        #treating l2 separately

这是我所尝试的,对我来说,这确实是行之有效的。

================================== RESTART: C:/Users/Harikrishnan/OneDrive/Desktop/peculiar shuffling q.py ==================================
List: [200,456,300,100,234,678]
[200, 456, 300, 100]
[234, 678]
Shuffled list: [300, 100, 200, 456, 678, 234]
List: [1,2,3,4]
[1, 2, 3, 4]
[]
Shuffled list: [3, 4, 1, 2]
List: [1,2,3,4,5]
[1, 2, 3, 4]
[5]
Shuffled list: [3, 4, 1, 2, 5]
List: [1,2,3,4,5,6,7]
[1, 2, 3, 4]
[5, 6, 7]
Shuffled list: [3, 4, 1, 2, 7, 6, 5]
List: [1,2,3,4,5,6,7,8]
[1, 2, 3, 4, 5, 6, 7, 8]
[]
Shuffled list: [3, 4, 5, 6, 1, 2, 7, 8]
List: 
Traceback (most recent call last):
  File "C:/Users/Harikrishnan/OneDrive/Desktop/peculiar shuffling q.py", line 2, in <module>
    l=eval(input( List:  ))
KeyboardInterrupt

我的逻辑是:直到第4点,所有东西都是罚款的。 Od的立场论点是 s弄的,甚至立场论点是 s弄的,在四起案件发生后:

  1. Nothing was left after the 4n elements.
  2. 1 element was left,
  3. 2 elements were left,
  4. 3 elements were left.

我的朋友有另一个逻辑:

i 认为这一模式是最后两个因素,最后三个是第5号,最后四个是第6号,最后七个是第10号,最后八个是第11号是第12号

现在我感到困惑。 我们谁是正确的?

最佳回答
#It is known that [200,456,300,100,234,678]
#has to be swapped to [300,200,100,456,234,678]
def swap(l):
    for i in range(2):
        while True:
            l[i],l[i+2]=l[i+2],l[i]
            i+=4
            if i>len(l)-3:
                break
while True:
    try:
        l=eval(input( Enter list:  ))
        swap(l)
        print(l)
    except KeyboardInterrupt:
        print( Testing done )
        break
问题回答
def swap(lst):
    l=len(lst)
    l2=list(lst)
    l3=[]
    if l<4:
        for i in range(len(l2)):
            if i+2<len(l2):
                l3.append(l2[i+2])
                l3.append(l2[i])
    elif l>4:
        l4=lst[:4]
        l5=lst[4:]
        for i in range(len(l4)):
            if i+2<len(l4):
                l3.append(l4[i+2])
                l3.append(l4[i])
        for i in range(0,len(l5)-1,2):
            l5[i],l5[i+1]=l5[i+1],l5[i]
    lst=l3+l6
    return lst
print(swap([200,456,300,100,234,678]))
print(swap([200,456,300,100,234,678,9,10,67,69]))

Output: [300, 200, 100, 456, 678, 234] [300, 200, 100, 456, 678, 234, 10, 9, 69, 67]

所提到的问题不是普通的sh/tern。

看看理解的形象:1

Here is my thought process: For the first four elements, last two elements replace [n-2]th element for the next consecutive terms, each term is swapped with next one continously PS: All the best for exams broo!

But I think your code only swaps 1st with the third and 2nd with the fourth And I get the output as [300,100,200,456,234,678] I can try refining my code..





相关问题
Get webpage contents with Python?

I m using Python 3.1, if that helps. Anyways, I m trying to get the contents of this webpage. I Googled for a little bit and tried different things, but they didn t work. I m guessing that this ...

What is internal representation of string in Python 3.x

In Python 3.x, a string consists of items of Unicode ordinal. (See the quotation from the language reference below.) What is the internal representation of Unicode string? Is it UTF-16? The items ...

What does Python s builtin __build_class__ do?

In Python 3.1, there is a new builtin function I don t know in the builtins module: __build_class__(...) __build_class__(func, name, *bases, metaclass=None, **kwds) -> class Internal ...

what functional tools remain in Python 3k?

I have have read several entries regarding dropping several functional functions from future python, including map and reduce. What is the official policy regarding functional extensions? is lambda ...

Building executables for Python 3 and PyQt

I built a rather simple application in Python 3.1 using PyQt4. Being done, I want the application to be distributed to computers without either of those installed. I almost exclusively care about ...

热门标签