Last active
February 20, 2017 03:31
-
-
Save tylersloeper/a149b19756ea12e6e1e714f164e321ce to your computer and use it in GitHub Desktop.
hackerrank.com Queues A Tale of Two Stacks in; C#
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/** | |
The challenge: | |
https://www.hackerrank.com/challenges/ctci-queue-using-two-stacks | |
**/ | |
using System; | |
using System.Collections.Generic; | |
using System.IO; | |
class Solution { | |
static void Main(String[] args) | |
{ | |
string thing = Console.ReadLine(); | |
//Console.WriteLine(thing); | |
int q; | |
Int32.TryParse(thing, out q); | |
//if q too big exit. | |
if(q<1 || q>100000) | |
{ | |
System.Environment.Exit(0); | |
} | |
int x = 0; | |
int y = 0; | |
List<int> list = new List<int>(); | |
for (int i = 0; i < q; i++) | |
{ | |
thing = Console.ReadLine(); | |
string[] commands = thing.Split(null); | |
int check = commands.Length; | |
if (commands != null) | |
{ | |
if(check == 1) | |
{ | |
if (commands[0] != null) | |
{ | |
Int32.TryParse(commands[0], out x); | |
//Console.WriteLine(x); | |
if (x == 2) | |
{ | |
list.RemoveAt(0); | |
} | |
if (x == 3) | |
{ | |
Console.WriteLine(list[0]); | |
} | |
} | |
} | |
if (check == 2) | |
{ | |
if (commands[0] != null) | |
{ | |
Int32.TryParse(commands[0], out x); | |
//Console.WriteLine(x); | |
} | |
if (commands[1] != null) | |
{ | |
Int32.TryParse(commands[1], out y); | |
//Console.WriteLine(y); | |
if (x == 1 && y< 1000000000 && y >1) | |
{ | |
list.Add(y); | |
} | |
} | |
} | |
//exit if too many arguments | |
if(check != 1 && check != 2) | |
{ | |
System.Environment.Exit(0); | |
} | |
// exit if command unknown | |
if (x != 1 && x != 2 && x!= 3) | |
{ | |
System.Environment.Exit(0); | |
} | |
} | |
//Console.WriteLine(" "); | |
} | |
/* | |
Console.WriteLine("List elements:"); | |
foreach ( var element in list) | |
{ | |
Console.WriteLine(element); | |
} | |
*/ | |
/* Enter your code here. Read input from STDIN. Print output to STDOUT. Your class should be named Solution */ | |
} | |
} |
Author
tylersloeper
commented
Feb 20, 2017
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment