Permutations of a list of length n in python -


so started learning python , thought exercise try write little script see if could. turns out couldn't right , have left it, got little determined , have vendetta against particular function.

i'm wanting code take raw input of given number , generate possible permutations of list of numbers it. eg. if input "5" generate permutations of length 5 [1, 2, 3, 4, 5].

what tried follows:

from itertools import permutations math import factorial  n = raw_input("input number generate permutation list")  factorial_func = factorial(n)  print "there %s permutations follows:" %(factorial_func)  print list(permutations([1:n], n)) 

i know faulty line line 10 because of [1:n] part , don't know how make list 1 n , put permutation function. (i hoping going [1:n] generate list 1 n in same way can use access parts of list b list_name[a:b] seems isn't case)

sorry if seems trivial or obvious mistake, started trying learn python few days ago.

yeah bad line. doing [1:n] called slicing , unrelated coming ranges. use range function instead:

range(1, n+1) 

(note n+1. range not inclusive of end number).

you aren't taking input properly. raw_input give string, , want int do:

n = int(raw_input("input number generate permutation list")) 

Comments

Popular posts from this blog

twig - Using Twigbridge in a Laravel 5.1 Package -

jdbc - Not able to establish database connection in eclipse -

firemonkey - How do I make a beep sound in Android using Delphi and the API? -