-
Notifications
You must be signed in to change notification settings - Fork 74
Ruby collections lesson (Lesson 2.1) #8
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
Dan88Hus
wants to merge
2
commits into
Code-the-Dream-School:master
Choose a base branch
from
Dan88Hus:ruby-collections-lesson
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,17 @@ | ||
| # (3) Write a program with a function add_up(i) . It is to be passed a positive integer, and it will add all the numbers from 1 to that integer and return the sum. Call the function three times within the program, and each time print out the return value. Call the program add_up.rb. | ||
|
|
||
|
|
||
| def add_up(i) | ||
| array = (1..i).to_a | ||
| sum = 0 | ||
| array.each { |a| sum+=a } | ||
| return sum | ||
|
|
||
| end | ||
|
|
||
| puts "Enter +Integer to sum" | ||
| upper_boundary = gets.chomp.to_i | ||
| 3.times do |i| | ||
| sum = add_up(upper_boundary) | ||
| puts sum | ||
| end |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,28 @@ | ||
| # (1) Write a program which asks for a person's first name, then middle, then last. It should store each of these parts in an array. Finally, it should greet the person using their full name. Call the program full_name.rb. | ||
|
|
||
| person_detail = {"f_name"=> "", "m_name"=> "", "l_name"=> ""} | ||
|
|
||
| def get_int | ||
| input = gets.chomp # this returns a string | ||
| input_str = input.to_s # this converts a string to an integer | ||
| # puts input_str | ||
| return input_str | ||
| end | ||
|
|
||
| puts "Enter your First Name" | ||
| first_name = get_int | ||
| # print "first name: ", first_name | ||
| person_detail['f_name'] = first_name | ||
|
|
||
| puts "Enter your Middle Name" | ||
| middle_name = get_int | ||
| # print "middle name: ", middle_name | ||
| person_detail['m_name'] = middle_name | ||
|
|
||
| puts "Enter your Last Name" | ||
| last_name = get_int | ||
| # print "last name: ", last_name | ||
| person_detail['l_name'] = last_name | ||
|
|
||
|
|
||
| puts "Greetings #{person_detail['f_name']} #{person_detail['m_name']} #{person_detail['l_name']}!" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| # (4) Write a program, leap_year.rb. It will ask the user for a starting year and an ending year, and it will then print out all the leap years between them, including the starting or ending year if those are leap years. The rules for leap years are: A leap year is divisible by 4, except for years that are divisible by 100 -- those aren't leap years -- except for years that are divisible by 400, which ARE leap years. | ||
|
|
||
| puts "Starting year to find leap year" | ||
| start_year = gets.chomp.to_i | ||
| puts "Ending year to find leap year" | ||
| end_year = gets.chomp.to_i | ||
|
|
||
| array = (start_year..end_year).to_a | ||
| array.each do |i| | ||
| if i % 400 == 0 | ||
| puts i.to_s + ' is leap year' | ||
| elsif i % 4==0 && i % 100 != 0 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Watch for the indentation here, make sure all lines are aligned in the block. |
||
| puts i.to_s + ' is leap year' | ||
| end | ||
| end | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| # (2) Write a program called sorted_words.rb. It should prompt the user for words and add each to an array. The user should be able to add as many words as they like, until they just hit enter to return a blank word. Then sort the array using the sort method and print it out. | ||
|
|
||
| def bubble_sort(array) | ||
| array_length = array.size | ||
| return array if array_length <= 1 | ||
|
|
||
| loop do | ||
| # we need to create a variable that will be checked so that we don't run into an infinite loop scenario. | ||
| swapped = false | ||
|
|
||
| # subtract one because Ruby arrays are zero-index based | ||
| (array_length-1).times do |i| | ||
| if array[i] > array[i+1] | ||
| array[i], array[i+1] = array[i+1], array[i] | ||
| swapped = true | ||
| end | ||
| end | ||
|
|
||
| break if not swapped | ||
| end | ||
|
|
||
| array | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Nice old-fashion way of sorting array elements. |
||
| end | ||
|
|
||
| array = [] | ||
|
|
||
| loop do | ||
| puts "Enter words to add each to an array for sorting, Double hit [Enter] to quit" | ||
| answer = gets.chomp.downcase | ||
| break if answer == '' | ||
| array.push(answer) | ||
| end | ||
|
|
||
| p bubble_sort(array) | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good use of a Ruby hash for storing data.