A. Round Up The Mean … Round up using math.ceil
from math import ceil print(ceil(sum(list(map(int, input().split())))/2))
B. Two Anagrams … make the ascending ordered string from s, and make the descending ordered string from t. Compare both of them. The for loop needs to have three conditions as first condition checks if s-string is shorter than t-string, the second condition checks if t-string is shorter than s-string OR the pointing character in t-string is bigger than the same index character in s-string. We cannot include s_chr < t_chr in the first condition as it will cause Error “None cannot be compared with int” if t-string is shorter than s-string.
from itertools import zip_longest def twoAnagrams(s, t): s_str = ''.join(sorted(s)) t_str = ''.join(sorted(t, reverse=True)) if t_str.startswith(s_str): if len(s_str) < len(t_str): return True # this implies s and t is the same string else: return False # zip_longest returns None if one list is shorter than the other for s_chr,t_chr in zip_longest(s_str, t_str): if s_chr is None: return True elif t_chr is None or t_chr < s_chr: return False elif s_chr < t_chr: return True s = list(input()) t = list(input()) print('Yes' if twoAnagrams(s,t) else 'No')
C. Good Sequence … I used collections.Counter function again. It makes this tasks very easy. Once dictionary is populated, I just need to go through all the keys and compare the key and value.
from collections import Counter n = int(input()) a = list(map(int, input().split())) c = Counter(a) result = 0 for num in c.keys(): if num < c[num]: result += c[num] - num elif num > c[num]: # numbers of appearance is less than the number. needs to delete all result += c[num] print(result)