Advertisement
If you have a new account but are having problems posting or verifying your account, please email us on hello@boards.ie for help. Thanks :)
Hello all! Please ensure that you are posting a new thread or question in the appropriate forum. The Feedback forum is overwhelmed with questions that are having to be moved elsewhere. If you need help to verify your account contact hello@boards.ie

Ruby - Recursion - sorting string alphabetically

Options
  • 10-09-2012 10:48pm
    #1
    Registered Users Posts: 16


    Yo!

    Any Ruby Code Warriors on here?

    Just a lickle silly problem from a beginner.

    I'm writing this recursive sorting method (I know Ruby has a sorting method a thousands times more efficient than anything an average programmer would write, but I'm a beginner learning about recursion, so its good to do) and I just can't see where I'm going wrong. The script enters the while loop and doesn't come out the other end.

    Here is my code:

    def sort_array array

    def rec_sort_array remaining_array, sorted_array

    x = 'keep going'

    a = remaining_array #


    b = sorted_array # []


    while a.length > 1 # 1 == 1

    i = 0
    j = a.length - 1
    k = 0
    x = 'keep going'
    puts 'get here?'
    puts a

    comment # check if a > a[j] && a > a[k]
    comment # increment k until k == i and decrement j until j == i
    comment # if k == i and j == i, b.push(a) and a.delete_at(i) => rec_sort_array a, b

    while (x == 'keep going' && i != a.length)
    while (i != k)
    if a.downcase >= a[k].downcase
    k = k + 1
    end
    end

    while i != j

    if (a.downcase > a[j].downcase || a.downcase == a[j].downcase)
    j = j - 1
    end
    end

    if (a.downcase < a[k].downcase || a.downcase < a[k].downcase)

    i = i + 1
    end

    if i == k && i == j

    b.push(a)

    puts b


    a.delete_at(i)


    break

    end
    end
    end

    if a.length == 1

    b.push(a[0])

    x = 'stop'

    elsif a.length == 0
    return 0

    x = 'stop'

    else
    rec_sort_array a, b
    end
    end

    a = array


    b = []


    rec_sort_array a, b

    end

    a =

    puts a[0].upcase

    sort_array


    And attached is a screenshot, which is easier to read.

    I just can't see why it goes into the first while loop and stops, doesn't print anything out, just freezes.

    I've done it three different ways and got the same result, which makes me think I'm using a method wrong or something, maybe "a.delete_at".

    What I'm doing is throwing in puts 'statements' in different places to see how far it gets.

    I would love some help :-)


Comments

  • Registered Users Posts: 16 Endlessly Manipulable


    Does this attachment thing work?


  • Registered Users Posts: 2,345 ✭✭✭Kavrocks


    Yes the attachment works.

    In your second while loop you have x == 'keep going' this will always be true because no where in that while loop do you set x = to anything else and before entering that while loop you set x == 'keep going' so the latter statements have no effect.

    Why do you do a.downcase > a[j].downcase || a.downcase == a[j].downcase instead of a.downcase >= a[j].downcase? I see above this you use the greater than or equal to operator but here you don't. Just cuts down on the code you need to write.
    if (a[i].downcase < a[k].downcase || a[i].downcase < a[k].downcase) 
    i = i + 1
    end
    

    In the above if statement both of your expressions are the exact same, did you mean for one of them to be a[j].downcase.

    Running through your code quickly in my head on the first go around i = i + 1 is never called so h is pushed into your sorted array which is wrong. Then on the second time around you hit a problem because none of your increment statements are called so where you break out of the loop is never called as i = k = 0 and j = 1 always.

    I hope that helps.


  • Registered Users Posts: 16 Endlessly Manipulable


    Kavrocks wrote: »
    Yes the attachment works.

    In your second while loop you have x == 'keep going' this will always be true because no where in that while loop do you set x = to anything else and before entering that while loop you set x == 'keep going' so the latter statements have no effect.

    Why do you do a.downcase > a[j].downcase || a.downcase == a[j].downcase instead of a.downcase >= a[j].downcase? I see above this you use the greater than or equal to operator but here you don't. Just cuts down on the code you need to write.
    if (a[i].downcase < a[k].downcase || a[i].downcase < a[k].downcase) 
    i = i + 1
    end
    
    In the above if statement both of your expressions are the exact same, did you mean for one of them to be a[j].downcase.

    Running through your code quickly in my head on the first go around i = i + 1 is never called so h is pushed into your sorted array which is wrong. Then on the second time around you hit a problem because none of your increment statements are called so where you break out of the loop is never called as i = k = 0 and j = 1 always.

    I hope that helps.

    You're a legend, amazing how obvious things are with a few hours away from it :-)


  • Registered Users Posts: 16 Endlessly Manipulable


    Kavrocks wrote: »
    Yes the attachment works.

    In your second while loop you have x == 'keep going' this will always be true because no where in that while loop do you set x = to anything else and before entering that while loop you set x == 'keep going' so the latter statements have no effect.

    Why do you do a.downcase > a[j].downcase || a.downcase == a[j].downcase instead of a.downcase >= a[j].downcase? I see above this you use the greater than or equal to operator but here you don't. Just cuts down on the code you need to write.
    if (a[i].downcase < a[k].downcase || a[i].downcase < a[k].downcase) 
    i = i + 1
    end
    
    In the above if statement both of your expressions are the exact same, did you mean for one of them to be a[j].downcase.

    Running through your code quickly in my head on the first go around i = i + 1 is never called so h is pushed into your sorted array which is wrong. Then on the second time around you hit a problem because none of your increment statements are called so where you break out of the loop is never called as i = k = 0 and j = 1 always.

    I hope that helps.

    I was also using the inequalities backwards.

    Still not working, but at least I'm not in an infinite while loop so can see where I'm going wrong :-)


  • Registered Users Posts: 16 Endlessly Manipulable


    Got it - don't ask me how, but it works :-)


  • Advertisement
  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    How does it work?


  • Registered Users Posts: 16 Endlessly Manipulable


    How does it work?

    I said don't ask :P

    I fixed the code from the above, was full of a cacophony of errors and mislogicals, but, ugly as it is, it works :-)


  • Registered Users Posts: 7,182 ✭✭✭Genghiz Cohen


    Ruby, ugly?!
    Well I never!


  • Registered Users Posts: 446 ✭✭Ant


    Ruby, ugly?!
    Well I never!

    There's always one! :)


Advertisement