语法练习:missing_char
迪丽瓦拉
2024-03-22 19:45:52
0

语法练习:missing_char

题目:missing_char

Given a non-empty string and an int n, return a new string where the char at index n has been removed. The value of n will be a valid index of a char in the original string (i.e. n will be in the range 0…len(str)-1 inclusive).

missing_char(‘kitten’, 1) → ‘ktten’
missing_char(‘kitten’, 0) → ‘itten’
missing_char(‘kitten’, 4) → ‘kittn’

我的解答:

def missing_char(str, n):return str[:n] + str[n+1:]

Expected Run
missing_char(‘kitten’, 1) → ‘ktten’ ‘ktten’ OK
missing_char(‘kitten’, 0) → ‘itten’ ‘itten’ OK
missing_char(‘kitten’, 4) → ‘kittn’ ‘kittn’ OK
missing_char(‘Hi’, 0) → ‘i’ ‘i’ OK
missing_char(‘Hi’, 1) → ‘H’ ‘H’ OK
missing_char(‘code’, 0) → ‘ode’ ‘ode’ OK
missing_char(‘code’, 1) → ‘cde’ ‘cde’ OK
missing_char(‘code’, 2) → ‘coe’ ‘coe’ OK
missing_char(‘code’, 3) → ‘cod’ ‘cod’ OK
missing_char(‘chocolate’, 8) → ‘chocolat’ ‘chocolat’ OK

All Correct

标答:

def missing_char(str, n):front = str[:n]   # up to but not including nback = str[n+1:]  # n+1 through end of stringreturn front + back

相关内容