from collections import deque # STACKS def push(stack, element): stack.append(element) def pop(stack): return stack.pop() def top(stack): return stack[-1] # Postfix calculator def evaluate_postfix(expr): stack = [] for token in expr.split(): if token.isnumeric(): push(stack, int(token)) else: b = pop(stack) a = pop(stack) if token == '*': push(stack, a * b) elif token == "/": push(stack, a // b) elif token == '+': push(stack, a + b) else: push(stack, a - b) return top(stack) print(evaluate_postfix("8 7 * 6 5 + 2 * +")) # 78 print(evaluate_postfix("6 5 -")) # 1 print(evaluate_postfix("15 7 1 1 + - / 3 * 2 1 1 + + -")) # 5 print() # DEQUE # Hot potato game def hot_potato(namelist, k): count = 0 print("The players are ", end="") for name in namelist[:-1]: print(name, end=", ") print(namelist[-1]) q = deque(namelist) while len(q) > 1: current = q.popleft() count += 1 if count%k != 0: q.append(current) else: print(current + " is a hot potato") print("Remaining players are ", end="") for name in list(q)[:-1]: print(name, end=", ") print(list(q)[-1]) print("The winner is " + q[0]) hot_potato(["Bill", "David", "Susan", "Jane", "Kent", "Brad", "Sam"], 7) print() # Dictionary # Dictionary representing the morse code chart MORSE_CODE_DICT = { 'A':'.-', 'B':'-...', 'C':'-.-.', 'D':'-..', 'E':'.', 'F':'..-.', 'G':'--.', 'H':'....', 'I':'..', 'J':'.---', 'K':'-.-', 'L':'.-..', 'M':'--', 'N':'-.', 'O':'---', 'P':'.--.', 'Q':'--.-', 'R':'.-.', 'S':'...', 'T':'-', 'U':'..-', 'V':'...-', 'W':'.--', 'X':'-..-', 'Y':'-.--', 'Z':'--..', '1':'.----', '2':'..---', '3':'...--', '4':'....-', '5':'.....', '6':'-....', '7':'--...', '8':'---..', '9':'----.', '0':'-----', ',':'--..--', '.':'.-.-.-', '?':'..--..', '/':'-..-.', '-':'-....-', '(':'-.--.', ')':'-.--.-'} MORSE_DECODE_DICT = {value:key for key, value in MORSE_CODE_DICT.items()} # Encoder to Morse def text2morse(message): cipher = '' for letter in message: if letter.upper() in MORSE_CODE_DICT: cipher += MORSE_CODE_DICT[letter.upper()] + ' ' elif letter == " ": cipher += ' ' return cipher # Decoder from Morse def morse2text(message): text = [] for word in message.split(" "): text.append("".join([MORSE_DECODE_DICT[code] for code in word.split()])) return " ".join(text) send = "Hello World, it's a nice day." print(send) msg = text2morse(send) print(msg) print(morse2text(msg)) print() dummy = """Monty Python and Monty Python all over here.""" # Letter frequency analysis of a text def freq_analysis(text): letters = {} for letter in text.lower(): letters[letter] = letters.get(letter, 0) + 1 for k in sorted(letters, key=letters.__getitem__, reverse=True): print(str(k) + ": " + str(letters[k])) freq_analysis(dummy) print() # Word frequency analysis of a text def word_freq(text): text = "".join(char for char in text if char.isalpha() or char.isspace()) word_list = text.lower().split() freq = {} for word in word_list: freq[word] = freq.get(word, 0) + 1 return freq def output_word_freq(text): freq = word_freq(text) my_list = [(freq[word], word) for word in freq] my_list.sort(reverse=True) print("Word frequencies, sorted from the most frequent:") for count, word in my_list: print(count, word) output_word_freq(dummy) print() # Sets # Checking whether the elements of tem are unique def unique_check(temp): return len(temp) == len(set(temp)) print(unique_check([1, 5, 6, 5, 4, 9])) # False print(unique_check([1, 5, 6, 3, 9])) # True print() # Checking validity of a sudoku line def sudoku_line_check(line): return set(line) == set(range(1, 10)) print(sudoku_line_check([1, 2, 8, 9, 3, 5, 6, 7, 4])) # True print(sudoku_line_check([1, 2, 8, 9, 3, 5, 7, 4])) # False print(sudoku_line_check([1, 1, 2, 8, 9, 3, 5, 7, 4])) # False print(sudoku_line_check([0, 1, 2, 3, 4, 5, 6, 7, 8])) # False