Esempio n. 1
1
 @Test
 public void shouldConvertToJavaStream() {
   final Value<Integer> value = of(1, 2, 3);
   final java.util.stream.Stream<Integer> s1 = value.toJavaStream();
   if (value.isSingleValued()) {
     final java.util.stream.Stream<Integer> s2 = java.util.stream.Stream.of(1);
     assertThat(List.ofAll(s1::iterator)).isEqualTo(List.ofAll(s2::iterator));
   } else {
     final java.util.stream.Stream<Integer> s2 = java.util.stream.Stream.of(1, 2, 3);
     assertThat(List.ofAll(s1::iterator)).isEqualTo(List.ofAll(s2::iterator));
   }
 }
Esempio n. 2
0
 public boolean isAllowed(Request request) {
   return isAnyOriginAllowed()
       || request
           .getHeader(ORIGIN_HEADER)
           .map(
               origin ->
                   allowedMethods.contains(request.getMethod())
                       && (isAnyOriginAllowed() || allowedOrigins.contains(origin)))
           .getOrElse(false);
 }
Esempio n. 3
0
 @Test
 public void shouldConvertToList() {
   final Value<Integer> value = of(1, 2, 3);
   final List<Integer> list = value.toList();
   if (value.isSingleValued()) {
     assertThat(list).isEqualTo(List.of(1));
   } else {
     assertThat(list).isEqualTo(List.of(1, 2, 3));
   }
 }
Esempio n. 4
0
 private static boolean isTruncatablePrime(int prime) {
   return Match(prime)
       .of(
           Case(
               $(p -> p > 7),
               p -> {
                 final CharSeq primeSeq = CharSeq.of(Integer.toString(p));
                 return List.rangeClosed(1, primeSeq.length() - 1)
                     .flatMap(i -> List.of(primeSeq.drop(i), primeSeq.dropRight(i)))
                     .map(CharSeq::mkString)
                     .map(Long::valueOf)
                     .forAll(Utils.MEMOIZED_IS_PRIME::apply);
               }),
           Case($(), false));
 }
Esempio n. 5
0
  /**
   * <strong>Problem 37 Truncatable primes</strong>
   *
   * <p>The number 3797 has an interesting property. Being prime itself, it is possible to
   * continuously remove digits from left to right, and remain prime at each stage: 3797, 797, 97,
   * and 7. Similarly we can work from right to left: 3797, 379, 37, and 3.
   *
   * <p>Find the sum of the only eleven primes that are both truncatable from left to right and
   * right to left.
   *
   * <p>NOTE: 2, 3, 5, and 7 are not considered to be truncatable primes.
   *
   * <p>See also <a href="https://projecteuler.net/problem=37">projecteuler.net problem 37</a>.
   */
  @Test
  public void shouldSolveProblem37() {
    assertThat(isTruncatablePrime(3797)).isTrue();
    List.of(2, 3, 5, 7).forEach(i -> assertThat(isTruncatablePrime(7)).isFalse());

    assertThat(sumOfTheElevenTruncatablePrimes()).isEqualTo(748_317);
  }
Esempio n. 6
0
  /**
   * <strong>Problem 39 Integer right triangles</strong>
   *
   * <p>If <i>p</i> is the perimeter of a right angle triangle with integral length sides,
   * {<i>a,b,c</i>}, there are exactly three solutions for <i>p</i> = 120.
   *
   * <p>{20,48,52}, {24,45,51}, {30,40,50}
   *
   * <p>For which value of <i>p</i> ≤ 1000, is the number of solutions maximised?
   *
   * <p>See also <a href="https://projecteuler.net/problem=39">projecteuler.net problem 39</a>.
   */
  @Test
  public void shouldSolveProblem39() {
    assertThat(SOLUTIONS_FOR_PERIMETERS_UP_TO_1000.get(120))
        .isEqualTo(some(List.of(Tuple.of(20, 48, 52), Tuple.of(24, 45, 51), Tuple.of(30, 40, 50))));

    assertThat(perimeterUpTo1000WithMaximisedNumberOfSolutions()).isEqualTo(840);
  }
Esempio n. 7
0
 public int abc(int sum) {
   return List.rangeClosed(1, sum)
       .crossProduct()
       .filter(t -> t._1 + t._2 < sum)
       .map(t -> Tuple.of(t._1, t._2, sum - t._1 - t._2))
       .filter(t -> t._1 * t._1 + t._2 * t._2 == t._3 * t._3)
       .map(t -> t._1 * t._2 * t._3)
       .head();
 }
Esempio n. 8
0
 public Map<String, String> getResponseHeaders(Request request) {
   String fallbackAllowOriginValue = allowedOrigins.mkString(",");
   return HashMap.of(
       ACCESS_CONTROL_ALLOW_ORIGIN_HEADER,
           isAllowed(request)
               ? request.getHeader(ORIGIN_HEADER).getOrElse(fallbackAllowOriginValue)
               : fallbackAllowOriginValue,
       ACCESS_CONTROL_ALLOW_CREDENTIALS_HEADER, "true",
       ACCESS_CONTROL_EXPOSE_HEADERS_HEADER, "*",
       ACCESS_CONTROL_ALLOW_HEADERS_HEADER, "*",
       ACCESS_CONTROL_MAX_AGE_HEADER, "1800",
       ACCESS_CONTROL_ALLOW_METHODS_HEADER, "*");
 }
Esempio n. 9
0
public class Euler39Test {

  /**
   * <strong>Problem 39 Integer right triangles</strong>
   *
   * <p>If <i>p</i> is the perimeter of a right angle triangle with integral length sides,
   * {<i>a,b,c</i>}, there are exactly three solutions for <i>p</i> = 120.
   *
   * <p>{20,48,52}, {24,45,51}, {30,40,50}
   *
   * <p>For which value of <i>p</i> ≤ 1000, is the number of solutions maximised?
   *
   * <p>See also <a href="https://projecteuler.net/problem=39">projecteuler.net problem 39</a>.
   */
  @Test
  public void shouldSolveProblem39() {
    assertThat(SOLUTIONS_FOR_PERIMETERS_UP_TO_1000.get(120))
        .isEqualTo(some(List.of(Tuple.of(20, 48, 52), Tuple.of(24, 45, 51), Tuple.of(30, 40, 50))));

    assertThat(perimeterUpTo1000WithMaximisedNumberOfSolutions()).isEqualTo(840);
  }

  private static int perimeterUpTo1000WithMaximisedNumberOfSolutions() {
    return SOLUTIONS_FOR_PERIMETERS_UP_TO_1000
        .map((perimeter, listOfSolutions) -> Tuple.of(perimeter, listOfSolutions.length()))
        .maxBy(Tuple2::_2)
        .get()
        ._1;
  }

  private static final Map<Integer, List<Tuple3<Integer, Integer, Integer>>>
      SOLUTIONS_FOR_PERIMETERS_UP_TO_1000 =
          List.rangeClosed(1, 500)
              .flatMap(a -> List.rangeClosed(a, 500).map(b -> Tuple.of(a, b, hypot(a, b))))
              .filter(t -> floor(t._3) == t._3)
              .map(t -> t.map3(Double::intValue))
              .groupBy(t -> t.apply((a, b, c) -> a + b + c))
              .filterKeys(d -> d <= 1_000);
}
Esempio n. 10
0
 @Test
 public void shouldConvertToSeq() {
   final Seq<?> actual = createIntTuple(1, 0, 0, 0).toSeq();
   assertThat(actual).isEqualTo(List.ofAll(1, 0, 0, 0));
 }
Esempio n. 11
0
 private boolean isAnyOriginAllowed() {
   return allowedOrigins.isEmpty() || allowedOrigins.contains(ANY_ORIGIN);
 }