Juliet C. answered 02/27/23
PhD in Computer Science with 8+ Years of Teaching Experience
As with most computer programming questions, there are many ways to answer this question. One idea involves calculating the maximum of the two list lengths and having a for loop from 0 to that length, using the loop variable as the index into the lists. Within the loop, strings from each list can be concatenated, using +=, to a result string, which starts as an empty string. Each time through the loop, it would determine the following:
- if the index is legal for the first list; if so, concatenate the string at the index in the first list to the result
- if a string from the first list was just concatenated and a string from the second list will be concatenated; if so, concatenate a space to the result
- if the index is legal for the second list; if so, concatenate the string at the index in the second list to the result
- if there are still strings in either list left; if so, concatenate a space to the result so that next time through the loop there will be a space before concatenating either the string from the first list or the string from the second list