Scala Program to Create a Square of the Maximum Side, Given two Planks of Wood with Size as Integer Numbers

Posted in

Sangeeta Gulia
Last updated on January 13, 2023

    Problem Statement

    Create a square of the maximum possible side from two planks of wood, where the size should be provided as input from the user. The two planks of wood should be cut into pieces so they can be used to create a square of maximum dimension. There may be leftover pieces of wood that you could not consume. The output of this program should be the dimension of the square. If the creation of the square is impossible, the program should return 0.

    Examples

    For two planks of wood of sizes 12 and 11, the output should be 5.

    For two planks of wood of sizes 1 and 16, the output should be 4.

    For two planks of wood of sizes 1 and 2, the output should be 0.

    Prerequisites

    Before writing this program, you should know about loop constructs, recursion, and how to take user inputs from the console.

    Algorithm

    Before deciding upon the steps to start designing the solution for the given problem, let us understand a few details about the problem.

    • A square is a shape that has four sides of equal length.
    • The following are multiple possibilities creating a square from two pieces of wood:
      • All four sides from the first wood plank.
      • All four sides from the second wood plan.
      • We get four sides using both of the two planks.
      • Square creation is not possible using the given planks.
      • The smallest possible square that can be created is of size 1, requiring that the sum of the sizes of the two wood sticks should be at least 4.

    Now we can start deciding our algorithm steps:

    1. We will first decide what should be the largest possible side. Assuming that number is the biggest dimension possible. We will check for its validity if it is possible to have four sticks of equal size using the two input wood planks.
    2. The largest possible wood dimension cannot be more than the sum_of_two_input_woods/4
    3. Let's start checking if we can create a square. Given the two input wood planks and our hypothetical largest possible side of the square, square creation is only possible once we reach the value of 0. We can define this recursively or iteratively.

    With the above algorithm in mind, let us start writing our program.

    Program

    import scala.annotation.tailrec
    
    objectSquareextendsApp {
    
    println("Enter size of First wood")
    val woodA = scala.io.StdIn.readInt()
    
    println("Enter size of Second wood")
    val woodB = scala.io.StdIn.readInt()
    
    defsolution(a: Int, b: Int): Int = {
    
    val totalWoodSizeAvailable = a + b
    val largestPossibleSide = totalWoodSizeAvailable / 4
    
    defisLargestPossibleSide(largestPossibleSide: Int, a: Int, b: Int): Boolean = {
    if (a >= largestPossibleSide && b >= largestPossibleSide && (a / largestPossibleSide) + (b / largestPossibleSide) >= 4) {
    true
    } elseif (a >= largestPossibleSide && b < largestPossibleSide && (a / largestPossibleSide) >= 4) {
    true
    } elseif (a < largestPossibleSide && b >= largestPossibleSide && (b / largestPossibleSide) >= 4) {
    true
    } elsefalse
    }
    
    @tailrec
    deffindLargestSide(largestPossibleSide: Int, a: Int, b: Int): Int = {
    if (largestPossibleSide == 0) {
    0
    } elseif (isLargestPossibleSide(largestPossibleSide, a, b)) {
    largestPossibleSide
    } else {
    findLargestSide(largestPossibleSide - 1, a, b)
    }
    }
    
    findLargestSide(largestPossibleSide, a, b)
    
    }
    
    println("\nLargest possible dimension of square is: ")
    println(solution(woodA, woodB))
    
    }

    Output:

    Sample output 1: 
    
    Enter size of First wood
    21
    Enter size of Second wood
    21
    
    Largest possible dimension of square is: 
    10

    Sample output 2:

    Enter size of First wood
    1
    Enter size of Second wood
    1
    
    Largest possible dimension of square is: 
    0

    Sample output 3:

    Enter size of First wood
    16
    Enter size of Second wood
    2
    
    Largest possible dimension of square is: 
    4

    Explanation

    In this program, we initially asked the user to enter the size of two wood sticks that will serve as input. We then stored their values in woodA and woodB variables.

    Later, we defined a function named “isLargestPossibleSide” responsible for checking if we can create a square with the “largestPossibleSide” that we considered the largest side and might be our result to create a square out of the two wooden sticks. This will return either true or false, where true denotes that squareCreation is possible and false otherwise.

    We used the if else statement in Scala to implement this. As discussed in our algorithm, we shall consider three observations to create a square, as follows:

    • a >= largestPossibleSide && b >= largestPossibleSide && (a / largestPossibleSide) + (b / largestPossibleSide) >= 4

    This case states that both planks of wood will participate in the square creation and contribute towards at least one side of the square while the other stick will contribute towards the remaining parts of the stick to create the square.

    • a >= largestPossibleSide && b < largestPossibleSide && (a / largestPossibleSide) >= 4

    This case states that only the first wood plank will participate in the square creation since the size of the second square is less than the size of the required dimension of the square.

    • a < largestPossibleSide && b >= largestPossibleSide && (b / largestPossibleSide) >= 4

    This case states that only the second wood will participate in the square creation process. All four sides will be created from the second wood.

    Note: In if conditions, the evaluation takes place from left to right. In the case of AND operator(&&), the next conditions in the series are evaluated only if the previous executions have returned true.

    For example, (b / largestPossibleSide) >= 4 will be evaluated only if b >= largestPossibleSide and a < largestPossibleSide has returned true .

    After writing our decision logic, we need to check the possible values that can be the largest side for square creation. In this program, we aim to find the largest value to check this decision logic from the largest value to the smaller one.

    The largest achievable value to create four sides from the two wood planks can be found using the below formula:

    (Size_of_first_wood + size_of_second_wood) / 4

    We will iteratively/recursively check from the above-returned value to 0. The findLargestSide() features this logic. In our case, the base condition is if largestPossibleSide == 0, return 0. For optimization, you can also make this as largestPossibleSide == 3 to save on some recursive calls.

    In the second condition, we check if square creation is possible with our hypothetical largestPossibleSide variable. If it returns true, we get our output. Otherwise, we will continue checking until we reach 0.

    Conclusion

    This is one way of writing the above program. Try it on your own system, run it, and understand how it works. Also, try different inputs to have a clear understanding. You can think of more possible ways of implementing the solution.

    Happy Coding!

    Leave a Comment on this Post

    0 Comments