diff options
| author | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-08-17 19:49:33 +0100 |
|---|---|---|
| committer | Mohammad S Anwar <mohammad.anwar@yahoo.com> | 2022-08-17 19:49:33 +0100 |
| commit | 6091c868f91a9200f810f18b96a33922c5224c60 (patch) | |
| tree | ff8934e2ffe1eee5a12658786d3593efed37f860 /challenge-178/eric-cheung/python | |
| parent | 76da9214e2c097009de0d631a2759269cb6a9c39 (diff) | |
| download | perlweeklychallenge-club-6091c868f91a9200f810f18b96a33922c5224c60.tar.gz perlweeklychallenge-club-6091c868f91a9200f810f18b96a33922c5224c60.tar.bz2 perlweeklychallenge-club-6091c868f91a9200f810f18b96a33922c5224c60.zip | |
- Added guest contributions by Eric Cheung.
Diffstat (limited to 'challenge-178/eric-cheung/python')
| -rwxr-xr-x | challenge-178/eric-cheung/python/ch-1.py | 152 | ||||
| -rwxr-xr-x | challenge-178/eric-cheung/python/ch-2.py | 43 |
2 files changed, 195 insertions, 0 deletions
diff --git a/challenge-178/eric-cheung/python/ch-1.py b/challenge-178/eric-cheung/python/ch-1.py new file mode 100755 index 0000000000..d13cf6ebb8 --- /dev/null +++ b/challenge-178/eric-cheung/python/ch-1.py @@ -0,0 +1,152 @@ +
+## Remarks
+## https://rosettacode.org/wiki/Imaginary_base_numbers#Python
+
+import math
+import re
+
+def inv(c):
+ denom = c.real * c.real + c.imag * c.imag
+ return complex(c.real / denom, -c.imag / denom)
+
+class QuaterImaginary:
+
+ twoI = complex(0, 2)
+ invTwoI = inv(twoI)
+
+ def __init__(self, str):
+
+ if not re.match("^[0123.]+$", str) or str.count(".") > 1:
+ raise Exception('Invalid base 2i number')
+
+ self.b2i = str
+
+ def toComplex(self):
+
+ pointPos = self.b2i.find(".")
+ posLen = len(self.b2i) if (pointPos < 0) else pointPos
+
+ sum = complex(0, 0)
+ prod = complex(1, 0)
+
+ for j in range(0, posLen):
+ k = int(self.b2i[posLen - 1 - j])
+ if k > 0:
+ sum = sum + prod * k
+
+ prod = prod * QuaterImaginary.twoI
+
+ if pointPos != -1:
+ prod = QuaterImaginary.invTwoI
+ for j in range(posLen + 1, len(self.b2i)):
+ k = int(self.b2i[j])
+ if k > 0:
+ sum = sum + prod * k
+
+ prod = prod * QuaterImaginary.invTwoI
+
+ return sum
+
+ def __str__(self):
+ return str(self.b2i)
+
+def toQuaterImaginary(c):
+ if c.real == 0.0 and c.imag == 0.0:
+ return QuaterImaginary("0")
+
+ re = int(c.real)
+ im = int(c.imag)
+
+ fi = -1
+ ss = ""
+
+ while re != 0:
+ re, rem = divmod(re, -4)
+
+ if rem < 0:
+ rem = rem + 4
+ re = re + 1
+
+ ss = ss + str(rem) + "0"
+
+ if im != 0:
+
+ f = c.imag / 2
+ im = int(math.ceil(f))
+
+ f = -4 * (f - im)
+ index = 1
+
+ while im != 0:
+ im, rem = divmod(im, -4)
+
+ if rem < 0:
+ rem += 4
+ im += 1
+
+ if index < len(ss):
+ ss[index] = str(rem)
+ else:
+ ss = ss + "0" + str(rem)
+
+ index = index + 2
+
+ fi = int(f)
+
+ ss = ss[::-1]
+
+ if fi != -1:
+ ss = ss + "." + str(fi)
+
+ ss = ss.lstrip("0")
+
+ if ss[0] == ".":
+ ss = "0" + ss
+
+ return QuaterImaginary(ss)
+
+def formatComplex(cInput):
+
+ strC = str(cInput)
+ strC = strC.replace("+", " + ")
+ strC = strC.replace("-", " - ")
+
+ return strC
+
+
+for i in range(1, 17):
+
+ c1 = complex(i, 0)
+ q1 = toQuaterImaginary(c1)
+ c2 = q1.toComplex()
+
+ print (formatComplex(c1) + " <--> " + formatComplex(q1) + " <--> " + formatComplex(c2))
+
+ c1 = -c1
+ q1 = toQuaterImaginary(c1)
+ c2 = q1.toComplex()
+
+ print (formatComplex(c1) + " <--> " + formatComplex(q1) + " <--> " + formatComplex(c2))
+
+ print ()
+
+
+print ()
+
+
+for i in range(1, 17):
+
+ c1 = complex(0, i)
+ q1 = toQuaterImaginary(c1)
+ c2 = q1.toComplex()
+
+ print (formatComplex(c1) + " <--> " + formatComplex(q1) + " <--> " + formatComplex(c2))
+
+ c1 = -c1
+ q1 = toQuaterImaginary(c1)
+ c2 = q1.toComplex()
+
+ print (formatComplex(c1) + " <--> " + formatComplex(q1) + " <--> " + formatComplex(c2))
+
+ print ()
+
diff --git a/challenge-178/eric-cheung/python/ch-2.py b/challenge-178/eric-cheung/python/ch-2.py new file mode 100755 index 0000000000..ee542becd0 --- /dev/null +++ b/challenge-178/eric-cheung/python/ch-2.py @@ -0,0 +1,43 @@ +
+###
+from datetime import timedelta, date
+
+class BusDate():
+
+ def __init__(self, nYear, nMonth, nDay, nHour, nMin):
+ self.BusDate = date(nYear, nMonth, nDay)
+ self.BusHour = nHour
+ self.BusMin = nMin
+
+ def AddHour(self, dHourAdd):
+
+ nHourAdd = int(dHourAdd)
+ nMinAdd = int((dHourAdd - nHourAdd) * 60)
+
+ self.BusHour = self.BusHour + nHourAdd
+ self.BusMin = self.BusMin + nMinAdd
+
+ if self.BusMin >= 60:
+ self.BusMin = self.BusMin - 60
+ self.BusHour = self.BusHour + 1
+
+ while (self.BusHour > 18 or self.BusHour == 18 and self.BusMin > 0):
+ self.BusHour = self.BusHour - 9
+ self.BusDate = self.BusDate + timedelta(days = 1)
+ if (self.BusDate.weekday() == 5):
+ self.BusDate = self.BusDate + timedelta(days = 2)
+ elif (self.BusDate.weekday() == 6):
+ self.BusDate = self.BusDate + timedelta(days = 1)
+
+ return self.BusDate.strftime("%Y-%m-%d") + " " + ("0" + str(self.BusHour) if self.BusHour < 10 else str(self.BusHour)) + ":" + ("0" + str(self.BusMin) if self.BusMin < 10 else str(self.BusMin))
+
+
+### Example 1
+## objInputDate = BusDate(2022, 8, 1, 10, 30)
+## print (objInputDate.AddHour(4))
+
+
+### Example 2
+objInputDate = BusDate(2022, 8, 1, 17, 0)
+print (objInputDate.AddHour(3.5))
+
|
