4 Replies - 1004 Views - Last Post: 12 March 2019 - 08:20 PM Rate Topic: -----

#1 jake41   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 16-May 15

Help to check if I did well an assigmnet in Ruby

Posted 10 February 2018 - 01:20 PM

Hello all,

I would like to ask you for an opinion.
I had to do an assignment in Ruby and I did 3 solutions as exercise and I would like to know what you think about or suggestions regarding the requirements I had below.

My Solutions:

https://paste.ofcode...AG4N2awc25mPQFc
def simplify(arr)
    # Use implicit function to sort the array
    sorted_array = arr.sort

    # Define new array to be returned
    uniq_array = Array.new

    if sorted_array.length > 0

      # If the array does not already contain the element, add it to the uniq_array
      sorted_array.map { |arr| uniq_array << arr unless uniq_array.include?(arr)}
      puts uniq_array

    end

    uniq_array
end

def simplify2(arr)
    arr = arr.inject([]) do |uniq_arr, value|
      uniq_arr << value unless uniq_arr.include? value
      uniq_arr
      arr.sort
    end
  end

  def simplify3(arr)
    ordered_array = arr.sort { |a,b| a <=> b }
    uniq_array    = []

    # look if next elem diff from current, skip if equals
    ordered_array.each_index do |i|
      current_element = ordered_array[i]

      next if current_element == ordered_array[i+1]
      uniq_array.push(current_element)
    end

    uniq_array
  end


Write a function called “simplify” that takes a single parameter: An array of integers.
Your function should return a new array that is sorted (in ascending order), with all duplicates removed:
So for instance, if I passed in:
[0, 9, 8, 3, 3, 3, 5, 9, 5, 0]

Your function would return:
[0, 3, 5, 8, 9]

1) We assume your language has function that can sort an array. If it doesn’t, just pretend it does. We don’t expect you to write a sort algorithm. If you’re lucky enough to use a language has a built-in way to remove duplicates, please don’t use that. We’d like to see you write that part out.
2) Clarity of your code is more important than raw typing speed. So, refactoring to improve your solution is just fine.

This post has been edited by modi123_1: 10 February 2018 - 01:36 PM
Reason for edit:: Added the actual code


Is This A Good Question/Topic? 0
  • +

Replies To: Help to check if I did well an assigmnet in Ruby

#2 xclite   User is offline

  • I wrote you an code
  • member icon


Reputation: 1393
  • View blog
  • Posts: 4,225
  • Joined: 12-May 09

Re: Help to check if I did well an assigmnet in Ruby

Posted 10 February 2018 - 05:06 PM

Why not write some tests to prove to yourself that it works?
Was This Post Helpful? 1
  • +
  • -

#3 sepp2k   User is offline

  • D.I.C Lover
  • member icon

Reputation: 2745
  • View blog
  • Posts: 4,405
  • Joined: 21-June 11

Re: Help to check if I did well an assigmnet in Ruby

Posted 11 February 2018 - 05:35 AM

Your simplify2 method looks all kinds of wrong. Basically you're sorting an array, potentially inserting something into the result, then throwing that away and sorting the original array again. Then you repeat that a bunch of times and finally return the sorted array (and also store it in a variable that you never use afterwards).

This post has been edited by sepp2k: 03 March 2018 - 11:08 PM

Was This Post Helpful? 1
  • +
  • -

#4 chili5   User is offline

  • D.I.C Lover

Reputation: 20
  • View blog
  • Posts: 1,146
  • Joined: 28-December 07

Re: Help to check if I did well an assigmnet in Ruby

Posted 03 March 2018 - 04:46 PM

1. Write tests to convince yourself that it works.
2. Also for the simplify method you would be better off using a Set since you are only adding an element to the array if it's not already in the array. A Set will do that for you then convert to an array later.
3. As already mentioned simplify2 is completely wrong. Write some tests to convince yourself that it's wrong and then fix it.
Was This Post Helpful? 0
  • +
  • -

#5 BobRodes   User is offline

  • Product Manager
  • member icon

Reputation: 603
  • View blog
  • Posts: 3,085
  • Joined: 19-May 09

Re: Help to check if I did well an assigmnet in Ruby

Posted 12 March 2019 - 08:20 PM

You're sort of dancing around the correct solution. I'll give you some observations on each in turn.

Your first one works, but don't use `map` for this; it's intended to transform existing values in an array and return the result. Since you ignore the result, that turns out not to be a problem, but not understanding the differences between `map`, `inject`/`reduce`, `select` and `each` will come back to haunt you one day. For a simple iteration where you don't care about the return value, it's best to use `each`.

In `simplify2`, don't use `inject`. It has purposes other than simply iterating the array. Use `each` for this. The reason that you have to add the line `uniq_arr` is because `inject` returns the result of the block back to the next iteration, and the result of the block is otherwise nil the first time you run into a repeated value. So, that's an extra complication that `each` doesn't have. Nevertheless, the reason that your code doesn't work properly isn't that, it's that you have your `arr.sort` inside the block. This passes the entire array (in sorted order) back to the next iteration every time, so you now have all the values and your `unless` condition is always true from then on. You can fix that by putting your `sort` below the `end` on line 24. But again, I wouldn't use `inject` in the first place.

So, in `simplify3`, you gave up on using `include?` when it's the right thing to use, using instead a convoluted mechanism based on running through your input array in sorted order and comparing each element to the next one.

So, you were close, but you need to understand these methods better. Here's a solution similar to your first one, using `each`:

def simplify(arr)
  result = []
  arr.each { |v| result << v unless result.include?(v) }
  result.sort
end

p simplify([0, 9, 8, 3, 3, 3, 5, 9, 5, 0]) == [0, 3, 5, 8, 9] # <= true



And if you want to, you can do what you did with `inject` (passing in an empty array) and turn it into a one-liner:

  arr.each_with_object([]) { |v, result| result << v unless result.include?(v) }



The `each_with_object` method returns whatever object you pass in as an argument, rather than the object (`arr` in this case) that calls the method as `each` does.

By the way, you are indeed lucky enough to be using a language that has a built-in method to remove duplicates:

def simplify(arr)
  arr.uniq
end


This post has been edited by BobRodes: 26 March 2019 - 06:03 PM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1