Mars Exploration

Sort by

recency

|

217 Discussions

|

  • + 0 comments

    My solution in C#

    public static int marsExploration(string s)
        {
            s = s.ToUpper();
            
            int count = 0;
            string messageSig = "SOS";
            
            int longitud = s.Length;
            var modules = new List<string>();
            
            if((longitud < 1 || longitud > 99) && longitud % 3 != 0) throw new InvalidDataException("Longitud invalida");
            
            for(int i = 0; i < longitud; i += 3){
                modules.Add(s.Substring(i, 3));
            }
            
            foreach(string subMessage in modules){
                for(int j = 0; j < 3; j++){
                    if(subMessage[j] != messageSig[j]) count++;
                }
            }
            
            return count;
        }
    
  • + 0 comments

    My solution in python:

    def marsExploration(s):
        nChanged = 0
        for i, value in enumerate(s):
            remainder =  i % 3
            if remainder in (0, 2) and value != "S":
                nChanged += 1
            elif remainder == 1 and value != "O":
                nChanged += 1
        return nChanged
    
  • + 0 comments

    swift

    func marsExploration(s: String) -> Int {
        var changed = 0
        for (index, c) in s.enumerated() {
            switch index % 3 {
                case 0, 2:
                   if c != "S" {
                        changed = changed + 1
                   }
                case 1:
                    if c != "O" {
                        changed = changed + 1
                   }
                default:
                    break
            }
        }
        return changed
    }
    
  • + 0 comments

    We need to keep track of the position since an S could also be changed for an O. So just counting isn't enough

      public static int marsExploration(String s) {
            
            int counter = 0;
            int position = 1;
            for(int i = 0; i < s.length(); i++){            
                System.out.println(position);
                
                if((position == 1 || position == 3) && s.charAt(i) != 'S') {
                counter++;
                }else if(position == 2 && s.charAt(i) != 'O'){
                counter++;
                }   
                
                if(position == 3){
                    position = 1;
                }else{
                    position++;
                }                                                                                                                                            
            }
            
            return counter;
        }
    
  • + 0 comments

    Here's my python code:

    def marsExploration(s):
        sos = "SOS" * (len(s)//3)
        changed = 0
        for index, item in enumerate(s):
            if item != sos[index]:
                changed += 1
        return changed