Wednesday, February 21, 2024
HomeSoftware Engineering“Dashatize It” in Java

“Dashatize It” in Java


The problem

Given a variable n,

If n is an integer, Return a string with sprint'-'marks earlier than and after every odd integer, however don’t start or finish the string with a splash mark. If n is destructive, then the destructive signal needs to be eliminated.

If n just isn’t an integer, return an empty worth.

Ex:

dashatize(274) -> '2-7-4'
dashatize(6815) -> '68-1-5'

The answer in Java code

Possibility 1:

public class Resolution {
    public static String dashatize(int num) {
      String n = String.valueOf(Math.abs(num)).replaceAll("-", "");
      return java.util.Arrays.asList(n.break up("")).stream()
        .map(ch -> Integer.valueOf(ch) % 2 !=0 ? "-"+ch+"-" : ch)
        .acquire(java.util.stream.Collectors.becoming a member of(""))
        .replaceAll("--", "-")
        .replaceAll("^-+", "")
        .replaceAll("-+$", "");
    }
}

Possibility 2:

public class Resolution {
  public static String dashatize(remaining int quantity) -$", "");
  
}

Possibility 3:

import java.util.*;
public class Resolution {
    public static String dashatize(int num) (?<=[1,3,5,7,9])"));
     
}

Take a look at circumstances to validate our answer

import org.junit.Take a look at;
import static org.junit.Assert.assertEquals;
import org.junit.runners.JUnit4;

public class SolutionTest {

    @Take a look at
    public void testBasic() {
        assertEquals("2-7-4", Resolution.dashatize(274));
        assertEquals("5-3-1-1", Resolution.dashatize(5311));
        assertEquals("86-3-20", Resolution.dashatize(86320));
        assertEquals("9-7-4-3-02", Resolution.dashatize(974302));
    }

    @Take a look at
    public void testWeird() {
        assertEquals("0", Resolution.dashatize(0));
        assertEquals("1", Resolution.dashatize(-1));
        assertEquals("28-3-6-9", Resolution.dashatize(-28369));
    }
    
    @Take a look at
    public void testEdgeCases() {
        assertEquals("2-1-4-7-48-3-64-7", Resolution.dashatize(Integer.MAX_VALUE));
        assertEquals("2-1-4-7-48-3-648", Resolution.dashatize(Integer.MIN_VALUE));
        assertEquals("1-1-1-1-1-1-1-1-1-1", Resolution.dashatize(-1111111111));
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments