I have a collection and want to return the nearest value to some fixed value but not exceed the fixed value. For example, if my collection looks like this:
val numbers = mutableListOf(23, 12, 64, 47, 36, 55)
and I have a target fixed value of 35, the value returned from the collection would be 23. Here are some other examples:
Target -> Returned
29 -> 23
5 -> null (there is no value less than 12, so null is returned)
70 -> 64
Is there some Collection function that I can use that would provide the expected result? Note: The list of numbers is not sorted. In the real app, these are not numbers but objects that do contain an integer property, but I could just as well sort the collection first on that value if that would help in the solution.
You can use fold function to hold the closest value in an accumalator. For instance,
val numbers = mutableListOf(23, 12, 64, 47, 36, 55)
val target = 35
val answer = numbers.fold(null){acc: Int?, num ->
if(num <= target && (acc == null || num > acc)) num
else acc
}
In case you want to break the loop if the target matches one of the values in the list you can have following
val numbers = mutableListOf(23, 12, 64, 47, 36, 55)
val target = 35
fun MutableList<Int>.findClosest(input: Int) = fold(null) { acc: Int?, num ->
val closest = if (num <= input && (acc == null || num > acc)) num else acc
if (closest == input) return#findClosest closest else return#fold closest
}
val answer = numbers.findClosest(target)
The return keyword in the inner function will return from findClosest function as soon as the target matches a particular value
I don't know of any function from the library that would solve it, but you can define your own extension as
fun <T: Comparable<T>> Iterable<T>.findPredecessor(value: T): T?{
var currentCandidate: T? = null
this.filter { it < value }. forEach {
currentCandidate = when {
currentCandidate == null -> it
currentCandidate!! < it -> it
else -> currentCandidate
}
}
return currentCandidate
}
Also if this is a one-two time operation then you are ok with iteration, otherwise you should sort the input and then use a modified binary search.
You should use minByOrNull. You want to find the number with the minimum difference from target. But before doing that, you should filter out those that are more than target.
numbers.filter { it <= target }.minByOrNull { target - it }
This will loop through numbers twice. If you don't like that, you can add asSequence() before .filter.
I would use
numbers.asSequence().filter { it <= target }.maxOfOrNull()
I think it's self-explanatory.
Related
I'm kind of stuck, I don't know how to make the second loop to start 1 position above the first loop in Kotlin.
I have an array (named myArray) with 10 elements, I need to Write a Kotlin program that prints the number that has the most consecutive repeated number in the array and also prints the number of times it appears in the sequence.
The program must parse the array from left to right so that if two numbers meet the condition, the one that appears first from left to right will be printed.
Longest: 3
Number: 8
fun main() {
val myArray: IntArray = intArrayOf(1,2,2,4,5,6,7,8,8,8)
for((index , value) in myArray.withIndex()){
var inx = index + 1
var count = 0
var longest = 0
var number = 0
for((inx,element) in myArray.withIndex()) {
if(value == element ){
count+=
}
}
if(longest < count){
longest = count
number = value
}
}
}
I'm against just dropping answers, but it is quite late for me, so I'll leave this answer here and edit it tomorrow with more info on how each part works. I hope that maybe in the meanwhile it will help you to gain some idea to where you might be going wrong.
val results = mutableMapOf<Int, Int>()
(0..myArray.size - 2).forEach { index ->
val current = myArray[index]
if (current == myArray[index + 1]) {
results[current] = (results[current] ?: 1) + 1
}
}
val (max, occurrences) = results.maxByOrNull { it.value } ?: run { println("No multiple occurrences"); return }
println("Most common consecutive number $max, with $occurrences occurrences")
Alternatively if the intArray would be a list, or if we allowed to change it to a list myArray.toList(), you could replace the whole forEach loop with a zipWithNext. But I'm pretty sure that this is a HW question, so I doubt this is the expected way of solving it.
myList.zipWithNext { a, b ->
if (a == b) results[a] = (results[a] ?: 1) + 1
}
The code below is for calculating exam results. 5 subject names and 5 points received from those subjects are recorded by the user in empty arrays created.
I have solved everything here. But I want to add "th" "st" "rd" "nd" after "cycle". Which is written "Please type lesson" and "Please type point"
For Example:
"Please type 1st point"
But with my code I can:
"Please type 1 point"
I tried to execute this process with the "When" condition, but I cannot because the loop argument "cycle" do not support the last() function
For example:
when (cycle.last()) {
1 -> "st"
2 -> "nd"
}
It will give me a result if worked 11st, 531st, 22nd, 232nd, etc. That's I want
fun main() {
var subject = Array<String>(5){""}
var point = Array<Int>(5){0}
for (cycle in 0 until subject.count()) {
println("Please type ${cycle+1} lesson")
var typeLesson = readLine()!!.toString()
subject[cycle] = typeLesson
println("Please type ${cycle+1} point")
var typePoint = readLine()!!.toInt()
point[cycle] = typePoint
}
var sum = 0
for (cycle in 0 until point.count()) {
println("${subject[cycle]} : ${point[cycle]}")
sum = sum + point[cycle]/point.count()
}
println("Average point: $sum")
}
You can divide the number by 10 and get the remainder using %. That is the last digit.
fun Int.withOrdinalSuffix(): String =
when (this % 10) {
1 -> "${this}st"
2 -> "${this}nd"
3 -> "${this}rd"
else -> "${this}th"
}
Usage:
println("Please type ${(cycle+1).withOrdinalSuffix()} lesson")
Note that in English, 11, 12, 13 have the suffix "th", so you might want to do:
fun Int.withOrdinalSuffix(): String =
if ((this % 100) in (11..13)) { // check last *two* digits first
"${this}th"
} else {
when (this % 10) {
1 -> "${this}st"
2 -> "${this}nd"
3 -> "${this}rd"
else -> "${this}th"
}
}
instead.
Here is what I am trying to say:
val firstNumbers = (1..69).random()
val secondNumbers = (1..69).random()
I would like the secondNumbers to omit the random number picked in firstNumbers
If you're just generating two numbers, what you could do is lower the upper bound for secondNumbers down to 68, then add 1 if it's greater than or equal to the first number. This will ensure an even distribution:
val firstNumber = (1..69).random()
var secondNumber = (1..68).random()
if (secondNumber >= firstNumber) {
secondNumber += 1
}
For generating more than 2 numbers, the following code should work:
fun randoms(bound: Int, n: Int): List<Int> {
val mappings = mutableMapOf<Int, Int>()
val ret = mutableListOf<Int>()
for (i in 0 until n) {
val num = (1..(bound - i)).random()
ret.add(mappings.getOrDefault(num, num))
mappings.put(num, mappings.getOrDefault(bound - i, bound - i))
}
return ret
}
It tries to emulate Fisher-Yates shuffling while only keeping track of swaps that happened, thus greatly reducing memory usage when n is much less than bound. If n is very close to bound, then the answer by #lukas.j is much cleaner to use and probably also faster.
It can be used like so:
randoms(69, 6) // might return [17, 36, 60, 48, 69, 21]
(I'd encourage people to double-check the uniformity and correctness of the algorithm, but it seems good to me)
random() is the wrong approach, rather use shuffled() and then take the first two elements from the list with take(). And it is a oneliner:
val (firstNumber, secondNumber) = (1..69).shuffled().take(2)
println(firstNumber)
println(secondNumber)
Another approach could be to find one number in range 1..69, remove that number from the range and find the second one.
val first = (1..69).random()
val second = ((1..69) - first).random()
Edit: As per your comment, you want 6 different numbers within this range. You can do that like this.
val values = (1..69).toMutableList()
val newList = List(6) {
values.random().also { values.remove(it) }
}
All the letters of the English alphabet are divided into vowels and consonants.
A word is considered euphonious if it doesn't have three or more vowels or consonants in a row.
My goal is to create euphonious words from the discordant ones and output the minimum number of characters needed to create a euphonious word from a given word.
Examples:
Input:
schedule
Output:
1
Input:
biiiiig
Output:
2
Code
fun main() {
val word = readLine()!!.toMutableList()
checkWord(word)
}
fun isVowel(c: Char): Boolean {
val vowels = listOf('a', 'e', 'i', 'o', 'u', 'y')
return c in vowels
}
fun checkWord(word: MutableList<Char>){
var counter = 0
for (number in 0 .. word.size - 2) {
if (isVowel(word[number]) && isVowel(word[number + 1]) && isVowel(word[number + 2])) {
counter++
word.add(number + 2, 'b')
// println(word)
}
if (!isVowel(word[number]) && !isVowel(word[number + 1]) && !isVowel(word[number + 2])) {
counter++
word.add(number + 2, 'a')
// println(word)
}
}
println(counter)
}
My code is working for those examples but not for a case like eeeeeeeeeeeeeeeee where the output is supposed to be 8 but my counter is 6.
Since the list is growing as you iterate, your for loop never reaches the end of the list. Your code can be fixed by replacing
for (number in 0 .. word.size - 2) {
with
var number = -1
while (++number < word.size - 1) {
so it checks the current list size on each iteration.
I want to point out however that it is unnecessary to use a MutableList and keep enlarging it since you don't use the "fixed" euphonious list afterwards. It is also unnecessary to repeatedly search neighbors on each iteration. You can just count as you go.
fun checkWord (word: String) {
var count = 0
var currentTypeCount = 0
var lastTypeVowel = true
for (c in word) {
if (isVowel(c) == lastTypeVowel) {
if (++currentTypeCount == 3) {
count++
currentTypeCount = 1
}
} else {
lastTypeVowel = !lastTypeVowel
currentTypeCount = 1
}
}
println(count)
}
Let's analyze the modifications of your word:
eebeeeeeeeeeeeeeee
eebeebeeeeeeeeeeeee
eebeebeebeeeeeeeeeee
eebeebeebeebeeeeeeeee
eebeebeebeebeebeeeeeee
eebeebeebeebeebeebeeeee
eebeebeebeebeebeebeebeee
eebeebeebeebeebeebeebeebe
Your last 2 modification take place on the letters with index, which is bigger than your word's original length. That happens because for loop iterations number is dependent on your word's original length.
I recommend you to use while loop, as its condition is always recalculated and word.size will be updated there
var i = 0
while (i + 2 < word.size) {
// the same logic
i++
}
val listNumbers = generateSequence(1) { it + 1 }
val listNumber1to100 = listNumbers.takeWhile { it < 100 }
val secNum:Unit = listNumber1to100.forEach {it}
println(listNumber1to100.asSequence().filter { it%(listNumber1to100.forEach { it })!=0 }.toList())
I have an error in reminder sign!
This is Error: None of the following functions can be called with the arguments supplied
In your first approach, the error appears in this line:
it%(listNumber1to100.forEach { it })
A Byte, Double, Float, Int, Long or Short is prefered right after the % operator, however, forEach is a function which the return type is Unit.
In your second approach, you have the correct expression in isPrime(Int). Here are some suggestions for you:
listNumber1to100 is excluding 100 in your code, if you want to include 100 in listNumber1to100, the lambda you pass to takeWhile should be changed like this:
val listNumber1to100 = listNumbers.takeWhile { it <= 100 }
listNumber1to100.asSequence() is redundant here since listNumber1too100 is itself a TakeWhileSequence which implements Sequence.
isPrime(Int) is a bit confusing since it is check for isComposite and it does not work for every input it takes(it works for 1 to 99 only). I will rewrite it in this way:
fun isPrime(num: Int): Boolean = if (num <= 1) false else !(2..num/2).any { num % it == 0 }
Since prime number must be positive and 1 is a special case(neither a prime nor composite number), it just return false if the input is smaller or equal to 1. If not, it checks if the input is divisible by a range of number from 2 to (input/2). The range ends before (input/2) is because if it is true for num % (num/2) == 0, it is also true for num % 2 == 0, vise versa. Finally, I add a ! operator before that because a prime number should not be divisible by any of those numbers.
Finally, you can filter a list by isPrime(Int) like this:
println(listNumber1to100.filter(::isPrime).toList())
PS. It is just for reference and there must be a better implementation than this.
To answer your question about it, it represents the only lambda parameter inside a lambda expression. It is always used for function literal which has only one parameter.
The error is because the expression: listNumber1to100.forEach { it } - is not a number, it is a Unit (ref).
The compiler try to match the modulo operator to the given function signatures, e.g.: mod(Byte) / mod(Int) / mod(Long) - etc.
val listNumbers = generateSequence(1) { it + 1 }
val listNumber1to100 = listNumbers.takeWhile { it < 100 }
fun isPrime(num: Int): Boolean = listNumber1to100.asSequence().any { num%it==0 && it!=num && it!=1 }
println(listNumber1to100.asSequence().filter { !isPrime(it)}.toList())
I found this solution and worked
But why can I have a non-number here in the right side of reminder