Question

In: Computer Science

r = range(10, 40, 3) def print_lv(strings): strings = strings if isinstance(strings, list) else [strings] for...

r = range(10, 40, 3)

def print_lv(strings):

strings = strings if isinstance(strings, list) else [strings]

for string in strings:

st = "f'" + string + ": {" + string + "}'"

print_stmt = 'print(' + st + ', end="; ")'

# Uncomment the following print statement to see the statement to be executed.

# Each will appear on a separate line.

# print(f'\n{print_stmt}')

exec(print_stmt)

print()

print_lv(['list(r)', 'r[-2:3:-1]', 'list(r[-2:3:-1])'])

OUTPUT:

list(r): [10, 13, 16, 19, 22, 25, 28, 31, 34, 37]; r[-2:3:-1]: range(34, 19, -3); list(r[-2:3:-1]): [34, 31, 28, 25, 22]; 

I need help explaining this bit of code. Particularly why r[-2:3:-1] is range(34, 19, -3).

The slice starts at position -2 and stops at position 3 in increments of step -1.

YET the range that results from taking that slice starts at value 34 and stops at value 19 in increments of step -3

Solutions

Expert Solution

If you need more help, please ask in comments


Related Solutions

def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's...
def warmer_year(temps_then: List[int], temps_now: List[int]) -> List[str]: """Return a list of strings representing whether this year's temperatures from temps_now are warmer than past temperatures in temps_then. The resulting list should contain "Warmer" at the indexes where this year's temperature is warmer, and "Not Warmer" at the indexes where the past year was warmer, or there is a tie. Precondition: len(temps_then) == len(temps_now) >>> warmer_year([10], [11]) ['Warmer'] >>> warmer_year([26, 27, 27, 28], [25, 28, 27, 30]) ['Not Warmer', 'Warmer', 'Not Warmer',...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def...
class Board: def __init__(self): self.pieces = [[None for i in range(8)] for j in range(8)] def __str__(self): s = "" for j in range(7, -1, -1): #for each row for i in range(8): # for each column if self.pieces[j][i] is None: # if self.pieces[j][i] is None s += "." # populate the row with '.' val for each column else: s += self.pieces [j][i].get_symbol() s += "\n" #after each row add a new line return s # return after iterating...
You are provided with an array of Strings and a list of Strings. Sort the elements...
You are provided with an array of Strings and a list of Strings. Sort the elements (1) in natural order, (2) in reverse natural order and (3) by the length of each String. You can fill in the details by using the following stub file: import java.util.Arrays; import java.util.Collections; import java.util.List; public class Midterm01 { public static void main(String[] args) { String[] arrayOfCities = { "Atlanta", "Savannah", "New York", "Dallas", "Rio" }; List<String> listOfCities = Arrays.asList("Atlanta", "Savannah", "New York", "Dallas",...
The elements of the list are going to be strings, and will be in order of...
The elements of the list are going to be strings, and will be in order of strictly increasing or decreasing length. Some examples of lists in strictly increasing length order are [“pan”, “banana”,”xylophone”], and [“kayn”, “swain”, “morgana”].   Function Name: fix_string_list Parameter: data - A list with 3 elements (that are all lowercase strings) that is either in order of strictly increasing or decreasing length. Return: A list with the same 3 elements as the parameter that is in order of...
- List the 3 reasons that price and quantity demanded are inversely related, all else equal...
- List the 3 reasons that price and quantity demanded are inversely related, all else equal (i.e., the Law of Demand). Give an example of each with an explanation of how your example helps explain the Law of Demand. - When lettuce prices doubled, from about $1.50 per head to about $3.00, the reaction of one consumer was quoted in a newspaper article: “I will not buy [lettuce] when it’s $3 per head,” she said, adding that other green vegetables...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings...
1. Implement the function calculate_score that consumes three parameters, two strings and a list. The strings will each be ONE character and will represent a nucleotide. The list will be a nested int list representing a 4x4 score matrix. This function will return the value (int) from the nested int list at the location of the two referenced nucleotides. a. An example call to calculate_score would be calculate_score(“A”, “T”, score_matrix). If we look at the alignment score table in the...
It is known that the sentence E: if (if P then not (Q or R) else...
It is known that the sentence E: if (if P then not (Q or R) else not P) then (not (Q and S) if and only if (not Q or not S)). Investigate whether I = {S ← false, R ← false, Q '← true, P ← false} interpretations are interpretations for sentence E.
def change_type(info: List[list]) -> None: """ Modify info to a float if and only if it...
def change_type(info: List[list]) -> None: """ Modify info to a float if and only if it represents a number that is not a whole number(a float), and convert info to an int if and only if it represents a whole number, keep everythingelse as a string. >>> i = [['apple', '888', 'School', '111.1']] >>> change_type(i) >>> i   [['apple', 888, 'School', '111.1']] Please help! Write as little code as possible! Please only use if statements, nested loop and lists to solve...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return...
from typing import List def longest_chain(submatrix: List[int]) -> int: """ Given a list of integers, return the length of the longest chain of 1's that start from the beginning. You MUST use a while loop for this! We will check. >>> longest_chain([1, 1, 0]) 2 >>> longest_chain([0, 1, 1]) 0 >>> longest_chain([1, 0, 1]) 1 """ i = 0 a = [] while i < len(submatrix) and submatrix[i] != 0: a.append(submatrix[i]) i += 1 return sum(a) def largest_rectangle_at_position(matrix: List[List[int]], x:...
def largest_rectangle_in_matrix(matrix: List[List[int]]) -> int: """ Returns the area of the largest rectangle in <matrix>. The...
def largest_rectangle_in_matrix(matrix: List[List[int]]) -> int: """ Returns the area of the largest rectangle in <matrix>. The area of a rectangle is defined as the number of 1's that it contains. Again, you MUST make use of <largest_rectangle_at_position> here. If you managed to code largest_rectangle_at_position correctly, this function should be very easy to implement. Similarly, do not modify the input matrix. Precondition: <matrix> will only contain the integers 1 and 0. >>> case1 = [[1, 0, 1, 0, 0], ... [1,...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT