diff options
| -rwxr-xr-x | challenge-054/user-person/perl/ch-1.pl | 72 | ||||
| -rwxr-xr-x | challenge-054/user-person/perl/ch-2.pl | 122 | ||||
| -rwxr-xr-x | challenge-054/user-person/python/ch-1.py | 66 | ||||
| -rwxr-xr-x | challenge-054/user-person/python/ch-2.py | 114 |
4 files changed, 374 insertions, 0 deletions
diff --git a/challenge-054/user-person/perl/ch-1.pl b/challenge-054/user-person/perl/ch-1.pl new file mode 100755 index 0000000000..b6c31d1f6a --- /dev/null +++ b/challenge-054/user-person/perl/ch-1.pl @@ -0,0 +1,72 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-1.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-054/ # +# # +# 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. # +# https://en.wikipedia.org/wiki/Permutation#k-permutations_of_n # +# 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. # +# # +########################################################################### + +use strict; +use warnings; +use diagnostics; + +use FindBin; +use List::Permutor; + +sub msgExit { + print $FindBin::Script . " requires 2 arguments. The first argument to determine the sequence ( 1 >= ), +The second argument to determine which entry to print. Both arguments should be >= 1 .\n"; + exit(1); +} + +msgExit if scalar @ARGV != 2; + +my $n = $ARGV[0]; +my $k = $ARGV[1]; + +if ( $n =~ m{\A\d+\Z} and $k =~ m{\A\d+\Z} ) { + if ($n < 1 or $k < 1) { + msgExit; + } +} else { + msgExit; +} +my @sequence = 1 .. $n-1; + +my $perm = new List::Permutor @sequence; + +my $kth = 1; + +my $failure++; + +LOOP: +while (my @set = $perm->next) { + if ( $kth++ == $k) { + print @set, "\n"; + $failure--; + last LOOP; + } +} + +print "There is no '$k'-th number in the '$n sequence'.\n" if $failure; + +endHandler() if $reporting || $logging; # TO CLEAN UP, AND CLOSE LOG ENTRIES diff --git a/challenge-054/user-person/perl/ch-2.pl b/challenge-054/user-person/perl/ch-2.pl new file mode 100755 index 0000000000..b864148840 --- /dev/null +++ b/challenge-054/user-person/perl/ch-2.pl @@ -0,0 +1,122 @@ +#!/usr/bin/env perl + +########################################################################### +# script name: ch-2.pl # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-054/ # +# # +# 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.perl # +# # +########################################################################### + +use strict; +use warnings; +use diagnostics; +use FindBin; + +++$|; + +sub msgExit { + print $FindBin::Script, " requires one positive integer as an argument (or extracredit).\n"; + exit(1); +} + +my %high = (); + +sub checkHighKeys { + my $key = $_[0]; + my $keyCount = $_[1]; + my $highKeys = $_[2]; + + if (keys %$highKeys == 20) { + my $keyToLowest = (sort {$highKeys->{$a} <=> $highKeys->{$b} or $a <=> $b } keys %$highKeys)[0]; + if ($highKeys->{$keyToLowest} < $keyCount) { + delete $highKeys->{$keyToLowest}; + $highKeys->{$key} = $keyCount; + } elsif ($highKeys->{$keyToLowest} == $keyCount) { + if ($keyToLowest < $key) { + delete $highKeys->{$keyToLowest}; + $highKeys->{$key} = $keyCount; + } + } + } else { + $highKeys->{$key} = $keyCount; + } +} + +msgExit if scalar @ARGV != 1; + +my $n = $ARGV[0]; +my $extraCredit = 0; + +if ($n =~ m{-{0,2}extra[-~`!@#$%^&*=+|\\;:'",.?/ ]?credit}i) { + $n = 2; + $extraCredit = 1; + print STDERR "Allow time for calculations.\n"; + print STDERR " They finish when '#'s reach this point-> |\n"; +} elsif ($n =~ m{\A\d+\Z}) { + msgExit if $n < 1; +} else { + msgExit; +} + +my $MAX = 1_000_000; +my $i = $n; + +OUTER_LOOP: +while ($i <= $MAX) { + my $count = 0; + + while ($n != 1) { + + if ($extraCredit) { + $count++; + } else { + print "$n -> "; + } + + if ($n % 2 == 0) { # EVEN + $n /= 2; + } else { # ODD + $n = 3*$n + 1; + } + } + + if ($extraCredit) { + checkHighKeys( $i, $count, \%high); + $n = ++$i; + if ($n % 20_000 == 0) { + print STDOUT "#"; + } + } else { + print "1\n"; + last OUTER_LOOP; + } +} + +if ($extraCredit) { + print STDERR "\n\n"; + foreach my $key (sort { $high{$b} <=> $high{$a} or $b <=> $a } keys %high) { + print "Starting number: ", $key, " with sequence length: ", $high{$key}, "\n"; + } +} diff --git a/challenge-054/user-person/python/ch-1.py b/challenge-054/user-person/python/ch-1.py new file mode 100755 index 0000000000..152337641e --- /dev/null +++ b/challenge-054/user-person/python/ch-1.py @@ -0,0 +1,66 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-054/ # +# # +# 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. # +# https://en.wikipedia.org/wiki/Permutation#k-permutations_of_n # +# 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. # +# # +########################################################################### + +from itertools import permutations +import os +import re +import sys + +def msgExit(): + print(os.path.basename(sys.argv[0]), " requires 2 arguments. The first argument to determine the sequence ( 1 >= ),\nThe second argument to determine which entry to print. Both arguments should be >= 1 .\n"); + exit(1) + +def printSmooshed(ints): + for num in ints: + print(num,end='') + print() + +if len(sys.argv) != 3: + msgExit() + +n = int(sys.argv[1]) +k = int(sys.argv[2]) + +if re.search(r'\A\d+\Z', str(n)) and re.search(r'\A\d+\Z', str(k)): + if n < 1 or k < 1: + msgExit() +else: + msgExit() + +kth = 1; +failure = True + +for i in permutations(range(1,n)): + if kth == k: + printSmooshed(i) + failure = False + break + else: + kth += 1 + +if failure: + print('There is no ' + str(k) + "-th number in the '" + str(n) + " sequence'.") diff --git a/challenge-054/user-person/python/ch-2.py b/challenge-054/user-person/python/ch-2.py new file mode 100755 index 0000000000..46c8613e79 --- /dev/null +++ b/challenge-054/user-person/python/ch-2.py @@ -0,0 +1,114 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# # +# https://github.com/user-person # +# # +# https://perlweeklychallenge.org/blog/perl-weekly-challenge-054/ # +# # +# 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.perl # +# # +########################################################################### + +import os +import re +import sys + +def msgExit(): + print(os.path.basename(sys.argv[0]), "requires one positive integer as an argument (or extracredit).") + exit(1) + +high = {} + +def checkHighKeys(key,keyCount,highKeys): + if len(highKeys) == 20: + highKeys = {k: v for k, v in sorted(highKeys.items(), key=lambda item: (item[1],item[0]))} + if highKeys[list(highKeys)[0]] < keyCount: + del highKeys[list(highKeys)[0]] + highKeys[key] = keyCount + elif highKeys[list(highKeys)[0]] == keyCount: + if list(highKeys)[0] < key: + del highKeys[list(highKeys)[0]] + highKeys[key] = keyCount + else: + highKeys[key] = keyCount + + return highKeys + +if len(sys.argv) != 2: + msgExit() + +n = sys.argv[1] +extraCredit = False +ecPattern = re.compile(r'-{0,2}extra[-~`!@#$%^&*=+|\\;:",.?/ ]?credit', flags=re.IGNORECASE) + +if ecPattern.search(str(n)): + n = 2 + extraCredit = True + sys.stderr.flush() + sys.stderr.write('Allow time for calculations.\n') + sys.stderr.write(" They finish when '#'s reach this point-> |\n") + +elif re.search(r'\A\d+\Z',str(n)): + n = int(n) + if n < 1: + msgExit() + if n == 1: + print(1) + exit() +else: + msgExit() + +MAX = 1000000 +i = n + +while i <= MAX: + count = 0 + + while n != 1: + if extraCredit: + count += 1 + else: + print(int(n),'-> ', end='',flush=True) + + if n % 2 == 0: # EVEN + n /= 2 + else: # ODD + n = 3*n + 1 + + if extraCredit: + high = checkHighKeys(i, count, high) + i += 1 + n = i + + if n % 20000 == 0: + sys.stderr.write('#') + sys.stderr.flush() + else: + print(1) + break + +if extraCredit: + sys.stderr.write('\n\n') + high = {k: v for k, v in sorted(high.items(), key=lambda item: (item[1],item[0]),reverse=True)} + for thisKey in high.keys(): + print('Starting number:', thisKey, 'with sequence length:', high[thisKey]) |
