All codes needs to be in python using while loop not for loo
All codes needs to be in python using while loop not for loop:
Also, Do not worry about testing the code I have the code to test it just focus on the questions.
| Problem 1: | Write code that checks every adjacent pair of numbers in a list to see if either is a factor of the other and returns all factor-pairs in order. For example, the list [2, 6, 12, 3, 7, 8, 16, 10] would yield [\"2-6\", \"6-12\", \"3-12\", \"8-16\"]. You may not use any built-in functions/methods besides len() and .append(). |
| Problem 2: | Write code that takes two strings from the user, replaces all instances of the second string with box in the first. For example, happymondayhappyday and happy yields the result boxmondayboxday. We will guarantee the second string will be less than four characters long. You may not use the built-in replace function. |
Solution
# Problem 1; def adjacent_pair(data): \"\"\" :param data :return: \"\"\" final_data = [] count = 0 data_length = len(data) - 1 while count < data_length: if data[count] % data[count + 1] == 0 or data[count + 1] % data[count] == 0: temp_str = str(data[count]) + \"-\" + str(data[count + 1]) if data[count] < data[count + 1] else str( data[count + 1]) + \"-\" + str(data[count]) final_data.append(temp_str) count += 1 return final_data # for taking input from terminal test_data_1 = [int(x) for x in input().split(\" \")] result_1 = adjacent_pair(test_data_1) print(result_1) # inputs are static test_data_2 = [2, 6, 12, 3, 7, 8, 16, 10] result_2 = adjacent_pair(test_data_2) print(result_2) # problem 2 string1 = input() string2 = input() def replace_str(text, pattern, replace_string): \"\"\" :param text: :type text:str :param pattern :type pattern:str :param replace_string: :type replace_string:str :return: \"\"\" count = 0 while pattern in text: index = text.index(pattern) # The place where pattern occurs in text # You need to cut text into two parts: # the part before index and # the part after index. Remember to consider in the length of pattern. text = text[:index] + replace_string + text[index + (len(pattern)):] return text data1 = replace_str(string1, string2,\"box\") print(data1)