Decomposing a function

Report a typo

Consider the absolute value function f|f| defined as:

f={2x if x0 3x if x>0|f| = \begin{cases} 2x \text{ if x0x \leq 0 } \\ 3x \text{ if x>0x > 0} \end{cases}

Your task is to decompose this function into two new functions: f1 and f2, that handle each case and complete the function f. The functions should each take one variable x as an argument and return a value.

Note that to solve this task, you must not delete or modify any code within the main function.

Sample Input 1:

-3

Sample Output 1:

-6

Sample Input 2:

0

Sample Output 2:

0
Write a program in Go
package main

import "fmt"

func f(x int) int {
if ??? {
return f1(x)
}
return f2(x)
}

// nolint: gomnd // <- DO NOT delete this comment!
func f1(x int) int {
// your code here
}

// nolint: gomnd // <- DO NOT delete this comment!
func f2(x int) int {
// your code here
}

// DO NOT delete or modify the contents of the main function!
func main() {
var n int
fmt.Scanln(&n)

result := f(n)
fmt.Println(result)
}
___

Create a free account to access the full topic