diff --git a/Day1/.assignment.rb b/Day1/.assignment.rb new file mode 100644 index 0000000..e69de29 diff --git a/Day1/assignment.rb b/Day1/assignment.rb index 2d1ce5d..e4e506c 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -2,26 +2,32 @@ # -------------------------------------------------------------------------- # 1. Write a method to swap two variables. -# def method(a, b) -# Your code here.... -# end + def swap(a,b) + a=a+b + b=a-b + a=a-b + puts "a=#{a} b=#{b}" + end + => swap(20,50) + [50,20] -# 2. Write any one use case of === operator. -# Your answer here... - +# 2. Write any one use case of === aperator + (1..9) === 5.16732 returns true + # 3. Print array of alphabates using Range operator. -# Your answer here... - - - -# 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operator. -# Your answer here... + ('a'..'m').to_a returns all alphabets starting from A to M. + ["a", "b", "c", "d", "e", "f", "g", "h", "i", "j", "k", "l", "m"] +# 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operato + a = 'Ho!' * 3 + b = 'Merry christmas!' + puts a+b + Ho!Ho!Ho!Merry Cristmas! # 5. Write a ruby program that perform following operations: # a. Ask user his/her name @@ -29,4 +35,10 @@ # c. Finally, print result in the form # "Your name is " # "Your age is " -# Your answer here... + @name = gets + @age = gets + + puts "Your name is #{@name}" + puts "your name is #{@age}" + Your name is Pankaj + Your age is 25 diff --git a/Day2/assignments/arrays_qs.rb b/Day2/assignments/arrays_qs.rb index a4d5731..f91a8c2 100644 --- a/Day2/assignments/arrays_qs.rb +++ b/Day2/assignments/arrays_qs.rb @@ -4,7 +4,11 @@ # Declare one array arr1 and method with array as parameter # return sum of elements in arr1 using each loop def sum_of_arr(arr) - # do whatever you want... + sum = 0 + arr.each do |i| + sum = sum + i + end + sum end # Question2 @@ -13,7 +17,8 @@ def sum_of_arr(arr) # Method will return array with new element. # Add new_element into array using 'push' method of Array def push_elements_into_array(arr, ele) - # you can do it.. :-) + arr.push(ele) + arr end # Question3 @@ -21,7 +26,11 @@ def push_elements_into_array(arr, ele) # Iterate on array and puts each element from array # using 'pop' method of Array def pop_from_array(arr) - # you can do it.. :-) + l=arr.length + l.each do + num=arr.pop + puts "#{num} is removed!" + end end # Question4 @@ -31,7 +40,16 @@ def pop_from_array(arr) # eg. a = [1, 2, 3] # a.each_with_index { |n, index| .... } # Here, n = element of array & index = index of element in array - + a=[9,2,4,6,8,1,3,7,5,10] +=> [9, 2, 4, 6, 8, 1, 3, 7, 5, 10] +a.sort +=> [1, 2, 3, 4, 5, 6, 7, 8, 9, 10] +a.reverse +=> [10, 5, 7, 3, 1, 8, 6, 4, 2, 9] +puts a[4] +=> 8 # Question5 # str = 'Hello Ruby!!!' # Write code to store str into array as ['Hello fun', 'Ruby!!! fun'] +arr = str.split(/ /) +arr.map {|i| i +" fun"} \ No newline at end of file