Advertisement
Guest User

Untitled

a guest
Feb 21st, 2020
95
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.21 KB | None | 0 0
  1. # This super_sum function accepts a list of one or more strings and sums up the index position of the first occurence of the letter "s"
  2.  
  3. def super_sum(ssum):
  4.     total = 0
  5.     s_counts = []
  6.    
  7.     for anystring in ssum:  #iterates over every character in the list passed to it
  8.         if "s" in anystring:  #checks for the character "s" in the elements of the strings passed to it
  9.             index_positions = (anystring.index("s")) #creating a variable to store the index position of the first occurence of the character "s"
  10.             #print(index_positions)  #To check if i was getting the right index positions of the found "s" character in the string
  11.             s_counts.append(index_positions)  #To add all the index positions to a list, to be able to iterate them and add them.
  12.             #print (s_count) #To check if the expected list was created
  13.    
  14.     for counts in s_counts:  #to iterate the generated index positions of the character "s", so as to add up easily
  15.         total += counts #the process of adding up the list of the index positions of the character "s"
  16.     return total
  17.  
  18.            
  19. print(super_sum(["Jesus", "sweet", "anybody", "grass"]))
  20. print(super_sum(["asus", "rest", "pies"]))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement