无法使其正常工作。 无法按照我想要的方式获得中位数。

huangapple go评论92阅读模式
英文:

Can't seem to make it work. Can't get the median out as I want

问题

不能取得中位数我想从这些单词中获取中位数`for` 循环中难以将值取出

public class MedianWord {
    static double medianWordLength(String words) {
        String[] parts = words.split(" ");
        int[] a;
        double median = 0;

        for (int i = 0; i < parts.length; i++) {
            a = new int[parts[i].length()];
            Arrays.sort(a);
            if (a.length % 2 == 0) {
                median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
            }
            else
                median = (double) a[a.length / 2];
        }
        return median;
    }
}
英文:

I can't get the median out. I want the median from the words. Having a hard time getting the value out of the for loop.

public class MedianWord {
    static double medianWordLength(String words) {
        String[] parts = words.split(&quot; &quot;);
        int[] a;
        double median = 0;

        for (int i = 0; i &lt; parts.length; i++) {
            a = new int[parts[i].length()];
            Arrays.sort(a);
            if (a.length % 2 == 0) {
                median = ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
            }
            else
                median = (double) a[a.length / 2];
        }
        return median;
    }
}

答案1

得分: 2

每次循环迭代都会用新数组覆盖 a 数组。您需要将问题分成两个部分 - 首先,迭代 parts 数组并将其转换为一个长度数组 a,然后找到该数组的中位数:

static double medianWordLength(String words) {
    String[] parts = words.split(" ");
    int[] a = new int[parts.length];
    for (int i = 0; i < parts.length; i++) {
        a[i] = parts[i].length();
    }

    Arrays.sort(a);
    if (a.length % 2 == 0) {
        return ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
    } else {
        return a[a.length / 2];
    }
}

顺便说一下:
使用流将单词转换为排序后的长度数组可以更优雅地完成(可以说是):

int[] a = Arrays.stream(words.split(" ")).mapToInt(String::length).sorted().toArray();
英文:

Each iteration of the loop overwrites the a array with a new array. You need to break the problem into two parts - first, iterate over the parts array and convert it to an array a of lengths, and then find the median of that array:

static double medianWordLength(String words) {
    String[] parts = words.split(&quot; &quot;);
    int[] a = new int[parts.length];
    for (int i = 0; i &lt; parts.length; i++) {
        a[i] = parts[i].length();
    }

    Arrays.sort(a);
    if (a.length % 2 == 0) {
        return ((double) a[a.length / 2] + (double) a[a.length / 2 - 1]) / 2;
    }
    else {
        return a[a.length / 2];
    }
}

Side note:<br/>
Converting the words to a sorted array of lengths could be done (arguably) more elegantly with streams:

int[] a = Arrays.stream(words.split(&quot; &quot;)).mapToInt(String::length).sorted().toArray();

答案2

得分: 1

应该可以正常工作:

static Integer getMedian(String sentence) {
    String[] str = sentence.split(" ");
    Integer[] strLen = new Integer[str.length];

    for (int i = 0; i < strLen.length; i++) {
        strLen[i] = str[i].length();
    }

    return strLen.length % 2 == 0 ?
            (strLen[strLen.length / 2] + strLen[strLen.length / 2 - 1]) / 2 : strLen[strLen.length / 2];
}
英文:

That should work fine:

static Integer getMedian(String sentence) {
        String[] str = sentence.split(&quot; &quot;);
        Integer[] strLen = new Integer[str.length];

        for (int i = 0; i &lt; strLen.length; i++) {
            strLen[i] = str[i].length();
        }

        return strLen.length % 2 == 0?
                (strLen[strLen.length / 2] + strLen[strLen.length / 2 - 1]) / 2 : strLen[strLen.length / 2];
    }

答案3

得分: 0

public static double medianWordLength(String str) {
    int[] arr = Arrays.stream(str.split("\\s+"))
                      .mapToInt(String::length)
                      .sorted()
                      .toArray();

    int mid = arr.length / 2;
    double median = (double)arr[mid];

    return arr.length % 2 == 0 ? (median + arr[mid - 1]) / 2 : median;
}
英文:
public static double medianWordLength(String str) {
    int[] arr = Arrays.stream(str.split(&quot;\\s+&quot;))
                      .mapToInt(String::length)
                      .sorted()
                      .toArray();

    int mid = arr.length / 2;
    double median = (double)arr[mid];

    return arr.length % 2 == 0 ? (median + arr[mid - 1]) / 2 : median;
}

答案4

得分: -1

首先,您一直在重复对数组进行排序,然后在循环内部替换数组。这是您的主要问题。

但为了简化这个过程,您只需要一个数组,不需要使用 for 循环。只需根据单词的长度对单词数组进行排序。我在其中留下了一些打印语句,以便您可以看到发生了什么。如果您在一个或多个空格上进行拆分,那么在输入单词字符串时就不需要太小心。

String quote = "To be or not to be that is the question";
System.out.println(medianWordLength(quote));

static double medianWordLength(String words) {
    String[] parts = words.split("\\s+");
    double median = 0;

    // 根据单词长度对数组进行排序
    Arrays.sort(parts,
            (a, b) -> Integer.compare(a.length(), b.length()));

    for (String v : parts) {
        System.out.println(v);
    }
    System.out.println("------------------");

    // 获取“中间”索引。
    int idx = parts.length / 2;

    // 根据数组大小调整索引
    if (parts.length % 2 == 0) {
        System.out.println(parts[idx-1] + " " + parts[idx]);
        median = (parts[idx-1].length() + parts[idx].length())
                / 2.;
    } else {
        System.out.println(parts[idx]);
        median = parts[idx + 1].length();
    }
    return median;
}

输出结果

To
be
or
to
be
is
not
the
that
question
----------
be is
2.0
英文:

First you keep sorting the array over and over again and then replacing the array within the loop. That is your main problem.

But to facilitate the process, you just need one array and no for loop. Just sort the word array based on length of the words. I left some print statements in so you could see what's going on. And if you split on one or more spaces you don't need to be as careful when you enter the string of words.

String quote = &quot;To be or not to be that is the question&quot;;
System.out.println(medianWordLength(quote));
	
static double medianWordLength(String words) {
	String[] parts = words.split(&quot;\\s+&quot;);
	double median = 0;

    // sort the array based on length of the words
	Arrays.sort(parts,
			(a, b) -&gt; Integer.compare(a.length(), b.length()));

	for (String v : parts) {
		System.out.println(v);
	}
	System.out.println(&quot;------------------&quot;);

    // get the &quot;middle&quot; index.
	int idx = parts.length / 2;

    // adjust index based on array size
	if (parts.length % 2 == 0) {
		System.out.println(parts[idx-1] + &quot; &quot; + parts[idx]);
		median = (parts[idx-1].length() + parts[idx].length())
				/ 2.;
	} else {
		System.out.println(parts[idx]);
		median = parts[idx + 1].length();
	}
	return median;
}

Prints

To
be
or
to
be
is
not
the
that
question
----------
be is
2.0

</details>



huangapple
  • 本文由 发表于 2020年8月21日 23:53:31
  • 转载请务必保留本文链接:https://go.coder-hub.com/63526117.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定