본문 바로가기
728x90

프로그래밍 문제53

CheckIO - first word Problem: py.checkio.org/en/mission/first-word/ My Solution; import re def first_word(text: str) -> str: """ returns the first word in a given text. """ # your code here words = re.sub('\.', " ", text) words = re.sub(",", " ", words) return words.split()[0] if __name__ == '__main__': print("Example:") print(first_word("Hello world")) # These "asserts" are used for self-checking and not for an aut.. 2020. 12. 11.
CheckIO - three words Problem: py.checkio.org/en/mission/three-words/ My solution: def checkio(words: str) -> bool: n = 0 for word in words.split(): if word.isalpha() == True: n = n + 1 else: n = 0 if n == 3: break if n == 3: return True else: return False #These "asserts" using only for self-checking and not necessary for auto-testing if __name__ == '__main__': print('Example:') print(checkio("Hello World hello")) a.. 2020. 12. 11.
[CheckIO] Right to Left Problems: py.checkio.org/en/mission/right-to-left/ My solution: import re def left_join(phrases: tuple) -> str: """ Join strings and replace "right" to "left" """ dummy = "" for string in phrases: dummy = dummy + re.sub('right', 'left', string) + "," return dummy[:-1] if __name__ == '__main__': print('Example:') print(left_join(("left", "right", "left", "stop"))) #These "asserts" using only for .. 2020. 12. 9.
CheckIO - Even the last Problem: py.checkio.org/en/mission/even-last/ My solution def checkio(array: list) -> int: """ sums even-indexes elements and multiply at the last """ if 0 < len(array) 2020. 12. 8.
CheckIO-sum-numbers Problem: py.checkio.org/en/mission/sum-numbers/ My solution: def sum_numbers(text: str) -> int: # your code here summation = 0 for string in text.split(' '): if string.isdecimal(): summation = summation + int(string) return summation if __name__ == '__main__': print("Example:") print(sum_numbers('hi')) # These "asserts" are used for self-checking and not for an auto-testing assert sum_numbers('h.. 2020. 12. 7.
CheckIO - correct-sentence Problems: py.checkio.org/en/mission/correct-sentence/ My Solutions def correct_sentence(text: str) -> str: """ returns a corrected sentence which starts with a capital letter and ends with a dot. """ # your code here if not (text[-1] == '.'): text = text + '.' text = text[0].upper() + text[1:] return text if __name__ == '__main__': print("Example:") print(correct_sentence("greetings, friends")) .. 2020. 12. 6.
CheckIO - between-markers-simplified Problems: py.checkio.org/en/mission/between-markers-simplified/ My Solution def between_markers(text: str, begin: str, end: str) -> str: """ returns substring between two given markers """ # your code here return text[text.index(begin)+1:text.index(end)] if __name__ == '__main__': print('Example:') print(between_markers('What is >apple', 'apple', '', 'apple', ' 2020. 12. 5.
CheckIO - Nearest value Problem: py.checkio.org/en/mission/nearest-value/ My solution def nearest_value(values: set, one: int) -> int: # your code here n = 0 while(not (one + n in values or one - n in values)): n = n + 1 if one - n in values: return one - n else: return one + n if __name__ == '__main__': print("Example:") print(nearest_value({4, 7, 10, 11, 12, 17}, 9)) # These "asserts" are used for self-checking and n.. 2020. 12. 2.
728x90