CoolFace
Apppublic

sanket3280/code-execution

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
stack.js66 linesDownload Raw Back to wrappers
1module.exports = function stackWrapper({ sourceCode, language }) {2  let prefix = '';3  switch (language) {4    case 'JAVASCRIPT_NODE':5      prefix = `6// Stack helper (JS)7class __Stack{constructor(){this.a=[]}push(x){this.a.push(x)}pop(){return this.a.pop()}peek(){return this.a[this.a.length-1]}isEmpty(){return this.a.length===0}size(){return this.a.length}}8`;9      break;10    case 'PYTHON3':11    case 'PYTHON3_8':12      prefix = `13# Stack helper (Python)14class __Stack:15    def __init__(self):16        self.a = []17    def push(self, x):18        self.a.append(x)19    def pop(self):20        return self.a.pop() if self.a else None21    def peek(self):22        return self.a[-1] if self.a else None23    def is_empty(self):24        return len(self.a) == 025    def size(self):26        return len(self.a)27`;28      break;29    case 'JAVA':30      prefix = `31// Stack helper (Java)32class __Stack {33  java.util.ArrayDeque<Integer> a = new java.util.ArrayDeque<>();34  void push(int x){a.push(x);} int pop(){return a.pop();}35  int peek(){return a.peek();} boolean isEmpty(){return a.isEmpty();}36  int size(){return a.size();}37}38`;39      break;40    case 'CPP14':41    case 'CPP':42      prefix = `43// Stack helper (C++)44struct __Stack { std::vector<int> a; void push(int x){a.push_back(x);} int pop(){int v=a.back(); a.pop_back(); return v;} int peek(){return a.back();} bool isEmpty(){return a.empty();} int size(){return (int)a.size();} };45`;46      break;47    case 'C':48      prefix = `49/* Stack helper (C) - simple int stack */50typedef struct { int a[10005]; int n; } __Stack; void __stack_init(__Stack* s){s->n=0;} void __stack_push(__Stack* s,int x){s->a[s->n++]=x;} int __stack_pop(__Stack* s){return s->a[--s->n];} int __stack_peek(__Stack* s){return s->a[s->n-1];} int __stack_empty(__Stack* s){return s->n==0;}51`;52      break;53    case 'CSHARP':54      prefix = `55// Stack helper (C#)56class __Stack { System.Collections.Generic.Stack<int> a = new System.Collections.Generic.Stack<int>(); public void Push(int x)=>a.Push(x); public int Pop()=>a.Pop(); public int Peek()=>a.Peek(); public bool IsEmpty()=>a.Count==0; public int Size()=>a.Count; }57`;58      break;59    default:60      return { sourceCode, usedRunner: false };61  }62  return { sourceCode: prefix + sourceCode, usedRunner: false };63};64 65 66