Level 1

1.

Print all elements of an array using for loop.

Answer

```
a = [1,2,3,4,5]
for i in a
puts i
end
```

2.

Take inputs from user to make an array. Again take one input from user and search it in the array and delete that element, if found. Iterate over array using for loop.

3.

Print multiplication table of 14 from an array in which multiplication table of 12 is stored.

Hint

This type of example is given in tutorial

4.

You are given with an array of integer elements. Make a new array which will store square of elements of previous array.

Hint

```
#hint
for i in a
b.push(i**2)
end
```

5.

Using **(1...101)**, make two array, one containing all even numbers and other containing all odd numbers.

Hint

```
#hint
for i in range(1..100)
if i%2 == 0
even.push(i)
else
odd.push(i)
end
end
```

6.

From the two array obtained in previous question, make new lists, containing only numbers which are divisible by 4, 6, 8, 10, 3, 5, 7 and 9 in separate lists.

Hint

```
#hint
for i in even
if i%4 == 0
four.push(i)
end
if i%6 == 0
six.push(i)
end
end
```

7.

From an array containing integers, strings and floats, make three lists to store them separately

Level 2

1.

Using **(1...101)**, make an array containing only prime numbers.

Hint

You can use sieve algorithm to find prime number and it is more efficent. See this answer

```
#hint
#Check for prime number
#is the the number to be checked for
j = 2
count = 0
while j <= i
if i%j == 0
count = count+1
end
j = j+1
end
#if count > 1 then it is not prime
```

2.

Initialize a 2D array of 3*3 matrix. E.g.-

Check if the matrix is symmetric or not.

1 | 2 | 3 |

4 | 5 | 6 |

7 | 8 | 9 |

Check if the matrix is symmetric or not.

Answer

```
a = [[1,2,3],[2,4,5],[3,5,6]]
sym = true
for i in (0...3)
for j in (0...3)
if a[i][j]!=a[j][i]
sym = false
end
end
end
puts "#{sym}"
```

3.

Sorting refers to arranging data in a particular format. Sort an array of integers in ascending order ( without using built-in sorted function ). One of the algorithm is selection sort. Use below explanation of selection sort to do this.

INITIAL ARRAY :

First iteration : Compare every element after first element with first element and if it is larger then swap. In first iteration, 2 is larger than 1. So, swap it.

Second iteration : Compare every element after second element with second element and if it is larger then swap. In second iteration, 3 is larger than 2. So, swap it.

Third iteration : Nothing will swap as 3 is smaller than every element after it.

Fourth iteration : Compare every element after fourth element with fourth element and if it is larger then swap. In fourth iteration, 45 is larger than 15. So, swap it.

INITIAL ARRAY :

2 | 3 | 1 | 45 | 15 |

1 | 3 | 2 | 45 | 15 |

1 | 2 | 3 | 45 | 15 |

1 | 2 | 3 | 45 | 15 |

1 | 2 | 3 | 15 | 45 |

Answer

```
a = [2,3,1,45,15]
for i in (0...a.length)
for j in (i+1...a.length)
if a[i]>a[j]
a[i],a[j] = a[j],a[i]
end
end
end
puts "#{a}"
```

Recent Posts

Beginning with ML 2.0: Multivariate Linear Regression

Beginning with Machine Learning: Linear Regression

Scrolling Progress Indicator

Getting Notebook Paper Effect with CSS