The Python Code Challenge to Count the Question Marks

My protracted, imperfect solution

Winding, non-straight road in the country
Photo by Kees Streefkerk on Unsplash

This week I decided to try another python code challenge and found these 5 problems in Daniel Borowski’s Medium article. I decided to try problem #1; it seemed straightforward enough — until it wasn’t. It took me several days to complete this — working on it here and there in fits and starts — and my solution is not pythonic or elegant. Here it is anyway.

The challenge:

“Take an input string parameter and determine if exactly 3 question marks exist between every pair of numbers that add up to 10. If so, return true, otherwise return false. “ 

Here are the example test cases I used (‘testa’ is my creation and ‘testb’ is Daniel’s) :

testa = “1??aaaaaaaaaaaaaaaaaaa?4?3”
testb = ‘acc?7??sss?3rr1??????5’

My first step was to find the digits in the test string and determine if there was a pair of numbers (I went with consecutive numbers, not combinations of all numbers) that equal 10. If there is a pair equal to 10, those two numbers are appended to a list called ‘success’, otherwise the list is empty.

And the result:

Step two was to split the test string into a list of separate elements.

The return:

Finally, time for the big show! And for me to put the bits and pieces together. The main function calls sum_finder and checks if the return is an empty string (print “False” and the program ends) or has a pair of numbers equal to 10.

The test string is split into a list, using the split function, the indices of the two numbers determined, and a smaller list is created. The smaller list is a slice of the larger string of the characters between the number pair. A question mark counter is set to 0, the slice is iterated over to count the question marks in the slice and if there are exactly 3 question marks “True” is printed out. Otherwise, it is “False”.

Challenge Solved!

Even as I write this, I know my code is overdone and could be much cleaner. However, success was reaching the end of this challenge on my own. Refactoring resides in the future.

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store