This file contains 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
string toString(int a){ | |
ostringstream os; | |
os<<a; | |
return os.str(); | |
} |
This file contains 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
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 |
This file contains 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
// convert an integer to string | |
string intToString (int a){ | |
ostringstream os(a); | |
os<<a; | |
return os.str(); | |
} |
This file contains 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
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]) | |
This file contains 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
# 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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
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 |
This file contains 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
# 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 |
This file contains 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
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) |
OlderNewer