diff --git a/Day1/assignment.rb b/Day1/assignment.rb index 2d1ce5d..b96d1c2 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -2,24 +2,26 @@ # -------------------------------------------------------------------------- # 1. Write a method to swap two variables. -# def method(a, b) -# Your code here.... -# end +def swap(a,b) +a,b = b,a +end # 2. Write any one use case of === operator. -# Your answer here... +Used to test equality within a when clause of a case statement. +ex: (1...10) === 5 +=> it returns true. # 3. Print array of alphabates using Range operator. -# Your answer here... +puts ('a'..'z').to_a # 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operator. -# Your answer here... +print "Ho! "*3 + "Merry Christmas!" @@ -30,3 +32,9 @@ # "Your name is " # "Your age is " # Your answer here... + +print "Enter Your Name :" +name = gets +print "Enter Your age :" +age = gets +print "Your Name is : " + name + "Your Age is : " + age \ No newline at end of file diff --git a/Day2/assignments/arrays_qs.rb b/Day2/assignments/arrays_qs.rb index a4d5731..2fcda52 100644 --- a/Day2/assignments/arrays_qs.rb +++ b/Day2/assignments/arrays_qs.rb @@ -4,26 +4,46 @@ # 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 +puts sum_of_arr(array) +=>55 + + # Question2 # Following method 'push_elements_into_array' accepts two parameters # 1. array & 2. new_element which user want to insert into array # 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 +puts push_elements_into array(arr,6) +=>[1, 2, 3, 4, 5, 6] + # Question3 # Pass array to below method # 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 +pop_from_array(arr) + + + # Question4 # a. Declare one array b. sort the array using 'sort' method # c. reverse the array d. puts element having index = 4 @@ -31,7 +51,20 @@ 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 diff --git a/Day2/assignments/file_qs.rb b/Day2/assignments/file_qs.rb index 7a4a288..65b21e4 100644 --- a/Day2/assignments/file_qs.rb +++ b/Day2/assignments/file_qs.rb @@ -4,15 +4,27 @@ # with the contents - # "This is a test. # Lets learn more about Ruby." +f = File.new("test.txt", "w") +f.write("This is a test.\nLets learn more about ruby.") +f.close - -# Question2 +# Questiont1.txt", "w") # Puts each line from file # Using method 'readlines' to read the above .txt file into an array - +File.readlines("test.txt").each do |line| + puts line +end +This is a test. + Lets learn more about ruby. + => ["This is a test. \n", " Lets learn more about ruby.\n"] # Question3 # Check if the file new.txt exists. +File.exists?("test.txt") +=>true + # Question4 -# Rename the test.txt file powers.txt. \ No newline at end of file +# Rename the test.txt file powers.txt. +File.rename("test.txt","power.txt") +=>0 diff --git a/Day2/assignments/string_qs.rb b/Day2/assignments/string_qs.rb index c2de2f4..163418f 100644 --- a/Day2/assignments/string_qs.rb +++ b/Day2/assignments/string_qs.rb @@ -1,19 +1,33 @@ # Question1 # Get the first letter from the string "Hello, Ruby!!". +str[0] +=> "H" # Question2 # Get the first through 5th elements from the "Hello, Ruby!!" string. +str[4] +=>"0" # Question3 # Check the 'sprintf' documentation and the % documentation in the String class # and write one line code to print exact following line # "1$ = Rs60.48" +sprintf("1$ = Rs %#.2f",60.48) +=> "1$ = Rs 60.48" + # Question4 # What is the problem with the following code? Correct it and puts. # my variable = 'Mr. White' +my_variable='Mr. White' +puts my_variable +=>Mr. White + + # Question5 # Print the result - Concatenate the following strings first = 'Beautiful ' -second = 'face tattoo' \ No newline at end of file +second = 'face tattoo' +puts first+second +=> Beautiful face Tattoo