In: Computer Science
Write a Haskell function
combine :: Int -> Int -> Int -> Int
with the following behavior:
• When x, y, and z all correspond to digit values (i.e., integers between 0 and 9, inclusive), combine x y z returns the integer given by the sequence of digits x y z. (That is, x is treated as the digit in the hundreds place, y is treated as the digit in the tens place, and z is treated as the digit in the ones place.)
• In all other cases, the function returns -1.
For example, combine 4 2 8 returns 428, combine 0 3 0 returns 30 (because 030 = 30), and combine 4 12 3 returns -1
Code
import java.util.Scanner;
public class Solution {
public static void main(String[] args) {
System.out.println(haskell_combine(0, 3, 0));
System.out.println(haskell_combine(5, 12, 3));
System.out.println(haskell_combine(4, 2, 8));
System.out.println(haskell_combine(-4, 2, 8));
}
public static int haskell_combine(int x, int y, int z) {
String t = "" + x + "" + y + "" + z;
if( x >=10 || x<0 || y>=10 || y<0 || z>=10 || z<0)
return -1;
else
return Integer.parseInt(t);
}
}
Sample output
30
-1
428
-1