aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-05-14 17:34:58 +0100
committerGitHub <noreply@github.com>2024-05-14 17:34:58 +0100
commit64b8c830c57e76dc96d88d66ead474e829ccb220 (patch)
tree1d59f43022551ba1bfe4c2c948074e3b1438f2e4
parentb7b89632947bf2c7f64b981a96a7e23b36d7f9bd (diff)
parente198f781b3927a5f404c8726301f46d8210ef24f (diff)
downloadperlweeklychallenge-club-64b8c830c57e76dc96d88d66ead474e829ccb220.tar.gz
perlweeklychallenge-club-64b8c830c57e76dc96d88d66ead474e829ccb220.tar.bz2
perlweeklychallenge-club-64b8c830c57e76dc96d88d66ead474e829ccb220.zip
Merge pull request #10098 from oWnOIzRi/week269
add solutions week 269 in python
-rw-r--r--challenge-269/steven-wilson/python/ch-1.py22
-rw-r--r--challenge-269/steven-wilson/python/ch-2.py42
2 files changed, 64 insertions, 0 deletions
diff --git a/challenge-269/steven-wilson/python/ch-1.py b/challenge-269/steven-wilson/python/ch-1.py
new file mode 100644
index 0000000000..e6e198f657
--- /dev/null
+++ b/challenge-269/steven-wilson/python/ch-1.py
@@ -0,0 +1,22 @@
+#!/usr/bin/env python3
+
+
+def bitwise_or_trailing_zeros(*integers):
+ ''' Given an array of positive integers, find out if it is possible to
+ select two or more elements of the given array such that the bitwise OR of
+ the selected elements has at least one trailing zero in its binary
+ representation.
+ >>> bitwise_or_trailing_zeros(1, 2, 3, 4, 5)
+ True
+ >>> bitwise_or_trailing_zeros(2, 3, 8, 16)
+ True
+ >>> bitwise_or_trailing_zeros(1, 2, 5, 7, 9)
+ False
+ '''
+ return sum(1 for i in integers if i % 2 == 0) >= 2
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)
diff --git a/challenge-269/steven-wilson/python/ch-2.py b/challenge-269/steven-wilson/python/ch-2.py
new file mode 100644
index 0000000000..6ce49f8fe8
--- /dev/null
+++ b/challenge-269/steven-wilson/python/ch-2.py
@@ -0,0 +1,42 @@
+#!/usr/bin/env python3
+
+
+def distribute_elements(integers):
+ ''' Given an array of distinct integers, distribute the elements as
+ described below:
+
+ 1) Put the 1st element of the given array to a new array arr1.
+ 2) Put the 2nd element of the given array to a new array arr2.
+
+ Once you have one element in each arrays, arr1 and arr2, then follow the
+ rule below:
+
+ If the last element of the array arr1 is greater than the last
+ element of the array arr2 then add the first element of the
+ given array to arr1 otherwise to the array arr2.
+
+ When done distribution, return the concatenated arrays. arr1 and arr2.
+
+ >>> distribute_elements([2, 1, 3, 4, 5])
+ (2, 3, 4, 5, 1)
+ >>> distribute_elements([3, 2, 4])
+ (3, 4, 2)
+ >>> distribute_elements([5, 4, 3 ,8])
+ (5, 3, 4, 8)
+ '''
+ first = [integers[0]]
+ second = [integers[1]]
+
+ for i in integers[2:]:
+ if first[-1] > second[-1]:
+ first.append(i)
+ else:
+ second.append(i)
+
+ return tuple(first + second)
+
+
+if __name__ == "__main__":
+ import doctest
+
+ doctest.testmod(verbose=True)