## Madhu Mohan

BAN USERPYTHON::

>>> def sum(l):

a = 0

d = 0

b = len(l)

c = b/2

for i in range(0, c):

a = a + l[i]

for j in range(c, ((c*2)+1)):

d = d + l[j]

if a == d:

print "both sides of sum is equal"

return a

elif a != d:

a = 0

d = 0

for i in range(0, c+1):

a = a + l[i]

for j in range(c+1, ((c*2)+1)):

d = d + l[j]

if a == d:

print "both sides of sum is equal at 2nd level"

return a

else:

print "both sides of sum is not equal"

return False

>>>

>>> sum([1,1,-11,1,12])

both sides of sum is equal

2

>>> sum([-11,1,12,1,1])

both sides of sum is equal at 2nd level

2

>>> sum([-11,1,12,1,0])

both sides of sum is not equal

False

>>>

Python:

def min_value(list):

pl= []

for j in list:

a = 0

if j>2:

for i in range(2, j):

if j%i == 0:

a = 1

if a == 0:

pl.append(j)

print pl

return sorted(pl)[1]-sorted(pl)[0]

>>> min_value([2,3,37,5,27,7,64,1,2])

[2, 3, 37, 5, 7, 1, 2]

1

PYTHON::

>>> def example(val, list):

a = []

b = []

for i in list:

if i < val:

a.append(i)

elif i > val:

b.append(i)

a.append(val)

a.extend(b)

return a

>>> example(2, [4,5,6,2,3,1,0])

[1, 0, 2, 4, 5, 6, 3]

>>> example(3, [4,5,6,2,3,1,0])

[2, 1, 0, 3, 4, 5, 6]

Python::

Descending order::

>>> def man(l1, l2):

c = []

for i in l2:

c.append(i)

for j in l1:

c.append(j)

c.sort(reverse=True)

return c

>>> man([2,3,4,2,3], [4,5,6,5,6])

[6, 6, 5, 5, 4, 4, 3, 3, 2, 2]

Ascending Order::

>>> def man(l1, l2):

c = []

for i in l2:

c.append(i)

for j in l1:

c.append(j)

c.sort()

return c

>>> man([2,3,4,2,3], [4,5,6,5,6])

[2, 2, 3, 3, 4, 4, 5, 5, 6, 6]

>>>

PYTHON::

>>> def str(string1, string2):

intersect = set(string1) & set(string2)

return ''.join(intersect)

>>> str('string', 'strong')

'srtgn'

OR

>>> def str(s1, s2):

a = ''

if len(s1) < len(s2):

for i in s2:

if i in s1:

a = '%s%s'%(a, i)

elif len(s1) > len(s2) or len(s1) == len(s2):

for i in s1:

if i in s2:

a = '%s%s'%(a, i)

return a

>>> str('string', 'strong')

'strng'

**CareerCup**is the world's biggest and best source for software engineering interview preparation. See all our resources.

Open Chat in New Window

PYTHON::

- Madhu Mohan May 18, 2017>>> def max_occ_char(str):

s = str.replace(' ', '')

a = {}

c = ''

for i in s:

if i not in a:

a[i] = 1

else:

a[i] += 1

highest = max(a.values())

for k, v in a.items():

if v == highest:

c = '%s%s'%(c, k)

for j in s:

if j in c:

return j

>>> max_occ_char("LHELOHH WORLD!")

'L'

>>> max_occ_char("HELLOHH WORLD!")

'H'

>>> max_occ_char("HELLO WORLD!")

'L'

>>>