diff --git a/Day1/assignment.rb b/Day1/assignment.rb index f5e05fe..b65acfe 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -1,25 +1,41 @@ # Day 1 : Assignments # -------------------------------------------------------------------------- -# 1. Write a method to swap two variables. -def method(a, b) - my_cde +<<<<<<< HEAD +# 1. Write a method to swap two variables. +def swap_num(a,b) + a = a + b + b = a - b + a = a - b + return a, b end +swap_num(10,20) + +======= +# 1. Write a method to swap two variables. +def swap_it(a, b) + return b, a +end +swap_it(10,20) +>>>>>>> upstream/master # 2. Write any one use case of === operator. -# Your answer here... - +def equals?(a, b) +a === b +end +equals?(1, "one") # 3. Print array of alphabates using Range operator. -# Your answer here... - +('a'..'h').to_a +=> ["a","b","c","d","e","f","g","h"] # 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operator. -# Your answer here... +"#{'Ho! '*3}Merry Christmas!" +=>Ho! Ho! Ho! Merry Christmas! @@ -29,4 +45,13 @@ def method(a, b) # c. Finally, print result in the form # "Your name is " # "Your age is " -# Your answer here... +def demo_func() + puts "Enter Your Name" + name = gets.chomp + puts "Enter Your Age" + age = gets.chomp + puts "hey #{name} of age #{age}" +end +demo_func() +=> Hey Rakesh of age 28 + diff --git a/Day2/assignments/.arrays_qs.rb.swp b/Day2/assignments/.arrays_qs.rb.swp new file mode 100644 index 0000000..2959031 Binary files /dev/null and b/Day2/assignments/.arrays_qs.rb.swp differ 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/control_statements.rb b/Day2/assignments/control_statements.rb index 50593cc..391b404 100644 --- a/Day2/assignments/control_statements.rb +++ b/Day2/assignments/control_statements.rb @@ -4,7 +4,27 @@ # Return true if any of the elements in the random_keys array # start with the letter "a" and false otherwise. random_keys = ["all", "alone", "children"] +def find_chr(random_keys,char) +random_keys.each do |e| + if e.include?(char) + puts "True" + else + puts "False" + end +end + +find_chr(random_keys,'a') + # Question2 # Return true if the string "stimpy" contains -# the letter "s" and false otherwise. \ No newline at end of file +# the letter "s" and false otherwise. +def check_str(str,s) + if str.include?s + puts "true" + else + puts "false" + end +end + +check_str(str,'s') \ No newline at end of file diff --git a/Day2/assignments/extras.rb b/Day2/assignments/extras.rb index b33bf7a..411a0bc 100644 --- a/Day2/assignments/extras.rb +++ b/Day2/assignments/extras.rb @@ -38,7 +38,9 @@ def foo puts foo # Write your difference here: +Code 1: Here the the loop goes for 10 times and prints prints each num from 0 to 9. foo function returns value 10 as loop goes for 10 times. +Code 2: here it would same as above but the change here is as we print the return value of function foo 10 gets printed at last and puts returns nil value. # Assignment 3 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