0

Have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and quest

def QuestionsMarks(strParam): len0=len(strParam) sums=0 sum1=0 list1=list(strParam) for i,s in enumerate(strParam): if s.isdigit(): index=i break for j in range(i+1,len0): if strParam[j].isdigit(): index2=j break for k in range(index,index2+1): if strParam[k] in ['1','2','3','4','5','6','7','8','9']: sum1=sum1+int(strParam[k]) print(sum1) for m in range(index,index2+1): if strParam[m]=='?': sums=sums+1 if sum1>9 and sums==3: return True else: return False '''if sum>10: return True else: return False ''' # code goes here ##return index # keep this function call here print(QuestionsMarks(input())) the code is lengthy .i dont whats wrong in this "acc?4??sss?3rr9??????5" 7 False its showing False

14th Nov 2022, 8:31 AM
Nevin Zachariah
Nevin Zachariah - avatar
3 Respostas
+ 5
Nevin Zachariah , we could also use regex to solve this task. seexmy thoughts written in the file: https://code.sololearn.com/czSU7XcFT7V5/?ref=app
14th Nov 2022, 9:12 PM
Lothar
Lothar - avatar
+ 1
It looks like your program will only check the first two digits it encounters, since there is a break in the first two for loops. Not clear what the 3rd loop is meant to do, but if just trying to add two digits, this makes more sense, instead of loop: sum1 = int(strParam[index]) + int(strParam[index2]) You can use the count() string method to count the number of occurrences of a substring ('?') in a string (strParam). Like this: sums = strParam[index:index2+1].count('?') I think you only need 2 loops to complete the function: 1) loop through strParam and build a list of index of ALL the digits, not just first 2. idxs = [] for i, c in enumerate(strParam): if c.isdigit(): idxs.append(i) 2) loop through the index list to check if each pair of digits satisfy your conditions. for k in range(len(idxs)-1): index = idxs[k] index2 = idxs[k] # calculate sum1 # count ?s into sums # if conditions satisfied, return True 3) Finally, last statement of the function: return False
14th Nov 2022, 10:06 AM
Mozzy
Mozzy - avatar
0
Have the function QuestionsMarks(str) take the str string parameter, which will contain single digit numbers, letters, and question marks, and check if there are exactly 3 question marks between every pair of two numbers that add up to 10. If so, then your program should return the string true, otherwise it should return the string false. If there aren't any two numbers that add up to 10 in the string, then your program should return false as well. For example: if str is "arrb6???4xxbl5???eee5" then your program should return true because there are exactly 3 question marks between 6 and 4, and 3 question marks between 5 and 5 at the end of the string. Examples Input: "aa6?9" Output: false Input: "acc?7??sss?3rr1??????5" Output: true. this is the question
14th Nov 2022, 8:33 AM
Nevin Zachariah
Nevin Zachariah - avatar