본문 바로가기

프로그래밍 문제53

[CheckIO] pawn-brotherhood Problems: py.checkio.org/en/mission/pawn-brotherhood/ My solution: def safe_pawns(pawns: set) -> int: position_data = list(pawns) def check_safe(position): x,y = list(position) defence_x1 = chr(ord(x) - 1) defence_x2 = chr(ord(x) + 1) defence_y = int(y) - 1 defence_pos1 = defence_x1 + str(defence_y) defence_pos2 = defence_x2 + str(defence_y) if defence_pos1 in pawns or defence_pos2 in pawns: ret.. 2021. 1. 2.
[CheckIO] sort array by element frequency Problems: py.checkio.org/en/mission/sort-array-by-element-frequency/ My Solution: def frequency_sort(items): # your code here count_value = dict.fromkeys(items) count_list = [items.count(x) for x in count_value] dict_list = dict(zip(count_value, count_list)) sort_list = dict(sorted(dict_list.items(), key = (lambda x: x[1]), reverse=True)) new_list = [] for x in sort_list: new_list.extend([x for .. 2020. 12. 30.
[CheckIO] Second Index Problem: py.checkio.org/en/mission/second-index/ My Solution def second_index(text: str, symbol: str) -> [int, None]: """ returns the second index of a symbol in a given text """ # your code here m = 0 for n in range(len(list(text))): if list(text)[n] == symbol: if m == 1: return n else: m = m + 1 return None if __name__ == '__main__': print('Example:') print(second_index("sims", "s")) # These ".. 2020. 12. 24.
[CheckIO] Popular words Problems: py.checkio.org/en/mission/popular-words/ My solution def popular_words(text: str, words: list) -> dict: # your code here counts = [] for f_word in words: counting_numbers = 0 for match_words in (text.lower()).split(): if match_words == f_word: counting_numbers = counting_numbers + 1 counts.append(counting_numbers) return dict(zip(words, counts)) if __name__ == '__main__': print("Exampl.. 2020. 12. 23.
[CheckIO] non unique elements Problem: py.checkio.org/en/mission/non-unique-elements/ My solution #Your optional code here #You can import some modules or create additional functions def checkio(data: list) -> list: #Your code here #It's main function. Don't remove this function #It's used for auto-testing and must return a result for check. #replace this for solution for number in data[::-1]: if data.count(number) == 1: del.. 2020. 12. 22.
CheckIO - Between Markers Problem: py.checkio.org/en/mission/between-markers/ My solution: def between_markers(text: str, begin: str, end: str) -> str: """ returns substring between two given markers """ # your code here if begin in text and end in text: # condition 1 & condition 5 # condition 1 if text.index(begin) < text.index(end): return text[text.index(begin)+len(begin):text.index(end)] # condition 5 else: return ''.. 2020. 12. 21.
CheckIO - Bigger price Problem: py.checkio.org/en/mission/bigger-price/ My Solution: def bigger_price(limit: int, data: list) -> list: """ TOP most expensive goods """ # your code here return sorted(data, key = (lambda x: x["price"]), reverse = True)[:limit] if __name__ == '__main__': from pprint import pprint print('Example:') pprint(bigger_price(2, [ {"name": "bread", "price": 100}, {"name": "wine", "price": 138}, {.. 2020. 12. 20.
CheckIO - Count digits Problem: py.checkio.org/en/mission/count-digits/ My Solution: def count_digits(text: str) -> int: # your code here count = 0 for n in list(text): if n.isdigit(): count = count + 1 return count if __name__ == '__main__': print("Example:") print(count_digits('hi')) # These "asserts" are used for self-checking and not for an auto-testing assert count_digits('hi') == 0 assert count_digits('who is 1s.. 2020. 12. 14.
CheckIO - Days between Problems: py.checkio.org/en/mission/days-diff/ My solutions from datetime import datetime, timedelta, timezone def days_diff(a, b): # your code here return abs((datetime(a[0],a[1],a[2]) - datetime(b[0], b[1], b[2])).days) if __name__ == '__main__': print("Example:") print(days_diff((1982, 4, 19), (1982, 4, 22))) # These "asserts" are used for self-checking and not for an auto-testing assert days.. 2020. 12. 12.