Mars Exploration

Sort by

recency

|

1064 Discussions

|

  • + 0 comments

    Perl solution:

    sub marsExploration {
        my $s = shift;
        
        my @arr = split("", $s);
        my $cnt = 0;
        for (my $i = 0; $i <= scalar(@arr) - 1; $i += 3) {
            $cnt++ if ($arr[$i] ne "S");
            $cnt++ if ($arr[$i+1] ne "O");
            $cnt++ if ($arr[$i+2] ne "S");
        }
        
        return $cnt
    }
    
  • + 0 comments

    My answer in Typescript, simple, not minimized

    const SOS = (l: number) => 'SOS'.repeat(l);
    const SBD = (l: number, cb: (i: number) => string) => Array(l).fill('').map((_, i) => cb(i)).join('');
    function marsExploration(s: string): number {
        let recieved_signal = s;
        let expected_signal = SOS(s.length / 3);
        let differen_signal = SBD(s.length, i => recieved_signal[i] != expected_signal[i] ? 'X' : ' ');
    
        console.log('Expected signal:', expected_signal)
        console.log('Recieved signal:', recieved_signal)
        console.log('Difference:     ', differen_signal)
    
        return differen_signal.split('').reduce((p, c) => c == 'X' ? p + 1 : p, 0)
    }
    
  • + 0 comments
    public static int marsExploration(String s) {
      // Write your code here
      int diff = 0;
      for (int i = 0; i < s.length(); i++) {
        if ((i % 3 == 0 || i % 3 == 2) && s.charAt(i) != 'S') {
          diff++;
        } else if (i % 3 == 1 && s.charAt(i) != 'O') {
          diff++;
        }
      }
      return diff;
    }
    
  • + 1 comment

    Here is my c++ solution, you can find the explanation here : https://youtu.be/Gf0fpGE83r4

    int marsExploration(string s) {
       int result = 0;
        string base = "SOS";
        for(int i = 0; i < s.size(); i++) if(s[i] != base[i%3]) result++;
        return result;
    }
    
  • + 0 comments
    def marsExploration(s):
        c=0
        for i in range(0,len(s)//3):
            if s[0+3*i]!='S':
                c+=1
            if s[1+3*i]!='O':
                c+=1
            if s[2+3*i]!='S':
                c+=1
            
        return c