aboutsummaryrefslogtreecommitdiff
path: root/challenge-054
diff options
context:
space:
mode:
authorMohammad Sajid Anwar <Mohammad.Anwar@yahoo.com>2024-09-08 21:15:27 +0100
committerGitHub <noreply@github.com>2024-09-08 21:15:27 +0100
commitc27f53ea3c7c6b8cd8a30c6ecb4894364e07f6ec (patch)
tree11a5d24647d3f330f797a4feceb591051619b926 /challenge-054
parent316afc1486285c67d08f4a6899f69c1baaae95bf (diff)
parent451d779420e30ac64a61beadea91da317b8dc6b2 (diff)
downloadperlweeklychallenge-club-c27f53ea3c7c6b8cd8a30c6ecb4894364e07f6ec.tar.gz
perlweeklychallenge-club-c27f53ea3c7c6b8cd8a30c6ecb4894364e07f6ec.tar.bz2
perlweeklychallenge-club-c27f53ea3c7c6b8cd8a30c6ecb4894364e07f6ec.zip
Merge pull request #10794 from pauloscustodio/master
Add Python solutions
Diffstat (limited to 'challenge-054')
-rw-r--r--challenge-054/paulo-custodio/perl/ch-2.pl36
-rw-r--r--challenge-054/paulo-custodio/python/ch-1.py29
-rw-r--r--challenge-054/paulo-custodio/python/ch-2.py58
-rw-r--r--challenge-054/paulo-custodio/t/test-2.yaml7
4 files changed, 117 insertions, 13 deletions
diff --git a/challenge-054/paulo-custodio/perl/ch-2.pl b/challenge-054/paulo-custodio/perl/ch-2.pl
index 5aa109c437..b6929a1e54 100644
--- a/challenge-054/paulo-custodio/perl/ch-2.pl
+++ b/challenge-054/paulo-custodio/perl/ch-2.pl
@@ -11,7 +11,8 @@
# $n = 3*$n + 1 when $n is odd
# For example, if we start at 23, we get the following sequence:
#
-# 23 ? 70 ? 35 ? 106 ? 53 ? 160 ? 80 ? 40 ? 20 ? 10 ? 5 ? 16 ? 8 ? 4 ? 2 ? 1
+# 23 -> 70 -> 35 -> 106 -> 53 -> 160 -> 80 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8
+# -> 4 -> 2 -> 1
#
# Write a function that finds the Collatz sequence for any positive integer.
# Notice how the sequence itself may go far above the original starting number.
@@ -23,21 +24,32 @@
use Modern::Perl;
-my @longest;
-for my $n (1..1e6) {
- my @seq = collatz($n);
- my $len = scalar(@seq);
- push @longest, [$n => $len];
- @longest = sort {$b->[1] <=> $a->[1]} @longest;
- pop @longest if @longest > 20;
+@ARGV or die "Usage: $0 -l|n\n";
+my $arg = shift;
+if ($arg eq "-l") {
+ print_longest(20)
}
+else {
+ print join(" -> ", collatz_seq($arg))
+}
+
+sub print_longest {
+ my @longest;
+ for my $n (1..1e6) {
+ my @seq = collatz_seq($n);
+ my $len = scalar(@seq);
+ push @longest, [$n => $len];
+ @longest = sort {$b->[1] <=> $a->[1]} @longest;
+ pop @longest if @longest > 20;
+ }
-for (@longest) {
- my($n, $len) = @$_;
- say "$n $len";
+ for (@longest) {
+ my($n, $len) = @$_;
+ say "$n $len";
+ }
}
-sub collatz {
+sub collatz_seq {
my($n) = @_;
my @out = ($n);
while ($n != 1) {
diff --git a/challenge-054/paulo-custodio/python/ch-1.py b/challenge-054/paulo-custodio/python/ch-1.py
new file mode 100644
index 0000000000..6bb39a6f43
--- /dev/null
+++ b/challenge-054/paulo-custodio/python/ch-1.py
@@ -0,0 +1,29 @@
+#!/usr/bin/env python3
+
+# Challenge 054
+#
+# TASK #1
+# kth Permutation Sequence
+# Write a script to accept two integers n (>=1) and k (>=1). It should print the
+# kth permutation of n integers. For more information, please follow the wiki
+# page.
+#
+# For example, n=3 and k=4, the possible permutation sequences are listed below:
+#
+# 123
+# 132
+# 213
+# 231
+# 312
+# 321
+# The script should print the 4th permutation sequence 231.
+
+import sys
+from itertools import permutations
+
+n = int(sys.argv[1])
+k = int(sys.argv[2])
+
+perm = permutations([x for x in range(1, n+1)], k)
+for i in list(perm):
+ print("".join([str(x) for x in i]))
diff --git a/challenge-054/paulo-custodio/python/ch-2.py b/challenge-054/paulo-custodio/python/ch-2.py
new file mode 100644
index 0000000000..860174b483
--- /dev/null
+++ b/challenge-054/paulo-custodio/python/ch-2.py
@@ -0,0 +1,58 @@
+#!/usr/bin/env python3
+
+# Challenge 054
+#
+# TASK #2
+# Collatz Conjecture
+# Contributed by Ryan Thompson
+# It is thought that the following sequence will always reach 1:
+#
+# $n = $n / 2 when $n is even
+# $n = 3*$n + 1 when $n is odd
+# For example, if we start at 23, we get the following sequence:
+#
+# 23 -> 70 -> 35 -> 106 -> 53 -> 160 -> 80 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8
+# -> 4 -> 2 -> 1
+#
+# Write a function that finds the Collatz sequence for any positive integer.
+# Notice how the sequence itself may go far above the original starting number.
+#
+# Extra Credit
+# Have your script calculate the sequence length for all starting numbers up to
+# 1000000 (1e6), and output the starting number and sequence length for the
+# longest 20 sequences.
+
+import sys
+
+def collatz_seq(n):
+ out = [n]
+ while n != 1:
+ if n % 2 == 0:
+ n = int(n/2)
+ else:
+ n = 3*n + 1
+ out.append(n)
+ return out
+
+def print_longest():
+ longest = []
+ for n in range(1, int(1e6)+1):
+ count = len(collatz_seq(n))
+ longest.append([n, count])
+ longest.sort(key=lambda x:x[0])
+ longest = longest[::-1]
+ longest.sort(key=lambda x:x[1])
+ longest = longest[::-1]
+ if len(longest) > 20:
+ longest.pop()
+
+ for i in longest:
+ print(str(i[0])+" "+str(i[1]))
+
+
+if sys.argv[1] == "-l":
+ print_longest()
+else:
+ n = int(sys.argv[1])
+ seq = collatz_seq(n)
+ print(" -> ".join([str(x) for x in seq]))
diff --git a/challenge-054/paulo-custodio/t/test-2.yaml b/challenge-054/paulo-custodio/t/test-2.yaml
index 78babb1408..eef2d4d68b 100644
--- a/challenge-054/paulo-custodio/t/test-2.yaml
+++ b/challenge-054/paulo-custodio/t/test-2.yaml
@@ -1,6 +1,11 @@
- setup:
cleanup:
- args: 20
+ args: 23
+ input:
+ output: 23 -> 70 -> 35 -> 106 -> 53 -> 160 -> 80 -> 40 -> 20 -> 10 -> 5 -> 16 -> 8 -> 4 -> 2 -> 1
+- setup:
+ cleanup:
+ args: -l
input:
output: |
|837799 525