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
Empty file added Day1/.assignment.rb
Empty file.
38 changes: 25 additions & 13 deletions Day1/assignment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,31 +2,43 @@
# --------------------------------------------------------------------------

# 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}"
Copy link
Collaborator

Choose a reason for hiding this comment

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

Don't write puts in method. It should only return me swapped values.

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
Copy link
Collaborator

Choose a reason for hiding this comment

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

Write here in text



# 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!
Copy link
Collaborator

Choose a reason for hiding this comment

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

👍


# 5. Write a ruby program that perform following operations:
Copy link
Collaborator

Choose a reason for hiding this comment

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

Write as:

What is your name : <user should enter name here>
What is your age   : <user should enter age here>

Your name is <user's name> .(full stop should be there)
Your age is <user's age>.

# a. Ask user his/her name
# b. Ask user his/her age
# c. Finally, print result in the form
# "Your name is <user's name>"
# "Your age is <user's age>"
# 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
26 changes: 22 additions & 4 deletions Day2/assignments/arrays_qs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -13,15 +17,20 @@ 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
# 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

# Question4
Expand All @@ -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"}