Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 2001f71

Browse files
Create signOfProduct.py
1 parent c2464ec commit 2001f71

File tree

1 file changed

+28
-0
lines changed

1 file changed

+28
-0
lines changed

‎signOfProduct.py

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
# Problem: Given an array arr[] of n integers,
2+
# the integers can be positive, negative or 0
3+
# return the sign of the product of the elements
4+
# 1 : positive
5+
# -1 : negative
6+
# 0 : zero
7+
8+
9+
def getSignOfProduct(array):
10+
11+
sign = 1
12+
13+
for num in array:
14+
15+
if num == 0:
16+
return 0
17+
18+
if num < 0:
19+
sign = -1 * sign
20+
21+
return sign
22+
23+
24+
arr = [10, 45, -9, 3, -4, -5, 7, 32 , 0, 12 , 45, -1]
25+
res = getSignOfProduct(arr)
26+
print(arr, res)
27+
28+
# Result: [10, 45, -9, 3, -4, -5, 7, 32, 0, 12, 45, -1] 0

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /