From b29059b50b3ebdc1f3d43dac4bd1ddaa25b863e0 Mon Sep 17 00:00:00 2001 From: "Anup.Kulkarni" Date: Mon, 6 Oct 2014 13:16:59 +0530 Subject: [PATCH 1/6] assignment submitted --- Day1/assignment.rb | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/Day1/assignment.rb b/Day1/assignment.rb index 2d1ce5d..b9a082d 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -2,24 +2,31 @@ # -------------------------------------------------------------------------- # 1. Write a method to swap two variables. -# def method(a, b) -# Your code here.... -# end +def swap_num(a,b) + c=a + a=b + b=c + puts "A=#{a} B=#{b}" +end +swap_num(10,20) +=>A=20 B=10 + + # 2. Write any one use case of === operator. -# Your answer here... +# # 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... + @@ -29,4 +36,4 @@ # c. Finally, print result in the form # "Your name is " # "Your age is " -# Your answer here... + From 15a7b79cee58064e90b5d58377467f6d803192a6 Mon Sep 17 00:00:00 2001 From: "Anup.Kulkarni" Date: Mon, 6 Oct 2014 15:14:11 +0530 Subject: [PATCH 2/6] Assignment Submitted --- Day1/assignment.rb | 22 +++++++++++++++++++--- 1 file changed, 19 insertions(+), 3 deletions(-) diff --git a/Day1/assignment.rb b/Day1/assignment.rb index b9a082d..7fd74bc 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -13,10 +13,16 @@ def swap_num(a,b) +# 2. Write any one use case of === operator. +str = 'fifty' +str === 50 +=> false +str === 'Fifty' +=> false -# 2. Write any one use case of === operator. -# +str === 'fifty' +=> true @@ -26,7 +32,8 @@ def swap_num(a,b) # 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operator. - +'Ho! '*3+"Merry Christmas!" +=>Ho! Ho! Ho! Merry Christmas! @@ -36,4 +43,13 @@ def swap_num(a,b) # c. Finally, print result in the form # "Your name is " # "Your age is " +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 From 71f8de3fcd20c873839c2063051be069e970456b Mon Sep 17 00:00:00 2001 From: "Anup.Kulkarni" Date: Tue, 7 Oct 2014 15:58:42 +0530 Subject: [PATCH 3/6] Assignment submittedgit status --- Day2/assignments/.arrays_qs.rb.swp | Bin 0 -> 12288 bytes Day2/assignments/arrays_qs.rb | 36 ++++++++++++++++++++++++++--- 2 files changed, 33 insertions(+), 3 deletions(-) create mode 100644 Day2/assignments/.arrays_qs.rb.swp diff --git a/Day2/assignments/.arrays_qs.rb.swp b/Day2/assignments/.arrays_qs.rb.swp new file mode 100644 index 0000000000000000000000000000000000000000..2959031c3fdf326c7920fafe276a379a6a9aec7b GIT binary patch literal 12288 zcmeI2&x;&I6vr!~2u7pEgO?oMW+0O#J>5GqI|(doU}2H05Q)J=BrZcucg;*gPgh%A zop#8IC;tXL+_`IfvKq?O#>zy?UQ|H83+Z z*|@!Tov!#pf#WS9&fXt>m}0W* zg}N}g3U=jk5Xd}_XX!$vHV>pRa+!}m%Y8FB(Jyyk0!-j}2%HhaL4TujevRHad+T`& zxDOLx0!)AjFaajO1egF5U;-}?0h^r?Pm#|{P#-$g2p2w>025#WOn?b60Vco%m;e)C z0!)AjFo73@fQ*Fby(Yvfr;!l;|98Iu{Q9O4zd%1jUqT;1TTlRLqCAp`A0pF%gFA@uZhA^w7XgMNg*haN!R zLLqbs`WxeY2>k$QD1|1_`;g1!WrzNfC(@GCh$xG9s1bBR#B)UMOIU8 zwNW@5ic>sRn^8&Tbfu)n|FBTGjdeN_ zc(|sF@+hTkQi*b#T}e-y2p148QlF!#zM`S+6Q^|_IFP=Q;e1@ic0R8A-J^rlt8EU* z$3b79pPKn3;pj>td zPe$Fv4qi~2t7g=ZPg!B})lld1u5;6-vRMt8c2$^2K-Fn$H}NUg#vbw1#CLCx1a0YU z3Zc?S5OrwTq!DR~Ez;fUt&2!aDbv|_YV=}U&Dcep*AcyOi^kTgL%!p77^8L1DkCxX zx*<$5jhcjjd6kuOyPAR<*s9!QkMfwPM3;w<+VNsX_4d^kzDJuLjXb)%FDh7Bk|0jk?DP)#-IfhM~$(aJJNxNh4u_l7y3%`~Zu5`|4;^qt1)PSWlg?gDaPZ zaM9w})>Y*o6W(COxbr271zy0ST5g0=JBSb5tGdrdRdBdJ;@bglP`OTr%Qr`|&Q-sG z?p7EjT4yjgzA&net_m9z74T~4?lWkW@sWh_rM*Kp)fA9>G`BX}3WBmM{c4MSq(fhu WSrFngtvB%d{vg55 + + # 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.. :-) + arr.each do |i| + 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,6 +50,17 @@ 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!!!' From c4fff404a2e2aa16112b5c7cb49b3850368fdeb8 Mon Sep 17 00:00:00 2001 From: "Anup.Kulkarni" Date: Mon, 13 Oct 2014 13:11:48 +0530 Subject: [PATCH 4/6] Day2 assignments --- Day2/assignments/file_qs.rb | 20 ++++++++++++++++---- Day2/assignments/string_qs.rb | 16 +++++++++++++++- 2 files changed, 31 insertions(+), 5 deletions(-) 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 From 2cdec85a7745ad4c33c3ac5df16f70e054da6c1f Mon Sep 17 00:00:00 2001 From: "Anup.Kulkarni" Date: Thu, 16 Oct 2014 17:34:13 +0530 Subject: [PATCH 5/6] assignments updated --- Day2/assignments/arrays_qs.rb | 5 ++++- Day2/assignments/extras.rb | 2 ++ 2 files changed, 6 insertions(+), 1 deletion(-) diff --git a/Day2/assignments/arrays_qs.rb b/Day2/assignments/arrays_qs.rb index 827515f..2fcda52 100644 --- a/Day2/assignments/arrays_qs.rb +++ b/Day2/assignments/arrays_qs.rb @@ -33,7 +33,8 @@ 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) - arr.each do |i| + l=arr.length + l.each do num=arr.pop puts "#{num} is removed!" end @@ -65,3 +66,5 @@ def pop_from_array(arr) # 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/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 From 9c1dcf767e3f444ce10e0b382f3f4737eee8f699 Mon Sep 17 00:00:00 2001 From: "Anup.Kulkarni" Date: Fri, 17 Oct 2014 18:28:21 +0530 Subject: [PATCH 6/6] assignments updated --- Day1/assignment.rb | 32 +++++++++++--------------- Day2/assignments/control_statements.rb | 22 +++++++++++++++++- 2 files changed, 34 insertions(+), 20 deletions(-) diff --git a/Day1/assignment.rb b/Day1/assignment.rb index 467ca16..b65acfe 100644 --- a/Day1/assignment.rb +++ b/Day1/assignment.rb @@ -4,34 +4,28 @@ <<<<<<< HEAD # 1. Write a method to swap two variables. def swap_num(a,b) - c=a - a=b - b=c - puts "A=#{a} B=#{b}" + a = a + b + b = a - b + a = a - b + return a, b end swap_num(10,20) -=>A=20 B=10 + ======= # 1. Write a method to swap two variables. -def method(a, b) - my_cde +def swap_it(a, b) + return b, a end - +swap_it(10,20) >>>>>>> upstream/master # 2. Write any one use case of === operator. -str = 'fifty' -str === 50 -=> false - -str === 'Fifty' -=> false - -str === 'fifty' -=> true - +def equals?(a, b) +a === b +end +equals?(1, "one") # 3. Print array of alphabates using Range operator. @@ -40,7 +34,7 @@ def method(a, b) # 4. Print 'Ho! Ho! Ho! Merry Christmas!' using string interpolation and * operator. -'Ho! '*3+"Merry Christmas!" +"#{'Ho! '*3}Merry Christmas!" =>Ho! Ho! Ho! Merry Christmas! 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