Write a user-defined function in Python named Puzzle(W, N) which takes the argument W
as an English word and N
as an integer and returns the string where every N
th alphabet of the word W
is replaced with an underscore ("_").
Example: If W
contains the word "TELEVISION" and N
is 3, then the function should return the string "TE_EV_SI_N". Likewise, for the word "TELEVISION" if N
is 4, the function should return "TEL_VIS_ON".
def Puzzle(W, N): # Function definition result = "" # Initialize an empty string to store the result for i in range(len(W)): # Loop through each character in the word if (i + 1) % N == 0: # Check if the (i+1)th character is the Nth result += "_" # Replace the Nth character with an underscore else: result += W[i] # Otherwise, keep the character as is return result # Return the resulting string # Example usage W = "TELEVISION" N = 3 print(Puzzle(W, N)) # Output: "TE_EV_SI_N" N = 4 print(Puzzle(W, N)) # Output: "TEL_VIS_ON"Explanation: The function
Puzzle(W, N)
iterates through each character of the input string W
.
Using the condition (i + 1) % N == 0
, it checks whether the position of the character (1-based index) is a multiple of N
.
If true, the character is replaced with an underscore ("_
"), otherwise, the original character is retained.
The resulting string is built incrementally and returned as output.
The SELECT statement when combined with \(\_\_\_\_\_\_\) clause, returns records without repetition.
In SQL, the aggregate function which will display the cardinality of the table is \(\_\_\_\_\_\).
myStr = "MISSISSIPPI" print(myStr[:4] + "#" + myStr[-5:])
Read the following characteristics of retail trading carefully and choose the correct option:
I. This is concerned with the sale of goods directly to the consumers.
II. Most of the retail trading takes place in fixed establishments or stores.
III. It is formed by many middlemen, traders, and suppliers.
IV. Hawkers, street peddling, automatic vending machines, etc., are its examples.
Read the programmes related to the proposed Bharatmala umbrella scheme carefully and choose the correct option:
I. Backward areas, religious and tourist places connectivity programme
2. To interconnect the four metropolitan cities of the country
3. To develop State roads in adjoining coastal areas
4. To connect non-major ports in coastal states