Legacy code

Report a typo

You get a legacy program. It reports the system status and outputs a message, depending on the total task count:

  • 0 tasks — "idle";
  • 1-2 tasks — "low" workload;
  • 3-4 tasks — "average" workload;
  • from 5 tasks — system "overload".

The count of tasks is a sum of active and pending tasks. You get it from the standard input. Rewrite the given code without using the recover function:

package main

import "fmt"

const (
    FiveTasks  = 5
    ThreeTasks = 3
    OneTask    = 1
)

func panicHandler() {
    status := recover()
    if status != nil {
        fmt.Println(status)
    } else {
        fmt.Println("idle")
    }
}

func main() {
    defer panicHandler()

    var activeTasks, pendingTasks int
    fmt.Scan(&activeTasks, &pendingTasks)

    totalTaskCount := activeTasks + pendingTasks

    switch {
    case totalTaskCount >= FiveTasks:
        panic("overload")
    case totalTaskCount >= ThreeTasks:
        panic("average")
    case totalTaskCount >= OneTask:
        panic("low")
    }
}

If you have any difficulties with this task, use the hint:

Tip:

package main

import "fmt"

func main() {
    var activeTasks, pendingTasks int
    fmt.Scan(&activeTasks, &pendingTasks)

    totalTaskCount := activeTasks + pendingTasks

    switch {
    case totalTaskCount >= FiveTasks:
        fmt.Println("overload")
    case totalTaskCount >= ?:
        fmt.Println("?")
    case totalTaskCount >= ?:
        fmt.Println("?")
    default:
        fmt.Println("?")
    }
}

Sample Input 1:

1 1

Sample Output 1:

low
Write a program in Go
package main

import "fmt"

const (
FiveTasks = 5
ThreeTasks = 3
OneTask = 1
)

func main() {
var activeTasks, pendingTasks int
fmt.Scan(&activeTasks, &pendingTasks)

totalTaskCount := ? + ?

switch {
case totalTaskCount >= FiveTasks:
fmt.Println("?")
// Write the rest of the switch case here
}
}
___

Create a free account to access the full topic