Advertisement
asweigart

Untitled

Dec 20th, 2016
254
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.94 KB | None | 0 0
  1. def replaceWithQuestionMarks(lines):
  2. # (assumes each line in lines is the same length)
  3.  
  4. newLine = [] # this will contain the characters/question marks
  5.  
  6. # loop over each index in a line:
  7. for i in range(len(lines[0])):
  8. charactersInAllLines = []
  9.  
  10. # loop over each line, populating charactersInAllLines with the ith character in the line
  11. for line in lines:
  12. charactersInAllLines.append(line[i])
  13.  
  14. # convert to a set to get rid of duplicate characters, check if there's only 1 unique character
  15. if len(set(charactersInAllLines)) == 1:
  16. newLine.append(charactersInAllLines[0]) # add the character to the new line
  17. else:
  18. newLine.append('?') # append a question mark
  19.  
  20. return ''.join(newLine) # convert the list of strings into a single string
  21.  
  22.  
  23. print(replaceWithQuestionMarks(['121212123434AA6AAA', '121212222234AAAAAB', 'D21212122434AAAAAF']))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement