anish531213
BAN USER
Comments (10)
Reputation 0
Page:
1
Comment hidden because of low score. Click to expand.
Comment hidden because of low score. Click to expand.
0
of 0 vote
Simple O(n) solution in Python.
def first_non_repeated(lst):
d = {}
for i in lst:
d[i] = d.get(i, 0) + 1
for i in lst:
if d[i] == 1:
return i

anish531213
December 23, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
def differs(A, k):
l = []
d = {}
for i in range(len(A)):
d[A[i]] = i
for j in range(len(A)):
temp = k + A[j]
if temp in d:
l.append((A[j], temp))
del d[A[j]]
return l
z = differs([1, 2, 3, 5, 6, 8, 9, 11, 12, 13], 3)
print(z)

anish531213
November 10, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
def occourance(l, n):
for i in range(0, len(l)):
if l[i] == n:
last= i
for i in range(0, len(l)):
if l[i] == n:
first= i
break
return ("the first occurance is at %ith 'index' and the last occurance is at %ith 'index'" %(first, last))
list1 = [5, 2, 3, 4, 5, 5, 7, 8, 5, 9]
z = occourance(list1, 5)
print(z)

anish531213
November 04, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
def sum_target(A, target):
for i in range(len(A)):
k = 0
for j in range(i, len(A)):
k += A[j]
if k == target:
return A[i:j+1]
return("no consecutive sum found")
z = sum_target([1, 2, 3, 5, 7], 6)
print(z)

anish531213
November 03, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
def print_missing(l):
sumn = (1 + l[len(l)1]) * l[len(l)1] * (0.5)
x = 0
for i in l:
x += i
return (sumn  x)
z = print_missing([0, 1, 2, 3, 4, 5, 7])
print(int(z))

anish531213
November 02, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
python
O(n)
def sum_0(l):
new = []
dn = dict()
dp = dict()
for i in l:
if i >= 0:
z = dp.get(i, 0)
z += 1
dp[i] = z
else:
z = dn.get(i, 0)
z += 1
dn[i] = z
for j in dp:
if (j) in dn:
new.append((j, j))
return new
z = sum_0([1, 1, 3, 2])
print(z)

anish531213
November 02, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
Python
O(n)
def move0(l):
new = []
for i in l:
if i != 0:
new.append(i)
for i in l:
if i == 0:
new.append(i)
return new
z = move0([1, 2, 4, 0, 0, 12])
print(z)

anish531213
November 02, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
simple python code
O(n2)
def move_0(l):
for i in range(len(l)1):
for j in range(0, len(l)1i):
if l[j] == 0:
l[j+1] , l[j] = l[j], l[j+1]
mylist = [1, 2, 4, 0, 0, 12]
move_0(mylist)
print(mylist)

anish531213
November 02, 2015 Comment hidden because of low score. Click to expand.
0
of 0 vote
max runtime is O(n)
 anish531213 November 01, 2015Page:
1
CareerCup is the world's biggest and best source for software engineering interview preparation. See all our resources.
Open Chat in New Window
Open Chat in New Window
Very elegant O(n) solution in python
 anish531213 December 23, 2015