# ENGG1330-Computer-Programming-I

## Level 3 

After passing all the tests in Level 2, the interviewers proceed to ask a follow-up question to truly test your ability in programming and problem solving. They tell you that it is computationally expensive to move the most recently used item to the end of the list in order to conform to the description in the Introduction, i.e., the 1st (leftmost) item is the least recently used while the last (rightmost) item is the most recently used. It is because all the subsequent items need to be moved to the left by 1. For example, the steps of moving 2 in [1,2,3,4] to the end of the list is [1,2,3,4] à [1,3,3,4] à [1,3,4,4] à [1,3,4,2]. As a result, moving item to the end of a list should be avoided if possible. The follow-up question is whether you can implement a solution without the need of moving items in the cache once they are inserted. Specifically, when accessing an item, NO movement should be made to any item but you are allowed to modify the item. When adding a new item to the list, it should be added to the end of the list if the cache is not full. Otherwise, the least recently used item is REPLACED by the new item and NO movement of other items are allowed. For example, given a cache of capacity 4: `[[1,’100’], [2,’200’], [3,’300’]]`, the cache
contents will be updated as follows.

1. get(2) -> `[[1,’100’], [2,’200’], [3,’300’]]`
1. put(4,’400’) -> `[[1,’100’], [2,’200’], [3,’300’], [4,’400’]]`
1. get(1) -> `[[1,’100’], [2,’200’], [3,’300’], [4,’400’]]`
1. put(5,’500’) -> `[[1,’100’], [2,’200’], [5,’500’], [4,’400’]]`

As a result, there is NO limitation on how you are going to store the data. However, ONLY one list can be created and used for the cache and no other auxiliary list is allowed.

### Note:
The test cases on VPL will be time limited. If you see timeout on VPL, it means your program is too slow and need further optimization. Nested loops are usually the most expensive operations and it is a good starting point for optimization.

### Inputs:
1. Capacity of the cache � (integer). � = -1 means that the cache has unlimited capacity.
2. A string of integral keys K1, K2, …, KNk separated by “,” where �! is number of keys.
3. A string of values V1, V2, …, VNv separated by “,” where �" is number of values.
4. A series of commands consisting of either “get,KEY” or “put,KEY,VALUE” separated by
a newline character.
5. The input must be ended with “end”.

### Outputs:
1. Warning message if �! ¹ �".
2. For every get operation, print the value of the key-value pair if the key exists,
otherwise print “NULL”.
3. Cache as a list with format ![�1, �1],[�2, �2], … [��, ��], where the ordering is no
longer based on recent usage but follows the specs listed in the question.

### Assumptions:
- 1 ≤ � ≤ 10000 �� � = −1
- 1 ≤ �!, �" ≤ 10000
- Input keys can be converted to integers
- Input keys are unique, no duplicates
- Input values are non-null string
- None of the values will be equal to “NULL”
- Time constraint will be imposed on VPL and timeout is possible

### Hints:
- You may want to change the structure of how you keep the data. For example, you can store extra information alongside with each data item if you find it useful.
- It is acceptable if put and/or other operations take more time to run as long as the total time is within the time constraint. Your program should be well within the time constraint unless it has multiple layers of loop



### Examples:
- Case 
  - Sample input 
  - Sample output


- Case 1
    - Sample input 
      ```
      -1
      1,2,3,4,5,6,7,8,9,10
      101,102,103,104,105,106,107,108,109,110
      get,3
      get,6
      get,1
      get,100
      get,5
      end
      ```
    - Sample output
      ```
      103
      106
      101
      NULL
      105
      [[1, '101'], [2, '102'], [3, '103'], [4, '104'], [5, '105'], [6, '106'], [7, '107'], [8, '108'], [9, '109'], [10, '110']]
      ```
- Case 2
  - Sample input 
    ```
    3
    1,2,3,4,5,6,7,8,9,10
    101,102,103,104,105,106,107,108,109,110
    get,1
    get,2
    get,3
    get,10
    get,9
    end
    ```
  - Sample output
    ```
    NULL
    NULL
    NULL
    110
    109
    [[8, '108'], [9, '109'], [10, '110']]
    ```

- Case 3
  - Sample input 
    ```
    6
    9,10
    101,102,103,104,105,106,107,108,109,110
    get,9
    put,1,100
    put,2,200
    put,3,300
    get,10
    get,2
    put,4,400
    put,5,500
    put,6,600
    put,1,10000
    put,7,700
    get,3
    get,2
    end
    ```
  - Sample output
    ```
    Warning: number of keys and values are not the same
    NULL
    NULL
    200
    NULL
    200
    [[1, '10000'], [2, '200'], [7, '700'], [4, '400'], [5, '500'], [6, '600']]
    ```
