/
HW2 10-27-13.txt
91 lines (78 loc) · 3.25 KB
/
HW2 10-27-13.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
88
89
90
91
#HWQ1
C:\Sites>irb
<a=[[2,5,7,8,10],[9,1,4,0,9],[10,1,3,4,9],[0,2,-1,4,19],[1,2,3,4,5]]
=> [[2, 5, 7, 8, 10], [9, 1, 4, 0, 9], [10, 1, 3, 4, 9], [0, 2, -1, 4, 19], [1,
2, 3, 4, 5]]
irb(main):002:0> a[0].sort!
=> [2, 5, 7, 8, 10]
irb(main):003:0> a[1].sort!
=> [0, 1, 4, 9, 9]
irb(main):004:0> a[2].sort!
=> [1, 3, 4, 9, 10]
irb(main):005:0> a[3].sort!
=> [-1, 0, 2, 4, 19]
irb(main):006:0> a[4].sort!
=> [1, 2, 3, 4, 5]
irb(main):007:0> a.sort
=> [[-1, 0, 2, 4, 19], [0, 1, 4, 9, 9], [1, 2, 3, 4, 5], [1, 3, 4, 9, 10], [2, 5
, 7, 8, 10]]
irb(main):008:0> a
=> [[2, 5, 7, 8, 10], [0, 1, 4, 9, 9], [1, 3, 4, 9, 10], [-1, 0, 2, 4, 19], [1,
2, 3, 4, 5]]
# I recognize that there is probably a simpler way to do this via loops
# but it escapes me, and I want to press on so that at least I've looked at every
# HW problem, instead of never getting past the first one. I suspect I will end up
# brute forcing all of these problems, for lack of time to search for a more elgant
# and shorter solution. :(
#HWQ2
# here is what I googled, but i don't really understand why it works, so it feels like cheating
C:\Sites>irb
<8,10],[9,1,4,0,9],[10,1,3,4,9],[0,2,-1,4,19],[1,2,3,4,5]]
=> [[2, 5, 7, 8, 10], [9, 1, 4, 0, 9], [10, 1, 3, 4, 9], [0, 2, -1, 4, 19], [1,
2, 3, 4, 5]]
irb(main):002:0> a.reduce(:concat)
=> [2, 5, 7, 8, 10, 9, 1, 4, 0, 9, 10, 1, 3, 4, 9, 0, 2, -1, 4, 19, 1, 2, 3, 4,
5]
irb(main):003:0> a
=> [[2, 5, 7, 8, 10, 9, 1, 4, 0, 9, 10, 1, 3, 4, 9, 0, 2, -1, 4, 19, 1, 2, 3, 4,
5], [9, 1, 4, 0, 9], [10, 1, 3, 4, 9], [0, 2, -1, 4, 19], [1, 2, 3, 4, 5]]
# and it screws up the array, evidently having all of the original arrays, and all of the
# items in the original array together in an array. IDEK
# here i am specifically going against directions. I used the flatten function. :(
# and then i shrunk it.
C:\Sites>irb
<f=[[2,5,7,8,9],[9,1,4,0,9],[10,1,3,4,9],[0,2,-1,4,19],[1,2,3,4,5]]
=> [[2, 5, 7, 8, 9], [9, 1, 4, 0, 9], [10, 1, 3, 4, 9], [0, 2, -1, 4, 19], [1, 2
, 3, 4, 5]]
irb(main):002:0> f.flatten
=> [2, 5, 7, 8, 9, 9, 1, 4, 0, 9, 10, 1, 3, 4, 9, 0, 2, -1, 4, 19, 1, 2, 3, 4, 5
]
irb(main):005:0> f.flatten!
=> [2, 5, 7, 8, 9, 9, 1, 4, 0, 9, 10, 1, 3, 4, 9, 0, 2, -1, 4, 19, 1, 2, 3, 4, 5
]
irb(main):009:0> f.sort!
=> [-1, 0, 0, 1, 1, 1, 2, 2, 2, 3, 3, 4, 4, 4, 4, 5, 5, 7, 8, 9, 9, 9, 9, 10, 19
irb(main):011:0> f.uniq!
=> [-1, 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 19]
irb(main):012:0> f
=> [-1, 0, 1, 2, 3, 4, 5, 7, 8, 9, 10, 19]
#HWQ 3
irb(main):015:0>a
=> [[2, 5, 7, 8,10], [9, 1, 4, 0, 9], [10, 1, 3, 4, 9], [0, 2, -1, 4, 19], [1, 2
, 3, 4, 5]]
irb(main):016:0>b=[[a[4][0],a[3][0],a[2][0],a[1][0],a[0][0]],[a[4][1],a[3][1],a[2][1],a[1][1],
a[0][1]],[a[4][2],a[3][2],a[2][2],a[1][2],a[0][2]],[a[4][3],a[3][3],a[2][3],a[1][3],a[0][3]],
[a[4][4],a[3][4],a[2][4],a[1][4],a[0][4]]]
=> [[1, 0, 10, 9, 2], [2, 2, 1, 1, 5], [3, -1, 3, 4, 7], [4, 4, 4, 0, 8], [5, 19
, 9, 9, 10]
irb(main):017:0> p(b)
[[1, 0, 10, 9, 2], [2, 2, 1, 1, 5], [3, -1, 3, 4, 7], [4, 4, 4, 0, 8], [5, 19, 9
, 9, 10]]
=> [[1, 0, 10, 9, 2], [2, 2, 1, 1, 5], [3, -1, 3, 4, 7], [4, 4, 4, 0, 8], [5, 19
, 9, 9, 10]]
irb(main):018:0> p b
[[1, 0, 10, 9, 2], [2, 2, 1, 1, 5], [3, -1, 3, 4, 7], [4, 4, 4, 0, 8], [5, 19, 9
, 9, 10]]
=> [[1, 0, 10, 9, 2], [2, 2, 1, 1, 5], [3, -1, 3, 4, 7], [4, 4, 4, 0, 8], [5, 19
, 9, 9, 10]]
irb(main):019:0>