Skip to content

Folders and files

NameName
Last commit message
Last commit date

Latest commit

History

11 Commits

Repository files navigation

java

Codewars Java Solutions

Challenges

KyuQuestions
8Beginner Series #2 Clock
8Century From Year
8Even or Odd
6Find the Odd Int
8Is n Divisible by x and y?
8Keep Hydrated!
6Multiples of 3 or 5
8Multiply
7Vowel Count

Beginner Series #2 Clock

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;
}
}

⬆ Back to Top

Century From Year

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)); }
}

⬆ Back to Top

Even or Odd

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";
}
}

⬆ Back to Top

Find the Odd Int

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);
}
}

⬆ Back to Top

Is n Divisible by x and y?

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);
}
}

⬆ Back to Top

Keep Hydrated!

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);
}
}

⬆ Back to Top

Multiples of 3 or 5

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;
}
}

⬆ Back to Top

Multiply

This code does not execute properly. Try to figure out why.

publicclassMultiply {
publicstaticDoublemultiply(Doublea, Doubleb) {
returna * b
}
}
Solution
publicclassMultiply {
publicstaticDoublemultiply(Doublea, Doubleb) {
returna * b;
}
}

⬆ Back to Top

Vowel Count

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;
}
}

⬆ Back to Top

About

Codewars Java solutions

Topics

Resources

Stars

2 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors