[lnkForumImage]
TotalShareware - Download Free Software

Confronta i prezzi di migliaia di prodotti.
Asp Forum
 Home | Login | Register | Search 


 

Forums >

comp.lang.ruby

a problem related string(250 score

Johnson Wang

11/7/2007 3:12:00 PM

How to solve this problem in Ruby????
==============================================
You are given a String disk representing the clusters on a disk. An 'X'
represents a used cluster, and a '.' represents an available cluster.
You are also given an int size representing the size, in clusters, of a
file waiting to be written to disk. A file can only be stored in
clusters not already being used.
Return the minimum number of groups of consecutive clusters needed to
store the file on the disk. (The disk does not wrap around at the end.)
Return -1 if the disk does not have enough space available to store the
file.
Definition

Class:
DiskClusters
Method:
minimumFragmentation
Parameters:
String, int
Returns:
int
Method signature:
int minimumFragmentation(String disk, int size)
(be sure your method is public)


Constraints
-
disk will contain between 1 and 50 characters, inclusive.
-
Each character of disk will be 'X' or '.'.
-
size will be between 1 and 50, inclusive.
Examples
0)

"."
2
Returns: -1
We can't fit the file on the disk.
1)

".XXXXXXXX.XXXXXX.XX.X.X."
6
Returns: 6
There is only ever one cluster together, so all six clusters are
separated.
2)

"XX..XX....X.XX........X...X.XX...XXXX..XX...XXXXX."
12
Returns: 2
We fit eight clusters together, and four clusters together.
3)

".X.XXXX.......XX....X.....X............XX.X.....X."
20
Returns: 3

4)
"....X...X..X"
11
Returns: -1
--
Posted via http://www.ruby-....

12 Answers

Cédric Finance

11/7/2007 3:44:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

This should solve the problem.

class DiskClusters
def self.minimumFragmentation(str,int)
groups = str.split(/X+/).map { |e| e.size }.sort
res = 0
while(val = groups.pop) do
int -= val
res += 1
return res if int <= 0
end
-1
end
end

Cedric

On Nov 7, 2007 4:12 PM, Johnson Wang <99100@163.com> wrote:

> How to solve this problem in Ruby????
> ==============================================
> You are given a String disk representing the clusters on a disk. An 'X'
> represents a used cluster, and a '.' represents an available cluster.
> You are also given an int size representing the size, in clusters, of a
> file waiting to be written to disk. A file can only be stored in
> clusters not already being used.
> Return the minimum number of groups of consecutive clusters needed to
> store the file on the disk. (The disk does not wrap around at the end.)
> Return -1 if the disk does not have enough space available to store the
> file.
> Definition
>
> Class:
> DiskClusters
> Method:
> minimumFragmentation
> Parameters:
> String, int
> Returns:
> int
> Method signature:
> int minimumFragmentation(String disk, int size)
> (be sure your method is public)
>
>
> Constraints
> -
> disk will contain between 1 and 50 characters, inclusive.
> -
> Each character of disk will be 'X' or '.'.
> -
> size will be between 1 and 50, inclusive.
> Examples
> 0)
>
> "."
> 2
> Returns: -1
> We can't fit the file on the disk.
> 1)
>
> ".XXXXXXXX.XXXXXX.XX.X.X."
> 6
> Returns: 6
> There is only ever one cluster together, so all six clusters are
> separated.
> 2)
>
> "XX..XX....X.XX........X...X.XX...XXXX..XX...XXXXX."
> 12
> Returns: 2
> We fit eight clusters together, and four clusters together.
> 3)
>
> ".X.XXXX.......XX....X.....X............XX.X.....X."
> 20
> Returns: 3
>
> 4)
> "....X...X..X"
> 11
> Returns: -1
> --
> Posted via http://www.ruby-....
>
>

Phrogz

11/7/2007 3:47:00 PM

0

On Nov 7, 8:12 am, Johnson Wang <99...@163.com> wrote:
> How to solve this problem in Ruby????
> ==============================================
> You are given a String disk representing the clusters on a disk. An 'X'
> represents a used cluster, and a '.' represents an available cluster.
> You are also given an int size representing the size, in clusters, of a
> file waiting to be written to disk. A file can only be stored in
> clusters not already being used.
> Return the minimum number of groups of consecutive clusters needed to
> store the file on the disk. (The disk does not wrap around at the end.)
> Return -1 if the disk does not have enough space available to store the
> file.
> Definition

This smells like a homework problem, so roughly:
Scan the string for groups of consecutive clusters.
Sort the clusters by length.
Iterate through the sorted group adding up the sizes and incrementing
a counter until you reach or exceed your goal.

Cédric Finance

11/7/2007 9:35:00 PM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

You may be right. I took it like a quizz and answered very fast.

On Nov 7, 2007 4:51 PM, Phrogz <phrogz@mac.com> wrote:

> On Nov 7, 8:12 am, Johnson Wang <99...@163.com> wrote:
> > How to solve this problem in Ruby????
> > ==============================================
> > You are given a String disk representing the clusters on a disk. An 'X'
> > represents a used cluster, and a '.' represents an available cluster.
> > You are also given an int size representing the size, in clusters, of a
> > file waiting to be written to disk. A file can only be stored in
> > clusters not already being used.
> > Return the minimum number of groups of consecutive clusters needed to
> > store the file on the disk. (The disk does not wrap around at the end.)
> > Return -1 if the disk does not have enough space available to store the
> > file.
> > Definition
>
> This smells like a homework problem, so roughly:
> Scan the string for groups of consecutive clusters.
> Sort the clusters by length.
> Iterate through the sorted group adding up the sizes and incrementing
> a counter until you reach or exceed your goal.
>
>
>

Todd Benson

11/7/2007 11:51:00 PM

0

On Nov 7, 2007 9:51 AM, Phrogz <phrogz@mac.com> wrote:
> On Nov 7, 8:12 am, Johnson Wang <99...@163.com> wrote:
> > How to solve this problem in Ruby????
> > ==============================================
> > You are given a String disk representing the clusters on a disk. An 'X'
> > represents a used cluster, and a '.' represents an available cluster.
> > You are also given an int size representing the size, in clusters, of a
> > file waiting to be written to disk. A file can only be stored in
> > clusters not already being used.
> > Return the minimum number of groups of consecutive clusters needed to
> > store the file on the disk. (The disk does not wrap around at the end.)
> > Return -1 if the disk does not have enough space available to store the
> > file.
> > Definition
>
> This smells like a homework problem,

Indeed. If it is a student, than they shoot themselves in the foot by
asking for answers here (even if they get the right ones). If it is a
professor just trying to learn the language, more power to him ;)

Todd

Phlip

11/8/2007 12:04:00 AM

0


> Indeed. If it is a student, than they shoot themselves in the foot by
> asking for answers here (even if they get the right ones). If it is a
> professor just trying to learn the language, more power to him ;)
>
> Todd

Emphasize that knowing the reason behind a question helps narrow our
useful answers.

Newbs often think they must disguise homework because "it's not
allowed". That's not the point!

--
Phlip

Johnson Wang

11/8/2007 2:10:00 AM

0

Gavin Kistner wrote:
> On Nov 7, 8:12 am, Johnson Wang <99...@163.com> wrote:
>> file.
>> Definition
>
> This smells like a homework problem, so roughly:
> Scan the string for groups of consecutive clusters.
> Sort the clusters by length.
> Iterate through the sorted group adding up the sizes and incrementing
> a counter until you reach or exceed your goal.

=========================
In fact,this is not a homework but a test question by Topcoder.
I am a student at the same time a ruby learner.I have solved this
problem with Java language.`Cause i am a newbie to Ruby,so i don`t know
how to do it.So here i am and ask for help.
Its not a homework,definitely. ^^
If u r Java developer,of course u know how to solve this
problem.btw,it`s more convenient if u use Jakarta Commons lib
package.(commons-lang-2.3.jar)
Tks for your answer.
--
Posted via http://www.ruby-....

Cédric Finance

11/8/2007 9:51:00 AM

0

Note: parts of this message were removed by the gateway to make it a legal Usenet post.

Topcoder challenges are only for C++, C# and Java. Are there other
challenges websites with ruby support?

On Nov 8, 2007 3:10 AM, Johnson Wang <99100@163.com> wrote:

> Gavin Kistner wrote:
> > On Nov 7, 8:12 am, Johnson Wang <99...@163.com> wrote:
> >> file.
> >> Definition
> >
> > This smells like a homework problem, so roughly:
> > Scan the string for groups of consecutive clusters.
> > Sort the clusters by length.
> > Iterate through the sorted group adding up the sizes and incrementing
> > a counter until you reach or exceed your goal.
>
> =========================
> In fact,this is not a homework but a test question by Topcoder.
> I am a student at the same time a ruby learner.I have solved this
> problem with Java language.`Cause i am a newbie to Ruby,so i don`t know
> how to do it.So here i am and ask for help.
> Its not a homework,definitely. ^^
> If u r Java developer,of course u know how to solve this
> problem.btw,it`s more convenient if u use Jakarta Commons lib
> package.(commons-lang-2.3.jar)
> Tks for your answer.
> --
> Posted via http://www.ruby-....
>
>

Johnson Wang

11/8/2007 2:51:00 PM

0

Cédric Finance wrote:
> Topcoder challenges are only for C++, C# and Java. Are there other
> challenges websites with ruby support?

:) Ruby is not surpported now.I wanna learn ruby compare with java so as
to understand it quickly.
Dear Cédric Finance ,I will ask a lot of questions like this,hope u can
help me.Tks a lot.
--
Posted via http://www.ruby-....

Cédric Finance

11/8/2007 3:55:00 PM

0

No problem.I'll do my best to answer.

On Nov 8, 2007 3:51 PM, Johnson Wang <99100@163.com> wrote:

> C=E9dric Finance wrote:
> > Topcoder challenges are only for C++, C# and Java. Are there other
> > challenges websites with ruby support?
>
> :) Ruby is not surpported now.I wanna learn ruby compare with java so as
> to understand it quickly.
> Dear C=E9dric Finance ,I will ask a lot of questions like this,hope u can
> help me.Tks a lot.
> --
> Posted via http://www.ruby-....
>
>

Johnson Wang

11/9/2007 12:20:00 PM

0

Dear Cédric Finance,
with ruby,we can use regex like this"str.split(/X+/)."
X+ stands for more than one 'X'.But java can not split the provided
text into an array by using regexm,right?
I have searched Java document for a long time,but maybe we couldn`t do
it like this.
My email is 99100@163.com, tks for your answer!
--
Posted via http://www.ruby-....