Skip to content

Commit c52f71f

Browse files
committed
built-in sequence type exercise
1 parent 8da9796 commit c52f71f

File tree

11 files changed

+181
-0
lines changed

11 files changed

+181
-0
lines changed

.DS_Store

8 KB
Binary file not shown.

general_problems/.DS_Store

6 KB
Binary file not shown.
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
def combinations(s):
2+
if len(s) < 2:
3+
return s
4+
res = []
5+
for i, c in enumerate(s):
6+
res.append(c)
7+
for j in combinations(s[:i] + s[i+1:]):
8+
res.append(c + j)
9+
return res
10+
11+
if __name__ == '__main__':
12+
result = combinations("abc")
13+
print(result)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
def is_palindrome(s):
2+
l = s.split(' ')
3+
s2 = ''.join(l)
4+
return s2 == s2[::-1]
5+
6+
def is_palindrome2(s):
7+
l = len(s)
8+
f, b = 0, l-1
9+
while f < l // 2:
10+
while s[f] == ' ':
11+
f+=1
12+
while s[b] == ' ':
13+
b-=1
14+
if s[f] != s[b]:
15+
return False
16+
f+=1
17+
b-=1
18+
return True
19+
20+
def is_palindrome3(s):
21+
s = s.strip()
22+
if len(s) < 2:
23+
return True
24+
if s[0] == s[-1]:
25+
return is_palindrome(s[1:-1])
26+
else:
27+
return False
28+
29+
if __name__ == '__main__':
30+
str1 = "subi no onibus"
31+
str2 = ""
32+
str3 = "hello"
33+
print(is_palindrome(str1))
34+
print(is_palindrome(str2))
35+
print(is_palindrome(str3))
36+
print(is_palindrome2(str1))
37+
print(is_palindrome2(str2))
38+
print(is_palindrome2(str3))
39+
print(is_palindrome3(str1))
40+
print(is_palindrome3(str2))
41+
print(is_palindrome3(str3))
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def perm(s):
2+
if len(s) < 2:
3+
return s
4+
res = []
5+
for i, c in enumerate(s):
6+
for cc in perm(s[i+1:] + s[:i]):
7+
res.append(c + cc)
8+
return res
9+
10+
if __name__ == '__main__':
11+
result = perm("123")
12+
print(result)
Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
def revert(s):
2+
if s:
3+
s = s[-1] + revert(s[:-1])
4+
return s
5+
6+
def revert2(string):
7+
return string[::-1]
8+
9+
if __name__ == '__main__':
10+
str1 = "Hello World!"
11+
str2 = revert(str1)
12+
str3 = revert2(str1)
13+
print(str2)
14+
print(str3)
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
def reverser(string1, p1=0, p2=None):
2+
if len(string1) < 2:
3+
return string1
4+
p2 = p2 or len(string1)-1
5+
while p1 < p2:
6+
string1[p1], string1[p2] = string1[p2], string1[p1]
7+
p1 += 1
8+
p2 -= 1
9+
10+
def reversing_words_setence_logic(string1):
11+
# 먼저, 문장 전체를 반전한다.
12+
reverser(string1)
13+
# print(string1)
14+
p = 0
15+
start = 0
16+
while p < len(string1):
17+
if string1[p] == u"\u0020":
18+
# 단어를 다시 반전한다(단어를 원위치로 돌려놓는다).
19+
reverser(string1, start, p-1)
20+
# print(string1)
21+
start = p+1
22+
p += 1
23+
# 마지막 단어를 반전한다(단어를 원위치로 돌려놓는다).
24+
reverser(string1, start, p-1)
25+
# print(string1)
26+
return ''.join(string1)
27+
28+
29+
if __name__ == '__main__':
30+
str1 = "Buffy is a Vampire Slayer"
31+
str2 = reversing_words_setence_logic(list(str1))
32+
print(str2)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
def reverse_words_brute(string):
2+
word, sentence = [], []
3+
for character in string:
4+
if character != ' ':
5+
word.append(character)
6+
else:
7+
# 조건문에서 빈 리스트는 False다. 여러 공백이 있는 경우, 조건문을 건너뛴다.
8+
if word:
9+
sentence.append(''.join(word))
10+
word = []
11+
12+
# 마지막 단어가 있다면, 문장에 추가한다.
13+
if word != '':
14+
sentence.append(''.join(word))
15+
sentence.reverse()
16+
return ' '.join(sentence)
17+
18+
19+
if __name__ == '__main__':
20+
str1 = "Buffy is a Vampire Slayer"
21+
str2 = reverse_words_brute(str1)
22+
print(str2)
Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
def reversing_words_slice(word):
2+
new_word = []
3+
words = word.split(' ')
4+
for word in words[::-1]:
5+
new_word.append(word)
6+
return ' '.join(new_word)
7+
8+
9+
if __name__ == '__main__':
10+
str1 = "Buffy is a Vampire Slayer"
11+
str2 = reversing_words(str1)
12+
print(str2)
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
def reversing_words(str1):
2+
words = str1.split(' ')
3+
rev_set = " ".join(reversed(words))
4+
return rev_set
5+
6+
def reversing_words2(str1):
7+
words = str1.split(' ')
8+
words.reverse()
9+
return ' '.join(words)
10+
11+
12+
if __name__ == '__main__':
13+
str1 = "Buffy is a Vampire Slayer"
14+
str2 = reversing_words(str1)
15+
str3 = reversing_words2(str1)
16+
print(str2)
17+
print(str3)

0 commit comments

Comments
 (0)