Skip to content

Commit 4c01223

Browse files
Aadit KamatMadhavBahl
authored andcommitted
Update personal solution to Day 10 problem in Python (#122)
* Add @aaditkamat as a contributor * Add Ruby code for Day 1: FizzBuzz problem * Add Ruby code for Day 2: String reverse problem * Update README.md for Day 2 * Modify Ruby code and README * Add condition for nil and wrong type edge cases * Add a seperate Ruby source code file for palindrome * Modify code for reverse.rb * Add seperate palindrome and reverse code sections in README * Update gitignore * Refactor palindrome.rb and rename heading in README * Add solution for Day 3: Hamming Problem * Add condition for strings of unequal lengths * Update README * Change project name and owner in.all-contributorsrc * Remove merge conflict lines * Add @shivank86 as a contributor * Add C++ files for different patterns * Add author and date comments at the top of C++ files * Update README.md * Add solution for Day 6 Problem in Python * Update README * Refactor code files * Modify string representation of output in python files * Add Ruby solutions for Day 6 problem * Update README for Ruby code * Add first version of solutions for Day 7 problem in C++, Java & Ruby * Modify solutions * Update Day 7 README * Remove merge conflicts from CONTRIBUTORS.md * Add back removed lines in CONTRIBUTORS.md * Add code sections contributed by @imkaka to day 6 README * Update README.md * Add C++ solution * Add Day 8 solution in C++ * Add Day 8 solution in Java * Add Day 8 solution in Ruby * Add Day 8 solution in Python * Add credits at the top of the code * Update README * Update C++ implementation * Update Python implementation * Add solution for Day 10: String Permutation Problem in Python Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Update Day 10 README Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Change heading in README and remove empty directory in day 9 Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Update README.md * Update day 10 Python solution Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com> * Add the updated solution to day 10 README Signed-off-by: Aadit Rahul Kamat <aadit.k12@gmail.com>
1 parent 5ee17fe commit 4c01223

File tree

2 files changed

+15
-16
lines changed

2 files changed

+15
-16
lines changed

day10/Python/permutations.py

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -5,25 +5,25 @@
55

66
def permutations(string):
77
if (len(string) <= 1):
8-
return [string]
9-
lst = []
8+
return {string}
9+
permutation_set = set()
1010
for i in range(len(string)):
1111
substring = ''
1212
for j in range(len(string)):
1313
if j != i:
1414
substring += string[j]
15-
lst.extend(list(set(map(lambda x: string[i] + x, permutations(substring)))))
16-
return lst
15+
permutation_set |= set(map(lambda x: string[i] + x, permutations(substring)))
16+
return permutation_set
1717

1818

19-
def printList(string_list):
20-
for string in string_list:
19+
def printSet(string_set):
20+
for string in string_set:
2121
print(string)
2222

2323
def main():
2424
print('Enter a string: ')
2525
string = input()
2626
print(f'The permutations of {string} are:')
27-
printList(permutations(string))
27+
printSet(permutations(string))
2828

2929
main()

day10/README.md

Lines changed: 8 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -122,34 +122,33 @@ permut('123');
122122

123123
### [Solution1](./Python/permutations.py)
124124
```python
125-
126125
'''
127126
@author: aaditkamat
128127
@date: 02/01/2019
129128
'''
130129

131130
def permutations(string):
132131
if (len(string) <= 1):
133-
return [string]
134-
lst = []
132+
return {string}
133+
permutation_set = set()
135134
for i in range(len(string)):
136135
substring = ''
137136
for j in range(len(string)):
138137
if j != i:
139138
substring += string[j]
140-
lst.extend(list(set(map(lambda x: string[i] + x, permutations(substring)))))
141-
return lst
139+
permutation_set |= set(map(lambda x: string[i] + x, permutations(substring)))
140+
return permutation_set
142141

143142

144-
def printList(string_list):
145-
for string in string_list:
143+
def printSet(string_set):
144+
for string in string_set:
146145
print(string)
147146

148147
def main():
149148
print('Enter a string: ')
150149
string = input()
151-
print('The permutations of {string} are:')
152-
printList(permutations(string))
150+
print(f'The permutations of {string} are:')
151+
printSet(permutations(string))
153152

154153
main()
155154
```

0 commit comments

Comments
 (0)