Skip to content

Instantly share code, notes, and snippets.

View shreezan123's full-sized avatar

Shrijan Aryal shreezan123

View GitHub Profile
@shreezan123
shreezan123 / arraypairsum
Created November 9, 2016 18:21
Find pairs in an integer array whose sum is equal to input parameter (bonus: do it in linear time)
def sumnums(arr,sum):
anslist = []
dict = {}
for each in arr:
if each not in dict:
dict[each] = 1
else:
dict[each] += 1
print(dict)
for each in dict:
#include <iostream>
#include <string>
#include <iomanip>
#include <fstream>
#include <vector>
using namespace std;
template<typename ItemType>
void Swap(ItemType& V1, ItemType& V2 )
{
@shreezan123
shreezan123 / StackOperations.java
Created February 22, 2017 14:00
Stack implementation in Java
public class StackOperations{
int mystack[];
int global_size;
int counter = 0;
int newcounter = 0;
int extensionstack[];
public StackOperations(int size){
this.mystack = new int[size]; //create a stack of size as mentioned while creating object
package Driver;
import howard.edu.ood.hw2.collections.ArrayQueue;
public class Main_Queue {
public static void main(String args[]){
ArrayQueue myqueue = new ArrayQueue(10);
//to enqueue elements onto the stack
for (int i = 0; i<20;i++){
@shreezan123
shreezan123 / indexquestion.py
Created February 26, 2017 21:17
A confusion related to index of for loop.
'''The code is meant to skip adding the character "a" and any other character that comes after "a". For this, I check if string's [i th] index = a, and add 1 to index, so that new index will be able to skip the character coming after "a". But this does not seem to be happening. Why is it so ?'''
str1 = "abcde"
str2 = ""
for i in range(len(str1)):
if str1[i] == "a":
i +=1
continue
else:
str2 += str1[i]
@shreezan123
shreezan123 / indexquestion.py
Created February 26, 2017 21:17
A confusion related to index of for loop.
'''The code is meant to skip adding the character "a" and any other character that comes after "a". For this, I check if string's [i th] index = a, and add 1 to index, so that new index will be able to skip the character coming after "a". But this does not seem to be happening. Why is it so ?'''
str1 = "abcde"
str2 = ""
for i in range(len(str1)):
if str1[i] == "a":
i +=1
continue
else:
str2 += str1[i]
@shreezan123
shreezan123 / SortByFrequency.py
Created February 27, 2017 07:13
Sorting items in the list by the number of occurences
def SortByFrequency(list1):
d1 = {}
list2 = []
for items in list1:
d1[items] = list1.count(items)
for thekeys in d1:
for i in range(0,d1[thekeys]):
list2.append(thekeys)
return list2
@shreezan123
shreezan123 / BSTimplementation.java
Created June 18, 2017 08:42
BST implementation. To keep all the classes under a same class file, I have commented out the main function
package lab5;
import java.util.ArrayList;
public class BinarySearchTree {
public class Node {
public int data;
public Node left;
public Node right;
/*
* Constructor if in which a value is passed, a Node
@shreezan123
shreezan123 / DataCenter.java
Created June 18, 2017 08:50
Take input from txt file and perform respective job in the tree
package lab5;
import java.util.Scanner;
public class DataCenterOrders {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int numOperations = in.nextInt();
// create object of BinarySearchTree class
BinarySearchTree tree1 = new BinarySearchTree();
@shreezan123
shreezan123 / TwoStacksQueue.py
Created March 6, 2018 00:22
Implement a queue using two stacks
class QueueUsingStacks:
def __init__(self):
self.stack1 = []
self.stack2 = []
def enqueue(self,value):
self.stack1.append(value)
#if empty, pop from stack1 and put each value in stack2.
def dequeue(self):