aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMohammad S Anwar <Mohammad.Anwar@yahoo.com>2021-11-09 19:10:14 +0000
committerGitHub <noreply@github.com>2021-11-09 19:10:14 +0000
commitd9a6e5bd4446752fa1710c60392a9919aa0eb007 (patch)
treebddea25bc393afcb9bd8f13c8e6c3872188a6ed2
parent26fb909f059b675fa6e0fa579522481337c54c23 (diff)
parenta77b120e0df6002c36515ea79cb84c801a18c88a (diff)
downloadperlweeklychallenge-club-d9a6e5bd4446752fa1710c60392a9919aa0eb007.tar.gz
perlweeklychallenge-club-d9a6e5bd4446752fa1710c60392a9919aa0eb007.tar.bz2
perlweeklychallenge-club-d9a6e5bd4446752fa1710c60392a9919aa0eb007.zip
Merge pull request #5189 from pauloscustodio/devel
Add Python solution to challenge 104
-rw-r--r--challenge-104/paulo-custodio/Makefile2
-rw-r--r--challenge-104/paulo-custodio/perl/ch-1.pl2
-rw-r--r--challenge-104/paulo-custodio/perl/ch-2.pl2
-rw-r--r--challenge-104/paulo-custodio/python/ch-1.py30
-rw-r--r--challenge-104/paulo-custodio/python/ch-2.py81
-rw-r--r--challenge-104/paulo-custodio/test.pl4
6 files changed, 115 insertions, 6 deletions
diff --git a/challenge-104/paulo-custodio/Makefile b/challenge-104/paulo-custodio/Makefile
new file mode 100644
index 0000000000..c3c762d746
--- /dev/null
+++ b/challenge-104/paulo-custodio/Makefile
@@ -0,0 +1,2 @@
+all:
+ perl ../../challenge-001/paulo-custodio/test.pl
diff --git a/challenge-104/paulo-custodio/perl/ch-1.pl b/challenge-104/paulo-custodio/perl/ch-1.pl
index 4736e2a0a3..43524f8121 100644
--- a/challenge-104/paulo-custodio/perl/ch-1.pl
+++ b/challenge-104/paulo-custodio/perl/ch-1.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 104
#
diff --git a/challenge-104/paulo-custodio/perl/ch-2.pl b/challenge-104/paulo-custodio/perl/ch-2.pl
index 7106fb6ea0..1296d650ac 100644
--- a/challenge-104/paulo-custodio/perl/ch-2.pl
+++ b/challenge-104/paulo-custodio/perl/ch-2.pl
@@ -1,4 +1,4 @@
-#!/usr/bin/perl
+#!/usr/bin/env perl
# Challenge 104
#
diff --git a/challenge-104/paulo-custodio/python/ch-1.py b/challenge-104/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..ae10182903
--- /dev/null
+++ b/challenge-104/paulo-custodio/python/ch-1.py
@@ -0,0 +1,30 @@
+#!/usr/bin/env python3
+
+# Challenge 104
+#
+# TASK #1 > FUSC Sequence
+# Submitted by: Mohammad S Anwar
+# Write a script to generate first 50 members of FUSC Sequence. Please refer to
+# OEIS for more information.
+#
+# The sequence defined as below:
+#
+# fusc(0) = 0
+# fusc(1) = 1
+# for n > 1:
+# when n is even: fusc(n) = fusc(n / 2),
+# when n is odd: fusc(n) = fusc((n-1)/2) + fusc((n+1)/2)
+
+import sys
+
+def fusc(num):
+ data = [0, 1]
+ for n in range(2, num):
+ if n%2==0:
+ data.append(data[int(n/2)])
+ else:
+ data.append(data[int((n-1)/2)] + data[int((n+1)/2)])
+
+ return data
+
+print(" ".join([str(x) for x in fusc(int(sys.argv[1]))]))
diff --git a/challenge-104/paulo-custodio/python/ch-2.py b/challenge-104/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..6a71800c90
--- /dev/null
+++ b/challenge-104/paulo-custodio/python/ch-2.py
@@ -0,0 +1,81 @@
+#!/usr/bin/env python3
+
+# Challenge 104
+#
+# TASK #2 > NIM Game
+# Submitted by: Mohammad S Anwar
+# Write a script to simulate the NIM Game.
+#
+# It is played between 2 players. For the purpose of this task, let assume you
+# play against the machine.
+#
+# There are 3 simple rules to follow:
+#
+# a) You have 12 tokens
+# b) Each player can pick 1, 2 or 3 tokens at a time
+# c) The player who picks the last token wins the game
+
+# A plays to win, B plays randomly, A wins 91% of the time
+# If both play to win, the second player to play always wins
+
+import sys
+import random
+
+matches = 100000
+random_play = True
+wins = {'A':0, 'B':0}
+
+def draw_to_win(t):
+ if t <= 3:
+ return t # win the game
+ elif (t % 4)==0:
+ return 1 # loose the game
+ elif (t % 5)==0:
+ return 1 # win the game
+ elif (t % 6)==0:
+ return 2 # win the game
+ elif (t % 7)==0:
+ return 3 # win the game
+ elif (t % 9)==0:
+ return 2 # win the game
+ elif (t % 11)==0:
+ return 2 # win the game
+ else:
+ return None # not reached
+
+def draw_random(t):
+ return random.randint(1, 3)
+
+def draw(t):
+ global random_play
+
+ if random_play:
+ return draw_random(t)
+ else:
+ return draw_to_win(t)
+
+def play_match():
+ global wins
+ t = 12
+ while t > 0:
+ for player in ['A', 'B']:
+ if player=='A':
+ move = draw_to_win(t)
+ else:
+ move = draw(t)
+ t -= move
+ if t <= 0:
+ wins[player] += 1
+ break
+
+def play_matches():
+ for i in range(0, matches):
+ play_match()
+
+if sys.argv[1]=="random":
+ random_play = True
+else:
+ random_play = False
+play_matches()
+a_wins = int(100*wins['A']/(wins['A']+wins['B']))
+print(f"A wins {a_wins}% of the matches.")
diff --git a/challenge-104/paulo-custodio/test.pl b/challenge-104/paulo-custodio/test.pl
deleted file mode 100644
index ba6c37260b..0000000000
--- a/challenge-104/paulo-custodio/test.pl
+++ /dev/null
@@ -1,4 +0,0 @@
-#!/usr/bin/env perl
-use Modern::Perl;
-use Test::More;
-require '../../challenge-001/paulo-custodio/test.pl';