Monday, April 15, 2024
HomeSoftware EngineeringHow you can Type the Reward Code in Java

How you can Type the Reward Code in Java


The problem

Santa’s senior present organizer Elf developed a method to signify as much as 26 presents by assigning a novel alphabetical character to every present. After every present was assigned a personality, the present organizer Elf then joined the characters to kind the present ordering code.

Santa requested his organizer to order the characters in alphabetical order, however the Elf fell asleep from consuming an excessive amount of sizzling chocolate and sweet canes! Are you able to assist him out?

Type the Reward Code

Write a perform referred to as sortGiftCode that accepts a string containing as much as 26 distinctive alphabetical characters, and returns a string containing the identical characters in alphabetical order.

Examples (Enter => Output):

"abcdef"                       => "abcdef"
"pqksuvy"                      => "kpqsuvy"
"zyxwvutsrqponmlkjihgfedcba"   => "abcdefghijklmnopqrstuvwxyz"

The answer in Java code

Possibility 1:

public class GiftSorter{
  public String sortGiftCode(String code){
    return code.chars()
      .sorted()
      .acquire(StringBuilder::new, StringBuilder::appendCodePoint, StringBuilder::append)
      .toString();
  }
}

Possibility 2:

public class GiftSorter{
  public String sortGiftCode(String code){
    char[] chars = code.toCharArray();
    java.util.Arrays.kind(chars);
    return new String(chars);
  }
}

Possibility 3:

import java.util.Arrays;
import java.util.stream.Collectors;

public class GiftSorter {
    public String sortGiftCode(String code) {
        return Arrays.stream(code.break up(""))
            .sorted()
            .acquire(Collectors.becoming a member of(""));
    }
}

Check circumstances to validate our resolution

import org.junit.Check;
import static org.junit.Assert.assertEquals;

public class GiftSorterTest {
    @Check
    public void Tests1() throws Exception {
        GiftSorter gs = new GiftSorter();
        assertEquals("kind fedcba", "abcdef", gs.sortGiftCode("fedcba"));
    }
       
    @Check
    public void Tests2() throws Exception {
        GiftSorter gs = new GiftSorter();
        assertEquals("reverse alphabet", "abcdefghijklmnopqrstuvwxyz", gs.sortGiftCode("zyxwvutsrqponmlkjihgfedcba"));
    }
 
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments