Skip to content

Commit fddbdf7

Browse files
Add files via upload
1 parent c19522f commit fddbdf7

File tree

2 files changed

+83
-0
lines changed

2 files changed

+83
-0
lines changed

Project 1/First-Project.rb

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
# In this project we will take input from user and then print some output based on this condition
2+
# Street Number and Letter is not Combined and we will see according to first,second etc
3+
4+
=begin
5+
What is Your Full Name: John Doe
6+
What is Your Street Address: 1002 First Street
7+
Your First Name is: John
8+
Your Last Name is: Doe
9+
Your Street Number is: 1002
10+
Your Street Letter means: A-Block
11+
=end
12+
13+
print "What is Your Full Name: "
14+
name=gets.chomp
15+
# Splitting First and Last Name using split function and storing them in array a1
16+
a1=name.split(" ")
17+
18+
print "What is Your Street Address: "
19+
address=gets.chomp
20+
# Splitting Street Number and Letter using split function and storing them in array a2
21+
a2=address.split(" ")
22+
23+
# Making hash to store block corresponding to Street Letter
24+
hash_add=Hash["First"=>"A-Block","Second"=>"B-Block","Third"=>"C-Block","fourth"=>"D-Block"]
25+
26+
# Directly accessing First Name from already created array a1
27+
print "Your First Name is: ",a1[0],"\n"
28+
29+
# Directly accessing Last Name from already created array a1
30+
print "Your Last Name is: ",a1[1],"\n"
31+
32+
# Directly accessing Street Number from already created array a2
33+
print "Your Street Number is: ",a2[0],"\n"
34+
35+
# Now since we are trying to find the block Corresponding to Street Letter, we need to access the key in hash Corresponding to the Street Letter in array a2
36+
# First with a2[1], we access Street Letter then convert it to string and pass it to hash as a key to access Corresponding block
37+
print "Your Street Letter means: ",hash_add[a2[1].to_s]

Project 1/Second-Project.rb

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# In this project we will take input from user and then print some output based on this condition
2+
# Street Number and Letter is Combined
3+
4+
=begin
5+
What is Your Full Name: John Doe
6+
What is Your Street Address: 1006D First Street
7+
Your First Name is: John
8+
Your Last Name is: Doe
9+
Your Street Number is: 1006
10+
Your Street Letter means: D-Block
11+
=end
12+
13+
print "What is Your Full Name: "
14+
name=gets.chomp
15+
# Splitting First and Last Name using split function and storing them in array a1
16+
a1=name.split(" ")
17+
18+
print "What is Your Street Address: "
19+
address=gets.chomp
20+
# Splitting Street Number and Letter using split function and storing them in array a2
21+
a2=address.split(" ")
22+
23+
# Making hash to store block corresponding to Street Letter
24+
hash_add=Hash["A"=>"A-Block","B"=>"B-Block","C"=>"C-Block","D"=>"D-Block"]
25+
26+
# Directly accessing First Name from already created array a1
27+
print "Your First Name is: ",a1[0],"\n"
28+
29+
# Directly accessing Last Name from already created array a1
30+
print "Your Last Name is: ",a1[1],"\n"
31+
32+
# Using Indexing
33+
# We cannot directly access Street Number from already created array a2 as Street Number and Letter is combined
34+
# First we extract first element of this array and then we access all elements except the last element which is street letter
35+
print "Your Street Number using Indexing Method is: ",a2[0].to_s[0,a2[0].length-1],"\n"
36+
37+
# Using Chomp method
38+
# Chomp is used to remove given argument from a variable
39+
# If we does not give any argument it takes newline as default argument
40+
# We are using this here for removing the Street Letter from a2[0]
41+
print "Your Street Number using Chomp Method is: ",a2[0].chomp(a2[0].to_s[-1]),"\n"
42+
43+
# Now since we are trying to find the block Corresponding to Street Letter, we need to access the key in hash Corresponding to the Street Letter in array a2
44+
# First with a2[0], we access first element which is Street Number and Letter then convert it to string
45+
# Since it is combined with street number,we access the last element using -1 and pass it to hash as a key to access Corresponding block
46+
print "Your Street Letter means: ",hash_add[a2[0].to_s[-1]]

0 commit comments

Comments
 (0)