Tuesday, April 23, 2024
HomeSoftware EngineeringHow you can Carry out Alphabetical Addition in Java

How you can Carry out Alphabetical Addition in Java


The problem

Your activity is so as to add up letters to at least one letter.

The operate shall be given an array of single-character Strings, each being a letter so as to add.

Notes:

  • Letters will at all times be lowercase.
  • Letters can overflow (see second to final instance of the outline)
  • If no letters are given, the operate ought to return 'z'

Examples:

addLetters("a", "b", "c") = "f"
addLetters("a", "b") = "c"
addLetters("z") = "z"
addLetters("z", "a") = "a"
addLetters("y", "c", "b") = "d" // discover the letters overflowing
addLetters() = "z"

The answer in Java code

Choice 1:

public class Answer {

    public static String addLetters(String... letters) {
        String abc = "zabcdefghijklmnopqrstuvwxyz";
        int sum = 0;
        for (int i = 0; i < letters.size; i++) {
            sum += abc.indexOf(letters[i]);
        }
        return String.valueOf(abc.charAt(sum % 26));
    }
}

Choice 2:

import static java.util.stream.Stream.of;

class Answer {
  static String addLetters(String... letters) {
    int sum = of(letters).mapToInt(l -> l.charAt(0) - 96).sum() % 26 + 96;
    return "" + (char) (sum != 96 ? sum : 122);
  }
}

Choice 3:

import java.util.Arrays;

public class Answer {
  public static String addLetters(String... letters) {
    int sum = Arrays.stream(letters)
                    .flatMapToInt(String::chars)
                    .map(letter -> letter == 'z' ? 0 : letter - 'a' + 1)
                    .sum();
    int overflow = sum % 26;
    return overflow == 0 ? "z" : Character.valueOf((char)(overflow + 'a' - 1)).toString();
  }
}

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;
import java.util.Arrays;
import java.util.Random;

public class SolutionTest {
    @Take a look at
    public void fixedTests() {
        assertEquals("f", Answer.addLetters("a", "b", "c"));
        assertEquals("z", Answer.addLetters("z"));
        assertEquals("c", Answer.addLetters("a", "b"));
        assertEquals("c", Answer.addLetters("c"));
        assertEquals("a", Answer.addLetters("z", "a"));
        assertEquals("d", Answer.addLetters("y", "c", "b"));
        assertEquals("z", Answer.addLetters());
    }
    
    non-public String answer(String... letters) {
        int sum = Arrays.stream(letters).mapToInt(s->s.charAt(0)-96).sum() % 26;
        return sum == 0 ? "z" : String.valueOf((char)(sum+96));
    }
    
    @Take a look at
    public void randomTests() {
        Random rnd = new Random();
        for (int i = 0; i < 100; ++i) {
            String[] letters = rnd.ints(97, 123)
                                  .restrict(rnd.nextInt(10) + 1)
                                  .mapToObj(c -> String.valueOf((char) c))
                                  .toArray(String[]::new);
            assertEquals(answer(letters), Answer.addLetters(letters));
        }
    }
}
RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments