Given a string, we’ll say that the front is the first 3 chars of the string. If the string length is less than 3, the front is whatever is there. Return a new string which is 3 copies of the front.
front3(‘Java’) → ‘JavJavJav’
front3(‘Chocolate’) → ‘ChoChoCho’
front3(‘abc’) → ‘abcabcabc’
def front3(str):if len(str) < 3:return str*3else:return (str[:3]*3)
Expected Run
front3(‘Java’) → ‘JavJavJav’ ‘JavJavJav’ OK
front3(‘Chocolate’) → ‘ChoChoCho’ ‘ChoChoCho’ OK
front3(‘abc’) → ‘abcabcabc’ ‘abcabcabc’ OK
front3(‘abcXYZ’) → ‘abcabcabc’ ‘abcabcabc’ OK
front3(‘ab’) → ‘ababab’ ‘ababab’ OK
front3(‘a’) → ‘aaa’ ‘aaa’ OK
front3(‘’) → ‘’ ‘’ OK
All Correct
def front3(str):# Figure the end of the frontfront_end = 3if len(str) < front_end:front_end = len(str)front = str[:front_end]return front + front + front # Could omit the if logic, and write simply front = str[:3]# since the slice is silent about out-of-bounds conditions.
上一篇:俄罗斯紧急情况部发布音乐厅内部画面,已被烧得面目全非 俄罗斯突发紧急事情 俄罗斯发生的紧急情况
下一篇:世预赛-日本1-0朝鲜三战全胜 朝鲜进球被吹1胜2负暂列小组第2 日本vs朝鲜世预赛2024 世预赛日本0-1爆冷不敌