# 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)

## 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

“…X…X…X”
11
Returns: -1

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, 8:12 am, Johnson W. [email protected] 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.

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

On Nov 7, 2007 9:51 AM, Phrogz [email protected] wrote:

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

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

Newbs often think they must disguise homework because “it’s not
allowed”. That’s not the point!

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

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.

Gavin K. wrote:

On Nov 7, 8:12 am, Johnson W. [email protected] 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)

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 [email protected], tks for your answer!

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

The argument of the split method in java is a string representing a
regexp.
So you can do this: str.split(“X+”);

The argument of the split method in java is a string representing a
regexp.
So you can do this: str.split(“X+”);

## public String[] split(String regex, int limit) { return Pattern.compile(regex).split(this, limit); }

public String[] split(CharSequence input, int limit) {
int index = 0;
boolean matchLimited = limit > 0;
ArrayList matchList = new ArrayList();
Matcher m = matcher(input);

``````    // Add segments before each match found
while(m.find()) {
if (!matchLimited || matchList.size() < limit - 1) {
String match = input.subSequence(index,
``````

m.start()).toString();