aboutsummaryrefslogtreecommitdiff
path: root/challenge-120/paulo-custodio/python
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2021-07-05 22:57:40 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2021-07-05 22:57:40 +0100
commit95db0bb6b8cd9762c26d8392cda04b3553b38bb4 (patch)
treebc5af7a96d8d474e7259d1f235ee64fb9516dbb3 /challenge-120/paulo-custodio/python
parent39253b822e4bfe790553c560fd7c377f92f6246c (diff)
downloadperlweeklychallenge-club-95db0bb6b8cd9762c26d8392cda04b3553b38bb4.tar.gz
perlweeklychallenge-club-95db0bb6b8cd9762c26d8392cda04b3553b38bb4.tar.bz2
perlweeklychallenge-club-95db0bb6b8cd9762c26d8392cda04b3553b38bb4.zip
Add solutions to challenge 120
Diffstat (limited to 'challenge-120/paulo-custodio/python')
-rw-r--r--challenge-120/paulo-custodio/python/ch-1.py43
-rw-r--r--challenge-120/paulo-custodio/python/ch-2.py40
2 files changed, 83 insertions, 0 deletions
diff --git a/challenge-120/paulo-custodio/python/ch-1.py b/challenge-120/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..74603af153
--- /dev/null
+++ b/challenge-120/paulo-custodio/python/ch-1.py
@@ -0,0 +1,43 @@
+#!/usr/bin/env python
+
+# Challenge 120
+#
+# TASK #1 - Swap Odd/Even bits
+# Submitted by: Mohammad S Anwar
+# You are given a positive integer $N less than or equal to 255.
+#
+# Write a script to swap the odd positioned bit with even positioned bit and
+# print the decimal equivalent of the new binary representation.
+#
+# Example
+# Input: $N = 101
+# Output: 154
+#
+# Binary representation of the given number is 01 10 01 01.
+# The new binary representation after the odd/even swap is 10 01 10 10.
+# The decimal equivalent of 10011010 is 154.
+#
+# Input: $N = 18
+# Output: 33
+#
+# Binary representation of the given number is 00 01 00 10.
+# The new binary representation after the odd/even swap is 00 10 00 01.
+# The decimal equivalent of 100001 is 33.
+
+import sys
+
+def swap_bits(n):
+ out = 0
+ shift = 0
+ while n > 0:
+ if (n & 1) != 0:
+ out |= 2 << shift
+ if (n & 2) != 0:
+ out |= 1 << shift
+ n >>= 2
+ shift += 2
+ return out
+
+n = int(sys.argv[1])
+n = swap_bits(n)
+print(n)
diff --git a/challenge-120/paulo-custodio/python/ch-2.py b/challenge-120/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..9cf0c89da7
--- /dev/null
+++ b/challenge-120/paulo-custodio/python/ch-2.py
@@ -0,0 +1,40 @@
+#!/usr/bin/env python
+
+# Challenge 120
+#
+# TASK #2 - Clock Angle
+# Submitted by: Mohammad S Anwar
+# You are given time $T in the format hh:mm.
+#
+# Write a script to find the smaller angle formed by the hands of an analog
+# clock at a given time.
+#
+# HINT: A analog clock is divided up into 12 sectors. One sector represents 30
+# degree (360/12 = 30).
+#
+# Example
+# Input: $T = '03:10'
+# Output: 35 degree
+#
+# The distance between the 2 and the 3 on the clock is 30 degree.
+# For the 10 minutes i.e. 1/6 of an hour that have passed.
+# The hour hand has also moved 1/6 of the distance between the 3 and the 4,
+# which adds 5 degree (1/6 of 30).
+# The total measure of the angle is 35 degree.
+#
+# Input: $T = '04:00'
+# Output: 120 degree
+
+import sys
+
+def clock_angles(hh, mm):
+ mm_angle = mm * 360 // 60
+ hh_angle = (hh % 12) * 360 // 12 + mm_angle // 12
+ return hh_angle, mm_angle
+
+hh, mm = [int(x) for x in sys.argv[1].split(':')]
+hh_angle, mm_angle = clock_angles(hh, mm)
+angle = abs(hh_angle - mm_angle)
+if angle > 180:
+ angle = 360 - angle
+print(angle)