diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-02 15:18:01 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-11-02 15:18:01 +0000 |
| commit | 5c5a6f7f53bbc0a190e692a312c50190c97985dd (patch) | |
| tree | 902813e564491630ce187e4921905fa052d2f2d4 | |
| parent | 6997cbd3aff33c517ec03a8a074f9702845dd567 (diff) | |
| download | perlweeklychallenge-club-5c5a6f7f53bbc0a190e692a312c50190c97985dd.tar.gz perlweeklychallenge-club-5c5a6f7f53bbc0a190e692a312c50190c97985dd.tar.bz2 perlweeklychallenge-club-5c5a6f7f53bbc0a190e692a312c50190c97985dd.zip | |
Add Python solution to challenge 137
| -rw-r--r-- | challenge-137/paulo-custodio/python/ch-1.py | 43 | ||||
| -rw-r--r-- | challenge-137/paulo-custodio/python/ch-2.py | 62 |
2 files changed, 105 insertions, 0 deletions
diff --git a/challenge-137/paulo-custodio/python/ch-1.py b/challenge-137/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..8decf87d5d --- /dev/null +++ b/challenge-137/paulo-custodio/python/ch-1.py @@ -0,0 +1,43 @@ +#!/usr/bin/env python3 + +# TASK #1 > Long Year +# Submitted by: Mohammad S Anwar +# Write a script to find all the years between 1900 and 2100 which is a Long +# Year. +# +# A year is Long if it has 53 weeks. +# +# +# [UPDATED][2021-11-01 16:20:00]: For more information about Long Year, please +# refer to wikipedia. +# +# Expected Output +# 1903, 1908, 1914, 1920, 1925, +# 1931, 1936, 1942, 1948, 1953, +# 1959, 1964, 1970, 1976, 1981, +# 1987, 1992, 1998, 2004, 2009, +# 2015, 2020, 2026, 2032, 2037, +# 2043, 2048, 2054, 2060, 2065, +# 2071, 2076, 2082, 2088, 2093, +# 2099 + +# https://en.wikipedia.org/wiki/ISO_week_date#Weeks_per_year +# years in which 1 January or 31 December are Thursdays + +import datetime + +def is_long_year(year): + dt = datetime.date(year, 1, 1) + if dt.isoweekday()==4: + return True + dt = datetime.date(year, 12, 31) + if dt.isoweekday()==4: + return True + return False + +years = list(filter(lambda x:is_long_year(x), range(1900, 2100+1))) +for i in range(0, len(years), 5): + out = ", ".join([str(x) for x in years[i:i+5]]) + if i+5 < len(years): + out += "," + print(out) diff --git a/challenge-137/paulo-custodio/python/ch-2.py b/challenge-137/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..cbdf04d268 --- /dev/null +++ b/challenge-137/paulo-custodio/python/ch-2.py @@ -0,0 +1,62 @@ +#!/usr/bin/env python3 + +# TASK #2 > Lychrel Number +# Submitted by: Mohammad S Anwar +# You are given a number, 10 <= $n <= 1000. +# +# Write a script to find out if the given number is Lychrel number. To keep the +# task simple, we impose the following rules: +# +# a. Stop if the number of iterations reached 500. +# b. Stop if you end up with number >= 10_000_000. +# +# [UPDATED][2021-11-01 16:20:00]: If you stop because of any of the above two +# rules then we expect 1 as an output. +# +# According to wikipedia: +# +# A Lychrel number is a natural number that cannot form a palindrome through +# the iterative process of repeatedly reversing its digits and adding the +# resulting numbers. +# +# Example 1 +# Input: $n = 56 +# Output: 0 +# +# After 1 iteration, we found palindrome number. +# 56 + 65 = 121 +# Example 2 +# Input: $n = 57 +# Output: 0 +# +# After 2 iterations, we found palindrome number. +# 57 + 75 = 132 +# 132 + 231 = 363 +# Example 3 +# Input: $n = 59 +# Output: 0 +# +# After 3 iterations, we found palindrome number. +# 59 + 95 = 154 +# 154 + 451 = 605 +# 605 + 506 = 1111 + +import sys + +MAX_ITER = 500 +MAX_NUM = 10000000 + +def is_lychrel(n): + for i in range(0, MAX_ITER): + if n > MAX_NUM: + break + rev_n = int(str(n)[::-1]) + if n == rev_n: + return False + n += rev_n + return True + +if is_lychrel(int(sys.argv[1])): + print(1) +else: + print(0) |
