将一个空字符串转换为摩尔斯码阅读器中的空格。

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

turning a null string into a white space in a morse code reader

问题

import java.util.Arrays;

public class MorseCodeDecoder {
    public static String decode(String morseCode) {
        String[] split = morseCode.trim().split(" ");
        int length = split.length;
        System.out.println(length);
        System.out.println("this is split  = " + Arrays.toString(split));

        MorseCode morseCoder = new MorseCode();

        String message = "";

        int nullCount = 0; // Move this outside the loop

        for (int i = 0; i < length; i++) {

            // Check for null Morse code
            if (morseCoder.get(split[i]) == null) {
                nullCount++;
                System.out.println(nullCount);
                if (nullCount == 2) {
                    nullCount = 0;
                    message = message + " ";
                }
            } else {
                nullCount = 0; // Reset nullCount if valid Morse code is found
                System.out.println(morseCoder.get(split[i]));
                message = message + morseCoder.get(split[i]);
            }

            System.out.println(message);

        }
        return message;
    }
}

Remember to replace the existing MorseCodeDecoder class in your code with this updated version. This version of the code should handle the null detection and space detection correctly, and it should produce the desired output: "HEY JUDE".

英文:

I am trying to write a morse code translator for a challenge. at the moment I have got as far as translating the morse code into a String array and am passing it through a for loop to build the message.

I am currently getting HEYnullnullJUDE and am working out how to detect 2 spaces which in morse code will act as a word space. however when I use an if statement to detect .isEmpty or .isBlank or .length() == 0 the for loop exits and I get a return of:

9
this is split  = [...., ., -.--, , , .---, ..-, -.., .]
H
H
E
HE
Y
HEY

for some reason once it hits Null, instead of executing the nullCount it just exits and return HEY and I'm not sure.

what is the best way of turning HEYnullnullJUDE which i am getting now into HEY JUDE and detecting the double space in the morse code?

this is taken from this codewars challenge: https://www.codewars.com/kata/54b724efac3d5402db00065e/train/java

import java.util.Arrays;
public class MorseCodeDecoder {
    public static String decode(String morseCode) {
        
       String[] split = morseCode.trim().split(&quot; &quot;);
        int length = split.length;
        System.out.println(length);
        System.out.println(&quot;this is split  = &quot; + Arrays.toString(split));

        MorseCode morseCoder = new MorseCode();
        
        String message = &quot;&quot;;

        for (int i =0; i &lt; length; i++){
          int nullCount = 0;
            

// this is where the code exits for some reason

            if(morseCoder.get(split[i]) == null{
              nullCount++;
              System.out.println(nullCount);
              if (nullCount == 2){
                nullCount =0;
                message = message + &quot; &quot;;
              }
            }
// if this is &quot;if&quot;statment is removed i get HEYnullnullJUDE

          System.out.println (morseCoder.get(split[i]));
        message = message + morseCoder.get(split[i]);
            System.out.println(message);
              
        }
      return message;
      
    }
}

samble test
import org.junit.Test;
import static org.hamcrest.CoreMatchers.*;
import static org.junit.Assert.assertThat;

public class MorseCodeDecoderTest {
    @Test
    public void testExampleFromDescription() {
      assertThat(MorseCodeDecoder.decode(&quot;.... . -.--   .--- ..- -.. .&quot;), is(&quot;HEY JUDE&quot;));
    }
}

答案1

得分: 1

你的输入是 &quot;.... . -.-- .--- ..- -.. .&quot;

由于你调用了 trim(),所以在 split() 中不会有前导或尾随空格。

为了将结果中的 3 个空格视为单个空格,并且不在这 3 个空格之间进行拆分,你基本上不希望在这 3 个空格之间拆分。换句话说,你只想在非空格之后或者紧跟在非空格之后的空格处进行拆分。

        在这些地方拆分
    ↓ ↓    ↓ ↓    ↓   ↓   ↓
.... . -.--   .--- ..- -.. .
            ↑
      不要在这里拆分

如果你使用 split(&quot;(?&lt;=[^ ]) | (?=[^ ])&quot;),那么结果将变为:

{ &quot;....&quot;, &quot;.&quot;, &quot;-.--&quot;, &quot; &quot;, &quot;.---&quot;, &quot;..-&quot;, &quot;-..&quot;, &quot;.&quot; }

在这里,摩斯码中的空格真正变成了一个包含单个空格的字符串。

剩下的部分就交给你处理了,例如如何使 morseCoder.get(&quot; &quot;) 返回一个 &quot; &quot;(或者 &#39; &#39;,具体类型可以根据情况而定)。


更新

根据评论:

> MorseCode.get 不包括对空格的检测

morseCoder.get(&quot; &quot;) 返回 &quot; &quot; 只是解决方案的一个示例。或者,可以在调用 morseCoder.get(...) 之前检查是否遇到了空格,例如:

StringBuilder buf = new StringBuilder();
for (String symbol : split) {
    if (symbol.equals(&quot; &quot;)) {
        buf.append(&#39; &#39;);
    } else {
        String str = morseCoder.get(split[i]);
        buf.append(str == null ? &quot;�&quot; : str);
    }
}
String message = buf.toString();
英文:

Your input is &quot;.... . -.-- .--- ..- -.. .&quot;.

Since you call trim(), there will be no leading or trailing spaces seen by split().

To see the 3 spaces as a single space in the result of the split(), you basically don't want to split on the middle of the 3 spaces. Said another way, you only want to split on a space that follows a non-space or a space that is followed by a non-space.

        Split on these
    ↓ ↓    ↓ ↓    ↓   ↓   ↓
.... . -.--   .--- ..- -.. .
            ↑
      Don&#39;t split on this

If you use split(&quot;(?&lt;=[^ ]) | (?=[^ ])&quot;), then the result becomes:

{ &quot;....&quot;, &quot;.&quot;, &quot;-.--&quot;, &quot; &quot;, &quot;.---&quot;, &quot;..-&quot;, &quot;-..&quot;, &quot;.&quot; }

Here the space in the morse code truly becomes a string with a single space.

I'll leave the rest to you, e.g. how to make morseCoder.get(&quot; &quot;) return a &quot; &quot; (or &#39; &#39;, whatever the type may be).


UPDATE

From comment:

> MorseCode.get does not include " " space detection

morseCoder.get(&quot; &quot;) return a &quot; &quot; was just an example of how it could be solved. Alternatively, simply check if a space is encountered, before calling morseCoder.get(...), e.g.

StringBuilder buf = new StringBuider();
for (String symbol : split) {
    if (symbol.equals(&quot; &quot;)) {
        buf.append(&#39; &#39;);
    } else {
        String str = morseCoder.get(split[i]);
        buf.append(str == null ? &quot;�&quot; : str);
    }
}
String message = buf.toString();

huangapple
  • 本文由 发表于 2020年8月26日 19:11:00
  • 转载请务必保留本文链接:https://go.coder-hub.com/63596376.html
匿名

发表评论

匿名网友

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

确定