Files
honore-beaugrand/Cours numero 3.ipynb
T

5.0 KiB

Class 3. keep going

The homework was way too hard!!

We needed some more tools...

The concat function is used to concatenate collections togheter.

Homework correction (Part 1)

In [6]:
(def school-url "https://gitlab.com/didiercrunch/honore-beaugrand/raw/master/resources/data/homework2/classes.edn")
(def school-data (read-string (slurp school-url)))

school-data
Out [6]:
[{:name "class 1", :url "https://gitlab.com/didiercrunch/honore-beaugrand/raw/master/resources/data/homework2/class1.edn"} {:name "class 2", :url "https://gitlab.com/didiercrunch/honore-beaugrand/raw/master/resources/data/homework2/class2.edn"} {:name "class 3", :url "https://gitlab.com/didiercrunch/honore-beaugrand/raw/master/resources/data/homework2/class3.edn"}]
In [12]:
(def class-urls (map :url school-data))
(def class-data-raw (map slurp class-urls))
(def class-data (map read-string class-data-raw))
class-data
Out [12]:
([{:name "boris", :sex :male, :age 22} {:name "bob", :sex :female, :age 25} {:name "andrea", :sex :male, :age 33} {:name "stephanie", :sex :female, :age 12} {:name "alexa", :sex :female, :age 90} {:name "boris", :sex :female, :age 12} {:name "nathasha", :sex :male, :age 32} {:name "marco", :sex :male, :age 30}] [{:name "enrico", :sex :male, :age 32} {:name "raquel", :sex :female, :age 15} {:name "alfred", :sex :male, :age 17} {:name "kery", :sex :female, :age 101} {:name "alexa", :sex :female, :age 43} {:name "dominique", :sex :female, :age 30} {:name "sofia", :sex :male, :age 33} {:name "clory", :sex :male, :age 45}] [{:name "Scot", :sex :male, :age 23} {:name "Annmarie", :sex :female, :age 15} {:name "Jeremy", :sex :male, :age 17} {:name "Theodora", :sex :female, :age 67} {:name "Andria", :sex :female, :age 45} {:name "Kyoko", :sex :female, :age 39} {:name "Jeanine", :sex :male, :age 41} {:name "Jeanine", :sex :male, :age 45}])
In [28]:
(def number-of-student-by-class (map count class-data))
(def number-of-student-in-the-school (apply + number-of-student-by-class))
(println "The total number of student in the school is:" number-of-student-in-the-school)
The total number of student in the school is: 24
In [32]:
(def all-student (apply concat class-data))
Out [32]:
#'user/all-student
In [29]:
(def all-ages (map :age all-student))

(def youger-age (apply min all-ages))
(def older-age (apply max all-ages))

(println "The youngest person is aged " youger-age)
(println "The oldest person is aged " older-age)
The youngest person is aged  12
The oldest person is aged  101
In [34]:
(def sum-of-all-ages (apply + all-ages))
(def average-age (/ sum-of-all-ages number-of-student-in-the-school))
(println "the average age is " (double average-age))
the average age is  36.0