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

  • SEARCH
  • Home
  • 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 4005828
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T08:26:28+00:00 2026-05-20T08:26:28+00:00

I am trying to reverse order of given numbers in python.The problem says that

  • 0

I am trying to reverse order of given numbers in python.The problem says that the first line of the standard input contains one integer t (t<1001) which is the number of numbers.In each of the next t lines there is one integer n (n<1001).

Now I am trying to solve this using recursion trick:

def f(n,a):
   if n > 0:
      a = input()
      f(n-1,a)
   print a    
f(input(),0)

But this is not working properly since the last number is output twice.How to fix this ?

My obvious solution for this is:

n=input()
a=[1]*n
for i in range(n):a[i]=input()
a=a[::-1]
for i in range(n):print a[i]

If anybody is aware of any other smart solution for the same please enlighten me too.
Thanks

  • 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-20T08:26:29+00:00Added an answer on May 20, 2026 at 8:26 am

    You probably don’t want to print in the ‘n == 0‘ case.
    Try indenting the ‘print‘ inside the ‘if‘.

    I imagine your ‘a‘ variable is getting re-used in the ‘n == 0‘ case, causing your problem.

    Also, is there a particular reason you don’t just read the input into an list, then reverse that list? [I see you know how to do that from your edit] If this is just to experiment, then more power to you. Lists in python have a reverse() function, though (:

    FWIW, here’s a slight variation on your solution I tried out:

    def f(n):
        if n == 0:
            return
        a = input()
        f(n-1)
        print a
    
    f(input())
    

    Also, there’s a fun function in Python called reversed(), which returns an iterator.

    x = [1,2,3]
    for i in reversed(x):
        print i    # prints 3, 2, 1
    

    And lastly, not exactly related, but since you seem to be learning recursion… A handy way to debug your problem might be to put this as the first line of your f() function:

    print '>' * n, n, a
    

    Maybe then it will be clearer why you have the double-print problem.

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

Sidebar

Related Questions

My URLconf contains this pattern: url(r'^accounts/logout/$','django.contrib.auth.views.logout', name=logout), And I've trying to reverse that in
I'm trying to reverse the order of bits in C (homework question, subject: bitwise
I am trying to reverse the order of an Array in java. What is
I've been trying to extend SimplePie to sort a feed in reverse order. The
I'm trying to basically reverse this algorithm so that I get the time for
Im trying to reverse the order of the following linked list, I've done so,
I'm trying to display images in the reverse order of when they were last
I'm trying to convert the variable $num into its reverse byte order and print
I've been trying to figure out how to reverse the order of a doubly-linked
Im learning arrays and Im trying to reverse the order in an array. This

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.