Skip to content

Instantly share code, notes, and snippets.

View mukulrawat1986's full-sized avatar
💭
It's compilcated

Mukul Rawat mukulrawat1986

💭
It's compilcated
View GitHub Profile
@mukulrawat1986
mukulrawat1986 / intToString.cpp
Created May 8, 2014 12:24
C++ function to convert string to int and int to string
string toString(int a){
ostringstream os;
os<<a;
return os.str();
}
@mukulrawat1986
mukulrawat1986 / stripText.py
Created May 8, 2014 17:41
Stripping text of words and punctutations
text = raw_input()
# split text into words based on white space and strip punctuation
texts = [word.strip(string.punctuation) for word in text.split()]
# remove words with length 0
texts = [word for word in texts if len(word)!=0]
# to remove punctuation from words which are only separated by punctuation
@mukulrawat1986
mukulrawat1986 / intToString.cpp
Created May 8, 2014 19:36
Snippets for competitive programming for manipulating inputs
// convert an integer to string
string intToString (int a){
ostringstream os(a);
os<<a;
return os.str();
}
@mukulrawat1986
mukulrawat1986 / LIS.py
Created May 9, 2014 21:03
Pocket Gems Engineering challenge
import random
def lis(a):
# a is list of tuples of form (sat, gpa)
# to find LIS such that sat1<sat2<sat3....
# and gpa1>gpa2>gpa>3.....
# sort the list in decreasing order of gpa
a = sorted(a, reverse = True, key = lambda k: k[-1])
# SRM 620 candidate selection easy
class CandidatesSelectionEasy(object):
def sort(self, score, x):
score1 = []
for pos,val in enumerate(score):
score1.append((val,pos))
score1 = sorted(score1, key = lambda k: k[0][x])
#print score1
@mukulrawat1986
mukulrawat1986 / cracklepop.py
Created September 15, 2014 20:05
Solution to CracklePop problem
li = ["CracklePop" if i%5==0 and i%3==0 else "Crackle" if i%3==0 else "Pop" if i%5==0 else i for i in xrange(1,101)]
for i in li:
print i
@mukulrawat1986
mukulrawat1986 / Description
Created September 15, 2014 20:12
Palantir Technologies hiring puzzle
Variable-Base Expression Evaluation:
You've woken up one day to find that everyone suddenly expresses numbers in different number bases. You're seeing prices in octal and phone numbers in hexadecimal. It's a numerical Tower of Babel! For your own sanity, you decide to program a simple mathematical expression evaluator that can handle numbers in different number bases. It should support addition, subtraction, and multiplication, should respect order of operations, and should handle number bases between 2 and 16.
While your language of choice may directly support expression evaluation, please create your own.
The input on stdin is a mathematical expression on a single line. Number constants are expressed like "123_4", which is "123" in base 4, which is 27 in base 10. Some digits in the higher bases will be represented with uppercase letters. Numbers within the expression will always be non-negative integers. The operators will be +, -, and *. Whitespace should be ignored.
Your program should emit to stdout
@mukulrawat1986
mukulrawat1986 / brainfry.py
Created September 15, 2014 20:15
A brainfuck interpreter in python
import sys
class Brainfry(object):
def __init__(self):
# we simulate the array with a list
# the array has a capacity of 30,000 byte cells
self.array = [0 for i in xrange(30000)]
# the data pointer, initially at position 0
self.dataptr = 0
@mukulrawat1986
mukulrawat1986 / peuler1.py
Created January 14, 2015 13:32
Project Euler Problem 1 in python
# Enter your code here. Read input from STDIN. Print output to STDOUT
def func(n):
return sum([i for i in xrange(n) if i%3==0 or i%5==0])
t = int(raw_input())
while t>0:
n = int(raw_input())
print func(n)
t-=1
@mukulrawat1986
mukulrawat1986 / peuler1_refactor.py
Created January 14, 2015 14:33
The re-factored fast solution to the project euler problem
def func(n):
n1 = ((n/3) - 1) if n%3 == 0 else (n/3)
n2 = ((n/5) - 1) if n%5 == 0 else (n/5)
n3 = ((n/15)- 1) if n%15 == 0 else (n/15)
s1 = (n1*(n1+1))/2
s2 = (n2*(n2+1))/2
s3 = (n3*(n3+1))/2
return (3*s1) + (5*s2) - (15*s3)