Algorithm

์ตœ๋นˆ๊ฐ’ ์ฐพ๊ธฐ

challnum 2022. 8. 5. 22:48
๐Ÿ’ก ์•ŒํŒŒ๋ฒณ์„ ์ž…๋ ฅ ๋ฐ›์•˜์„ ๋•Œ ํ•ด๋‹น ์•ŒํŒŒ๋ฒณ์˜ ์ตœ๋นˆ ๊ฐ’์„ ์ฐพ๊ธฐ
input = "hello my name is sparta"

def find_max_occurred_alphabet(input):
    alphabet_array = ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m", "n", "o", "p", "q", "r", "s",
                      "t", "u", "v", "x", "y", "z"]
    max_occurrence = 0
    max_alphabet = alphabet_array[0]

    for alphabet in alphabet_array:
        occurrence = 0
        for char in input:
            if char == alphabet:
                occurrence += 1

        if occurrence > max_occurrence:
            max_alphabet = alphabet
            max_occurrence = occurrence

    return max_alphabet

result = find_max_occurred_alphabet(input)
print(result)
input = "hello my name is sparta"

def find_max_occurred_alphabet(string):
    alphabet_occurrence_array = [0] * 26

    for char in string:
        if not char.isalpha():
            continue
        arr_index = ord(char) - ord('a')
        alphabet_occurrence_array[arr_index] += 1

    max_occurrence = 0
    max_alphabay_index = 0
    for index in range(len(alphabet_occurrence_array)):
        alphabet_occurrence = alphabet_occurrence_array[index]
        if alphabet_occurrence > max_occurrence:
            max_alphabay_index = index
            max_occurrence = alphabet_occurrence
    return(chr(max_alphabay_index + 97))

result = find_max_occurred_alphabet(input)
print(result)