forked from gleventhal/SysAdmin
-
Notifications
You must be signed in to change notification settings - Fork 0
/
interview_questions.py
58 lines (53 loc) · 1.4 KB
/
interview_questions.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#!/usr/bin/python
def findsum(a, n):
''' Find whether a consecutive array slice sums up to n '''
for i in range(len(a)):
j = i + 1
while j <= len(a):
if sum(a[i:j]) == n:
return True
j += 1
return False
def nonzero(a):
''' given an array of ints, push all zeros to the end '''
zeros = [0 for i in range(a.count(0))]
x = [ i for i in a if i != 0]
x.extend(zeros)
return(x)
def egg_drop(n):
''' Find the highest floor in a building that an egg can drop from without breaking '''
t = [1,11]
while t[0] < 100:
if t[1] >= n:
print 'break_an_egg on floor %d' % t[1]
for i in range(t[0],t[1]):
if i >= n:
print 'break_an_egg on floor %d' % i
return i - 1
else:
t[0] += 10
t[1] += 10
return(100)
def sum_to_zero(a):
''' given an array of ints, find 3 elements that sum to 0 '''
for i in a:
for j in a[1:]:
for k in a[2:]:
if i + j + k == 0:
return (i,j,k)
def unique_char(s):
''' Find first non-repeated char in a string '''
myDict = {}
first = len(s)
for c in s:
if c in myDict.keys():
myDict[c] += 1
else:
myDict[c] = 1
if 1 in myDict.values():
for k,v in myDict.items():
if v == 1:
if s.index(k) < first:
first = s.index(k)
return s[first]
return(False)