첫번째 문제는 6kyu 난이도 문제로
Examples: spinWords( "Hey fellow warriors" ) => returns "Hey wollef sroirraw" spinWords( "This is a test") => returns "This is a test" spinWords( "This is another test" )=> returns "This is rehtona test"
즉 문자열의 길이가 5글자 이상이면 반대로 출력 아닐시 그대로 출력하는 비교적 간단한 문제였다.
def spin_words(sentence): empty = [] senten = sentence.split(" ") for i in senten: if len(i)>=5: empty.append(i[::-1]) else: empty.append(i) return " ".join(empty)
띄어쓰는 부분에서 문자열을 나눈 뒤 만약 길이가 5 이상이면 [::-1]을 통해서 뒤 문자부터 출력하고 아닐시 그냥 출력하는 코드를 작성했다.
두번째 문제는 5kyu 난이도 문제로
For example, if given the input 'stress', the function should return 't', since the letter t only occurs once in the string, and occurs first in the string.
As an added challenge, upper- and lowercase letters are considered the same character, but the function should return the correct case for the initial letter. For example, the input 'sTreSS' should return 'T'.
즉 문자열에서 한번만 반복되는 요소를 출력하는 것이다. sTreSS와 같이 대문자와 소문자가 섞일 경우 같은 것으로 간주하고 대문자 T를 출력한다.
def first_non_repeating_letter(string): emptylist = [] for i in string: emptylist.append(i.lower()) for i in range(len(emptylist)): if emptylist.count(emptylist[i]) == 1: return string[i] return ""
먼저 소문자로 바꾼 후 새로운 리스트에 담고, count()를 활용하여 count한 숫자가 1인 경우 그 문자를 출력하는 방법을 사용했다.
세번째 문제는
In this example you have to validate if a user input string is alphanumeric. The given string is not nil/null/NULL/None, so you don't have to check that.
The string has the following conditions to be alphanumeric:
At least one character ("" is not valid)
Allowed characters are uppercase / lowercase latin letters and digits from 0 to 9
No whitespaces / underscore
비밀번호에 소문자, 대문자, 0~9사이 숫자가 꼭 들어가야 True 그 외에는 False를 리턴해야한다.
import re def alphanumeric(string): return bool(re.match(r'^[A-Za-z0-9]+$', string))
re모듈을 사용해서 match를 사용해서 문자열의 처음부터 정규식과 일치하는지 확인한다.
네번째 문제는 6kyu 난이도 문제로
"abcde" -> 0 # no characters repeats more than once
"aabbcde" -> 2 # 'a' and 'b'
"aabBcde" -> 2 # 'a' occurs twice and 'b' twice (b
and B
)
"indivisibility" -> 1 # 'i' occurs six times
"Indivisibilities" -> 2 # 'i' occurs seven times and 's' occurs twice
"aA11" -> 2 # 'a' and '1'
"ABBA" -> 2 # 'A' and 'B' each occur twice
문자 중 2개 이상 겹치는게 있으면 그 숫자를 나타내는 것이다.
def duplicate_count(text): num = 0 count={} emptylist = [] for i in range(len(text)): emptylist.append(text[i].lower()) for i in emptylist: try: count[i] += 1 except: count[i]=1 for i in count: if count[i] >=2: num +=1 return num
소문자로 바꾼 후 list에 넣어주고 중복요소를 카운팅하는 for문을 작성 후
키의 벨류가 2가 넘을 경우 하나씩 더해줬다.