title: Program Page Replacement Algorithms Set 2 Fifo
signature: program_page_replacement_algorithms_set_2_fifo <- function(pages, n, capacity) {
test_code: 
  
  
  # SOLUTION CODE
  # ============================================
  PLACEHOLDER_CODE_BODY
  
  # TESTING CODE 
  # ============================================
  validate_solution <- function(actual, expected) {
      toString(actual)==toString(expected)
  }
  
  driver <- function(pages, n, capacity, expected){
      if (validate_solution(PLACEHOLDER_FN_NAME(pages, n, capacity), expected)){
          "PASSED"
      } else{
          "FAILED"
      }
  }
  
  main <- function() {
      cat(sprintf("TEST-0...%s\n",driver(
          list(4L, 4L, 6L, 7L, 8L, 11L, 13L, 18L, 26L, 35L, 36L, 37L, 45L, 46L, 46L, 47L, 48L, 49L, 51L, 52L, 53L, 56L, 61L, 74L, 75L, 77L, 80L, 83L, 85L, 86L, 87L, 90L, 93L, 95L, 97L, 98L, 99L, 99L), 36L, 37L, 
          34L
      )))
  
      cat(sprintf("TEST-1...%s\n",driver(
          list(78L, -48L, 50L, -20L, -6L, 58L, -8L, 66L, 72L, 68L, 4L, 80L, 58L, -26L, -82L, -56L, 92L, 76L, 20L, 82L, -46L, 86L, 38L, 60L, -62L, -48L, 76L, 8L, -66L, -4L, -98L, -96L, -52L, 92L), 33L, 23L, 
          30L
      )))
  
      cat(sprintf("TEST-2...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 19L, 13L, 
          2L
      )))
  
      cat(sprintf("TEST-3...%s\n",driver(
          list(98L, 78L, 94L, 42L, 62L, 83L, 7L, 62L, 60L, 94L, 16L, 28L, 50L, 15L, 18L, 71L, 86L, 47L, 62L, 89L), 15L, 11L, 
          13L
      )))
  
      cat(sprintf("TEST-4...%s\n",driver(
          list(-82L, -70L, -68L, -56L, -50L, -44L, 4L, 18L, 28L, 30L, 30L, 42L, 66L, 78L, 80L), 9L, 11L, 
          9L
      )))
  
      cat(sprintf("TEST-5...%s\n",driver(
          list(1L, 1L, 0L, 0L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 0L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 0L, 0L, 0L, 0L, 1L, 0L, 0L), 25L, 25L, 
          2L
      )))
  
      cat(sprintf("TEST-6...%s\n",driver(
          list(4L, 5L, 13L, 15L, 18L, 28L, 32L, 40L, 46L, 46L, 55L, 57L, 61L, 63L, 65L, 68L, 77L, 79L, 79L, 96L), 17L, 18L, 
          16L
      )))
  
      cat(sprintf("TEST-7...%s\n",driver(
          list(-2L, 82L, 2L, -74L, -6L, -24L, 54L, -74L, -98L, 8L, -94L, -60L, -42L, -38L, 36L, -38L, -58L, -70L, -28L, -34L, 70L, -6L, -2L, -76L, -40L, -4L, 0L, -4L, 76L, 48L, -34L, -26L, -48L, -58L, -88L, -44L, 20L, -22L, 78L), 31L, 24L, 
          25L
      )))
  
      cat(sprintf("TEST-8...%s\n",driver(
          list(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), 26L, 24L, 
          2L
      )))
  
      cat(sprintf("TEST-9...%s\n",driver(
          list(4L, 90L, 28L, 71L, 69L, 45L, 92L, 63L, 72L, 76L, 47L, 85L, 36L, 59L, 88L, 46L, 28L, 19L, 50L, 31L, 63L, 13L), 15L, 12L, 
          15L
      )))
  
  }
  
  main()
source_py: def program_page_replacement_algorithms_set_2_fifo(pages, n, capacity):
      s = set()
      indexes = Queue()
      page_faults = 0
      for i in range(n):
          if (len(s) < capacity):
              if (pages[i] not in s):
                  s.add(pages[i])
                  page_faults += 1
                  indexes.put(pages[i])
          elif (pages[i] not in s):
              val = indexes.queue[0]
              indexes.get()
              s.remove(val)
              s.add(pages[i])
              indexes.put(pages[i])
              page_faults += 1
      return page_faults
source_cpp: int program_page_replacement_algorithms_set_2_fifo(vector<int> pages, int n, int capacity) {
    unordered_set < int > s;
    queue < int > indexes;
    int page_faults = 0;
    for ( int i = 0;
    i < n;
    i ++ ) {
      if ( s . size ( ) < capacity ) {
        if ( s . find ( pages [ i ] ) == s . end ( ) ) {
          s . insert ( pages [ i ] );
          page_faults ++;
          indexes . push ( pages [ i ] );
        }
      }
      else {
        if ( s . find ( pages [ i ] ) == s . end ( ) ) {
          int val = indexes . front ( );
          indexes . pop ( );
          s . erase ( val );
          s . insert ( pages [ i ] );
          indexes . push ( pages [ i ] );
          page_faults ++;
        }
      }
    }
    return page_faults;
  }