Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Special Interest Groups
  3. C++ Gurus
  4. Riddle: count the moves
Forum Updated to NodeBB v4.3 + New Features

Riddle: count the moves

Scheduled Pinned Locked Moved Unsolved C++ Gurus
18 Posts 3 Posters 2.2k Views 2 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Natural_Bugger

    @Chris-Kawa said in Riddle: count the moves:

    thnx

    ya, that's the difference between someone that has or had computer science teacher could ask questions and someone that is doing this on his own.

    30 days ago, i started @ the 2.2million+ position and currently I'm ranked at slightly under 200.000.
    the solution was probably in their discussion board on the topic, but that the same as cheating.
    so i insist on doing it on my own, but this one ... beat me.

    but i did had my download website ranking top 10 position in Google exactly 20 years ago, coded in Perl.

    i also codec a Midi sequencer in a Pic18F452, acurate to 999BPM, with a linked list, function pointers in C.
    but i never got a programming job.

    Chris KawaC Offline
    Chris KawaC Offline
    Chris Kawa
    Lifetime Qt Champion
    wrote on last edited by
    #9

    @Natural_Bugger:

    ya, that's the difference between someone that has or had computer science teacher could ask questions and someone that is doing this on his own.

    I don't think you need a teacher or a class for this. Bubble sort is literally somewhere in the first 5 things every programming tutorial/video/book/magazine article does right after hello world.
    My honestly brutal and divisive opinion on formal programming education is that it only gives you a bunch of keywords that you can follow up on your own, so it maybe cuts the first step in some cases. The sad truth is most people straight out of school are useless and have theoretical knowledge that is often harmful because thinking that you know something is far worse than knowing and admitting you don't know :)
    As for a degree - I never worked at a place that paid any attention to that. Honestly if an interview started with that I would have walked out because it gives an idea of priorities at that place. But that's just the comfort of the niche I work in I guess. There are lines of work that simply require a degree because of regulations or such, but if you can't get into that there's a lot of programming job lines that value an actual skill over a piece of paper with a stamp.

    N 1 Reply Last reply
    1
    • Chris KawaC Chris Kawa

      @Natural_Bugger:

      ya, that's the difference between someone that has or had computer science teacher could ask questions and someone that is doing this on his own.

      I don't think you need a teacher or a class for this. Bubble sort is literally somewhere in the first 5 things every programming tutorial/video/book/magazine article does right after hello world.
      My honestly brutal and divisive opinion on formal programming education is that it only gives you a bunch of keywords that you can follow up on your own, so it maybe cuts the first step in some cases. The sad truth is most people straight out of school are useless and have theoretical knowledge that is often harmful because thinking that you know something is far worse than knowing and admitting you don't know :)
      As for a degree - I never worked at a place that paid any attention to that. Honestly if an interview started with that I would have walked out because it gives an idea of priorities at that place. But that's just the comfort of the niche I work in I guess. There are lines of work that simply require a degree because of regulations or such, but if you can't get into that there's a lot of programming job lines that value an actual skill over a piece of paper with a stamp.

      N Offline
      N Offline
      Natural_Bugger
      wrote on last edited by Natural_Bugger
      #10
      This post is deleted!
      1 Reply Last reply
      0
      • Chris KawaC Offline
        Chris KawaC Offline
        Chris Kawa
        Lifetime Qt Champion
        wrote on last edited by Chris Kawa
        #11

        Hey, in today's world a "social media influencer" is an actual job title and often paid better than technical positions ;)
        What can you do. Times they are a-changin', adapt or get left behind.

        @Natural_Bugger:

        i have a Book on:
        Perl
        Java
        Java2
        programming in C# 3.0
        adobe Flex
        but i didn't know that expression (bubble sort).

        I have an equally controversial opinion on all these languages and what they did to the industry, but I feel I'm already being too negative, so I'll restrain myself from voicing that opinion ;) All I can say is if a book on a programming language doesn't mention basic algorithms then there's probably a better book out there. I'd say you would be far better to read a book on algorithms and data structures, which are universal across technologies, instead of another language, because that's just slightly different syntax solving the same basic problems - data relations, finding, indexing, structured traversal, sorting, data access patterns, etc.

        N 1 Reply Last reply
        1
        • Chris KawaC Chris Kawa

          I know this is probably a homework or something, but it's been probably close to two decades since I last implemented bubble sort so why not :)

          Printing:

          void print(const std::vector<int>& v)
          {
              for (int val : v)
                  std::cout << val << " ";
              std::cout << "\n";
          }
          

          swapping with outputing the move:

          void swap(int& a, int& b)
          {
              std::cout << a << " jumps " << b << "\n";
              int temp = a;
              a = b;
              b = temp;
          }
          

          sorting:

          void bubbleSort(std::vector<int>& v)
          {
              for (int i = v.size() - 1; i > 0; --i)
              {
                  for (int j = 0; j < i; ++j)
                  {
                      if (v[j] > v[j + 1])
                           swap(v[j], v[j+1]);
                  }
              }
          }
          

          and then use:

          std::vector<int> v {1,2,5,3,7,8,6,4};
          
          print(v);
          bubbleSort(v);
          print(v);
          

          Output:

          1 2 5 3 7 8 6 4 
          5 jumps 3
          8 jumps 6
          8 jumps 4
          7 jumps 6
          7 jumps 4
          6 jumps 4
          5 jumps 4
          1 2 3 4 5 6 7 8 
          
          N Offline
          N Offline
          Natural_Bugger
          wrote on last edited by
          #12

          @Chris-Kawa said in Riddle: count the moves:

          thnx, it does indeed work in the required or expected steps, but i also need to keep track of the swap distance.
          so i had to modify the swap function.

          void swap(vector< pair <int, int> > &data, int& a, int& b)
          {
              if (std::find(data.begin(), data.end(), std::pair<std::int, std::int>(a, 0)) != data.end()){ // Zero is problem
                  for(auto ii : data){
                      if(ii.first == a){
                          ii.second = ii.second +1;
                      }
                  }
              }
              else{
                  data.push_back( make_pair(a, 0) );
              }
          
              std::cout << a << " jumps " << b << "\n";
              int temp = a;
              a = b;
              b = temp;
          }
          

          error:

          Solution.cpp:16:73: error: wrong number of template arguments (1, should be 2)
          

          i know only the first, if not present in the vector add, otherwise update it.

          regards

          1 Reply Last reply
          0
          • Chris KawaC Chris Kawa

            Hey, in today's world a "social media influencer" is an actual job title and often paid better than technical positions ;)
            What can you do. Times they are a-changin', adapt or get left behind.

            @Natural_Bugger:

            i have a Book on:
            Perl
            Java
            Java2
            programming in C# 3.0
            adobe Flex
            but i didn't know that expression (bubble sort).

            I have an equally controversial opinion on all these languages and what they did to the industry, but I feel I'm already being too negative, so I'll restrain myself from voicing that opinion ;) All I can say is if a book on a programming language doesn't mention basic algorithms then there's probably a better book out there. I'd say you would be far better to read a book on algorithms and data structures, which are universal across technologies, instead of another language, because that's just slightly different syntax solving the same basic problems - data relations, finding, indexing, structured traversal, sorting, data access patterns, etc.

            N Offline
            N Offline
            Natural_Bugger
            wrote on last edited by Natural_Bugger
            #13
            This post is deleted!
            1 Reply Last reply
            0
            • Chris KawaC Offline
              Chris KawaC Offline
              Chris Kawa
              Lifetime Qt Champion
              wrote on last edited by
              #14

              @Natural_Bugger said:

              so i had to modify the swap function.

              I think you've overcomplicated this. Taking a step back and looking at the problem - you want to count number of jumps for given number. Vector of pairs can work but a far simpler structure for this is a map. The key would be the number and the value is the amount of swaps it did, e.g.

              void swap(std::map<int, int>& counts, int& a, int& b)
              {
                  counts[a]++;
                  std::cout << a << " jumps " << b << "\n";
                  int temp = a;
                  a = b;
                  b = temp;
              }
              

              Then you can print it like this:

              for (auto& c : counts)
                      std::cout << "Number " << c.first << " jumped " << c.second << " time(s)\n";
              

              If you want to count both the number jumping and being jumped you can add counts[b]++ in there too. Or you could count those cases in separate maps, whatever your need is.

              N 3 Replies Last reply
              1
              • Chris KawaC Chris Kawa

                @Natural_Bugger said:

                so i had to modify the swap function.

                I think you've overcomplicated this. Taking a step back and looking at the problem - you want to count number of jumps for given number. Vector of pairs can work but a far simpler structure for this is a map. The key would be the number and the value is the amount of swaps it did, e.g.

                void swap(std::map<int, int>& counts, int& a, int& b)
                {
                    counts[a]++;
                    std::cout << a << " jumps " << b << "\n";
                    int temp = a;
                    a = b;
                    b = temp;
                }
                

                Then you can print it like this:

                for (auto& c : counts)
                        std::cout << "Number " << c.first << " jumped " << c.second << " time(s)\n";
                

                If you want to count both the number jumping and being jumped you can add counts[b]++ in there too. Or you could count those cases in separate maps, whatever your need is.

                N Offline
                N Offline
                Natural_Bugger
                wrote on last edited by
                #15

                @Chris-K
                thnx,

                that works ... some minor things to do and done!

                1 Reply Last reply
                0
                • Chris KawaC Chris Kawa

                  @Natural_Bugger said:

                  so i had to modify the swap function.

                  I think you've overcomplicated this. Taking a step back and looking at the problem - you want to count number of jumps for given number. Vector of pairs can work but a far simpler structure for this is a map. The key would be the number and the value is the amount of swaps it did, e.g.

                  void swap(std::map<int, int>& counts, int& a, int& b)
                  {
                      counts[a]++;
                      std::cout << a << " jumps " << b << "\n";
                      int temp = a;
                      a = b;
                      b = temp;
                  }
                  

                  Then you can print it like this:

                  for (auto& c : counts)
                          std::cout << "Number " << c.first << " jumped " << c.second << " time(s)\n";
                  

                  If you want to count both the number jumping and being jumped you can add counts[b]++ in there too. Or you could count those cases in separate maps, whatever your need is.

                  N Offline
                  N Offline
                  Natural_Bugger
                  wrote on last edited by
                  #16

                  @Chris-Kawa

                  sometimes you can oversee the simple things.
                  counts[a]++;

                  1 Reply Last reply
                  0
                  • Chris KawaC Chris Kawa

                    @Natural_Bugger said:

                    so i had to modify the swap function.

                    I think you've overcomplicated this. Taking a step back and looking at the problem - you want to count number of jumps for given number. Vector of pairs can work but a far simpler structure for this is a map. The key would be the number and the value is the amount of swaps it did, e.g.

                    void swap(std::map<int, int>& counts, int& a, int& b)
                    {
                        counts[a]++;
                        std::cout << a << " jumps " << b << "\n";
                        int temp = a;
                        a = b;
                        b = temp;
                    }
                    

                    Then you can print it like this:

                    for (auto& c : counts)
                            std::cout << "Number " << c.first << " jumped " << c.second << " time(s)\n";
                    

                    If you want to count both the number jumping and being jumped you can add counts[b]++ in there too. Or you could count those cases in separate maps, whatever your need is.

                    N Offline
                    N Offline
                    Natural_Bugger
                    wrote on last edited by
                    #17

                    @Chris-Kawa

                    the explication of some of the riddles there is written complicatedly.
                    as in this.

                    : )

                    1 Reply Last reply
                    0
                    • Chris KawaC Chris Kawa

                      I know this is probably a homework or something, but it's been probably close to two decades since I last implemented bubble sort so why not :)

                      Printing:

                      void print(const std::vector<int>& v)
                      {
                          for (int val : v)
                              std::cout << val << " ";
                          std::cout << "\n";
                      }
                      

                      swapping with outputing the move:

                      void swap(int& a, int& b)
                      {
                          std::cout << a << " jumps " << b << "\n";
                          int temp = a;
                          a = b;
                          b = temp;
                      }
                      

                      sorting:

                      void bubbleSort(std::vector<int>& v)
                      {
                          for (int i = v.size() - 1; i > 0; --i)
                          {
                              for (int j = 0; j < i; ++j)
                              {
                                  if (v[j] > v[j + 1])
                                       swap(v[j], v[j+1]);
                              }
                          }
                      }
                      

                      and then use:

                      std::vector<int> v {1,2,5,3,7,8,6,4};
                      
                      print(v);
                      bubbleSort(v);
                      print(v);
                      

                      Output:

                      1 2 5 3 7 8 6 4 
                      5 jumps 3
                      8 jumps 6
                      8 jumps 4
                      7 jumps 6
                      7 jumps 4
                      6 jumps 4
                      5 jumps 4
                      1 2 3 4 5 6 7 8 
                      
                      N Offline
                      N Offline
                      Natural_Bugger
                      wrote on last edited by Natural_Bugger
                      #18

                      @Chris-Kawa

                      ha, the code is good, but does not pas all tests.
                      it doesn't pass the tests within in the time limit, handling set of numbers up to 100000.

                      Your code did not execute within the time limits
                      
                      
                      void swap(std::map<int, int>& countsdata, int &count, int& a, int& b)
                      {
                          countsdata[a]++;
                          int temp = a;
                          a = b;
                          b = temp;
                          count++;
                      }
                      
                      void minimumMoves(vector<int> q) {
                      std::map<int, int> countsdata;
                      int count = 0;
                      
                      for (int i = q.size() - 1; i > 0; --i)
                          {
                              for (int j = 0; j < i; ++j)
                              {
                                  if (q[j] > q[j + 1])
                                       swap(countsdata, count, q[j], q[j+1]);
                              }
                          }
                      
                      bool tooChoatic = false;
                      for (auto& c : countsdata){ // i suspect this is the culprit.
                              if(c.second > 2){
                                  tooChoatic = true;
                                  break;
                              }
                      }
                      
                      if(tooChoatic){
                          std::cout << "Too chaotic" << std::endl; // jumped more than 2 positions.
                      }
                      else{
                          std::cout << count << std::endl;
                      }
                      }
                      
                      1 Reply Last reply
                      0

                      • Login

                      • Login or register to search.
                      • First post
                        Last post
                      0
                      • Categories
                      • Recent
                      • Tags
                      • Popular
                      • Users
                      • Groups
                      • Search
                      • Get Qt Extensions
                      • Unsolved