Extracting digits from a number

Report a typo

Below is a Go program that takes as input two lines: the first line contains a number, and the second line contains three space-separated digits (digit1, digit2, digit3).

Your task is to write the additional required code to create two functions:

  • convertNumberToSlice() — To convert the input number into a slice of its digits.
  • suitableDigits() — That takes a slice of digits from a number and a variadic slice of digits to check. This function should return a slice of the digits included in the number.

Finally, your program should print the result of the function suitableDigits() to the console.

Sample Input 1:

1234
1 5 6

Sample Output 1:

[1]
Write a program in Go
package main

import "fmt"

// Write the code to convert the input `number` into a slice of its digits below:
func convertNumberToSlice(number int64) []int8 {
var digits []int8

for number > 0 {
digit := int8(number % 10)
? /= 10 // Divide by 10 to remove the last digit from the `number`
digits = append(?, ?)
}

return ? // Return the slice of digits
}

// Write the code to check which of the provided digits are included in the `number` below:
func suitableDigits(numberAsDigits []int8, digitsToCheck ...int8) []int8 {
var included []int8

for _, digit := range ? {
for i, digitToCheck := range ? {
if ? == ? {
included = append(?, ?)
? = -1 // Set the found digit to -1 to prevent it from being included again
break
}
}
}

return ? // Return the slice of included digits
}

// DO NOT delete or modify the contents of the main() function!
func main() {
___

Create a free account to access the full topic