Game development as an Art

Sato Game Dev

Code Exercises


Dynamic Array


Source: Hacker rank

-Create a list, seqList, of N empty sequences, where each sequence is indexed from 0 to N-1. The elements within each of N the sequences also use 0-indexing.
-Create an integer, lastAnswer, and initialize it to 0.
The 2 types of queries that can be performed on your list of sequences (seqList) are described below:

1.Query: 1 x y
1.Find the sequence, seq, at index in
2.Append integer y to sequence seq.

2.Query: 2 x y
1.Find the sequence, seq, at index in seqList.
2.Find the value of element y % size in (where size is the size of seq) and assign it to lastAnswer.
3.Print the new value of lastAnswer on a new line.

Task
Given N, Q, and Q queries, execute each query.
Input Format

The first line contains two space-separated integers, (the number of sequences) and (the number of queries), respectively.
Each of the Q subsequent lines contains a query in the format defined above.

Constraints

It is guaranteed that query type 2 will never query an empty sequence or index.

Output Format

For each type 2 query, print the updated value of lastAnswer on a new line.

Sample Input

2 5
1 0 5
1 1 7
1 0 3
2 1 0
2 1 1

Sample Output
7
3
Explanation
Initial Values:
N = 2
lastAnswer = 0
= [ ]
= [ ]
Query 0: Append 5 to sequence ((0^0)%2)=0.
lastAnswer = 0
= [5]
= [ ]
Query 1: Append 7 to sequence ((1^0)%2)=1.
lastAnswer = 0
= [5]
= [7]
Query 2: Append to sequence .
lastAnswer = 0
= [5, 3]
= [7]
Query 3: Assign the value at index 0 of sequence ((1^0)%2)=1 to lastAnswer, print lastAnswer.
lastAnswer = 7
= [5, 3]
= [7]
Output: 7
Query 4: Assign the value at index 1 of sequence ((1^7)%2)=0 to lastAnswer, print lastAnswer.
lastAnswer = 3
= [5, 3]
= [7]
Output: 3

My solution:
C#:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
using System.CodeDom.Compiler;
using System.Collections.Generic;
using System.Collections;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Runtime.Serialization;
using System.Text.RegularExpressions;
using System.Text;
using System;

class Solution
{

     static void dynamicArray(int n, List<List<int>> queries)
    {
        //List<List<int>> seqList = new List<List<int>>();
        List<int>[] seqList = new List<int>[n];
        //initiate seqList
        for (int aux=0; aux<n; aux++)
        {
            seqList[aux] = new List<int>();
        }


        int lastAnswer = 0;

        foreach (List<int> query in queries)
        {
            if (query[0]==1)
            {
                //finding the sequece
                int seqNum = ((query[1] ^ lastAnswer) % n);

                //Appending integer y to sequence
                seqList[seqNum].Add(query[2]);
            }
            else if (query[0] == 2)
            {
                //finding the sequece
                int seqNum = ((query[1] ^ lastAnswer) % n);

                //Find the value of element in (where is the size of ) and assign it to .
                int result = query[2] % seqList[seqNum].Count;
                lastAnswer = seqList[seqNum][result];

                //Print the new value of lastAnswer on a new line
                Console.WriteLine(lastAnswer);
            }
            else
            {
                Console.WriteLine("Type query not found: " + query[0]);
            }
        }

    }

    static void Main(string[] args)
    {


        string[] nq = Console.ReadLine().TrimEnd().Split(' ');

        int n = Convert.ToInt32(nq[0]);

        int q = Convert.ToInt32(nq[1]);

        List<List<int>> queries = new List<List<int>>();

        for (int i = 0; i < q; i++)
        {
            queries.Add(Console.ReadLine().TrimEnd().Split(' ').ToList().Select(queriesTemp => Convert.ToInt32(queriesTemp)).ToList());
        }

        dynamicArray(n, queries);

    }
}