Files
wehub-resource-sync dde272c4b8
i18n - Build Validation / Validate i18n Builds (24) (push) Has been cancelled
CI - Node.js / Lint (24) (push) Has been cancelled
CI - Node.js / Build (24) (push) Has been cancelled
CI - Node.js / Test (24) (push) Has been cancelled
CI - Node.js / Test - Upcoming Changes (24) (push) Has been cancelled
CI - Node.js / Test - i18n (italian, 24) (push) Has been cancelled
CI - Node.js / Test - i18n (portuguese, 24) (push) Has been cancelled
CD - Docker - GHCR Images / Build and Push Images (push) Has been cancelled
chore: import upstream snapshot with attribution
2026-07-13 11:55:53 +08:00

3.1 KiB

id, title, challengeType, dashedName
id title challengeType dashedName
69b1028d6e265413d0198a2e Challenge 235: Capitalized Fibonacci 29 challenge-235

--description--

Given a string, return a new string where each letter is capitalized if its index is a Fibonacci number, and lowercased otherwise.

The Fibonacci sequence is a series of numbers where each number is the sum of the two preceding ones. The first 10 numbers in the sequence are 0, 1, 1, 2, 3, 5, 8, 13, 21, 34.

  • The first character is at index 0.
  • If the index of non-letter characters is a Fibonacci number, leave it unchanged.

--hints--

capitalize_fibonacci("hello world") should return "HELLo woRld".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("hello world"), "HELLo woRld")`)
}})

capitalize_fibonacci("HELLO WORLD") should return "HELLo woRld".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("HELLO WORLD"), "HELLo woRld")`)
}})

capitalize_fibonacci("hello, world!") should return "HELLo, wOrld!".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("hello, world!"), "HELLo, wOrld!")`)
}})

capitalize_fibonacci("The quick brown fox jumped over the lazy dog.") should return "THE qUicK broWn fox jUmped over thE lazy dog.".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("The quick brown fox jumped over the lazy dog."), "THE qUicK broWn fox jUmped over thE lazy dog.")`)
}})

capitalize_fibonacci("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pulvinar ex nibh, vel ullamcorper ligula egestas quis. Integer tincidunt fringilla accumsan. Integer et metus placerat, gravida felis at, pellentesque nisl.") should return "LOREm ipSum dOlor sit amet, consecTetur adipiscing elit. proin pulvinar ex nibh, vel ullaMcorper ligula egestas quis. integer tincidunt fringillA accumsan. integer et metus placerat, gravida felis at, pellentesque nisl.".

({test: () => { runPython(`
from unittest import TestCase
TestCase().assertEqual(capitalize_fibonacci("Lorem ipsum dolor sit amet, consectetur adipiscing elit. Proin pulvinar ex nibh, vel ullamcorper ligula egestas quis. Integer tincidunt fringilla accumsan. Integer et metus placerat, gravida felis at, pellentesque nisl."), "LOREm ipSum dOlor sit amet, consecTetur adipiscing elit. proin pulvinar ex nibh, vel ullaMcorper ligula egestas quis. integer tincidunt fringillA accumsan. integer et metus placerat, gravida felis at, pellentesque nisl.")`)
}})

--seed--

--seed-contents--

def capitalize_fibonacci(s):

    return s

--solutions--

def capitalize_fibonacci(s):
    fibs = {0, 1}
    a, b = 0, 1
    while b < len(s):
        a, b = b, a + b
        fibs.add(b)

    result = []
    for i, char in enumerate(s):
        if not char.isalpha():
            result.append(char)
        elif i in fibs:
            result.append(char.upper())
        else:
            result.append(char.lower())

    return "".join(result)