InterviewSolution
| 1. |
Display the given series in Python. 2 3 2 3 2 3 . . . . . .N terms. Don't use any conditional constructs or arrays or strings. Use numeric approach in solving. |
|
Answer» to write a Python program to display 2 3 2 3 .... following series till n terms.Restrictions:-No conditional constructNo arraysNo strings(since arrays and LISTS are different, here we can use lists)Main concept:-On DIVIDING integers (considering only whole numbers here) by 2, the remainder is in the pattern of 0 1 0 1 ....We observe that,0 + 2 = 21 + 2 = 30 + 2 = 21 + 2 = 3and so on.Required program:-PRINT(*[n%2 + 2 for n in range (0, (int(input("Enter the NUMBER of terms: "))))])Output:-Enter the number of terms: 52 3 2 3 2Algorithm:-TAKING input from the user about the number of terms of the series to be printed.Using the for loop and the range function to iterate the loop the required number of times.Storing the values of n%2 + 2 as the for loop iterates, in a list.Unpacking the list using * and printing the required pattern. |
|