# ENGG1330-Computer-Programming-I

## Level 2 (45%)

Next, you are going to implement two main functions, get(key) and put(key, value) to access a data item in the cache. The get(key) function retrieves the value of the data item given by the key parameter if the key exists in the cache. The put(key, value) function modifies the value of the data item, if the key exists, or inserts a new data item, if the key does not exist.

The ordering of data items in the cache must 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. Every time a data item is accessed, whether using get or put,
the data item has to be moved to the end (rightmost) of the cache to become the most recently used item. When a new data item is inserted to a full cache, the least recently used (leftmost) item needs to be evicted.

For example, given a cache of capacity 5: 
  `[[1,'100'], [2,'200'], [3,'300'], [4,'400']]`
, the cache contents will be updated as follows.

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

In addition to the inputs you received from Level 1, you will also receive a list of commands. "get,KEY" denotes the get operation with key KEY and "put,KEY,VALUE" denotes the put operation with key KEY and value VALUE. Get operation prints the value of the keyvalue pair if the key exists, otherwise prints "NULL". Put operation does not need to print anything. The command "end" denotes the end of the inputs and you should then print the final contents of the cache.

### Inputs:
1. Capacity of the cache `C` (integer). `C` = -1 means that the cache has unlimited capacity.
1. A string of integral keys K1, K2, …, KNk separated by "," where Nk is number of keys.
1. A string of values V1, V2, …, VNv separated by "," where Nv is number of values.
1. A series of commands consisting of either "get,KEY" or "put,KEY,VALUE" separated by a newline character.
1. 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], … [��, ��], with the 1st (leftmost)
item being the least recently used while the last (rightmost) item being the most
recently used.

### Assumptions:
- 1 ≤ � ≤ 1000 �� � = −1
- 1 ≤ �!, �" ≤ 1000
- 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"

### 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
      [[2, '102'], [4, '104'], [7, '107'], [8,'108'], [9, '109'], [10, '110'], [3,'103'], [6, '106'], [1, '101'], [5,'105']]
      ```
- 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'], [10, '110'], [9, '109']]
    ```

- 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   <- get,9
    NULL   <- get,10
    200    <- get,2
    NULL   <- get,3
    200    <- get,2
    [[4, '400'], [5, '500'], [6, '600'], [1, '10000'], [7, '700'], [2, '200']]
    ```
