Transforming an array: take the square root of each perfect square, else square the number











up vote
4
down vote

favorite
1












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]
}









share|improve this question
















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















up vote
4
down vote

favorite
1












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]
}









share|improve this question
















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













up vote
4
down vote

favorite
1









up vote
4
down vote

favorite
1






1





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]
}









share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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


















  • 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










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 :-)






share|improve this answer





















    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














     

    draft saved


    draft discarded


















    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

























    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 :-)






    share|improve this answer

























      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 :-)






      share|improve this answer























        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 :-)






        share|improve this answer












        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 :-)







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Jul 28 '17 at 13:03









        Ted

        635112




        635112






























             

            draft saved


            draft discarded



















































             


            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            404 Error Contact Form 7 ajax form submitting

            How to know if a Active Directory user can login interactively

            TypeError: fit_transform() missing 1 required positional argument: 'X'