Skip to content

Instantly share code, notes, and snippets.

@theBikz
Created September 22, 2018 20:53
Show Gist options
  • Save theBikz/4a316b4ac553c3555f0b99aa8821bd4d to your computer and use it in GitHub Desktop.
Save theBikz/4a316b4ac553c3555f0b99aa8821bd4d to your computer and use it in GitHub Desktop.
"""Write a function intreverse(n) that takes as input a positive integer n and returns the integer obtained by reversing the digits in n.
Here are some examples of how your function should work.
>>> intreverse(783)
387
>>> intreverse(242789)
987242
>>> intreverse(3)
3
Write a function matched(s) that takes as input a string s and checks if the brackets "(" and ")" in s are matched: that is, every "(" has a matching ")" after it and every ")" has a matching "(" before it. Your function should ignore all other symbols that appear in s. Your function should return True if s has matched brackets and False if it does not.
Here are some examples to show how your function should work.
>>> matched("zb%78")
True
>>> matched("(7)(a")
False
>>> matched("a)*(?")
False
>>> matched("((jkl)78(A)&l(8(dd(FJI:),):)?)")
True
Write a function sumprimes(l) that takes as input a list of integers l and retuns the sum of all the prime numbers in l.
Here are some examples to show how your function should work.
>>> sumprimes([3,3,1,13])
19
>>> sumprimes([2,4,6,9,11])
13
>>> sumprimes([-3,1,6])
0"""
def intreverse(n):
r=0
temp=n
while(temp>0):
a=temp%10
r=a+r*10
temp=temp//10
return (r)
def matched(str):
count = 0
for i in str:
if i == "(":
count += 1
elif i == ")":
count -= 1
if count < 0:
return False
return count == 0
def sumprimes(n):
sum=0
fact=[]
for i in range (0,len(n)):
num=n[i]
if num>1:
fact=[]
for j in range (1,num+1):
if num%j==0:
fact=fact+[j]
if fact==[1,num]:
sum=sum+num
return(sum)
import ast
def tolist(inp):
inp = "["+inp+"]"
inp = ast.literal_eval(inp)
return (inp[0],inp[1])
def parse(inp):
inp = ast.literal_eval(inp)
return (inp)
fncall = input()
lparen = fncall.find("(")
rparen = fncall.rfind(")")
fname = fncall[:lparen]
farg = fncall[lparen+1:rparen]
if fname == "intreverse":
arg = parse(farg)
print(intreverse(arg))
elif fname == "matched":
arg = parse(farg)
print(matched(arg))
elif fname == "sumprimes":
arg = parse(farg)
print(sumprimes(arg))
else:
print("Function", fname, "unknown")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment