Skip to content

daniel-enqz/CollatzConjecture

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

15 Commits
 
 
 
 
 
 
 
 

Repository files navigation

Collatz Conjection Problem 🦾

"Just pick a number, any number: If the number is even, cut it in half; if it’s odd, triple it and add 1. Take that new number and repeat the process, again and again." - Patrick Honner

Goals:🏁

✅ Create a function that receives a positive integer x, sequentially applies the Collatz Conjecture process and returns how many steps it took to reach integer 1.
✅ Create another function that returns all the integers in the sequence generated by starting in x and finishing in 1, given the Collatz Conjecture process.
✅ Store Collatz Conjecture Sequence in memory

How to run this project 🤠

git clone git@github.com:daniel-enqz/CollatzConjecture.git
cd CollatzConjecture

👽 If you want to try the interactive terminal project, run: ruby lib/interface

👻 Type rake the to run tests:

I tried my best commenting each part of code, I hope it helps👌

🤔Question:

Imagine a social media platform where users can reply to other user’s comments creating a thread. What should happen to a user’s comments if such a user wants to be deleted from the database?

🤐Answer:

As we want to mantain those comments (from the user we are deleting) active in the database, because other Users may have responded to that comment before. We can follow the next apporach:

This is how active record models will look like🤓

I would say this model is one of the most relevant for the exercise, we will nullify the index from the user in the comments table, if that user is destroyed. This will help us not deleting records(messages) from the database, and get RubyOnRails error message "null"😣

# app/models/user.rb
class User < ApplicationReord
  has_many :comments, dependent: :nullify
end

The comment model is important, here, we are defining how the reply system will be. We are creating a self-joining association with the comment model, allowing us to store replies in the comments table, and using parent_id column to store the comment ID of the reply.😉

# app/models/comment.rb
  belongs_to  :post
  belongs_to  :parent, class_name: 'Comment', optional: true
  has_many    :replies, class_name: 'Comment', foreign_key: :parent_id, dependent: :destroy

  validates :body, presence: :true

Perhaps, we can suppose that all comments will belonh to a specific post.🤔

# app/models/post.rb
  has_many :comments

😀 Conclusion: At the end we will not be deleting any comments, they will still be there for other users to continue replying, and we will not affect other persisted users in our database

THANK YOU SO MUCH FOR READING UNTIL HERE, HOPE WE CAN BUILD GREAT THINGS TOGETHER!!!!🤩🤩

I would like to say that I tried to do less than an hour, which I did with the main challengues, I ended up doing a little bit more time, because of formatting some stuff like this README and trying to clean my code a little bit as well.

Where to reach me....

About

No description, website, or topics provided.

Resources

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages