Skip to content

Exercises: Series I

Perfect numbers

A so-called perfect number is an integer that is the sum of all its divisors (except itself, of course).

For example, 6 is a perfect number since 6 can be divided by 1, 2, and 3, and 6=1+2+3

  1. write a series of statements that computes the sum of a list of integer numbers
  2. write a series of statements that create, from a given integer, the list of its divisors
  3. write a series of statements that evaluates (True or False) if an integer is a perfect number
  4. find all perfect numbers < 10000

Solution

Password

Using the random module, write a Python script that creates a password using random characters (digit, letter, and non-letter/digit like &@.-+)

Solution

square root (integer ensemble)

Write a python script that computes the integer square root of a given integer n, i.e., the highest number for which its square is lower than n.

For example:

square_root(9) = 3
square_root(10) = 3
square_root(15) = 3
square_root(16) = 4

Solution

binary transform

Write a python script that converts a string of "0" and "1" into a decimal number.

For example:

binary("0") = 0
binary("1") = 1
binary("01") = 2
binary("001") = 4
binary("101001") = 37

Solution

Using functions...

Rewrite all your previous scripts using functions

Solutions:

Using classes

Rewrite all your previous scripts using classes

Solutions: