Ad
  • Custom User Avatar
  • Custom User Avatar

    From the Problem Description section: You start at any initial index of your choice..

    Consider the first mountain as the start.

  • Default User Avatar

    why undefined behavior exists is a complex question. It certainly has a lot to do with the fact that C is a low-level language, providing direct access to memory and with a focus on runtime speed. It dates back from a time (50 years ago) where compilers and similar tools were much more limited than the powerhouses we know today. Many programmer errors were not detectable by compilers at the time; and even nowadays, you need a static or dynamic analyzer on top of the compiler to find most UBs in your code, because of how lax the language rules are, especially regarding memory manipulation.

    It's also hard to change the rules of the languages now without breaking compatibility with the colossal amount of C/C++ code that has been written over the decades and that powers pretty much everything we take for granted, from OS kernels and embedded systems to web broswers, game engines and interpreters (Ruby, Python, PHP... all have engines written in C; major JavaScript engines are written in C++).


    As textninja mentionned above, an insane amount of work has been done over the years on C/C++ compilers to make the generated code run really fast. Very often, there is a tradeoff between runtime speed and safety: for example, Java will check at runtime if you try to access an array outside of its bounds, and gracefully throw an error if you do so. This kind of checks have an impact on runtime performance. In C/C++, your array accesses are faster than Java's, but if you do an out-of-bounds access, you are on your own.

    (Modern compiled languages, e.g. Rust or OCaml, try to do as many safety checks as possible at compile time, to remove as many of them as possible from the run time, thus imrpoving on both aspects at once).


    Unfortunately, all of this means that undefined behavior is here to stay for the foreseeable future.

  • Custom User Avatar

    This seems a little "thin" to me. If we assume that programmer's time is more important than machine time, making the compiler faster at the cost of potential programmer confusion seems like a bad trade-off.

    One of C++'s main selling points is speed, so I think it's fair to make the tradeoff for that language in particular. N.B. it doesn't just make the compiler faster, but actually allows the compiler to generate faster code.

  • Default User Avatar

    by nature of the C/C++ languages, these kind of things are unpredictable - they are called undefined behavior for that reason. the state of uninitialized variables depends on a lot of factors, such as the code that surrounds them, how they were allocated (e.g. heap versus stack), and more broadly the entire environment (compiler, architecture, OS...)

    it is easier to visualize it with an array:

    #include <iostream>
    #include <array>
    
    int main() {
    	std::array<bool, 32> booleans; // 'booleans' is uninitialized
    	for (bool boolean : booleans)
    		std::cout << boolean << std::endl;
    }
    

    if you run that on either Codewars or your machine you will most likely see many different values for the booleans (as they are bytes behind the scene)

  • Default User Avatar

    in

    bool boolean1, boolean2 = false;
    

    the boolean1 variable is left uninitialized. On your computer you were being 'lucky' that the memory happened to be zeroed, so it was initialized to false

  • Custom User Avatar

    approuvé

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    approved by someone

  • Custom User Avatar

    lgtm, approved

  • Custom User Avatar
  • Custom User Avatar
  • Custom User Avatar

    My knowledge about Java is None, probably I need to find someone to check it. But thank you for your work.

  • Custom User Avatar

    Thank you for your reply.

    This is my own idea, from an idea that simply observing the math pattern of a perfect binary tree.
    When I was thinkng about using the BFS or DFS, then arrived at this one.

  • Loading more items...