What programming language you start with really all depends on where you want to go with programming/coding. The great thing about this field is that there are an absolute abundance of smaller fields that you can go into, all using programming in their own unique ways. For web applications, a good start would be with HTML and later moving your way through CSS, JavaScript, JQuery, PHP, SQL, and any of the JavaScript libraries. Ruby is also a popular choice, so I would recommend checking that out too. For more scientific fields or areas with more machine learning and A.I., Python is generally a great place to start as it is widely used in that field of study. C++ is also a very useful language to know for that, but it can be a little more challenging for beginners. For game and application design, languages such as C#, C, Swift, Kotlin, and Java are most often used for that.
Description
You run a moving truck business, and you can pack the most in your truck when you have stacks of equal size - no slack space. So, you're an enterprising person, and you want to write some code to help you along.
Input Description
You'll be given two numbers per line. The first number is the number of stacks of boxes to yield. The second is a list of boxes, one integer per size, to pack.
Example:
3 34312332
That says "make three stacks of boxes with sizes 3, 4, 3, 1 etc".
Output Description
Your program should emit the stack of boxes as a series of integers, one stack per line. From the above example:
331
322
34
If you can't make equal sized stacks, your program should emit nothing.
Challenge Input
3 912743471352
3 42137586
9 2
4 064876318535318
Challenge Output
9124
7342
7135
426
138
75
(nothing)
0665
4733
8315
881
Solution
in Scala
import scala.annotation.tailrec
def elemsEqual(L:List[Int]): Boolean =
L.distinct.length == 1
def pack(n:Int, boxes:List[Int]): List[List[Int]] = {
val g = boxes.permutations
@tailrec def loop(g:Iterator[List[Int]]): List[List[Int]]= {
val s = g.next.grouped(n).toList
(elemsEqual(s.map(_.sum)) && (s.length == n)) match {
case true => s
case false => loop(g)
}
}
try {loop(g)}
catch {
case _ => List(List[Int]())
}
}
val boxes = args(2).toCharArray.map(_.toString.toInt).toList
println(pack(args(1)toInt, boxes).map(_.mkString).mkString("\n"))
You run a moving truck business, and you can pack the most in your truck when you have stacks of equal size - no slack space. So, you're an enterprising person, and you want to write some code to help you along.
Input Description
You'll be given two numbers per line. The first number is the number of stacks of boxes to yield. The second is a list of boxes, one integer per size, to pack.
Example:
3 34312332
That says "make three stacks of boxes with sizes 3, 4, 3, 1 etc".
Output Description
Your program should emit the stack of boxes as a series of integers, one stack per line. From the above example:
331
322
34
If you can't make equal sized stacks, your program should emit nothing.
Challenge Input
3 912743471352
3 42137586
9 2
4 064876318535318
Challenge Output
9124
7342
7135
426
138
75
(nothing)
0665
4733
8315
881
Solution
in Scala
import scala.annotation.tailrec
def elemsEqual(L:List[Int]): Boolean =
L.distinct.length == 1
def pack(n:Int, boxes:List[Int]): List[List[Int]] = {
val g = boxes.permutations
@tailrec def loop(g:Iterator[List[Int]]): List[List[Int]]= {
val s = g.next.grouped(n).toList
(elemsEqual(s.map(_.sum)) && (s.length == n)) match {
case true => s
case false => loop(g)
}
}
try {loop(g)}
catch {
case _ => List(List[Int]())
}
}
val boxes = args(2).toCharArray.map(_.toString.toInt).toList
println(pack(args(1)toInt, boxes).map(_.mkString).mkString("\n"))
Comments
Post a Comment