loading...

Day 20: #100DaysofCode - Practice makes perfect

sincerelybrittany profile image Brittany ・2 min read

#100DaysofCode (65 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 63 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables

Today I had a rest day. I watched a few videos on Big O Notation and practice the Ruby section on HackerRank

I pushed it to my Github here

In addition, I did one coding challenge on HackerRank.

The question ask: Given an array of integers, print each element in reverse order as a single line of space-separated integers.

Now the simple way to do this was to use the .reverse method like the following:

def reverseArray(a)
   a.reverse
end

But that is too easy and it would not really make it a challenge now would it? I decided to use repl.it and work this example out using [1,2,3,4,5] as my sample array.

This first thing I wanted to do was iterate over the array like so:

def reverseArray(a)
    a.each_with_index do |num, index|  
    end
end

Now essentially you want to change the index of the array, but first its easier to reference it if the numbers are by 1 so lets add the (index+1)

def reverseArray(a)
    a.each_with_index do |num, index|  
       puts (index+1)
    end
end

which will return your indexes plus one:

1
2
3
4
5

So if you want to reference all an index in the a array you might try this:

def reverseArray(a)
    a.each_with_index do |num, index|  
        puts (a[index+1])
    end
end

But that wont work because when you think about it the code above is looking for the following in your array:

a[1] // 2
a[2] // 3
a[3] // 4
a[4] // 5
a[5] // N/A

I had to find a way to reference the index in reverse. I had to think back to what can reverse/negate something. The negative(-) sign helped me with this:

def reverseArray(a)
    a.each_with_index do |num, index|  
        puts (a[-(index+1)])
    end
end

Which returned the following:

5
4
3
2
1

Look at that, I returned a reversed array.

Next, using the array review I accomplished on HackerRank, I was able to complete this challenge. I knew that the .push() method adds new items to the end of an array, and returns the new length. I needed to push the a array into a new_array so that it could be returned, like so:

def reverseArray(a)
  new_array = []
    a.each_with_index do |num, index|  
        new_array.push(a[-(index+1)])
    end
    return new_array
end

This took me a hour and a half and I do not want people who read this to think this was simple. I am a code newbie and although I was able to figure this code out, it was not easy. Keep that in mind as you code. It may take hours, but you will get it and feel great when you accomplish something you never thought was possible.

Happy Saturday!

Song of the Day

#100DaysofCode (65 Part Series)

1) Day 1 of #100DaysofCode - Understanding Iterators 2) Day 2: #100DaysofCode - I updated to Catalina and All HELL Broke Loose 3 ... 63 3) Day 3: #100DaysofCode - Oh No, SQL 4) Day 4: #100DaysofCode - More Iterators 5) Day 5 - #100DaysofCode - Setting up a Sinatra App 6) Day 6 : #100DaysofCode - Setting up a Sinatra App : Part 2 - Config.ru 7) Day 7: #100DaysofCode - Setting up a Sinatra App - Part 3 8) Day 8: #100DaysofCode - Setting up Sinatra Database - Part 4 9) Day 9: #100DaysofCode - ActiveRecord and a Database 10) Day 10: #100DaysofCode - ActiveRecord and a Database 11) Day 11 : #100DaysofCode - RESTful Routes 12) Day 12: #100DaysofCode and Day 1 of #Javascript30 13) Day 13 - #100DaysofCode - #Javascript30 - Day 2 - CSS + JS Clock 14) Day 14: #100DaysofCode - Finalized my Sinatra Project -Security 15) #Day 15: #100DaysofCode - View my Sinatra Project 16) Day 16 - #100DaysofCode - Understanding MVC 17) Day 17 : #100DaysofCode - Knowing your Ruby Version & what Errno::EADDRINUSE means is important! 18) Day 18 of #100daysofCode - 3 challenges 19) Day 19 of #100daysofCode - Hashes 101 20) Day 20: #100DaysofCode - Practice makes perfect 21) Day 21 : #100DaysofCode - Cascading Style Sheets 101 22) Day 22 - #100daysofCode - CSS 101 Part 2 23) Day 23 - #100DaysofCode - Updated My Portfolio 24) Day 24 - #100DaysofCode - Practicing CSS Grid and Emmet Shortcuts on VSCode 25) Day 25 - #100DaysofCode - Intro to Rails 26) Day 26 - #100DaysofCode - Accessing Rails Commands 27) Day 27 : #100DaysofCode - Still reviewing the basic rails concepts . . . 28) Day 28 : #100DaysofCode - Adding Dev.to blogs to personal page 29) Day 29 : #100DaysofCode - Very Simple Rails App CRUD Practice 30) Day 30 : #100DaysofCode - Very basic rails continued 31) Day 31 - #100DaysofCode - Rails Routes 101 32) Day 32 : #100DaysofCode - Resources for learning to code 33) Day 33 - #100DaysofCode - Setting up my app on Heroku 34) Day 34 - #100DaysofCode - New/edit action versus create/update action 35) Day 35 : #100DaysofCode - A Code Challenge Completed 36) Day 36 - #100DaysofCode - Rails form_for versus form_tag 37) Day 37 : #100DaysofCode - 30 seconds of code 38) Day 38 - #100DaysofCode - Built my first basic rails application 39) Day 39 : #100DaysofCode - I updated my github profile page 40) Day 40 : #100DaysofCode - I need project ideas 41) Day 41 : #100DaysofCode - Collaboration 42) Day 42 : #100DaysofCode - The Planning of a Project 43) Day 43 : #100DaysofCode - The Amazing Faker Gem 44) Day 44 : #100DaysofCode - Code or Youtube? 45) Day 45 : #100DaysofCode - Basic Nested Forms 46) Day 46 : #100DaysofCode - Still reviewing form_for 47) Day 47 : #100DaysofCode - How do you code? 48) Day 48 : #100DaysofCode - Code Along 49) Day 49 : #100DaysofCode - Still Coding Along 50) Day 50 : #100DaysofCode - Keep Coding Along 51) Day 51 : #100DaysofCode - Code Along 52) Day 52 : #100DaysofCode - No Wifi, No Problem 53) Day 53: #100DaysofCode - Created user log in and log out functionality 54) Day 54 : #100DaysofCode -Review on setting up log in & sign up 55) Day 55 : #100DaysofCode - In Need of Advice on Reading Poorly Written Documentation 56) Day 56 : #100DaysofCode - A Tweet - Devise - Users 57) Day 57 : 100DaysofCode - Remembering to Git Commit Often 58) Day 58 : #100DaysofCode - Beginning to Learn Python 59) Day 59 : #100DaysofCode - Focused on My Rails Project 60) Day 60 : #100DaysofCode - CSS in Rails 61) Day 61 : #100DaysofCode - Omniauth? 62) Day 62 : #100DaysofCode - Sign in using a Third Party, Rails 63) Day 63 : #100DaysofCode - Refactoring 64) Day 64 : #100DaysofCode - Still Playing Around With OmniAuth 65) Day 65 : #100DaysofCode - Second Rails Project - I Want to Try Self-Referencing Tables

Posted on by:

sincerelybrittany profile

Brittany

@sincerelybrittany

Web Developer | Software Engineer 👩🏾‍💻 | Determined | Music & Dance | #100DaysofCode | #WomenWhoCode

Discussion

markdown guide