Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
43 changes: 34 additions & 9 deletions Day1/assignment.rb
Original file line number Diff line number Diff line change
@@ -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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please resolve the merge conflicts.



# 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!

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of + operator please use string interpolation.
like :-
"#{ 'Ho! ' * 3 } Merry Christmas!"




Expand All @@ -29,4 +45,13 @@ def method(a, b)
# c. Finally, print result in the form
# "Your name is <user's name>"
# "Your age is <user's age>"
# 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

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@anupk18 ,

Added some comments for the Day-1 Assignment.

Binary file added Day2/assignments/.arrays_qs.rb.swp
Binary file not shown.
39 changes: 36 additions & 3 deletions Day2/assignments/arrays_qs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,34 +4,67 @@
# 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
# You can get index directly by using 'each_with_index' method
# 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"
22 changes: 21 additions & 1 deletion Day2/assignments/control_statements.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
# 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')
2 changes: 2 additions & 0 deletions Day2/assignments/extras.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 16 additions & 4 deletions Day2/assignments/file_qs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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.
# Rename the test.txt file powers.txt.
File.rename("test.txt","power.txt")
=>0
16 changes: 15 additions & 1 deletion Day2/assignments/string_qs.rb
Original file line number Diff line number Diff line change
@@ -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'
second = 'face tattoo'
puts first+second
=> Beautiful face Tattoo