Examness

Intermediate Python

12 latihan · dalam urutan pembelajaran

  1. 1Squares of the even numbersWrite `even_squares(numbers)` returning the squares of only the even values, in the order they appeared.list comprehensionfiltering8m
  2. 2Group words by first letterWrite `group_by_initial(words)` returning a dictionary that maps each lower-case first letter to the list of words starting with it.dict of listssetdefaultgrouping10m
  3. 3Sort records by a fieldWrite `sort_by_score(people)` where `people` is a list of dicts each holding `"name"` and `"score"`.sortedkey functionstability10m
  4. 4Merge two talliesWrite `merge_counts(a, b)` where both arguments map a word to a count.dict mergingget with default10m
  5. 5Flatten a nested listWrite `flatten(items)` turning an arbitrarily nested list into a flat one.recursionisinstancebase case12m
  6. 6Divide without crashingWrite `safe_divide(a, b)` returning `a / b`.try/exceptZeroDivisionErrorTypeError9m
  7. 7Running totalWrite `running_total(numbers)` as a generator that yields the total so far after each number.generatorsyieldlazy evaluation11m
  8. 8A bank account that refuses to go negativeWrite a `BankAccount` class.classes__init__invariantsexceptions14m
  9. 9Most common wordsWrite `top_words(text, n)` returning the `n` most frequent words as `(word, count)` pairs, most frequent first.countingsorting by two keystie-breaking12m
  10. 10Binary searchWrite `binary_search(sorted_numbers, target)` returning the index of `target`, or `-1` when it is absent.divide and conquerloop invariantsO(log n)13m
  11. 11Transpose a matrixWrite `transpose(matrix)` swapping rows and columns.zipunpackingnested lists10m
  12. 12Retry on failureWrite `retry(times)` - a decorator factory that retries a function when it raises.decoratorsclosures*args/**kwargs15m