Mars Exploration

Sort by

recency

|

1066 Discussions

|

  • + 0 comments
    def marsExploration(s):
        # Write your code here
        count=0
        for i in range(1,len(s)-1,3):
            if s[i-1]!='S':
                count+=1
            if s[i]!='O':
                count+=1
            if s[i+1]!='S':
                count+=1
        return count
    
  • + 0 comments

    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

    Here is my one line Python solution!

    def marsExploration(s):
        return len([True for letter in range(len(s)) if s[letter] != ["S", "O", "S"][letter % 3]])
    
  • + 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)
    }