-
-
Notifications
You must be signed in to change notification settings - Fork 19
/
Julia100Exercises.jl
1343 lines (942 loc) · 33.3 KB
/
Julia100Exercises.jl
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
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
# [![Visitors](https://hits.seeyoufarm.com/api/count/incr/badge.svg?url=https%3A%2F%2Fgithub.com%2FRoyiAvital%2FStackExchangeCodes&count_bg=%2379C83D&title_bg=%23555555&icon=&icon_color=%23E7E7E7&title=Visitors+%28Daily+%2F+Total%29&edge_flat=false)](https://github.com/RoyiAvital/Julia100Exercises)
#
# # 100 Julia Exercises with Solutions
#
# A set of introductory exercises for Julia. Based on [100 NumPy Exercises](https://github.com/rougier/numpy-100).
#
# In order to generate this file:
# 1. Clone the repository (Or download).
# 2. Activate and instantiate the project.
# 3. Run:
# ```Julia
# using Literate;
# Literate.markdown("Julia100Exercises.jl", name = "README", execute = true, flavor = Literate.CommonMarkFlavor());
# ```
#
# **Remark**: Tested with Julia `1.8.2`.
#
# **To Do**:
# 1. Reevaluate the difficulty level of each question.
using Literate;
using LinearAlgebra;
using Statistics;
using Dates;
using DelimitedFiles;
using UnicodePlots;
using Random;
using Tullio;
using StaticKernels;
# ## Question 001
# Import the `LinearAlgebra` package under the name `LA`. (★☆☆)
import LinearAlgebra as LA;
# ## Question 002
# Print the version of Julia. (★☆☆)
println(VERSION);
# ## Question 003
# Create a non initialized vector of size 10 of `Float64`. (★☆☆)
vA = Vector{Float64}(undef, 10)
# Which is equivalent of
vA = Array{Float64, 1}(undef, 10)
# ## Question 004
# Find the memory size of any array. (★☆☆)
sizeof(vA)
# ## Question 005
# Show the documentation of the `+` (Add) method. (★☆☆)
@doc +
# ## Question 006
# Create a vector of zeros of size 10 but the fifth value which is 1. (★☆☆)
vA = zeros(10);
vA[5] = 1.0;
vA
# ## Question 007
# Create a vector with values ranging from 7 to 12. (★☆☆)
vA = 7:12
# The above is efficient type. In order to explicitly create a vector:
vA = collect(7:12)
# ## Question 008
# Reverse a vector (first element becomes last). (★☆☆)
vA = collect(1:3);
vB = vA[end:-1:1];
vB
# Alternative 001:
vB = reverse(vA);
# Alternative 002 (In place):
reverse!(vA);
vA
# ## Question 009
# Create a `3x3` matrix with values ranging from 0 to 8. (★☆☆)
mA = reshape(0:8, 3, 3)
# Another way would be:
mA = Matrix{Float64}(undef, 3, 3);
mA[:] = 0:8;
# ## Question 010
# Find indices of non zero elements from `[1, 2, 0, 0, 4, 0]`. (★☆☆)
findall(!iszero, [1, 2, 0, 0, 4, 0])
# ## Question 011
# Create a 3x3 identity matrix. (★☆☆)
mA = I(3)
# An alternative method (Explicit matrix) would be:
mA = Matrix(I, 3, 3) #<! For Float64: Matrix{Float64}(I, 3, 3)
# ## Question 012
# Create a `2x2x2` array with random values. (★☆☆)
mA = randn(2, 2, 2)
# ## Question 013
# Create a `5x5` array with random values and find the minimum and maximum values. (★☆☆)
mA = rand(5, 5);
minVal = minimum(mA)
#+
maxVal = maximum(mA)
# Using `extrema()` one could get both values at once:
minVal, maxVal = extrema(mA);
# ## Question 014
# Create a random vector of size 30 and find the mean value. (★☆☆)
meanVal = mean(randn(30))
# ## Question 015
# Create a 2d array with 1 on the border and 0 inside. (★☆☆)
mA = zeros(4, 4);
mA[:, [1, end]] .= 1;
mA[[1, end], :] .= 1;
mA
# An alternative way (Different dimensions):
mA = ones(4, 5);
mA[2:(end - 1), 2:(end - 1)] .= 0;
# Using one line code:
mA = zeros(4, 5);
mA[[LinearIndices(mA)[cartIdx] for cartIdx in CartesianIndices(mA) if (any(cartIdx.I .== 1) || cartIdx.I[1] == size(mA, 1) || cartIdx.I[2] == size(mA, 2))]] .= 1;
# By [Tomer Arnon](https://github.com/tomerarnon):
numRows = 5;
numCols = 4;
mA = Int[ii ∈ (1, numRows) || jj ∈ (1, numCols) for ii in 1:numRows, jj in 1:numCols];
# ## Question 016
# Add a border of zeros around the array. (★☆☆)
mB = zeros(size(mA) .+ 2);
mB[2:(end - 1), 2:(end - 1)] = mA;
mB
# ## Question 017
# Evaluate the following expressions. (★☆☆)
0 * NaN
#+
NaN == NaN
#+
Inf > NaN
#+
NaN - NaN
#+
NaN in [NaN]
#+
0.3 == 3 * 0.1
# ## Question 018
# Create a `5x5` matrix with values `[1, 2, 3, 4]` just below the diagonal. (★☆☆)
mA = diagm(5, 5, -1 => 1:4)
# ## Question 019
# Create a `8x8` matrix and fill it with a checkerboard pattern. (★☆☆)
mA = zeros(8, 8);
mA[2:2:end, 1:2:end] .= 1;
mA[1:2:end, 2:2:end] .= 1;
mA
# By Tomer Arnon (https://github.com/tomerarnon):
mA = Int[isodd(ii + jj) for ii in 1:8, jj in 1:8];
# ## Question 020
# Convert the linear index 100 to a _Cartesian Index_ of a size `(6,7,8)`. (★☆☆)
mA = rand(6, 7, 8);
cartIdx = CartesianIndices(mA)[100]; #<! See https://discourse.julialang.org/t/14666
mA[cartIdx] == mA[100]
# ## Question 021
# Create a checkerboard `8x8` matrix using the `repeat()` function. (★☆☆)
mA = repeat([0 1; 1 0], 4, 4)
# ## Question 022
# Normalize a `4x4` random matrix. (★☆☆)
mA = rand(4, 4);
mA .= (mA .- mean(mA)) ./ std(mA) #<! Pay attention that `@.` will yield error (`std()` and `mean()`)
# ## Question 023
# Create a custom type that describes a color as four unsigned bytes (`RGBA`). (★☆☆)
struct sColor
R::UInt8;
G::UInt8;
B::UInt8;
A::UInt8;
end
sMyColor = sColor(rand(UInt8, 4)...)
# ## Question 024
# Multiply a `2x4` matrix by a `4x3` matrix. (★☆☆)
mA = rand(2, 4) * randn(4, 3)
# ## Question 025
# Given a 1D array, negate all elements which are between 3 and 8, in place. (★☆☆)
vA = rand(1:10, 8);
map!(x -> ((x > 3) && (x < 8)) ? -x : x, vA, vA)
# Julia allows Math like notation as well (See `Q0027`):
vA = rand(1:10, 8);
map!(x -> 3 < x < 8 ? -x : x, vA, vA)
# Using logical indices one could use:
vA[3 .< vA .< 8] .*= -1;
# ## Question 026
# Sum the array `1:4` with initial value of -10. (★☆☆)
sum(1:4, init = -10)
# ## Question 027
# Consider an integer vector `vZ` validate the following expressions. (★☆☆)
# ```julia
# vZ .^ vZ
# 2 << vZ >> 2
# vZ <- vZ
# 1im * vZ
# vZ / 1 / 1
# vZ < Z > Z
# ```
vZ = rand(1:10, 3);
#+
vZ .^ vZ
#+
try
2 << vZ >> 2
catch e
println(e)
end
#+
vZ <- vZ
#+
1im * vZ
#+
vZ / 1 / 1
#+
vZ < vZ > vZ
# ## Question 028
# Evaluate the following expressions. (★☆☆)
[0] ./ [0]
#+
try
[0] .÷ [0]
catch e
println(e)
end
#+
try
convert(Float, convert(Int, NaN))
catch e
println(e)
end
# ## Question 029
# Round away from zero a float array. (★☆☆)
vA = randn(10);
map(x -> x > 0 ? ceil(x) : floor(x), vA)
# ## Question 030
# Find common values between two arrays. (★☆☆)
vA = rand(1:10, 6);
vB = rand(1:10, 6);
vA[findall(in(vB), vA)]
# ## Question 031
# Suppress Julia's warnings. (★☆☆)
# One could use [Suppressor.jl](https://github.com/JuliaIO/Suppressor.jl).
# ## Question 032
# Compare `sqrt(-1)` and `sqrt(-1 + 0im)`. (★☆☆)
try
sqrt(-1)
catch e
println(e)
end
#+
sqrt(-1 + 0im)
# ## Question 033
# Display yesterday, today and tomorrow's date. (★☆☆)
println("Yesterday: $(today() - Day(1))");
println("Today: $(today())");
println("Tomorrow: $(today() + Day(1))");
# ## Question 034
# Display all the dates corresponding to the month of July 2016. (★★☆)
collect(Date(2016,7,1):Day(1):Date(2016,7,31))
# ## Question 035
# Compute `((mA + mB) * (-mA / 2))` in place. (★★☆)
mA = rand(2, 2);
mB = rand(2, 2);
mA .= ((mA .+ mB) .* (.-mA ./ 2))
# Using the dot macro:
@. mA = ((mA + mB) * (-mA / 2));
# ## Question 036
# Extract the integer part of a random array of positive numbers using 4 different methods. (★★☆)
mA = 5 * rand(3, 3);
# Option 1:
floor.(mA)
# Option 2:
round.(mA .- 0.5) #<! Generates -0.0 for numbers smaller than 0.5
# Option 3:
mA .÷ 1
# Option 4:
mA .- rem.(mA, 1)
# ## Question 037
# Create a `5x5` matrix with row values ranging from 0 to 4. (★★☆)
mA = repeat(reshape(0:4, 1, 5), 5, 1)
# One could also generate _row like_ range using tranpose:
mA = repeat((0:4)', 5, 1);
# ## Question 038
# Generate an array using a generator of 10 numbers. (★☆☆)
vA = collect(x for x in 1:10)
# In Julia the result of collect can be achieved directly using _Array Comprehension_:
vA = [x for x in 1:10];
# ## Question 039
# Create a vector of size 10 with values ranging from 0 to 1, both excluded. (★★☆)
vA = LinRange(0, 1, 12)[2:(end - 1)]
# ## Question 040
# Create a random vector of size 10 and sort it. (★★☆)
vA = rand(1:10, 10);
sort(vA) #<! Use `sort!()` for inplace sorting
# ## Question 041
# Implement the `sum()` function manually. (★★☆)
vA = rand(100);
function MySum(vA::Vector{T}) where{T <: Number}
sumVal = vA[1];
for ii in 2:length(vA)
sumVal += vA[ii];
end
return sumVal;
end
MySum(vA)
# ## Question 042
# Check for equality of 2 arrays. (★★☆)
vA = rand(10);
vB = rand(10);
all(vA .== vB)
# ## Question 043
# Make an array immutable (Read only). (★★☆)
# This is a work in progress for Julia at in [Issue 31630](https://github.com/JuliaLang/julia/pull/31630).
# ## Question 044
# Consider a random `10x2` matrix representing cartesian coordinates, convert them to polar coordinates. (★★☆)
mA = rand(10, 2);
ConvToPolar = vX -> [hypot(vX[1], vX[2]), atan(vX[2], vX[1])]
mB = [ConvToPolar(vX) for vX in eachrow(mA)]
# In order to have the same output size:
mC = reduce(hcat, mB)';
# ## Question 045
# Create random vector of size 10 and replace the maximum value by 0. (★★☆)
vA = randn(10);
# In case of a single maximum or all different values:
vA[argmax(vA)] = 0;
vA
# General solution:
maxVal = maximum(vA);
vA .= (valA == maxVal ? 0 : valA for valA in vA); #<! Non allocating generator by using `.=`
# ## Question 046
# Create a a grid of `x` and `y` coordinates covering the `[0, 1] x [0, 1]` area. (★★☆)
numGridPts = 5;
vX = LinRange(0, 1, numGridPts);
vY = LinRange(0, 1, numGridPts);
MeshGrid = (vX, vY) -> ([x for _ in vY, x in vX], [y for y in vY, _ in vX]);
mX, mY = MeshGrid(vX, vY); #<! See https://discourse.julialang.org/t/48679
@show mX
#+
@show mY
# By [Tomer Arnon](https://github.com/tomerarnon):
mXY = [(ii, jj) for ii in 0:0.25:1, jj in 0:0.25:1]; #<! Also `tuple.(0:0.25:1, (0:0.25:1)')`
# ## Question 047
# Given two vectors, `vX` and `vY`, construct the Cauchy matrix `mC`: `(Cij = 1 / (xi - yj))`. (★★☆)
vX = rand(5);
vY = rand(5);
mC = 1 ./ (vX .- vY')
# ## Question 048
# Print the minimum and maximum representable value for each Julia scalar type. (★★☆)
vT = [UInt8 UInt16 UInt32 UInt64 Int8 Int16 Int32 Int64 Float16 Float32 Float64]
for juliaType in vT
println(typemin(juliaType));
println(typemax(juliaType));
end
# ## Question 049
# Print all the values of an array. (★★☆)
mA = rand(3, 3);
print(mA);
# ## Question 050
# Find the closest value to a given scalar in a vector. (★★☆)
inputVal = 0.5;
vA = rand(10);
vA[argmin(abs.(vA .- inputVal))]
# By [Tomer Arnon](https://github.com/tomerarnon):
function ClosestValue(vA::Vector{T}, inputVal::T) where{T <: Number}
return argmin(y -> abs(y - inputVal), vA);
end
ClosestValue(vA, inputVal)
# ## Question 051
# Create a structured array representing a position `(x, y)` and a color `(r, g, b)`. (★★☆)
struct sPosColor
x::Int
y::Int
R::UInt8;
G::UInt8;
B::UInt8;
A::UInt8;
end
numPixels = 10;
maxVal = typemax(UInt32);
vMyColor = [sPosColor(rand(1:maxVal, 2)..., rand(UInt8, 4)...) for _ in 1:numPixels];
# ## Question 052
# Consider a random vector with shape `(5, 2)` representing coordinates, find the distances matrix `mD`: $ {D}_{i, j} = {\left\| {x}_{i} - {x}_{j} \right\|}_{2} $. (★★☆)
mX = rand(5, 2);
vSumSqr = sum(vX -> vX .^ 2, mX, dims = 2);
mD = vSumSqr .+ vSumSqr' - 2 * (mX * mX');
mD #<! Apply `sqrt.()` for the actual norm
# ## Question 053
# Convert a float (32 bits) array into an integer (32 bits) in place. (★★☆)
vA = 9999 .* rand(Float32, 5);
vB = reinterpret(Int32, vA); #<! Creates a view
@. vB = trunc(Int32, vA) #<! Updates the byes in th view (Inplace for `vA`)
# The above is equivalent of:
# ```julia
# for ii in eachindex(vB)
# vB[ii] = trunc(Int32, vA[ii]);
# end
# vB
# ```
# ## Question 054
# Read the following file (`Q0054.txt`). (★★☆)
# ```
# 1, 2, 3, 4, 5
# 6, , , 7, 8
# , , 9,10,11
# ```
mA = readdlm("Q0054.txt", ',')
# ## Question 055
# Enumerate array in a loop. (★★☆)
mA = rand(3, 3);
for (elmIdx, elmVal) in enumerate(mA) #<! See https://discourse.julialang.org/t/48877
println(elmIdx);
println(elmVal);
end
# ## Question 056
# Generate a generic 2D Gaussian like array with `μ = 0`, `σ = 1` and indices over `{-5, -4, ..., 0, 1, ..., 5}`. (★★☆)
vA = -5:5;
μ = 0;
σ = 1;
mG = [(1 / (2 * pi * σ)) * exp(-0.5 * ((([x, y] .- μ)' * ([x, y] .- μ)) / (σ * σ))) for x in vA, y in vA];
heatmap(mG)
# Using the separability of the Gaussian function:
vG = (1 / (sqrt(2 * pi) * σ)) .* exp.(-0.5 .* (((vA .- μ) .^ 2) / (σ * σ)));
mG = vG * vG';
# ## Question 057
# Place `5` elements in a `5x5` array randomly. (★★☆)
mA = rand(5, 5);
mA[rand(1:25, 5)] = rand(5);
# Another option which avoids setting into the same indices:
mA[randperm(25)[1:5]] = rand(5);
# ## Question 058
# Subtract the mean of each row of a matrix. (★★☆)
mA = rand(3, 3);
mA .-= mean(mA, dims = 2);
mean(mA, dims = 1)
# ## Question 059
# Sort an array by a column. (★★☆)
colIdx = 2;
mA = rand(3, 3);
mA[sortperm(mA[:, colIdx]), :]
# Using `sortslices()`:
sortslices(mA, dims = 1, by = x -> x[colIdx]);
# ## Question 060
# Tell if a given 2D array has null (All zeros) columns. (★★☆)
mA = rand(0:1, 3, 9);
any(all(iszero.(mA), dims = 1))
# ## Question 061
# Find the 2nd nearest value from a given value in an array. (★★☆)
inputVal = 0.5;
vA = rand(10);
vA[sortperm(abs.(vA .- inputVal))[2]]
# Alternative way (More efficient)
closeFirst = Inf;
closeSecond = Inf;
closeFirstIdx = 0;
closeSecondIdx = 0;
## Using `global` for scope in Literate
for (elmIdx, elmVal) in enumerate(abs.(vA .- inputVal))
if (elmVal < closeFirst)
global closeSecond = closeFirst;
global closeFirst = elmVal;
global closeSecondIdx = closeFirstIdx;
global closeFirstIdx = elmIdx;
elseif (elmVal < closeSecond)
global closeSecond = elmVal;
global closeSecondIdx = elmIdx;
end
end
vA[closeSecondIdx] == vA[sortperm(abs.(vA .- inputVal))[2]]
# By [Tomer Arnon](https://github.com/tomerarnon):
vA[partialsortperm(abs.(vA .- inputVal), 2)]
# ## Question 062
# Considering two arrays with shape `(1, 3)` and `(3, 1)`, Compute their sum using an iterator. (★★☆)
vA = rand(1, 3);
vB = rand(3, 1);
sum(aVal + bVal for aVal in vA, bVal in vB)
# ## Question 063
# Create an array class that has a name attribute. (★★☆)
#+
# One could use `NamedArrays.jl` or `AxisArrays.jl`.
# ## Question 064
# Given a vector, add `1` to each element indexed by a second vector (Be careful with repeated indices). (★★★)
vA = rand(1:10, 5);
vB = rand(1:5, 3);
println(vA);
## Julia is very efficient with loops
for bIdx in vB
vA[bIdx] += 1;
end
println(vA);
# ## Question 065
# Accumulate elements of a vector `X` to an array `F` based on an index list `I`. (★★★)
vX = rand(1:5, 10);
vI = rand(1:15, 10);
numElements = maximum(vI);
vF = zeros(numElements);
for (ii, iIdx) in enumerate(vI)
vF[iIdx] += vX[ii];
end
println("vX: $vX");
println("vI: $vI");
println("vF: $vF");
# One could also use `counts()` from `StatsBase.jl`.
# ## Question 066
# Considering an image of size `w x h x 3` image of type `UInt8`, compute the number of unique colors. (★★☆)
mI = rand(UInt8, 1000, 1000, 3);
numColors = length(unique([reinterpret(UInt32, [iPx[1], iPx[2], iPx[3], 0x00])[1] for iPx in eachrow(reshape(mI, :, 3))])); #<! Reshaping as at the moment `eachslice()` doesn't support multiple `dims`.
print("Number of Unique Colors: $numColors");
# Another option:
numColors = length(unique([UInt32(iPx[1]) + UInt32(iPx[2]) << 8 + UInt32(iPx[3]) << 16 for iPx in eachrow(reshape(mI, :, 3))]));
print("Number of Unique Colors: $numColors");
# Simpler way to slice a pixel:
numColors = length(unique([UInt32(mI[ii, jj, 1]) + UInt32(mI[ii, jj, 2]) << 8 + UInt32(mI[ii, jj, 3]) << 16 for ii in 1:size(mI, 1), jj in 1:size(mI, 2)]));
print("Number of Unique Colors: $numColors");
# ## Question 067
# Considering a four dimensions array, get sum over the last two axis at once. (★★★)
mA = rand(2, 2, 2, 2);
sum(reshape(mA, (2, 2, :)), dims = 3)
# ## Question 068
# Considering a one dimensional vector `vA`, how to compute means of subsets of `vA` using a vector `vS` of same size describing subset indices. (★★★)
## Bascially extending `Q0065` with another vector of number of additions.
vX = rand(1:5, 10);
vI = rand(1:15, 10);
numElements = maximum(vI);
vF = zeros(numElements);
vN = zeros(Int, numElements);
for (ii, iIdx) in enumerate(vI)
vF[iIdx] += vX[ii];
vN[iIdx] += 1;
end
## We only divide the mean if the number of elements accumulated is bigger than 1
for ii in 1:numElements
vF[ii] = ifelse(vN[ii] > 1, vF[ii] / vN[ii], vF[ii]);
end
println("vX: $vX");
println("vI: $vI");
println("vF: $vF");
# ## Question 069
# Get the diagonal of a matrix product. (★★★)
mA = rand(5, 7);
mB = rand(7, 4);
numDiagElements = min(size(mA, 1), size(mB, 2));
vD = [dot(mA[ii, :], mB[:, ii]) for ii in 1:numDiagElements]
# Alternative way:
vD = reshape(sum(mA[1:numDiagElements, :]' .* mB[:, 1:numDiagElements], dims = 1), numDiagElements)
# ## Question 070
# Consider the vector `[1, 2, 3, 4, 5]`, build a new vector with 3 consecutive zeros interleaved between each value. (★★★)
vA = 1:5;
## Since Julia is fast with loops, it would be the easiest choice
numElements = (4 * length(vA)) - 3;
vB = zeros(Int, numElements);
for (ii, bIdx) in enumerate(1:4:numElements)
vB[bIdx] = vA[ii];
end
println(vB);
## Alternative (MATLAB style) way:
mB = [reshape(collect(vA), 1, :); zeros(Int, 3, length(vA))];
vB = reshape(mB[1:(end - 3)], :);
println(vB);
# ## Question 071
# Consider an array of dimension `5 x 5 x 3`, mulitply it by an array with dimensions `5 x 5` using broadcasting. (★★★)
mA = rand(5, 5, 3);
mB = rand(5, 5);
mA .* mB #<! Very easy in Julia
# ## Question 072
# Swap two rows of a 2D array. (★★★)
mA = rand(UInt8, 3, 2);
println(mA);
mA[[1, 2], :] .= mA[[2, 1], :];
println(mA);
# ## Question 073
# Consider a set of 10 triplets describing 10 triangles (with shared vertices), find the set of unique line segments composing all the triangles. (★★★)
mA = rand(0:100, 10, 3); #<! Each row composes 3 veritces ([1] -> [2], [2] -> [3], [3] -> [1])
mC = [sort!([vC[mod1(ii, end)], vC[mod1(ii + 1, end)]]) for ii in 1:(size(mA, 2) + 1), vC in eachrow(mA)][:] #<! Sorted combinations of vertices
mC = unique(mC)
# ## Question 074
# Given a sorted array `vC` that corresponds to a bincount, produce an array `vA` such that `bincount(vA) == vC`. (★★★)
vC = rand(0:7, 5);
numElements = sum(vC);
vA = zeros(Int, numElements);
elmIdx = 1;
## Using `global` for scope in Literate
for (ii, binCount) in enumerate(vC)
for jj in 1:binCount
vA[elmIdx] = ii;
global elmIdx += 1;
end
end
# ## Question 075
# Compute averages using a sliding window over an array. (★★★)
numElements = 10;
winRadius = 1;
winReach = 2 * winRadius;
winLength = 1 + winReach;
vA = rand(0:3, numElements);
vB = zeros(numElements - (2 * winRadius));
aIdx = 1 + winRadius;
## Using `global` for scope in Literate
for ii in 1:length(vB)
vB[ii] = mean(vA[(aIdx - winRadius):(aIdx + winRadius)]); #<! Using integral / running sum it would be faster.
global aIdx += 1;
end
# Another method using running sum:
vC = zeros(numElements - winReach);
jj = 1;
sumVal = sum(vA[1:winLength]);
vC[jj] = sumVal / winLength;
jj += 1;
## Using `global` for scope in Literate
for ii in 2:(numElements - winReach)
global sumVal += vA[ii + winReach] - vA[ii - 1];
vC[jj] = sumVal / winLength;
global jj += 1;
end
maximum(abs.(vC - vB)) < 1e-8
# ## Question 076
# Consider a one dimensional array `vA`, build a two dimensional array whose first row is `[ vA[0], vA[1], vA[2] ]` and each subsequent row is shifted by 1. (★★★)
vA = rand(10);
numCols = 3;
numRows = length(vA) - numCols + 1;
mA = zeros(numRows, numCols);
for ii in 1:numRows
mA[ii, :] = vA[ii:(ii + numCols - 1)]; #<! One could optimize the `-1` out
end
# ## Question 077
# Negate a boolean or to change the sign of a float inplace. (★★★)
vA = rand(Bool, 10);
vA .= .!vA;
vA = randn(10);
vA .*= -1;
# ## Question 078
# Consider 2 sets of points `mP1`, `mP2` describing lines (2d) and a point `vP`, how to compute distance from the point `vP` to each line `i`: `[mP1[i, :], mP2[i, :]`. (★★★)
## See distance of a point from a line in Wikipedia (https://en.wikipedia.org/wiki/Distance_from_a_point_to_a_line).
## Specifically _Line Defined by Two Points_.
numLines = 10;
mP1 = randn(numLines, 2);
mP2 = randn(numLines, 2);
vP = randn(2);
vD = [(abs(((vP2[1] - vP1[1]) * (vP1[2] - vP[2])) - ((vP1[1] - vP[1]) * (vP2[2] - vP1[2]))) / hypot((vP2 - vP1)...)) for (vP1, vP2) in zip(eachrow(mP1), eachrow(mP2))];
minDist = minimum(vD);
println("Min Distance: $minDist");
# ## Question 079
# Consider 2 sets of points `mP1`, `mP2` describing lines (2d) and a set of points `mP`, how to compute distance from the point `vP = mP[j, :]` to each line `i`: `[mP1[i, :], mP2[i, :]`. (★★★)
numLines = 5;
mP1 = randn(numLines, 2);
mP2 = randn(numLines, 2);
mP = randn(numLines, 2);
mD = [(abs(((vP2[1] - vP1[1]) * (vP1[2] - vP[2])) - ((vP1[1] - vP[1]) * (vP2[2] - vP1[2]))) / hypot((vP2 - vP1)...)) for (vP1, vP2) in zip(eachrow(mP1), eachrow(mP2)), vP in eachrow(mP)];
for jj in 1:numLines
minDist = minimum(mD[jj, :]);
println("The minimum distance from the $jj -th point: $minDist");
end
# ## Question 080
# Consider an arbitrary 2D array, write a function that extract a subpart with a fixed shape and centered on a given element (Handel out of bounds). (★★★)
## One could use `PaddedViews.jl` to easily solve this.
arrayLength = 10;
winRadius = 3;
vWinCenter = [7, 9];
mA = rand(arrayLength, arrayLength);
winLength = (2 * winRadius) + 1;
mB = zeros(winLength, winLength);
verShift = -winRadius;
## Using `global` for scope in Literate
for ii in 1:winLength
horShift = -winRadius;
for jj in 1:winLength
mB[ii, jj] = mA[min(max(vWinCenter[1] + verShift, 1), arrayLength), min(max(vWinCenter[2] + horShift, 1), arrayLength)]; #<! Nearest neighbor extrapolation
horShift += 1;
end
global verShift += 1;
end
# ## Question 081
# Consider an array `vA = [1, 2, 3, ..., 13, 14]`, generate an array `vB = [[1, 2, 3, 4], [2, 3, 4, 5], [3, 4, 5, 6], ..., [11, 12, 13, 14]]`. (★★★)
vA = collect(1:14);
winNumElements = 4;
winReach = winNumElements - 1;
vB = [vA[ii:(ii + winReach)] for ii in 1:(length(vA) - winReach)]
# ## Question 082
# Compute a matrix rank. (★★★)
numRows = 5;
numCols = 4;
mA = randn(numRows, numCols);
rank(mA)
# ## Question 083
# Find the most frequent value in an array. (★★★)
vA = rand(1:5, 15);
# MATLAB Style (Manual loop might be faster)
vB = unique(vA);
## vB[argmax(sum(vA .== vB', dims = 1)[:])] #<! The input to `argmax()` is a `1 x n` vector, hence squeezed so `argmax()` won't return Cartesian Index.
vB[argmax(dropdims(sum(vA .== vB', dims = 1), dims = 1))] #<! The input to `argmax()` is a `1 x n` vector, hence squeezed so `argmax()` won't return Cartesian Index.
# Comparing bits:
# One could convert at the bits level to integers and then use something like `counts()` from `StatsBase.jl`.
# Support to 1:4 bytes of data:
# ```julia
# numBytes = sizeof(vA[1]);
# if (sizeof(vA[1]) == 1)
# vB = reinterpret(UInt8, vA);
# elseif (sizeof(vA[1]) == 2)
# vB = reinterpret(UInt16, vA);
# elseif (sizeof(vA[1]) == 4)
# vB = reinterpret(UInt32, vA);
# elseif (sizeof(vA[1]) == 8)
# vB = reinterpret(UInt64, vA);
# end
# ```
# ## Question 084
# Extract all the contiguous `3x3` blocks from a random `5x5` matrix. (★★★)
numRows = 5;
numCols = 5;
mA = rand(1:9, numRows, numCols);
winRadius = 1;
winReach = 2 * winRadius;
winLength = winReach + 1;
mB = [mA[ii:(ii + winReach), jj:(jj + winReach)] for ii in 1:(numRows - winReach), jj in 1:(numCols - winReach)]
# ## Question 085
# Create a 2D array struct such that `mA[i, j] == mA[j, i]` (Symmetric matrix). (★★★)