나는 이렇게 학습한다/Algorithm & SQL

1107. I love you, a little , a lot, passionately ... not at all

daco2020 2022. 11. 7. 22:06
반응형

Who remembers back to their time in the schoolyard, when girls would take a flower and tear its petals, saying each of the following phrases each time a petal was torn:

  1. "I love you"
  2. "a little"
  3. "a lot"
  4. "passionately"
  5. "madly"
  6. "not at all"

If there are more than 6 petals, you start over with "I love you" for 7 petals, "a little" for 8 petals and so on.

When the last petal was torn there were cries of excitement, dreams, surging thoughts and emotions.

Your goal in this kata is to determine which phrase the girls would say at the last petal for a flower of a given number of petals. The number of petals is always greater than 0.



Solution:

LOVE = {
    0: "not at all",
    1: "I love you",
    2: "a little",
    3: "a lot",
    4: "passionately",
    5: "madly"
}

def how_much_i_love_you(nb_petals: int, love: str = LOVE) -> str:
    num = nb_petals % 6
    import functools

    @functools.lru_cache(maxsize=6)
    def calc_love(num: int) -> str:
        return love[num]

    return calc_love(num)


반응형

'나는 이렇게 학습한다 > Algorithm & SQL' 카테고리의 다른 글

1109. Sentence Smash  (0) 2022.11.09
1108. JavaScript Array Filter  (0) 2022.11.09
1106. Grasshopper - Messi Goals  (0) 2022.11.07
1105. Remove exclamation marks  (0) 2022.11.05
1104. Sum Mixed Array  (0) 2022.11.04