diff options
| author | drbaggy <js5@sanger.ac.uk> | 2022-04-30 00:22:29 +0100 |
|---|---|---|
| committer | drbaggy <js5@sanger.ac.uk> | 2022-04-30 00:22:29 +0100 |
| commit | 3ae59ea102b98dcbd9d7dcd3e30a49422191d90c (patch) | |
| tree | 3a6f137496d8745534bf280e2be0e4a5cde907f2 | |
| parent | 16e89bebcfa7e9ff3c620eaaa9a2dfa5515738cc (diff) | |
| parent | c9c34ed6afd8b14f76e75ccdacf7d2c4a6eaa88a (diff) | |
| download | perlweeklychallenge-club-3ae59ea102b98dcbd9d7dcd3e30a49422191d90c.tar.gz perlweeklychallenge-club-3ae59ea102b98dcbd9d7dcd3e30a49422191d90c.tar.bz2 perlweeklychallenge-club-3ae59ea102b98dcbd9d7dcd3e30a49422191d90c.zip | |
Merge remote-tracking branch 'upstream/master'
78 files changed, 4861 insertions, 1795 deletions
diff --git a/challenge-037/paulo-custodio/perl/ch-2.pl b/challenge-037/paulo-custodio/perl/ch-2.pl index 45340c7c08..7a2ae807d3 100644 --- a/challenge-037/paulo-custodio/perl/ch-2.pl +++ b/challenge-037/paulo-custodio/perl/ch-2.pl @@ -92,7 +92,8 @@ sub parse_sun_length { open(my $fh, "<", \$data) or die; while (<$fh>) { next unless /^(\d+)/; - next unless $1 >= 1 && $1 <= 30; # check only 30 days so that periods are same + # check only 30 days so that periods are same + next unless $1 >= 1 && $1 <= 30; my @f = split(' ', $_); my $day_seconds = parse_hms_seconds($f[5]); $sun_lenght_seconds += $day_seconds; diff --git a/challenge-037/paulo-custodio/python/ch-1.py b/challenge-037/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..b6db7ec458 --- /dev/null +++ b/challenge-037/paulo-custodio/python/ch-1.py @@ -0,0 +1,41 @@ +#!/usr/bin/env python3 + +# Challenge 037 +# +# TASK #1 +# Write a script to calculate the total number of weekdays (Mon-Fri) in each +# month of the year 2019. +# Jan: 23 days +# Feb: 20 days +# Mar: 21 days +# Apr: 22 days +# May: 23 days +# Jun: 20 days +# Jul: 23 days +# Aug: 22 days +# Sep: 21 days +# Oct: 23 days +# Nov: 21 days +# Dec: 22 days + +from datetime import date, timedelta + +months = {1:'Jan', 2:'Feb', 3:'Mar', 4:'Apr', 5:'May', 6:'Jun', + 7:'Jul', 8:'Aug', 9:'Sep', 10:'Oct', 11:'Nov', 12:'Dec'} + +def month_work_days(year, month): + work_days = 0 + dt = date(year=year, month=month, day=1) + while dt.month == month: + if dt.weekday() < 5: + work_days += 1 + dt += timedelta(days=1) + return work_days + +def say_work_days(year): + for month in range(1, 13): + print(months[month]+": "+ \ + str(month_work_days(year, month))+ \ + " days") + +say_work_days(2019) diff --git a/challenge-037/paulo-custodio/python/ch-2.py b/challenge-037/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..6f44d8f49c --- /dev/null +++ b/challenge-037/paulo-custodio/python/ch-2.py @@ -0,0 +1,102 @@ +#!/usr/bin/env python3 + +# Challenge 037 +# +# TASK #2 +# Write a script to find out the DayLight gain/loss in the month of December +# 2019 as compared to November 2019 in the city of London. You can find out +# sunrise and sunset data for November 2019 and December 2019 for London. + +import re + +november_data = """ +2019 Sunrise/Sunset Daylength Astronomical Twilight Nautical Twilight Civil Twilight Solar Noon +Nov Sunrise Sunset Length Diff. Start End Start End Start End Time Mil. km +1 06:53 ? 16:34 ? 9:40:44 -3:35 05:00 18:27 05:38 17:48 06:18 17:09 11:44 148.491 +2 06:55 ? 16:32 ? 9:37:10 -3:33 05:01 18:25 05:40 17:47 06:19 17:07 11:44 148.451 +3 06:56 ? 16:30 ? 9:33:38 -3:32 05:03 18:24 05:41 17:45 06:21 17:05 11:44 148.412 +4 06:58 ? 16:28 ? 9:30:07 -3:30 05:04 18:22 05:43 17:43 06:23 17:04 11:44 148.373 +5 07:00 ? 16:27 ? 9:26:38 -3:29 05:06 18:21 05:45 17:42 06:24 17:02 11:44 148.334 +6 07:02 ? 16:25 ? 9:23:11 -3:27 05:07 18:19 05:46 17:40 06:26 17:01 11:44 148.296 +7 07:03 ? 16:23 ? 9:19:45 -3:25 05:09 18:18 05:48 17:39 06:28 16:59 11:44 148.258 +8 07:05 ? 16:22 ? 9:16:22 -3:23 05:10 18:16 05:49 17:38 06:29 16:58 11:44 148.221 +9 07:07 ? 16:20 ? 9:13:01 -3:21 05:12 18:15 05:51 17:36 06:31 16:56 11:44 148.184 +10 07:09 ? 16:18 ? 9:09:42 -3:18 05:13 18:14 05:52 17:35 06:32 16:55 11:44 148.148 +11 07:10 ? 16:17 ? 9:06:25 -3:16 05:15 18:13 05:54 17:34 06:34 16:53 11:44 148.112 +12 07:12 ? 16:15 ? 9:03:11 -3:14 05:16 18:11 05:55 17:32 06:36 16:52 11:44 148.077 +13 07:14 ? 16:14 ? 8:59:59 -3:11 05:18 18:10 05:57 17:31 06:37 16:51 11:44 148.043 +14 07:16 ? 16:12 ? 8:56:50 -3:09 05:19 18:09 05:58 17:30 06:39 16:49 11:44 148.009 +15 07:17 ? 16:11 ? 8:53:44 -3:06 05:20 18:08 06:00 17:29 06:40 16:48 11:45 147.976 +16 07:19 ? 16:10 ? 8:50:40 -3:03 05:22 18:07 06:01 17:28 06:42 16:47 11:45 147.944 +17 07:21 ? 16:08 ? 8:47:39 -3:00 05:23 18:06 06:03 17:26 06:44 16:46 11:45 147.912 +18 07:22 ? 16:07 ? 8:44:42 -2:57 05:25 18:05 06:04 17:25 06:45 16:44 11:45 147.881 +19 07:24 ? 16:06 ? 8:41:48 -2:54 05:26 18:04 06:06 17:24 06:47 16:43 11:45 147.850 +20 07:26 ? 16:05 ? 8:38:57 -2:50 05:27 18:03 06:07 17:24 06:48 16:42 11:46 147.820 +21 07:27 ? 16:04 ? 8:36:09 -2:47 05:29 18:02 06:08 17:23 06:50 16:41 11:46 147.791 +22 07:29 ? 16:03 ? 8:33:25 -2:43 05:30 18:01 06:10 17:22 06:51 16:40 11:46 147.762 +23 07:31 ? 16:01 ? 8:30:45 -2:40 05:31 18:01 06:11 17:21 06:53 16:39 11:46 147.733 +24 07:32 ? 16:00 ? 8:28:09 -2:36 05:33 18:00 06:13 17:20 06:54 16:39 11:47 147.705 +25 07:34 ? 15:59 ? 8:25:36 -2:32 05:34 17:59 06:14 17:19 06:56 16:38 11:47 147.677 +26 07:35 ? 15:59 ? 8:23:08 -2:28 05:35 17:59 06:15 17:19 06:57 16:37 11:47 147.650 +27 07:37 ? 15:58 ? 8:20:44 -2:23 05:37 17:58 06:17 17:18 06:58 16:36 11:47 147.623 +28 07:38 ? 15:57 ? 8:18:24 -2:19 05:38 17:57 06:18 17:17 07:00 16:35 11:48 147.596 +29 07:40 ? 15:56 ? 8:16:09 -2:15 05:39 17:57 06:19 17:17 07:01 16:35 11:48 147.570 +30 07:41 ? 15:55 ? 8:13:59 -2:10 05:40 17:57 06:20 17:16 07:02 16:34 11:49 147.544 +* All times are local time for London. They take into account refraction. Dates are based on the Gregorian calendar. +""" + +december_data = """ +2019 Sunrise/Sunset Daylength Astronomical Twilight Nautical Twilight Civil Twilight Solar Noon +Dec Sunrise Sunset Length Diff. Start End Start End Start End Time Mil. km +1 07:43 ? 15:55 ? 8:11:53 -2:05 05:41 17:56 06:22 17:16 07:04 16:34 11:49 147.518 +2 07:44 ? 15:54 ? 8:09:53 -2:00 05:42 17:56 06:23 17:15 07:05 16:33 11:49 147.493 +3 07:46 ? 15:53 ? 8:07:57 -1:55 05:44 17:55 06:24 17:15 07:06 16:33 11:50 147.469 +4 07:47 ? 15:53 ? 8:06:07 -1:50 05:45 17:55 06:25 17:15 07:08 16:32 11:50 147.446 +5 07:48 ? 15:53 ? 8:04:22 -1:45 05:46 17:55 06:26 17:14 07:09 16:32 11:50 147.423 +6 07:49 ? 15:52 ? 8:02:42 -1:39 05:47 17:55 06:27 17:14 07:10 16:32 11:51 147.400 +7 07:51 ? 15:52 ? 8:01:08 -1:34 05:48 17:55 06:28 17:14 07:11 16:31 11:51 147.379 +8 07:52 ? 15:51 ? 7:59:40 -1:28 05:49 17:54 06:29 17:14 07:12 16:31 11:52 147.358 +9 07:53 ? 15:51 ? 7:58:17 -1:22 05:50 17:54 06:30 17:14 07:13 16:31 11:52 147.338 +10 07:54 ? 15:51 ? 7:57:01 -1:16 05:51 17:54 06:31 17:14 07:14 16:31 11:53 147.319 +11 07:55 ? 15:51 ? 7:55:50 -1:10 05:52 17:54 06:32 17:14 07:15 16:31 11:53 147.301 +12 07:56 ? 15:51 ? 7:54:45 -1:04 05:52 17:54 06:33 17:14 07:16 16:31 11:54 147.283 +13 07:57 ? 15:51 ? 7:53:46 -0:58 05:53 17:55 06:34 17:14 07:17 16:31 11:54 147.267 +14 07:58 ? 15:51 ? 7:52:54 -0:52 05:54 17:55 06:35 17:14 07:18 16:31 11:54 147.251 +15 07:59 ? 15:51 ? 7:52:08 -0:46 05:55 17:55 06:36 17:14 07:19 16:31 11:55 147.237 +16 08:00 ? 15:51 ? 7:51:28 -0:39 05:56 17:55 06:36 17:14 07:19 16:31 11:55 147.223 +17 08:00 ? 15:51 ? 7:50:54 -0:33 05:56 17:56 06:37 17:15 07:20 16:32 11:56 147.210 +18 08:01 ? 15:52 ? 7:50:27 -0:27 05:57 17:56 06:38 17:15 07:21 16:32 11:56 147.198 +19 08:02 ? 15:52 ? 7:50:06 -0:20 05:58 17:56 06:38 17:15 07:22 16:32 11:57 147.187 +20 08:02 ? 15:52 ? 7:49:52 -0:14 05:58 17:57 06:39 17:16 07:22 16:33 11:57 147.177 +21 08:03 ? 15:53 ? 7:49:44 -0:07 05:59 17:57 06:40 17:16 07:23 16:33 11:58 147.167 +22 08:04 ? 15:53 ? 7:49:43 -0:01 05:59 17:58 06:40 17:17 07:23 16:34 11:58 147.158 +23 08:04 ? 15:54 ? 7:49:49 +0:05 06:00 17:58 06:41 17:17 07:24 16:34 11:59 147.149 +24 08:04 ? 15:54 ? 7:50:01 +0:11 06:00 17:59 06:41 17:18 07:24 16:35 11:59 147.141 +25 08:05 ? 15:55 ? 7:50:19 +0:18 06:01 17:59 06:41 17:19 07:25 16:35 12:00 147.133 +26 08:05 ? 15:56 ? 7:50:44 +0:24 06:01 18:00 06:42 17:19 07:25 16:36 12:00 147.126 +27 08:05 ? 15:57 ? 7:51:15 +0:31 06:01 18:01 06:42 17:20 07:25 16:37 12:01 147.120 +28 08:06 ? 15:57 ? 7:51:53 +0:37 06:01 18:01 06:42 17:21 07:25 16:38 12:01 147.114 +29 08:06 ? 15:58 ? 7:52:37 +0:44 06:02 18:02 06:42 17:21 07:26 16:38 12:02 147.109 +30 08:06 ? 15:59 ? 7:53:28 +0:50 06:02 18:03 06:43 17:22 07:26 16:39 12:02 147.104 +31 08:06 ? 16:00 ? 7:54:24 +0:56 06:02 18:04 06:43 17:23 07:26 16:40 12:03 147.100 +* All times are local time for London. They take into account refraction. Dates are based on the Gregorian calendar. +""" + +def parse_sun_length(data): + sun_length_seconds = 0 + for line in data.split("\n"): + if m := re.match(r"^(\d+)", line): + day = int(m.group(1)) + # check only 30 days so that periods are same + if 1 <= day <= 30: + if m := re.search(r"\s(\d+):(\d+):(\d+)\s", line): + hour = int(m.group(1)) + minute = int(m.group(2)) + second = int(m.group(3)) + day_seconds = 3600*hour+60*minute+second + sun_length_seconds += day_seconds + return sun_length_seconds + +november_sun_length = parse_sun_length(november_data) +december_sun_length = parse_sun_length(december_data) +print("{:.1f} minutes gained".format((december_sun_length-november_sun_length)/60)) diff --git a/challenge-038/paulo-custodio/python/ch-1.py b/challenge-038/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..d90e25f8bb --- /dev/null +++ b/challenge-038/paulo-custodio/python/ch-1.py @@ -0,0 +1,50 @@ +#!/usr/bin/env python3 + +# Challenge 038 +# +# TASK #1 +# Date Finder +# Create a script to accept a 7 digits number, where the first number can only +# be 1 or 2. The second and third digits can be anything 0-9. The fourth and +# fifth digits corresponds to the month i.e. 01,02,03...,11,12. And the last +# 2 digits respresents the days in the month i.e. 01,02,03...29,30,31. Your +# script should validate if the given number is valid as per the rule and then +# convert into human readable format date. +# +# RULES +# If 1st digit is 1, then prepend 20 otherwise 19 to the 2nd and 3rd digits to +# make it 4-digits year. +# +# The 4th and 5th digits together should be a valid month. +# +# The 6th and 7th digits together should be a valid day for the above month. +# +# For example, the given number is 2230120, it should print 1923-01-20. + +from datetime import date +from calendar import monthrange +import re +import sys + +input = sys.argv[1] +if not (m := re.match(r"^([12])(\d\d)([01]\d)([0-3]\d)$", input)): + print("malformed input") +else: + year_msb = int(m.group(1)) + year_lsb = int(m.group(2)) + month = int(m.group(3)) + day = int(m.group(4)) + + if year_msb == 1: + year = 2000+year_lsb + else: + year = 1900+year_lsb + + if not 1 <= month <= 12: + print("malformed month") + else: + if not 1 <= day <= monthrange(year, month)[1]: + print("malformed day") + else: + dt = date(year=year, month=month, day=day) + print(dt.strftime("%Y-%m-%d")) diff --git a/challenge-038/paulo-custodio/python/ch-2.py b/challenge-038/paulo-custodio/python/ch-2.py new file mode 100644 index 0000000000..9d7feca634 --- /dev/null +++ b/challenge-038/paulo-custodio/python/ch-2.py @@ -0,0 +1,79 @@ +#!/usr/bin/env python3 + +# Challenge 038 +# +# TASK #2 +# Word Game +# Lets assume we have tiles as listed below, with an alphabet (A..Z) printed +# on them. Each tile has a value, e.g. A (1 point), B (4 points) etc. You are +# allowed to draw 7 tiles from the lot randomly. Then try to form a word using +# the 7 tiles with maximum points altogether. You don't have to use all the +# 7 tiles to make a word. You should try to use as many tiles as possible to +# get the maximum points. +# +# For example, A (x8) means there are 8 tiles with letter A. +# +# 1 point +# A (x8), G (x3), I (x5), S (x7), U (x5), X (x2), Z (x5) +# +# 2 points +# E (x9), J (x3), L (x3), R (x3), V (x3), Y (x5) +# +# 3 points +# F (x3), D (x3), P (x5), W (x5) +# +# 4 points +# B (x5), N (x4) +# +# 5 points +# T (x5), O (x3), H (x3), M (x4), C (x4) +# +# 10 points +# K (x2), Q (x2) + +import sys +import re + +g_score = {'a':1,'g':1,'i':1,'s':1,'u':1,'x':1,'z':1, + 'e':2,'j':2,'l':2,'r':2,'v':2,'y':2, + 'f':3,'d':3,'p':3,'w':3, + 'b':4,'n':4, + 't':5,'o':5,'h':5,'m':5,'c':5, + 'k':10,'q':10} +g_count = {'a':8,'g':3,'i':5,'s':7,'u':5,'x':2,'z':5, + 'e':9,'j':3,'l':3,'r':3,'v':3,'y':5, + 'f':3,'d':3,'p':5,'w':5, + 'b':5,'n':4, + 't':5,'o':3,'h':3,'m':4,'c':4, + 'k':2,'q':2} + +def check_word(word): + # maximium 7 letters, only lower case letters + if not re.match(r"^[a-z]{1,7}$", word): + return -1 + + # less than count tiles for each letter + for letter in word: + if letter.count(letter) > g_count[letter]: + return -1 + + # compute score + score = 0 + for letter in word: + score += g_score[letter] + + return score + + +dict = sys.argv[1] +max_score = 0 +max_word = "" + +with open(dict) as f: + for line in f.readlines(): + word = line.rstrip() + score = check_word(word) + if score > 0 and score > max_score: + max_score, max_word = score, word + +print(max_score, max_word) diff --git a/challenge-039/paulo-custodio/python/ch-1.py b/challenge-039/paulo-custodio/python/ch-1.py new file mode 100644 index 0000000000..d0372e0e1f --- /dev/null +++ b/challenge-039/paulo-custodio/python/ch-1.py @@ -0,0 +1,63 @@ +#!/usr/bin/env python3 + +# Challenge 039 +# +# TASK #1 +# A guest house had a policy that the light remain ON as long as the at least +# one guest is in the house. There is guest book which tracks all guest in/out +# time. Write a script to find out how long in minutes the light were ON. +# Guest Book |
