aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPaulo Custodio <pauloscustodio@gmail.com>2024-09-17 10:13:26 +0100
committerPaulo Custodio <pauloscustodio@gmail.com>2024-09-17 10:13:26 +0100
commit53ea7400f83c18278c85c96e5592af784705fe81 (patch)
tree0dbc98b848f152a3ede4c8907a1b55cc6aba5290
parentd59b6127562ef7d0b9e1e9ee8ae5b554ab7618aa (diff)
downloadperlweeklychallenge-club-53ea7400f83c18278c85c96e5592af784705fe81.tar.gz
perlweeklychallenge-club-53ea7400f83c18278c85c96e5592af784705fe81.tar.bz2
perlweeklychallenge-club-53ea7400f83c18278c85c96e5592af784705fe81.zip
Add Pythonsolution to challenge 286
-rw-r--r--challenge-286/paulo-custodio/python/ch-1.py36
-rw-r--r--challenge-286/paulo-custodio/python/ch-2.py94
2 files changed, 130 insertions, 0 deletions
diff --git a/challenge-286/paulo-custodio/python/ch-1.py b/challenge-286/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..28e9703901
--- /dev/null
+++ b/challenge-286/paulo-custodio/python/ch-1.py
@@ -0,0 +1,36 @@
+#!/usr/bin/env python3
+
+# Challenge 286
+#
+# Task 1: Self Spammer
+# Submitted by: David Ferrone
+#
+# Write a program which outputs one word of its own script / source code at
+# random. A word is anything between whitespace, including symbols.
+# Example 1
+#
+# If the source code contains a line such as:
+# 'open my $fh, "<", "ch-1.pl" or die;'
+# then the program would output each of the words { open, my, $fh,, "<",,
+# "ch-1.pl", or, die; }
+# (along with other words in the source) with some positive probability.
+#
+# Example 2
+#
+# Technically 'print(" hello ");' is *not* an example program, because it does
+# not assign positive probability to the other two words in the script.
+# It will never display print(" or ");
+#
+# Example 3
+#
+# An empty script is one trivial solution, and here is another:
+# echo "42" > ch-1.pl && perl -p -e '' ch-1.pl
+
+import sys
+from random import shuffle
+
+f = open(sys.argv[0], "r")
+text = f.read()
+words = text.split()
+shuffle(words)
+print(words[0])
diff --git a/challenge-286/paulo-custodio/python/ch-2.py b/challenge-286/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..b31367eb72
--- /dev/null
+++ b/challenge-286/paulo-custodio/python/ch-2.py
@@ -0,0 +1,94 @@
+#!/usr/bin/env python3
+
+# Challenge 286
+#
+# Task 2: Order Game
+# Submitted by: Mohammad Sajid Anwar
+#
+# You are given an array of integers, @ints, whose length is a power of 2.
+#
+# Write a script to play the order game (min and max) and return the last element.
+# Example 1
+#
+# Input: @ints = (2, 1, 4, 5, 6, 3, 0, 2)
+# Output: 1
+#
+# Operation 1:
+#
+# min(2, 1) = 1
+# max(4, 5) = 5
+# min(6, 3) = 3
+# max(0, 2) = 2
+#
+# Operation 2:
+#
+# min(1, 5) = 1
+# max(3, 2) = 3
+#
+# Operation 3:
+#
+# min(1, 3) = 1
+#
+# Example 2
+#
+# Input: @ints = (0, 5, 3, 2)
+# Output: 0
+#
+# Operation 1:
+#
+# min(0, 5) = 0
+# max(3, 2) = 3
+#
+# Operation 2:
+#
+# min(0, 3) = 0
+#
+# Example 3
+#
+# Input: @ints = (9, 2, 1, 4, 5, 6, 0, 7, 3, 1, 3, 5, 7, 9, 0, 8)
+# Output: 2
+#
+# Operation 1:
+#
+# min(9, 2) = 2
+# max(1, 4) = 4
+# min(5, 6) = 5
+# max(0, 7) = 7
+# min(3, 1) = 1
+# max(3, 5) = 5
+# min(7, 9) = 7
+# max(0, 8) = 8
+#
+# Operation 2:
+#
+# min(2, 4) = 2
+# max(5, 7) = 7
+# min(1, 5) = 1
+# max(7, 8) = 8
+#
+# Operation 3:
+#
+# min(2, 7) = 2
+# max(1, 8) = 8
+#
+# Operation 4:
+#
+# min(2, 8) = 2
+
+import sys
+
+def reduce(nums):
+ out = []
+ while len(nums) > 0:
+ if len(nums) >= 2:
+ out.append(min(nums[0], nums[1]))
+ if len(nums) >= 4:
+ out.append(max(nums[2], nums[3]))
+ nums = nums[4:]
+ return out
+
+nums = list(map(int, sys.argv[1:]))
+while len(nums) > 1:
+ nums = reduce(nums)
+
+print(nums[0])