• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Ron McLeod
  • Tim Cooke
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • Junilu Lacar
  • Rob Spoor
  • Jeanne Boyarsky
Saloon Keepers:
  • Stephan van Hulst
  • Carey Brown
  • Tim Holloway
  • Piet Souris
Bartenders:

Trying to teach myself kotlin, any help would be great

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Any small challenges like making a small app in kotlin would be really helpful, I have been trying to build a small program that you can pick a few random numbers, don't repeat any numbers and prints the numbers in order ( 1..150)
Is this code effective and how could I improve the code?

import kotlin.random.Random

fun main(args: Array<String>) {
 
   /** Random number picker with no repeating numbers.
    * will display them in number order.
    * Random numbers upto 150.
    * Upto 150 numbers balls.
    *
    * randomList(max number int 1..) ( how many numbers/ balls)
**/
   randomList( 27, 30)
    }


    fun randomList(maxNumber : Int, totalNumbers : Int){
    if (totalNumbers > 150){
        print("please pick a lower number")
       }else if (totalNumbers > maxNumber){
            print("maxNumber has to be same or higher than totalNumbers")
               }else{  
       
   var list = mutableListOf<Int>()
   var totalNumbersMinus1 = totalNumbers-1
 
    var randomList = (1..maxNumber).shuffled().take(totalNumbers)
     for(I in 0..totalNumbersMinus1){
     
    var number = randomList[I]
 
   list.add(number)
  }
 list.sort()
 for(I in 0..totalNumbersMinus1){
     print("${list[I]} ")
     }
   }
   
 }
 
Rancher
Posts: 4381
59
  • Likes 1
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Well, perhaps the first thing to do is compile and run your program - you will see that it does compile, which is good, and then you get an error message when you run it - which may or may not have been intentional, if you were testing what happens for different input values.

My IntelliJ shows "import kotlin.random.Random" greyed out - you don't need it.  Maybe it was needed in some older version of Kotlin.  But nowadays Random is directly available without that.

IntelliJ also shows messages indicating that each of your var declarations can actually be a val, since you never reassign a variable to be something else.  This is good practice - use val rather than var whenever you can, to minimize unexpected changes.  If you need something to be a var, fine, make it a var.  But don't do it if you don't need it.

For the rest of the code...

You seem to be doing the same thing several different ways. The one I like best is this one:

Here you've accomplished most of what you want in one line - great!  You don't need to copy another list with the same contents.   And you don't need to worry about only getting totalNumbers elements from this list - it's already limited in length, to have only totalNumbers elements.  So you just need sort it, and print each element.  Elsewhere you use the sort() method, which is good, but won't work on randomList because it's immutable.  You could fix thaqt with toMutableList():

But it's easier to replace sort() with sorted().  The latter creates a new immutable list, in sorted order, leaving the original immutable list alone:

But at this point, the randomList variable itself is no longer needed:
 
Jason Edwards
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Hi Mike Simmons,
Thanks for looking over my code and giving me some help.

Mike Simmons wrote:Well, perhaps the first thing to do is compile and run your program - you will see that it does compile, which is good, and then you get an error message when you run it - which may or may not have been intentional, if you were testing what happens for different input values.


I was paying around with inputs, testing my code and checking the errors.

Mike Simmons wrote:My IntelliJ shows "import kotlin.random.Random" greyed out - you don't need it.  Maybe it was needed in some older version of Kotlin.  But nowadays Random is directly available without that.


My workplace is quiet for the next 2 weeks. So I was using https://play.kotlinlang.org/ to build the app(spent about 3 hours on the code)


Mike Simmons wrote:IntelliJ also shows messages indicating that each of your var declarations can actually be a val, since you never reassign a variable to be something else.  This is good practice - use val rather than var whenever you can, to minimize unexpected changes.  If you need something to be a var, fine, make it a var.  But don't do it if you don't need it.


Will do from now on.

Mike Simmons wrote:You seem to be doing the same thing several different ways. The one I like best is this one:



I really like this code but from what I can tell, its read only!, you can print the full list or an element, looked online for random list but could not find any post talking about .sorted() or .forEach({print("$it ")})
so moved the numbers over to an array I could sort and edit the list.

Thank you for the help.
I really enjoy reading code like this, makes perfect sense.

Mike Simmons wrote:(1..maxNumber).shuffled().take(totalNumbers).sorted().forEach({print("$it ")})











 
Jason Edwards
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Jason Edwards wrote:Hi Mike Simmons,
Thanks for looking over my code and giving me some help.

Well, perhaps the first thing to do is compile and run your program - you will see that it does compile, which is good, and then you get an error message when you run it - which may or may not have been intentional, if you were testing what happens for different input values.
I was paying around with inputs, testing my code and checking the errors.

My IntelliJ shows "import kotlin.random.Random" greyed out - you don't need it.  Maybe it was needed in some older version of Kotlin.  But nowadays Random is directly available without that.
My workplace is quiet for the next 2 weeks. So I was using https://play.kotlinlang.org/ to build the app(spent about 3 hours on the code)


IntelliJ also shows messages indicating that each of your var declarations can actually be a val, since you never reassign a variable to be something else.  This is good practice - use val rather than var whenever you can, to minimize unexpected changes.  If you need something to be a var, fine, make it a var.  But don't do it if you don't need it.
Will do from now on.

You seem to be doing the same thing several different ways. The one I like best is this one:


I really like this code but from what I can tell, its read only!, you can print the full list or an element, looked online for random list but could not find any post talking about .sorted() or .forEach({print("$it ")})
so moved the numbers over to an array I could sort and edit the list.

Thank you for the help.
I really enjoy reading code like this, makes perfect sense.











 
Water! People swim in water! Even tiny ads swim in water:
The Low Tech Laboratory Movie Kickstarter is LIVE NOW!
https://www.kickstarter.com/projects/paulwheaton/low-tech
reply
    Bookmark Topic Watch Topic
  • New Topic