Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 383019
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T15:14:32+00:00 2026-05-12T15:14:32+00:00

Question source: SPOJ.. ORDERS def swap(ary,idx1,idx2): tmp = ary[idx1] ary[idx1] = ary[idx2] ary[idx2] =

  • 0

Question source: SPOJ.. ORDERS

def swap(ary,idx1,idx2):
    tmp = ary[idx1]
    ary[idx1] = ary[idx2]
    ary[idx2] = tmp

def mkranks(size):
    tmp = []
    for i in range(1, size + 1):
        tmp = tmp + [i]
    return tmp

def permutations(ordered, movements):
    size = len(ordered)
    for i in range(1, size): # The leftmost one never moves
        for j in range(0, int(movements[i])):
            swap(ordered, i-j, i-j-1)
    return ordered

numberofcases = input()
for i in range(0, numberofcases):
    sizeofcase = input()
    tmp = raw_input()
    movements = ""
    for i in range(0, len(tmp)):
        if i % 2 != 1:
            movements = movements + tmp[i]
    ordered = mkranks(sizeofcase)
    ordered = permutations(ordered, movements)
    output = ""
    for i in range(0, sizeofcase - 1):
        output = output + str(ordered[i]) + " "
    output = output + str(ordered[sizeofcase - 1])
    print output
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T15:14:32+00:00Added an answer on May 12, 2026 at 3:14 pm

    Having made your code a bit more Pythonic (but without altering its flow/algorithm):

    def swap(ary, idx1, idx2):
        ary[idx1], ary[idx2] = [ary[i] for i in (idx2, idx1)]
    
    def permutations(ordered, movements):
        size = len(ordered)
        for i in range(1, len(ordered)):
            for j in range(movements[i]):
                swap(ordered, i-j, i-j-1)
        return ordered
    
    numberofcases = input()
    for i in range(numberofcases):
        sizeofcase = input()
        movements = [int(s) for s in raw_input().split()]
        ordered = [str(i) for i in range(1, sizeofcase+1)]
        ordered = permutations(ordered, movements)
        output = " ".join(ordered)
        print output
    

    I see it runs correctly in the sample case given at the SPOJ URL you indicate. What is your failing case?

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

My source is at: https://gist.github.com/f01685376a02a577a9cb the method in question: def is_valid_email?(address) User.find_by_email(address) end I
This question is about etiquette and open source projects. I have forked an application
I asked a question earlier today ( Methods of Managing Source Code ) as
The question is simple, can I have the java source code (e.g. FooParser.java) generated
I have the following question. I have a set of Fortran90 source files, most
Thanks to this question (click me!) , I have the Source property of my
This is just a simple question. I've been reading the source of something which
This is probably an easy question... I have 4 source versions of the same
Link to the site in question . You can look at the source for
Can't find an answer to my exact question. We migrated to a different source

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.