From bfd7d38a64d2c0973a67b8e20147278f80d1abc9 Mon Sep 17 00:00:00 2001 From: SOURAB-BAPPA <62734958+SOURAB-BAPPA@users.noreply.github.com> Date: Sat, 3 Oct 2020 16:19:46 +0530 Subject: [PATCH 1/2] permutation sequence added --- LeetCode/0060 - Permutation_Sequence.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) create mode 100644 LeetCode/0060 - Permutation_Sequence.py diff --git a/LeetCode/0060 - Permutation_Sequence.py b/LeetCode/0060 - Permutation_Sequence.py new file mode 100644 index 0000000..06f45ba --- /dev/null +++ b/LeetCode/0060 - Permutation_Sequence.py @@ -0,0 +1,23 @@ +class Solution: + data = [] + + def getPermutation(self, n: int, k: int) -> str: + string = [] + for i in range(1, n+1): + string.append(str(i)) + self.permutations(string, 0, len(string)) + return self.data[k-1] + + def permutations(self, string, val, length): + if val == length - 1: + self.data.append("".join(string)) + else: + for i in range(val, length): + string[val], string[i] = string[i], string[val] + self.permutations(string, val + 1, length) + string[val], string[i] = string[i], string[val] + + +user = Solution() +output = user.getPermutation(int(input("Input: n=")), int(input("k="))) +print("\nOutput: {}".format(output)) From 069354d31461ea41766cb40c02b71ab13641ebf3 Mon Sep 17 00:00:00 2001 From: Sourab Maity <62734958+SOURAB-BAPPA@users.noreply.github.com> Date: Mon, 5 Oct 2020 08:51:26 +0530 Subject: [PATCH 2/2] Update 0060 - Permutation_Sequence.py after LeetCode success --- LeetCode/0060 - Permutation_Sequence.py | 23 ++++++++--------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/LeetCode/0060 - Permutation_Sequence.py b/LeetCode/0060 - Permutation_Sequence.py index 06f45ba..d28e294 100644 --- a/LeetCode/0060 - Permutation_Sequence.py +++ b/LeetCode/0060 - Permutation_Sequence.py @@ -1,23 +1,16 @@ +from itertools import permutations class Solution: data = [] def getPermutation(self, n: int, k: int) -> str: + self.data = [] string = [] for i in range(1, n+1): string.append(str(i)) - self.permutations(string, 0, len(string)) - return self.data[k-1] - - def permutations(self, string, val, length): - if val == length - 1: - self.data.append("".join(string)) - else: - for i in range(val, length): - string[val], string[i] = string[i], string[val] - self.permutations(string, val + 1, length) - string[val], string[i] = string[i], string[val] - + string = list(permutations(string)) + temp = string[k-1] + string = [] + string.append("".join(temp)) + return string[0] -user = Solution() -output = user.getPermutation(int(input("Input: n=")), int(input("k="))) -print("\nOutput: {}".format(output)) +