aboutsummaryrefslogtreecommitdiff
path: root/challenge-048
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2020-02-23 06:58:56 +0000
committerGitHub <noreply@github.com>2020-02-23 06:58:56 +0000
commit4ff526d8945dae50562cff383f57b01b681eac8f (patch)
tree75168a6cd62c8b29e1dbbef4943c862093fa1665 /challenge-048
parentee6309f98684d886d23fc27af2d4170d792905fd (diff)
parent71a208ec3da5563438ccae7fe88e680d8bac5efe (diff)
downloadperlweeklychallenge-club-4ff526d8945dae50562cff383f57b01b681eac8f.tar.gz
perlweeklychallenge-club-4ff526d8945dae50562cff383f57b01b681eac8f.tar.bz2
perlweeklychallenge-club-4ff526d8945dae50562cff383f57b01b681eac8f.zip
Merge pull request #1292 from kolcon/ch48_lk
Challenges 048 LK
Diffstat (limited to 'challenge-048')
-rw-r--r--challenge-048/lubos-kolouch/perl/ch-1.pl67
-rw-r--r--challenge-048/lubos-kolouch/perl/ch-2.pl50
-rw-r--r--challenge-048/lubos-kolouch/python/ch-1.py52
-rw-r--r--challenge-048/lubos-kolouch/python/ch-2.py33
4 files changed, 202 insertions, 0 deletions
diff --git a/challenge-048/lubos-kolouch/perl/ch-1.pl b/challenge-048/lubos-kolouch/perl/ch-1.pl
new file mode 100644
index 0000000000..f8f6e39eac
--- /dev/null
+++ b/challenge-048/lubos-kolouch/perl/ch-1.pl
@@ -0,0 +1,67 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.pl
+#
+# USAGE: ./ch-1.pl
+#
+# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/
+#
+# Survivor
+#
+# There are 50 people standing in a circle in position 1 to 50. The person standing at position 1 has a sword. He kills the next person i.e. standing at position 2 and pass on the sword to the immediate next i.e. person standing at position 3. Now the person at position 3 does the same and it goes on until only one survives.
+#
+# Write a script to find out the survivor.
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Lubos Kolouch
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 02/21/2020 09:15:59 PM
+# REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+use feature qw/say/;
+
+sub get_last_man_standing {
+ my $count = shift;
+
+ my %people;
+
+ for ( 1 .. $count ) {
+ $people{$_} = 1;
+ }
+
+ my $last = 0;
+ my $switch = 0;
+
+ while (%people) {
+ for my $key ( sort { $a <=> $b } keys %people ) {
+
+ delete $people{$key} if $switch;
+
+ $last = $key;
+ $switch = $switch == 0 ? 1 : 0;
+ }
+ }
+
+ return $last;
+}
+
+my $people_count = $ARGV[0];
+
+say get_last_man_standing($people_count);
+
+# TESTS
+
+use Test::More tests => 3;
+
+is get_last_man_standing(50),37;
+is get_last_man_standing(2),1;
+is get_last_man_standing(3),3;
+
diff --git a/challenge-048/lubos-kolouch/perl/ch-2.pl b/challenge-048/lubos-kolouch/perl/ch-2.pl
new file mode 100644
index 0000000000..6e8dd9311a
--- /dev/null
+++ b/challenge-048/lubos-kolouch/perl/ch-2.pl
@@ -0,0 +1,50 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-2.pl
+#
+# USAGE: ./ch-2.pl
+#
+# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/
+#
+# Palindrome Dates
+#
+# Write a script to print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001.
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: YOUR NAME (),
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 02/22/2020 10:11:25 AM
+# REVISION: ---
+#===============================================================================
+
+use strict;
+use warnings;
+use DateTime;
+use feature qw/say/;
+
+sub is_palindrome {
+ my $dt = shift;
+
+ return 1 if $dt->mdy('') eq reverse $dt->mdy('');
+
+ return 0;
+}
+
+my $dt_start = DateTime->new( year => 2000, month => 1, day => 1 );
+my $epoch_test = $dt_start->epoch;
+
+my $dt_end = DateTime->new( year => 2999, month => 12, day => 31 );
+my $epoch_end = $dt_end->epoch;
+
+while ($epoch_test < $epoch_end) {
+ my $dt = DateTime->from_epoch( epoch => $epoch_test);
+
+ say $dt->mdy if is_palindrome($dt);
+ $epoch_test += 60 * 60 * 24;
+}
+
diff --git a/challenge-048/lubos-kolouch/python/ch-1.py b/challenge-048/lubos-kolouch/python/ch-1.py
new file mode 100644
index 0000000000..2f3e0090e6
--- /dev/null
+++ b/challenge-048/lubos-kolouch/python/ch-1.py
@@ -0,0 +1,52 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-1.py
+#
+# USAGE: ./ch-1.py
+#
+# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/
+#
+# Survivor
+#
+# There are 50 people standing in a circle in position 1 to 50. The person standing at position 1 has a sword. He kills the next person i.e. standing at position 2 and pass on the sword to the immediate next i.e. person standing at position 3. Now the person at position 3 does the same and it goes on until only one survives.
+#
+# Write a script to find out the survivor.
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: Lubos Kolouch
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 02/21/2020 09:15:59 PM
+# REVISION: ---
+#===============================================================================
+
+from collections import deque
+
+def get_last_man_standing(count):
+
+ people = deque(range(count))
+ killed = 0
+ switch = 0
+
+ while killed < count-1:
+ if switch == 0:
+ switch = 1
+ people.rotate(-1)
+ else:
+ switch = 0
+ people.popleft()
+ killed += 1
+
+ return people.popleft()+1
+
+print(get_last_man_standing(50))
+
+# TESTS
+
+assert(get_last_man_standing(50) == 37)
+assert(get_last_man_standing(2) == 1)
+assert(get_last_man_standing(3) == 3)
diff --git a/challenge-048/lubos-kolouch/python/ch-2.py b/challenge-048/lubos-kolouch/python/ch-2.py
new file mode 100644
index 0000000000..064cd8c831
--- /dev/null
+++ b/challenge-048/lubos-kolouch/python/ch-2.py
@@ -0,0 +1,33 @@
+#!/usr/bin/perl
+#===============================================================================
+#
+# FILE: ch-2.py
+#
+# USAGE: ./ch-2.py
+#
+# DESCRIPTION: https://perlweeklychallenge.org/blog/perl-weekly-challenge-048/
+#
+# Palindrome Dates
+#
+# Write a script to print all Palindrome Dates between 2000 and 2999. The format of date is mmddyyyy. For example, the first one was on October 2, 2001 as it is represented as 10022001.
+#
+# OPTIONS: ---
+# REQUIREMENTS: ---
+# BUGS: ---
+# NOTES: ---
+# AUTHOR: YOUR NAME (),
+# ORGANIZATION:
+# VERSION: 1.0
+# CREATED: 02/22/2020 10:11:25 AM
+# REVISION: ---
+#===============================================================================
+
+from datetime import date, timedelta
+
+dt = date(2000,1,1)
+
+while dt.year < 3000:
+ dt += timedelta(days=1)
+ if dt.strftime("%m%d%Y") == dt.strftime("%m%d%Y")[::-1]:
+ print(dt)
+