08-02-03 Sets - set iteration

Pataasin ang iyong marka sa homework at exams ngayon gamit ang Quizwiz!

Remove the smallest element from the set, s. If the set is empty, it remains empty.

if len(s) != 0 : [Tab Key]min = None [Tab Key]for e in s : [Tab Key][Tab Key]if min == None or e < min : [Tab Key][Tab Key][Tab Key]min = e [Tab Key]s.remove(min)

Remove the smallest element from the set, s. Assume the set is not empty.

min = None for e in s : [Tab Key]if min == None or e < min : [Tab Key][Tab Key]min = e s.remove(min)

iven a set, weights, and an integer desired_weight, remove the element of the set that is closest to desired_weight (the closest element can be less than, equal to OR GREATER THAN desired_weight), and assign it to the variable actual_weight. For example, if weights is (12, 19, 6, 14, 22, 7) and desired_weight is 18, then the resulting set would be (12, 6, 14, 22, 7) and actual_weight would be 19. If there is a tie, the element LESS THAN desired_weight is to be chosen. Thus if the set is (2, 4, 6, 8, 10) and desired_weight is 7, the value chosen would be 6, not 8. Assume there is at least one value in the set.

actual_weight = None for e in weights : [Tab Key]if actual_weight == None or (abs(e - desired_weight) < abs(actual_weight - desired_weight)) or (abs(e - desired_weight) == abs(actual_weight - desired_weight) and e < desired_weight) : [Tab Key][Tab Key]actual_weight = e weights.remove(actual_weight)


Kaugnay na mga set ng pag-aaral

LX0-104 Flashcards - Networking Fundamentals

View Set

Unit 9 - Personal Auto Insurance

View Set

General Insurance - Risk Management Key Terms

View Set

Tx Teachers / Part 5: Assessing Learning (Quizzes)

View Set