Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Re: Sorting challenge

by davido (Archbishop)
on Jul 22, 2013 at 23:10 UTC ( #1045742=note: print w/replies, xml ) Need Help??

in reply to Sorting challenge

I suggest you read each line of code and decide what it is for. Follow the logic, and see if it makes sense.

  • In your first line, why are you splitting on whitespace? Are you expecting a whitespace-delimited list of numbers? Why?
  • while(<>): I thought user-input was supposed to be terminated after the number of items specified initially. Your while loop will continue until EOF.
  • In your while loop, you're also reading a list of numbers, but instead of space-delimited, they're "\n" delimited.
  • last if $_ eq ''; This will never be true, because you never chomped your input.
  • shift @numbers; Now you're ignoring the input where the user presumably told you how many integers he was going to enter. Shouldn't you be using that number somewhere, like as one of the conditions of your while loop?
  • foreach(@sorted) { print $_; } This will place all your output on a single line, with no space separating it. ...oh, except you never chomped, so each item will be printed to its own line, probably for the wrong reason. ;)

I was going to offer a more elegant (and working) solution, but it turns out that your specification is incomplete, and partially contradicts the code you demonstrated. So until that's straightened out, I'm a little hesitant to demonstrate a solution that might be misinterpreting what you're trying to accomplish.


Replies are listed 'Best First'.
Re^2: Sorting challenge
by PerlSufi (Friar) on Jul 23, 2013 at 01:16 UTC
    Great response davido. Your posts are always very well thought out. Each number needs to be on its own line. The input/output needs to look like:
    #input 4 3 6 7 2 #output 2 3 6 7

      I'm still unclear on what's supposed to happen if you get input outside of (0,1000000]. I assume you don't need to prompt; the harness is not expecting any output except for the result. But what's supposed to happen on bad input?

      Maybe you should just post the exact problem specification.


        Sorry davido, I should have did it this way instead. Here are the specifications:
        Given the list of numbers, you are to sort them in non decreasing orde +r. Input t the number of numbers in list, then t lines follow [t <= 10^6]. Each line contains one integer: N [0 <= N <= 10^6] Output Output given numbers in non decreasing order. Example Input: 5 5 3 6 7 1 Output: 1 3 5 6 7
        I should specify that the input is from a machine- not a human user. So prompting with messages will not give the correct answer either. The site I am working off of is

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1045742]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others studying the Monastery: (7)
As of 2017-03-27 22:20 GMT
Find Nodes?
    Voting Booth?
    Should Pluto Get Its Planethood Back?

    Results (324 votes). Check out past polls.