On the sequence of logical values (True or False) we can build up an OR-Pascal-triangle structure. Instead of summing the values, as in a standard Pascal-triangle, we will combine them using the OR function. That means that the lowest row is simply the input sequence, and every entry in each subsequent row is the OR of the two elements below it. For example, the OR-Pascal-triangle built on the array [True, False, False, True, False] is as follows:
Your job is to count the number of nodes in the OR-Pascal-triangle that contain the value True (this number is 11 for the animation above).
Write a function:
def solution(P)
that, given an array P of N Booleans, returns the number of fields in the OR-Pascal-triangle built on P that contain the value True. If the result is greater than 1,000,000,000, your function should return 1,000,000,000.
Given P = [True, False, False, True, False], the function should return 11, as explained above.
Given P = [True, False, False, True], the function should return 7, as can be seen in the animation below.
Write an efficient algorithm for the following assumptions:
- N is an integer within the range [1..100,000].
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P):
# write your code in Python 3.6
N=len(P)
nFalse=0 #nuber of current consecutive nodes with value False in the base of the triangle
sFalse=0 #sum of the nodes with value False so far
sNodes=0 #sum of all nodes in a sub-triangle so far
for i in range(N):
sNodes=sNodes+i+1
if P[i]==True:
nFalse=0
else:
nFalse=nFalse+1
sFalse=sFalse+nFalse
sTrue=sNodes-sFalse
if sTrue> 1000000000:
return 1000000000
return sTrue
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P):
# write your code in Python 3.6
N=len(P)
nFalse=0 #nuber of current consecutive nodes with value False in the base of the triangle
sFalse=0 #sum of the nodes with value False so far
sNodes=0 #sum of all nodes in a sub-triangle so far
for i in range(N):
sNodes=sNodes+i+1
if P[i]==True:
nFalse=0
else:
nFalse=nFalse+1
sFalse=sFalse+nFalse
sTrue=sNodes-sFalse
if sTrue> 1000000000:
return 1000000000
return sTrue
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P):
# write your code in Python 3.6
N=len(P)
nFalse=0 #nuber of current consecutive nodes with value False in the base of the triangle
sFalse=0 #sum of the nodes with value False so far
sNodes=0 #sum of all nodes in a sub-triangle so far
for i in range(N):
sNodes=sNodes+i+1
if P[i]==True:
nFalse=0
else:
nFalse=nFalse+1
sFalse=sFalse+nFalse
sTrue=sNodes-sFalse
if sTrue> 1000000000:
return 1000000000
return sTrue
# you can write to stdout for debugging purposes, e.g.
# print("this is a debug message")
def solution(P):
# write your code in Python 3.6
N=len(P)
nFalse=0 #nuber of current consecutive nodes with value False in the base of the triangle
sFalse=0 #sum of the nodes with value False so far
sNodes=0 #sum of all nodes in a sub-triangle so far
for i in range(N):
sNodes=sNodes+i+1
if P[i]==True:
nFalse=0
else:
nFalse=nFalse+1
sFalse=sFalse+nFalse
sTrue=sNodes-sFalse
if sTrue> 1000000000:
return 1000000000
return sTrue
The solution obtained perfect score.