Show List
Groovy Coding Questions
- Write a Groovy script to count the number of occurrences of each word in a given string.
groovyCopy code
def countWords(String str) {
def words = str.tokenize()
def counts = [:].withDefault { 0 }
words.each { counts[it]++ }
return counts
}
// Example usage:
def counts = countWords("the quick brown fox jumps over the lazy dog")
println(counts) // prints [the: 2, quick: 1, brown: 1, fox: 1, jumps: 1, over: 1, lazy: 1, dog: 1]
- Write a Groovy function to remove all duplicates from a list.
groovyCopy code
def removeDuplicates(List list) {
list.unique()
}
// Example usage:
def list = [1, 2, 3, 1, 4, 2]
def result = removeDuplicates(list)
println(result) // prints [1, 2, 3, 4]
- Write a Groovy function to calculate the sum of all elements in a list.
groovyCopy code
def sum(List list) {
list.sum()
}
// Example usage:
def list = [1, 2, 3, 4, 5]
def result = sum(list)
println(result) // prints 15
- Write a Groovy function to sort a list of integers in ascending order.
groovyCopy code
def sort(List list) {
list.sort()
}
// Example usage:
def list = [3, 1, 4, 2, 5]
sort(list)
println(list) // prints [1, 2, 3, 4, 5]
- Write a Groovy function to find the maximum element in a list of integers.
groovyCopy code
def findMax(List list) {
list.max()
}
// Example usage:
def list = [3, 1, 4, 2, 5]
def max = findMax(list)
println(max) // prints 5
- Write a Groovy function to find the minimum element in a list of integers.
groovyCopy code
def findMin(List list) {
list.min()
}
// Example usage:
def list = [3, 1, 4, 2, 5]
def min = findMin(list)
println(min) // prints 1
- Write a Groovy function to find the median of a list of integers.
groovyCopy code
def findMedian(List list) {
def size = list.size()
def mid = size / 2
list.sort()
size % 2 == 0 ? (list[mid - 1] + list[mid]) / 2 : list[mid]
}
// Example usage:
def list = [3, 1, 4, 2, 5]
def median = findMedian(list)
println(median) // prints 3
- Write a Groovy function to reverse a given string.
groovyCopy code
def reverse(String str) {
str.reverse()
}
// Example usage:
def str = "hello world"
def reversed = reverse(str)
println(reversed) // prints "dlrow olleh"
- Write a Groovy function to check if a given string is a palindrome.
groovyCopy code
def isPalindrome(String str) {
str == str.reverse()
}
// Example usage:
def str1 = "racecar"
def str2 = "hello"
assert isPalindrome(str1) == true
assert isPalindrome(str2) == false
- Write a Groovy function to find the factorial of a given number.
groovyCopy code
def factorial(int n) {
n == 0 ? 1 : n * factorial(n - 1)
}
// Example usage:
def n = 5
def result = factorial(n)
println(result) // prints 120
- Write a Groovy function to find the Fibonacci sequence up to a given limit.
groovyCopy code
def fibonacci(int limit) {
def sequence = [0, 1]
while (sequence[-1] < limit) {
sequence << sequence[-1] + sequence[-2]
}
sequence.pop()
sequence
}
// Example usage:
def limit = 100
def sequence = fibonacci(limit)
println(sequence) // prints [0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89]
- Write a Groovy function to find the GCD (Greatest Common Divisor) of two given numbers.
groovyCopy code
def gcd(int a, int b) {
b == 0 ? a : gcd(b, a % b)
}
// Example usage:
def a = 24
def b = 18
def result = gcd(a, b)
println(result) // prints 6
- Write a Groovy function to find the LCM (Least Common Multiple) of two given numbers.
groovyCopy code
def lcm(int a, int b) {
(a * b) / gcd(a, b)
}
// Example usage:
def a = 24
def b = 18
def result = lcm(a, b)
println(result) // prints 72
- Write a Groovy function to find the prime numbers up to a given limit.
groovyCopy code
def primes(int limit) {
def sieve = (2..limit).toList()
def primes = []
while (sieve) {
def prime = sieve.remove(0)
primes << prime
sieve.removeAll { it % prime == 0 }
}
primes
}
// Example usage:
def limit = 30
def result = primes(limit)
println(result) // prints [2, 3, 5, 7, 11, 13, 17, 19, 23, 29]
- Write a Groovy function to check if a given number is a prime number.
groovyCopy code
def isPrime(int n) {
if (n < 2) {
return false
}
for (int i = 2; i <= Math.sqrt(n); i++) {
if (n % i == 0) {
return false
}
}
return true
}
// Example usage:
def n1 = 7
def n2 = 12
assert isPrime(n1) == true
- Write a Groovy function to find the sum of all the digits in a given number.
groovyCopy code
def sumOfDigits(int n) {
n.toString().toCharArray().sum() - (n.toString().length() * '0'.toInteger())
}
// Example usage:
def n = 12345
def result = sumOfDigits(n)
println(result) // prints 15
- Write a Groovy function to check if a given number is an Armstrong number.
groovyCopy code
def isArmstrong(int n) {
def digits = n.toString().toCharArray()
def sum = digits.sum { it.toInteger() ** digits.size() }
sum == n
}
// Example usage:
def n1 = 153
def n2 = 370
def n3 = 371
def n4 = 407
assert isArmstrong(n1) == true
assert isArmstrong(n2) == true
assert isArmstrong(n3) == true
assert isArmstrong(n4) == true
- Write a Groovy function to find the factorial of a given number using recursion.
groovyCopy code
def factorial(int n) {
n == 0 ? 1 : n * factorial(n - 1)
}
// Example usage:
def n = 5
def result = factorial(n)
println(result) // prints 120
- Write a Groovy function to find the power of a given number using recursion.
groovyCopy code
def power(int base, int exponent) {
exponent == 0 ? 1 : base * power(base, exponent - 1)
}
// Example usage:
def base = 2
def exponent = 3
def result = power(base, exponent)
println(result) // prints 8
- Write a Groovy function to convert a decimal number to a binary number.
groovyCopy code
def decimalToBinary(int decimal) {
Integer.toBinaryString(decimal)
}
// Example usage:
def decimal = 10
def binary = decimalToBinary(decimal)
println(binary) // prints "1010"
- Write a Groovy function to convert a binary number to a decimal number.
groovyCopy code
def binaryToDecimal(String binary) {
Integer.parseInt(binary, 2)
}
// Example usage:
def binary = "1010"
def decimal = binaryToDecimal(binary)
println(decimal) // prints 10
- Write a Groovy function to convert a decimal number to a hexadecimal number.
groovyCopy code
def decimalToHex(int decimal) {
Integer.toHexString(decimal)
}
// Example usage:
def decimal = 1234
def hex = decimalToHex(decimal)
println(hex) // prints "4d2"
- Write a Groovy function to convert a hexadecimal number to a decimal number.
groovyCopy code
def hexToDecimal(String hex) {
Integer.parseInt(hex, 16)
}
// Example usage:
def hex = "4d2"
def decimal = hexToDecimal(hex)
println(decimal) // prints 1234
- Write a Groovy function to generate a random number within a given range.
groovyCopy code
def randomNumber(int min, int max) {
new Random().nextInt((max - min) + 1) + min
}
// Example usage:
def min = 1
def max = 10
def number = randomNumber(min, max)
println(number) // prints a random number between 1 and 10
Leave a Comment