Transforming an array: take the square root of each perfect square, else square the number
up vote
4
down vote
favorite
SquareOrSquareRoot
should get an array of integers and return a new array. If the number at index i
is a "square" number the returned array at index i
should have its square root. If the original number is not a "square" number then the returned array in index i
should be the number squared.
Coming from Python (where this could be done in a single line using list comprehension or using map
), I find it very odd that this is probably one of the shortest ways to achieve that in Go (please prove me wrong).
package main
import (
"fmt"
"math"
)
func SquareOrSquareRoot(arr int) int{
arr_to_return := make(int, len(arr))
for index, value := range arr {
val_sqrt := math.Sqrt(float64(value))
if val_sqrt == math.Trunc(val_sqrt) {
arr_to_return[index] = int(val_sqrt)
} else {
arr_to_return[index] = value * value
}
}
return arr_to_return
}
func main() {
arr := int{100, 101, 5, 5, 1, 1}
fmt.Println(SquareOrSquareRoot(arr))
// [10 10201 25 25 1 1]
}
go
bumped to the homepage by Community♦ 7 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
4
down vote
favorite
SquareOrSquareRoot
should get an array of integers and return a new array. If the number at index i
is a "square" number the returned array at index i
should have its square root. If the original number is not a "square" number then the returned array in index i
should be the number squared.
Coming from Python (where this could be done in a single line using list comprehension or using map
), I find it very odd that this is probably one of the shortest ways to achieve that in Go (please prove me wrong).
package main
import (
"fmt"
"math"
)
func SquareOrSquareRoot(arr int) int{
arr_to_return := make(int, len(arr))
for index, value := range arr {
val_sqrt := math.Sqrt(float64(value))
if val_sqrt == math.Trunc(val_sqrt) {
arr_to_return[index] = int(val_sqrt)
} else {
arr_to_return[index] = value * value
}
}
return arr_to_return
}
func main() {
arr := int{100, 101, 5, 5, 1, 1}
fmt.Println(SquareOrSquareRoot(arr))
// [10 10201 25 25 1 1]
}
go
bumped to the homepage by Community♦ 7 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Where you say "array" you mean, and should say, "slice". Arrays and slices are different things in Go, see "The Go Blog - Arrays, slices (and strings): The mechanics of 'append".
– Dave C
Sep 24 '17 at 16:39
add a comment |
up vote
4
down vote
favorite
up vote
4
down vote
favorite
SquareOrSquareRoot
should get an array of integers and return a new array. If the number at index i
is a "square" number the returned array at index i
should have its square root. If the original number is not a "square" number then the returned array in index i
should be the number squared.
Coming from Python (where this could be done in a single line using list comprehension or using map
), I find it very odd that this is probably one of the shortest ways to achieve that in Go (please prove me wrong).
package main
import (
"fmt"
"math"
)
func SquareOrSquareRoot(arr int) int{
arr_to_return := make(int, len(arr))
for index, value := range arr {
val_sqrt := math.Sqrt(float64(value))
if val_sqrt == math.Trunc(val_sqrt) {
arr_to_return[index] = int(val_sqrt)
} else {
arr_to_return[index] = value * value
}
}
return arr_to_return
}
func main() {
arr := int{100, 101, 5, 5, 1, 1}
fmt.Println(SquareOrSquareRoot(arr))
// [10 10201 25 25 1 1]
}
go
SquareOrSquareRoot
should get an array of integers and return a new array. If the number at index i
is a "square" number the returned array at index i
should have its square root. If the original number is not a "square" number then the returned array in index i
should be the number squared.
Coming from Python (where this could be done in a single line using list comprehension or using map
), I find it very odd that this is probably one of the shortest ways to achieve that in Go (please prove me wrong).
package main
import (
"fmt"
"math"
)
func SquareOrSquareRoot(arr int) int{
arr_to_return := make(int, len(arr))
for index, value := range arr {
val_sqrt := math.Sqrt(float64(value))
if val_sqrt == math.Trunc(val_sqrt) {
arr_to_return[index] = int(val_sqrt)
} else {
arr_to_return[index] = value * value
}
}
return arr_to_return
}
func main() {
arr := int{100, 101, 5, 5, 1, 1}
fmt.Println(SquareOrSquareRoot(arr))
// [10 10201 25 25 1 1]
}
go
go
edited Jul 28 '17 at 4:34
200_success
127k15148410
127k15148410
asked Jul 28 '17 at 1:02
DeepSpace
25519
25519
bumped to the homepage by Community♦ 7 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 7 hours ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
Where you say "array" you mean, and should say, "slice". Arrays and slices are different things in Go, see "The Go Blog - Arrays, slices (and strings): The mechanics of 'append".
– Dave C
Sep 24 '17 at 16:39
add a comment |
Where you say "array" you mean, and should say, "slice". Arrays and slices are different things in Go, see "The Go Blog - Arrays, slices (and strings): The mechanics of 'append".
– Dave C
Sep 24 '17 at 16:39
Where you say "array" you mean, and should say, "slice". Arrays and slices are different things in Go, see "The Go Blog - Arrays, slices (and strings): The mechanics of 'append".
– Dave C
Sep 24 '17 at 16:39
Where you say "array" you mean, and should say, "slice". Arrays and slices are different things in Go, see "The Go Blog - Arrays, slices (and strings): The mechanics of 'append".
– Dave C
Sep 24 '17 at 16:39
add a comment |
1 Answer
1
active
oldest
votes
up vote
0
down vote
Naming conventions aside (short names in camelCase are typically used in Go), this is the right way of doing what you want. The authors of Go have a different view of what is more readable & maintainable than the authors of Python :-)
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
0
down vote
Naming conventions aside (short names in camelCase are typically used in Go), this is the right way of doing what you want. The authors of Go have a different view of what is more readable & maintainable than the authors of Python :-)
add a comment |
up vote
0
down vote
Naming conventions aside (short names in camelCase are typically used in Go), this is the right way of doing what you want. The authors of Go have a different view of what is more readable & maintainable than the authors of Python :-)
add a comment |
up vote
0
down vote
up vote
0
down vote
Naming conventions aside (short names in camelCase are typically used in Go), this is the right way of doing what you want. The authors of Go have a different view of what is more readable & maintainable than the authors of Python :-)
Naming conventions aside (short names in camelCase are typically used in Go), this is the right way of doing what you want. The authors of Go have a different view of what is more readable & maintainable than the authors of Python :-)
answered Jul 28 '17 at 13:03
Ted
635112
635112
add a comment |
add a comment |
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f171367%2ftransforming-an-array-take-the-square-root-of-each-perfect-square-else-square%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Where you say "array" you mean, and should say, "slice". Arrays and slices are different things in Go, see "The Go Blog - Arrays, slices (and strings): The mechanics of 'append".
– Dave C
Sep 24 '17 at 16:39