Codewars Java Solutions
| Kyu | Questions |
|---|---|
| 8 | Beginner Series #2 Clock |
| 8 | Century From Year |
| 8 | Even or Odd |
| 6 | Find the Odd Int |
| 8 | Is n Divisible by x and y? |
| 8 | Keep Hydrated! |
| 6 | Multiples of 3 or 5 |
| 8 | Multiply |
| 7 | Vowel Count |
The clock shows h hours (0 <= h <= 23), m minutes (0 <= m <= 59) and s seconds (0 <= s <= 59) after midnight. Your task is to write a function which returns the time since midnight in milliseconds.
Examples:
h = 0, m = 0, s = 0 -> res = 0
h = 0, m = 1, s = 1 -> res = 61000
h = 1, m = 0, s = 1 -> res = 3601000
publicclassClock {
publicstaticintPast(inth, intm, ints) {
// Your solution
}
}Solution
publicclassClock {
publicstaticintPast(inth, intm, ints) {
return ((h * 60 * 60) + (m * 60) + s) * 1000;
}
}The first century spans from the year 1 up to and including the year 100, The second - from the year 101 up to and including the year 200, etc. Given a year, return the century it is in.
publicclassSolution {
publicstaticintcentury(intnumber) {
// Your solution
}
}Solution
publicclassSolution {
publicstaticintcentury(intnumber) {
return (int)(Math.ceil(number / 100.0)); }
}Create a function that takes an integer as an argument and returns "Even" for even numbers or "Odd" for odd numbers.
publicclassEvenOrOdd {
publicstaticStringeven_or_odd(intnumber) {
// Your solution
}
}Solution
publicclassEvenOrOdd {
publicstaticStringeven_or_odd(intnumber) {
returnnumber % 2 == 0 ? "Even" : "Odd";
}
}Given an array of integers, find the one that appears an odd number of times. There will always be only one integer that appears an odd number of times.
publicclassFindOdd {
publicstaticintfindIt(int[] a) {
// Your solution
}
}Solution
importstaticjava.util.Arrays.stream;
publicclassFindOdd {
publicstaticintfindIt(int[] arr) {
returnstream(arr).reduce(0, (a, b) -> a ^ b);
}
}Create a function that checks if a number n is divisible by two numbers x AND y. All inputs are positive, non-zero digits.
publicclassDivisibleNb {
publicstaticbooleanisDivisible(longn, longx, longy) {
// Your solution
}
}Solution
publicclassDivisibleNb {
publicstaticbooleanisDivisible(longn, longx, longy) {
return (n % x == 0) && (n % y == 0);
}
}Nathan loves cycling. Because Nathan knows it is important to stay hydrated, he drinks 0.5 litres of water per hour of cycling. You get given the time in hours and you need to return the number of litres Nathan will drink, rounded to the smallest value.
publicclassKeepHydrated {
publicintLiters(doubletime) {
// Your solution
}
}Solution
publicclassKeepHydrated {
publicintLiters(doubletime) {
return (int)(time / 2);
}
}If we list all the natural numbers below 10 that are multiples of 3 or 5, we get 3, 5, 6 and 9. The sum of these multiples is 23. Finish the solution so that it returns the sum of all the multiples of 3 or 5 below the number passed in.
Note: If the number is a multiple of both 3 and 5, only count it once. Also, if a number is negative, return 0.
publicclassSolution {
publicintsolution(intnumber) {
// Your solution
}
}Solution
publicclassSolution {
publicintsolution(intnumber) {
intsum = 0;
for (inti = 3; i < number; i++) {
if (i % 3 == 0 || i % 5 == 0) {
sum += i;
}
}
returnsum;
}
}This code does not execute properly. Try to figure out why.
publicclassMultiply {
publicstaticDoublemultiply(Doublea, Doubleb) {
returna * b
}
}Solution
publicclassMultiply {
publicstaticDoublemultiply(Doublea, Doubleb) {
returna * b;
}
}Return the number (count) of vowels (a, e, i, o, u) in the given string. The input string will only consist of lower case letters and/or spaces.
publicclassVowels {
publicstaticintgetCount(Stringstr) {
// Your solution
}
}Solution
publicclassVowels {
publicstaticintgetCount(Stringstr) {
intvowelsCount = 0;
Stringvowels = "aeiou";
for (inti = 0; i < str.length(); i++) {
if (vowels.contains(String.valueOf(str.charAt(i)))) {
vowelsCount++;
}
}
returnvowelsCount;
}
}