Computer Science
Predict the output of the following code:
S = "LOST"
L = [10, 21, 33, 4]
D = {}
for I in range(len(S)):
if I % 2 == 0:
D[L.pop()] = S[I]
else:
D[L.pop()] = I + 3
for K, V in D.items():
print(K, V, sep = "*")
Python
Python List Manipulation
7 Likes
Answer
4*L
33*4
21*S
10*6
Working
The code initializes variables S, L, and D, representing a string, list, and dictionary, respectively. The code iterates through the indices of string S. If the index is even, it assigns the popped element from list L as a key in dictionary D with the corresponding character from S as its value. If the index is odd, it assigns the popped element from L as a key in D with the value being the index plus 3. This process continues until the loop ends. After the loop, it prints each key-value pair from D, with keys and values separated by "*".
Answered By
2 Likes
Related Questions
Write a function countNow(PLACES) in Python, that takes the dictionary, PLACES as an argument and displays the names (in uppercase) of the places whose names are longer than 5 characters. For example, Consider the following dictionary:
PLACES = {1: "Delhi", 2: "London", 3: "Paris", 4: "New York", 5:"Doha"}
The output should be:
LONDON
NEW YORKWrite a function, lenWords(STRING), that takes a string as an argument and returns a tuple containing length of each word of a string. For example, if the string is "Come let us have some fun", the tuple will have (4, 3, 2, 4, 4, 3).
Write the Python statement for each of the following tasks using BUILT-IN functions/methods only:
(i) To insert an element 200 at the third position, in the list L1.
(ii) To check whether a string named, message ends with a full stop / period or not.
A list named studentAge stores age of students of a class. Write the Python command to import the required module and (using built-in function) to display the most common age value from the given list.