diff options
| author | User Person <usermanperson+github@gmail.com> | 2020-04-05 12:26:57 -0400 |
|---|---|---|
| committer | User Person <usermanperson+github@gmail.com> | 2020-04-05 12:26:57 -0400 |
| commit | 9f00beded9b8b4f82108267812063ac17b6e5eed (patch) | |
| tree | c94a7fae2574d2bec700a89b80304c7e55149dfd /challenge-054/user-person/python | |
| parent | 8c5b3672a4de55da80fad8423b2341350cff4c2b (diff) | |
| download | perlweeklychallenge-club-9f00beded9b8b4f82108267812063ac17b6e5eed.tar.gz perlweeklychallenge-club-9f00beded9b8b4f82108267812063ac17b6e5eed.tar.bz2 perlweeklychallenge-club-9f00beded9b8b4f82108267812063ac17b6e5eed.zip | |
User-person's solutions for challenge 54.
Diffstat (limited to 'challenge-054/user-person/python')
| -rwxr-xr-x | challenge-054/user-person/python/ch-1.py | 67 | ||||
| -rwxr-xr-x | challenge-054/user-person/python/ch-2.py | 115 |
2 files changed, 182 insertions, 0 deletions
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..e95fc7aa7e --- /dev/null +++ b/challenge-054/user-person/python/ch-1.py @@ -0,0 +1,67 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-1.py # +# Wed Apr 1 04:40:14 2020 | 1585730414 # +# # +# 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..fe8dc75fdd --- /dev/null +++ b/challenge-054/user-person/python/ch-2.py @@ -0,0 +1,115 @@ +#!/usr/bin/env python3 + +########################################################################### +# script name: ch-2.py # +# Wed Apr 1 13:45:26 2020 | 1585763126 # +# # +# 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]) |
