Computer Science
What are the possible outcome(s) executed from the following code ? Also specify the maximum and minimum values that can be assigned to variable PICK.
import random
PICK = random.randint(0, 3)
CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"];
for I in CITY :
for J in range(1, PICK):
print(I, end = " ")
print()
- DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA - DELHI
DELHIMUMBAI
DELHIMUMBAICHENNAI - DELHI
MUMBAI
CHENNAI
KOLKATA - DELHI
MUMBAIMUMBAI
KOLKATAKOLKATAKOLKATA
Answer
DELHI
MUMBAI
CHENNAI
KOLKATA
DELHIDELHI
MUMBAIMUMBAI
CHENNAICHENNAI
KOLKATAKOLKATA
The minimum value for PICK is 0 and the maximum value for PICK is 3.
Explanation
import random
— Imports therandom
module.PICK = random.randint(0, 3)
— Generates a random integer between 0 and 3 (inclusive) and assigns it to the variablePICK
.CITY = ["DELHI", "MUMBAI", "CHENNAI", "KOLKATA"]
— Defines a list of cities.for I in CITY
— This loop iterates over each city in theCITY
.for J in range(1, PICK)
— This loop will iterate from 1 up to PICK - 1. The value ofPICK
can be an integer in the range [0, 1, 2, 3]. For the cases when value ofPICK
is 0 or 1, the loop will not execute asrange(1, 0)
andrange(1, 1)
will return empty range.
For the cases when value ofPICK
is 2 or 3, the names of the cities inCITY
will be printed once or twice, respectively. Hence, we get the possible outcomes of this code.
Related Questions
Consider the following code :
import random print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10), end = ' ' ) print(100 + random.randint(5, 10))
Find the suggested output options 1 to 4. Also, write the least value and highest value that can be generated.
- 102 105 104 105
- 110 103 104 105
- 105 107 105 110
- 110 105 105 110
Consider the following package
music/ Top-level package ├── __init__.py ├── formats/ Subpackage for file format │ ├── __init__.py │ └── wavread.py │ └── wavwrite.py │ ├── effects/ Subpackage for sound effects │ └── __init__.py │ └── echo.py │ └── surround.py │ └── reverse.py │ └── filters/ Subpackage for filters ├── __init__.py └── equalizer.py └── vocoder.py └── karaoke.py
Each of the above modules contain functions play(), writefile() and readfile().
(a) If the module wavwrite is imported using command import music.formats.wavwrite. How will you invoke its writefile() function ? Write command for it.
(b) If the module wavwrite is imported using command from music.formats import wavwrite. How will you invoke its writefile() function ? Write command for it.
Write a Python program having following functions :
- A function with the following signature :
remove_letter(sentence, letter)
This function should take a string and a letter (as a single-character string) as arguments, returning a copy of that string with every instance of the indicated letter removed. For example, remove_letter("Hello there!", "e") should return the string "Hllo thr!".
Try implementing it using<str>.split()
function. - Write a function to do the following :
Try implementing the capwords() functionality using other functions, i.e., split(), capitalize() and join(). Compare the result with the capwords() function's result.
- A function with the following signature :
Create a module lengthconversion.py that stores functions for various lengths conversion e.g.,
- miletokm() — to convert miles to kilometer
- kmtomile() — to convert kilometers to miles
- feettoinches()
- inchestofeet()
It should also store constant values such as value of (mile in kilometers and vice versa).
[1 mile = 1.609344 kilometer ; 1 feet = 12 inches]
Help() function should display proper information.