Skip to content

Commit a5ba6d8

Browse files
authored
Create pivot.py
1 parent 84f528d commit a5ba6d8

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

src/chapter11searching/pivot.py

+26
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# Copyright (c) Oct 22, 2018 CareerMonk Publications and others.
2+
# E-Mail : info@careermonk.com
3+
# Creation Date : 2014-01-10 06:15:46
4+
# Last modification : 2008-10-31
5+
# by : Narasimha Karumanchi
6+
# Book Title : Data Structures And Algorithmic Thinking With Python
7+
# Warranty : This software is provided "as is" without any
8+
# warranty; without even the implied warranty of
9+
# merchantability or fitness for a particular purpose.
10+
11+
def findPivot1(A):
12+
for i in range(len(A)):
13+
leftSum = sum(A[:i])
14+
rightSum = sum(A[i+1:])
15+
if leftSum == rightSum:
16+
return A[i]
17+
18+
def findPivot2(A):
19+
S= sum(A)
20+
leftSum = 0
21+
for i in range(len(A)):
22+
if leftSum == S-A[i]-leftSum:
23+
return A[i]
24+
leftSum += A[i]
25+
26+
print findPivot2([12,6,9,3,5,2,1,9,10])

0 commit comments

Comments
 (0)