diff options
| author | Dave Jacoby <jacoby.david@gmail.com> | 2021-10-11 19:43:55 -0400 |
|---|---|---|
| committer | Dave Jacoby <jacoby.david@gmail.com> | 2021-10-11 19:43:55 -0400 |
| commit | 38cc632e38adbe3eb4a0291a2e6f0068f27b963d (patch) | |
| tree | 12028ac3fd75e81825e3ce2645f0b4198af3e268 | |
| parent | 159c6c3ea8cb8a319ffcc11bb6f48074c2deda9a (diff) | |
| download | perlweeklychallenge-club-38cc632e38adbe3eb4a0291a2e6f0068f27b963d.tar.gz perlweeklychallenge-club-38cc632e38adbe3eb4a0291a2e6f0068f27b963d.tar.bz2 perlweeklychallenge-club-38cc632e38adbe3eb4a0291a2e6f0068f27b963d.zip | |
A commented solution in Python
| -rw-r--r-- | challenge-134/dave-jacoby/python/ch-1.py | 59 |
1 files changed, 59 insertions, 0 deletions
diff --git a/challenge-134/dave-jacoby/python/ch-1.py b/challenge-134/dave-jacoby/python/ch-1.py new file mode 100644 index 0000000000..9dbfd418f0 --- /dev/null +++ b/challenge-134/dave-jacoby/python/ch-1.py @@ -0,0 +1,59 @@ +#!/usr/bin/env python3 + +# would want to pass an array reference, but +# haven't found how you can do that in Python. +# It might not be possible. +output=[] + +def main(): + pandigital() + +def pandigital(): + # because we see 0123 as 123, we cannot start + # with 0, so we force the issue by starting with + # one + state=["1"] + _pandigital(state) + for o in output: + print( o ) + +def _pandigital(state): + # we're dumping to a global array + # so we can know when we've hit five + # and can just bail + if len(output)>4: + return + # if length is 10, we've used all the + # digits and can convert the array of + # digits into a number and append that + # to output + if len(state)==10: + pandigital = int("".join(list(state))) + output.append(pandigital) + return + # dicts are like hashes and useful to + # keep you from using the same thing twice + # (asterisk) + mydict = {} + numbers = [] + # thing is, join wants a string, so we have + # to always treat the digits as strings, and + # so we must cast to string before adding to + # the dictionary + for n in state: + mydict[str(n)] = 1 + # and also here, so we know that once we've + # used 0, we don't use it again + for n in range(10): + if str(n) not in mydict: + numbers.append(str(n)) + # and here, we copy the current state array, + # append what numbers we have, and recurse + for n in numbers: + newstate = state.copy() + newstate.append(n) + _pandigital(newstate) + return + +if __name__ == '__main__': + main()
\ No newline at end of file |
