1

わかりました..私は完全な Python マニアであり、Java とそのメソッドを使用したことはほとんどありません。条件は、インストラクターに説明しなければならない Java 関数を持っていることですが、その方法についての手がかりがありません。と説明しています。また、その操作(ループの使用など)に欠陥がある場合はそれを見つける必要があります。最後に、「string」タイプと「string[]」タイプの違いは何ですか?

public static void search(String findfrom, String[] thething){
  if(thething.length > 5){
      System.err.println("The thing is quite long");
  }

  else{
      int[] rescount = new int[thething.length];
      for(int i = 0; i < thething.length; i++){
          String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);
          for(int j = 0; j < characs.length; j++){
              if(characs[j].compareTo(thething[i]) == 0){
                  rescount[i]++;
        }
    }
      }
      for (int j = 0; j < thething.length; j++) {
          System.out.println(thething[j] + ": " + rescount[j]);
      }
  }
}
4

4 に答える 4

1
public class Test {

    public static void main(String[] args) {
        search(
            "you like me but do you \"like like\" me", 
            new String[]{"you", "like", "me", "not"}
        );
    }

    /**
     * Given a string of words (each word separated by one or more of the
     * following characters: tab, carriage return, newline, single quote, double
     * quote, a form feed, or a word boundary) count the occurrence of each
     * search term provided, with a 5 term limit.
     * 
     * @param findfrom
     *            the string of words
     * @param thething
     *            the search terms.  5 at most, or count will not be performed.
     */
    public static void search(String findfrom, String[] thething) {
        if (thething.length > 5) {
            System.err.println("The thing is quite long");
        }
        else {
            String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);
            int[] rescount = new int[thething.length];
            for (int i = 0; i < thething.length; i++) {
                for (int j = 0; j < characs.length; j++) {
                    if (characs[j].compareTo(thething[i]) == 0) {
                        rescount[i]++;
                    }
                }
            }
            for (int j = 0; j < thething.length; j++) {
                System.out.println(thething[j] + ": " + rescount[j]);
            }
        }
    }
}

出力

you: 2
like: 3
me: 2
not: 0
于 2012-10-04T09:29:54.940 に答える
1
  • "[ \"\'\t\n\b\f\r]"第 1 パラ: findfrom は文字列であり、 (正規表現) で区切られているはずです。

  • 2 番目のパラグラフ: thething は文字列配列で、最大 5 つの文字列が含まれています。結果を印刷します。

例えば

findfrom="hello'there'happy'birthday'"
thething={"there","birthday","hello","birthday"}

result would be:
there: 1
birthday: 2
hello: 1
birthday: 2

ところで、ライン

String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);

for ループの外に移動する可能性があります。findfrom は変更されないため、繰り返し分割する必要はありません。

于 2012-10-04T09:15:27.207 に答える
1

そのコードのPythonバージョンは次のようになります。

import sys
import re

def search(findfrom, thething):
  """Take a string and a list of strings. Printout each of the strings
in the list and a count of how many times that string appeared in the
input string. The string comparison will be based on the input string being
divided up into tokens. The delimiter for each token will be either a whitespace
character, a single quote, or a double quote. """
  if len(thething) > 5:
      sys.stderr.write("The thing is quite long")
  else:
      rescount = [0] * len(thething)
      for i in range(0,len(thething)):
          characs = re.split("[ \"\'\t\n\b\f\r]", findfrom)
          for j in range(0,len(characs)):
              if characs[j] == thething[i]:
                  rescount[i] = rescount[i] + 1

      for j in range(0,len(thething)):
          print thething[j] + ": " + str(rescount[j])


string = 'you like me but do you "like like" me'
strings = ["you", "like", "me", "not"]
search(string,strings)

出力:

you: 2
like: 3
me: 2
not: 0
于 2012-10-04T10:01:57.303 に答える
1
if(thething.length > 5){
      System.err.println("The thing is quite long");
}

の長さがString[] thething5 より大きい場合は、エラーを出力します。そうでない場合は、次の else ブロック内にあることを実行します。

else{

int[] rescount = new int[thething.length];

intの長さに等しいサイズの s の新しい配列を作成します。String[] thething

for(int i = 0; i < thething.length; i++)

インデックスごとiに String[] というもの。

String[] characs = findfrom.split("[ \"\'\t\n\b\f\r]", 0);

文字列 findfrom を正規表現に基づいていくつかの部分に分割する、characs という名前の新しい String[] を作成します"[ \"\'\t\n\b\f\r]"。0 は、このパターンが可能な限り何度も適用されることを意味します。

for(int j = 0; j < characs.length; j++){

jString[] 文字の各インデックスについて...

if(characs[j].compareTo(thething[i]) == 0){

index のcharacs String[] の文字列と、index の文字列[] の文字列を比較jしますi

2 つが一致する場合、つまりcompareToメソッドは 0 を返します。

rescount[i]++;

intのインデックスiにある をインクリメントしint[] rescountます。

  for (int j = 0; j < thething.length; j++) {
      System.out.println(thething[j] + ": " + rescount[j]);
  }

最後に、各インデックスについてjString[] thethingそのインデックスの文字列とintint[] rescount

また、aStringは文字の配列です。たとえばString string = "word"、aString[]は文字列の配列です。例えばString[] strings = new String[]{"word1", "word2",....}.

于 2012-10-04T09:26:19.097 に答える