diff options
| author | Mohammad Sajid Anwar <Mohammad.Anwar@yahoo.com> | 2024-03-20 19:16:18 +0000 |
|---|---|---|
| committer | GitHub <noreply@github.com> | 2024-03-20 19:16:18 +0000 |
| commit | 78a1fd4752bcec4d417fa7ae8c205703c9767b2a (patch) | |
| tree | 2af43bea103f0948ec7f113a41f8ab263b921234 /challenge-261/spadacciniweb/python/ch-2.py | |
| parent | 5395f01802fbf21afed5c5e25a44e46361431be1 (diff) | |
| parent | a53fd1be185e212f71d48a8112f20a5a2ed86120 (diff) | |
| download | perlweeklychallenge-club-78a1fd4752bcec4d417fa7ae8c205703c9767b2a.tar.gz perlweeklychallenge-club-78a1fd4752bcec4d417fa7ae8c205703c9767b2a.tar.bz2 perlweeklychallenge-club-78a1fd4752bcec4d417fa7ae8c205703c9767b2a.zip | |
Merge pull request #9778 from spadacciniweb/PWC-261
Add ch-1 and ch-2 in Perl, Elixir, Go, Python, Ruby
Diffstat (limited to 'challenge-261/spadacciniweb/python/ch-2.py')
| -rw-r--r-- | challenge-261/spadacciniweb/python/ch-2.py | 61 |
1 files changed, 61 insertions, 0 deletions
diff --git a/challenge-261/spadacciniweb/python/ch-2.py b/challenge-261/spadacciniweb/python/ch-2.py new file mode 100644 index 0000000000..ff17b0fa2b --- /dev/null +++ b/challenge-261/spadacciniweb/python/ch-2.py @@ -0,0 +1,61 @@ +# Task 2: Multiply by Two +# Submitted by: Mohammad Sajid Anwar +# +# You are given an array of integers, @ints and an integer $start.. +# Write a script to do the followings: +# +# a) Look for $start in the array @ints, if found multiply the number by 2 +# b) If not found stop the process otherwise repeat +# +# In the end return the final value. +# +# Example 1 +# Input: @ints = (5,3,6,1,12) and $start = 3 +# Output: 24 +# +# Step 1: 3 is in the array so 3 x 2 = 6 +# Step 2: 6 is in the array so 6 x 2 = 12 +# Step 3: 12 is in the array so 12 x 2 = 24 +# +# 24 is not found in the array so return 24. +# +# Example 2 +# Input: @ints = (1,2,4,3) and $start = 1 +# Output: 8 +# +# Step 1: 1 is in the array so 1 x 2 = 2 +# Step 2: 2 is in the array so 2 x 2 = 4 +# Step 3: 4 is in the array so 4 x 2 = 8 +# +# 8 is not found in the array so return 8. +# +# Example 3 +# Input: @ints = (5,6,7) and $start = 2 +# Output: 2 +# +# 2 is not found in the array so return 2. + +def multiply_by_two(start, ints): + curr = start + while (curr in ints): + curr *= 2 + + print("start %s (%s) -> %d" % + ( start, + ",".join(map(str, ints)), + curr + ) + ) + +if __name__ == "__main__": + ints = [5,3,6,1,12] + start = 3 + multiply_by_two(start, ints); + + ints = [1,2,4,3] + start = 1 + multiply_by_two(start, ints); + + ints = [5,6,7] + start= 2 + multiply_by_two(start, ints); |
