11 Replies - 1722 Views - Last Post: 05 May 2013 - 07:36 PM Rate Topic: -----

#1 waleedshiekh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 11

string encoding in Ruby

Posted 02 May 2013 - 09:10 AM

I want to encode a string in Ruby such that output should be in pairs so that I could decode it. I want to encode in such a way that each pair contains the next distinct letter in the string, and the number consecutive repeats.

e.g If I encode "aaabbcbbaaa" output should
[["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]
Is This A Good Question/Topic? 0
  • +

Replies To: string encoding in Ruby

#2 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8001
  • View blog
  • Posts: 13,709
  • Joined: 19-March 11

Re: string encoding in Ruby

Posted 02 May 2013 - 09:11 AM

You may do that. I give you permission.

You can even ask questions about how to do it if you like.
Was This Post Helpful? 0
  • +
  • -

#3 waleedshiekh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 11

Re: string encoding in Ruby

Posted 02 May 2013 - 09:13 AM

View Postjon.kiparsky, on 02 May 2013 - 09:11 AM, said:

You may do that. I give you permission.

You can even ask questions about how to do it if you like.

I can do that in java but I'm trying to do is ruby but having issues. I want to know what's the optimal solution to this. Thanks.
Was This Post Helpful? 0
  • +
  • -

#4 xclite  Icon User is offline

  • LIKE A BOSS
  • member icon


Reputation: 916
  • View blog
  • Posts: 3,206
  • Joined: 12-May 09

Re: string encoding in Ruby

Posted 02 May 2013 - 09:19 AM

What have you tried that isn't working?

We can help you reach an optimal solution.

To my knowledge there isn't some built-in call that's going to do this for you. You're going to have to write a block of code to do it. Give us something to work with and we'll help out.
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is online

  • Pancakes!
  • member icon


Reputation: 8001
  • View blog
  • Posts: 13,709
  • Joined: 19-March 11

Re: string encoding in Ruby

Posted 02 May 2013 - 09:19 AM

Start with a working solution. What's a non-optimal solution that gets this done? Maybe something based on your java experience would work?

Once you have that, you can think about what's not optimal about it in Ruby and how you'd make it better.
Was This Post Helpful? 0
  • +
  • -

#6 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: string encoding in Ruby

Posted 03 May 2013 - 08:37 AM

View Postwaleedshiekh, on 02 May 2013 - 09:10 AM, said:

I want to encode a string in Ruby such that output should be in pairs so that I could decode it. I want to encode in such a way that each pair contains the next distinct letter in the string, and the number consecutive repeats.

e.g If I encode "aaabbcbbaaa" output should
[["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]


Hey waleedshiekh, welcome to the forums. Around here, it's considered polite to post the current state of your code when asking for help. It's a way of proving that you're not a useless computer science major who has no intent of learning how to program (they show up in the other forums A LOT, so it's become a pretty big issue).

I couldn't resist your puzzle so I came up with a solution that uses the array methods #take_while and #drop to sort out the issue in a fairly articulate manor. Feel free to share your code if you hit a dead end. And thanks for the puzzle, it's good to keep sharp on these kinds of array problems.
Was This Post Helpful? 0
  • +
  • -

#7 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: string encoding in Ruby

Posted 04 May 2013 - 12:51 AM

http://www.ruby-doc....Enumerable.html

</hint>
Was This Post Helpful? 1
  • +
  • -

#8 waleedshiekh  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 3
  • Joined: 11-April 11

Re: string encoding in Ruby

Posted 04 May 2013 - 10:52 PM

View PostNotarySojac, on 03 May 2013 - 08:37 AM, said:

View Postwaleedshiekh, on 02 May 2013 - 09:10 AM, said:

I want to encode a string in Ruby such that output should be in pairs so that I could decode it. I want to encode in such a way that each pair contains the next distinct letter in the string, and the number consecutive repeats.

e.g If I encode "aaabbcbbaaa" output should
[["a", 3], ["b", 2], ["c", 1], ["b", 2], ["a", 3]]


Hey waleedshiekh, welcome to the forums. Around here, it's considered polite to post the current state of your code when asking for help. It's a way of proving that you're not a useless computer science major who has no intent of learning how to program (they show up in the other forums A LOT, so it's become a pretty big issue).

I couldn't resist your puzzle so I came up with a solution that uses the array methods #take_while and #drop to sort out the issue in a fairly articulate manor. Feel free to share your code if you hit a dead end. And thanks for the puzzle, it's good to keep sharp on these kinds of array problems.


I did this

def encode(input)
    groupsOfChars = []
    input.each_char do |c|
      if groupsOfChars.empty? || groupsOfChars.last[0] != c
        groupsOfChars << [c, 1]
      else
        groupsOfChars.last[1] += 1
      end
    end
    groupsOfChars
  end


I tried to use the final groupOfChars in the function and tried to follow the approach that if char then insert else take sum of the one then insert. I'm very new to Ruby. I know Java programming that's why I'm having issues.
Was This Post Helpful? 0
  • +
  • -

#9 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: string encoding in Ruby

Posted 05 May 2013 - 05:24 AM

That's not bad, but you could simplify this a lot by using built-in methods like Enumerable#chunk or String#scan.

PS: The convention in Ruby is to use snake_case for variable names - not camelCase.
Was This Post Helpful? 2
  • +
  • -

#10 NotarySojac  Icon User is offline

  • D.I.C Regular
  • member icon

Reputation: 53
  • View blog
  • Posts: 428
  • Joined: 30-September 10

Re: string encoding in Ruby

Posted 05 May 2013 - 09:46 AM

View Postwaleedshiekh, on 04 May 2013 - 10:52 PM, said:

I did this

def encode(input)
    groupsOfChars = []
    input.each_char do |c|
      if groupsOfChars.empty? || groupsOfChars.last[0] != c
        groupsOfChars << [c, 1]
      else
        groupsOfChars.last[1] += 1
      end
    end
    groupsOfChars
  end


I tried to use the final groupOfChars in the function and tried to follow the approach that if char then insert else take sum of the one then insert. I'm very new to Ruby. I know Java programming that's why I'm having issues.


That code is pretty tight, I expected the algo to look a little more painful on the eyes, actually.

Here's an algo I came up with using #drop_while and #take_while to sort of 'sculpt' the array. It's less efficient, but with Ruby, the emphasis is placed on code beauty and maintainability (until you start missing important benchmark figures, that is).


def flatten_stack(str)
  a = str.split('') # convert the string into an array
  output = []
  while (a.length != 0) do    # loop until our read array is depleted
    char_of_clump = a.first
    clump = a.take_while { |el| el == char_of_clump } # grab the nth clump from the array
    a = a.drop(clump.length)   # remove the clump of chars from the array we're reading since we're done with them
    
    output << [char_of_clump, clump.length]
  end
  output
end


# begin:

str = "aaabbcbbaaa"

p flatten_stack(str)



I couldn't figure out how to use #chunk to get the job done though it seems like such a function would excel in chunking scenarios like this one.
Was This Post Helpful? 0
  • +
  • -

#11 sepp2k  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2153
  • View blog
  • Posts: 3,313
  • Joined: 21-June 11

Re: string encoding in Ruby

Posted 05 May 2013 - 04:13 PM

View PostNotarySojac, on 05 May 2013 - 06:46 PM, said:

I couldn't figure out how to use #chunk to get the job done


Spoiler

This post has been edited by sepp2k: 05 May 2013 - 04:14 PM

Was This Post Helpful? 3
  • +
  • -

#12 Lemur  Icon User is offline

  • Pragmatism over Dogma
  • member icon


Reputation: 1383
  • View blog
  • Posts: 3,514
  • Joined: 28-November 09

Re: string encoding in Ruby

Posted 05 May 2013 - 07:36 PM

View Postsepp2k, on 05 May 2013 - 06:13 PM, said:

View PostNotarySojac, on 05 May 2013 - 06:46 PM, said:

I couldn't figure out how to use #chunk to get the job done


Spoiler


Learn something new every day I suppose. Man would that have been handy earlier. Now to read back through the base class libraries.
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1