HomeSoftware EngineeringTips on how to “Dashatize It” in Java

Tips on how to “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 unfavourable, then the unfavourable signal must be eliminated.

If n is just not an integer, return an empty worth.


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

The answer in Java code

Possibility 1:

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

Possibility 2:

public class Answer {
  public static String dashatize(last int quantity) -$", "");

Possibility 3:

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

Take a look at instances 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", Answer.dashatize(274));
        assertEquals("5-3-1-1", Answer.dashatize(5311));
        assertEquals("86-3-20", Answer.dashatize(86320));
        assertEquals("9-7-4-3-02", Answer.dashatize(974302));

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

Most Popular

Recent Comments