Sort by

recency

|

510 Discussions

|

  • + 0 comments

    typescript boilerplate:

    `use strict";

    process.stdin.resume(); process.stdin.setEncoding("utf-8"); let inputString: string = ""; let inputLines: string[] = []; let currentLine: number = 0; process.stdin.on("data", function (inputStdin: string): void { inputString += inputStdin; });

    process.stdin.on("end", function (): void { inputLines = inputString.split("\n"); inputString = ""; main(); });

    function readLine(): string { return inputLines[currentLine++]; }

    class stackQueue { private s1: number[] = []; private s2: number[] = [];

    constructor() { this.s1 = []; this.s2 = []; }

    enqueue(x: number) { // Enter your code here }

    dequeue() { // Enter your code here }

    print() { // Enter your code here } }

    function main() { const testQueue = new stackQueue(); const commandMap = { "1": "enqueue", "2": "dequeue", "3": "print" } as const;

    inputLines.shift();

    inputLines.map((inputLine: string) => { const [command, input] = inputLine.split(" ") as ["1" | "2" | "3", string]; const method = commandMap[command]; if (method == "enqueue") { return testQueue.enqueue(Number(input)); } return testQueuemethod; }); }

    `

    javascript boilerplate "use strict";

    process.stdin.resume(); process.stdin.setEncoding("utf-8"); let inputString = ""; let inputLines = []; let currentLine = 0; process.stdin.on("data", function (inputStdin) { inputString += inputStdin; });

    process.stdin.on("end", function () { inputLines = inputString.split("\n"); inputString = ""; main(); });

    function readLine() { return inputLines[currentLine++]; }

    class stackQueue { constructor() { this.s1 = []; this.s2 = []; }

    enqueue(x) { // Enter your code here }

    dequeue() { // Enter your code here }

    print() { // Enter your code here } }

    function main() { const testQueue = new stackQueue(); const commandMap = { 1: "enqueue", 2: "dequeue", 3: "print" };

    inputLines.shift();

    inputLines.map((inputLine) => { const [command, input] = inputLine.split(" "); const method = commandMap[command]; if (method == "enqueue") { return testQueue.enqueue(Number(input)); } return testQueuemethod; }); }

    `

  • + 0 comments
    que=[]
    for i in range(int(input())):
        x=input()
        if x[0]=='1':
            val=int(x[2:])
            que.append(val)
        elif x[0]=='2':
            que.pop(0)
        elif x[0]=='3':
            print(que[0])
    
  • + 1 comment

    Java solution

    public class Solution { private Stack stackEnqueue = new Stack<>(); private Stack stackDequeue = new Stack<>();

    // Enqueue
    public void enqueue(int n){
        stackEnqueue.push(n);
    }
    
    // Dequeue
    public void dequeue(){
        reverseStack();
        if(!stackDequeue.isEmpty()){
            stackDequeue.pop();
        }
    }
    
    //Reverse
    private void reverseStack(){
        if(stackDequeue.isEmpty()){
            while(!stackEnqueue.isEmpty()){
                stackDequeue.push(stackEnqueue.pop());
            }
        }
    }
    
    //Print front element
    public void printFrontElement(){
        reverseStack();
        if(!stackDequeue.isEmpty()){
            System.out.println(stackDequeue.peek());
        }
    }
    public static void main(String[] args) {
        /* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution. */
        Scanner scanner = new Scanner((System.in));
        Solution twoStackQueue = new Solution();
        int count = scanner.nextInt();
        for(int i = 0; i< count; i++){
            int choice = scanner.nextInt();
            switch (choice){
                case 1:
                    int enqueueNumber = scanner.nextInt();
                    twoStackQueue.enqueue(enqueueNumber);
                    break;
                case 2:
                    twoStackQueue.dequeue();
                    break;
                case 3:
                    twoStackQueue.printFrontElement();
                    break;
            }
        }
        scanner.close();
    }
    

    }

  • + 0 comments

    Using Python3

    old, new = [], []
    for _ in range(int(input())):
        val = list(map(int,input().split()))
        if val[0] == 1:
            new.append(val[1])
        elif val[0] == 2:
            if not old :
                while new : old.append(new.pop())
            old.pop()
        else:
            print(old[-1] if old else new[0])
    
  • + 0 comments

    Use perl language:

    It's a one liner code.

    while(<>){(v)=split;v:c==3&&print"$q[0]\n"}