-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdata-science-from-scratch-2-singkong.txt
89 lines (77 loc) · 2.13 KB
/
data-science-from-scratch-2-singkong.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
#
Data Science from Scratch
Second Edition
(First Principles with Python)
By: Joel Grus (O'Reilly)
Copyright 2019 Joel Grus
ISBN: 978-1-492-04113-9
Translated to Singkong Programming Language
(c) Noprianto <nopri.anto@icloud.com>
Free to use and/or redistribute
https://nopri.github.io/singkong.html
;
load_module("util")
var sep = "-" * 60
var nl = newline()
#page 3-5;
println("Page 3 - 5", sep)
var users = [
{"id": 0, "name": "Hero"},
{"id": 1, "name": "Dunn"},
{"id": 2, "name": "Sue"},
{"id": 3, "name": "Chi"},
{"id": 4, "name": "Thor"},
{"id": 5, "name": "Clive"},
{"id": 6, "name": "Hicks"},
{"id": 7, "name": "Devin"},
{"id": 8, "name": "Kate"},
{"id": 9, "name": "Klein"}
]
var friendship_pairs = [
[0, 1],
[0, 2],
[1, 2],
[1, 3],
[2, 3],
[3, 4],
[4, 5],
[5, 6],
[5, 7],
[6, 8],
[7, 8],
[8, 9]
]
var friendships = {}
each(users, fn(e, i) {
set(friendships, e["id"], [])
})
each(friendship_pairs, fn(e, i) {
set(friendships, e[0], friendships[e[0]] + e[1])
set(friendships, e[1], friendships[e[1]] + e[0])
})
print("Friendships: ", friendships, nl)
var number_of_friends = fn(u) {
var user_id = u["id"]
var friend_ids = friendships[user_id]
return len(friend_ids)
}
var temp = []
each(users, fn(e, i) {
var temp = temp + number_of_friends(e)
})
var total_connections = sum(temp)
print("Total connections: ", total_connections, nl)
var num_users = len(users)
var avg_connections = total_connections / num_users
print("Average connections: ", avg_connections, nl)
var num_friends_by_id = []
each(users, fn(e, i) {
var num_friends_by_id = num_friends_by_id + [e["id"], number_of_friends(e)]
})
print("Number of friends by id (not sorted): ", num_friends_by_id, nl)
sort_rect_array_of_number_by_index(num_friends_by_id, 1)
print("Number of friends by id (sorted asc): ", num_friends_by_id, nl)
reverse(num_friends_by_id)
print("Number of friends by id (sorted desc): ", num_friends_by_id, nl)
println("Note: [user_id, num_friends] order differs from the illustrated output
in the book, but the array is still sorted, based on num_friends")