[1] [2] [3] [4] [5] [6] [7] [8] [9] [10] [11]
Write a Python fragment that creates a list named
powers
containing the first 100 powers of 2:
1, 2, 4, 8,….
powers = 100 * [0]
for i in range(100):
powers[i] = 2 ** i
Tabulate the values taken on by each variable as the program fragment below executes.
k = 50
for i in range(6):
j = 2 ** i
if i % 2 == 0:
k = j + k
i : |
0, | 1, | 2, | 3, | 4, | 5 | |
j : |
1, | 2, | 4, | 8, | 16, | 32 | |
k : |
50, | 51, | 55, | 71 |
Tabulate the values taken on by each variable as the program fragment below executes.
n = 32
k = n + 1
for i in range(2, 7):
if n % i == 0:
k = k + i + (n // i)
n |
32 | ||||
k |
33, | 51, | 63 | ||
i |
2, | 3, | 4, | 5, | 6 |
Tabulate the values taken on by the variables i
and
result
as the program fragment below executes.
word = 'bananas'
result = 'f'
for i in range(4):
if word[i:i + 2] != 'na':
result = result + word[i]
i : | 0, | 1, | 2, | 3 | |
result : | f, | fb, | fba, | fbaa |
What will be in the list nums
after executing the
following fragment?
nums = [4, 2, 2, 3, 5, 5]
for i in range(6):
if nums[i] == i:
nums[i] = nums[i - 1] + 1
Suppose nums
were a list of integers containing
[11, 12, 12, 13, 15, 15, 15, 18].
What would be in nums
after executing the
below fragment?
j = 0
for i in range(1, len(nums)):
if nums[i] != nums[i - 1]:
nums[j] = num[i]
j = j + 1
[12, 13, 15, 18, 15, 15, 15, 18]
What are the first and last lines displayed by the below program fragment?
for i in range(1234, 4321):
s = str(i)
if s[1] == '4':
print(s)
The first is 1400
and the last is 3499
.
This question has been moved to Hydra.
This question has been moved to Hydra.
This question has been moved to Hydra.
This question has been moved to Hydra.
This question has been moved to Hydra.
This question has been moved to Hydra.
Suppose we have two lists, an lists of strings called
candidates
which enumerates the names of candidates
running in an election, and a list of integers (of the same
length) called votes
which has the number of votes tallied
for each candidate. For example, votes[0]
contains the
number of votes garnered by the candidate whose name is in
candidates[0]
.
Write a program fragment that prints the name of the candidate who received the most votes in the election. You may assume that there is not a tie.
An answer | Alternative answer |
max_pos = 0 | max_votes = max(votes) |