-
Couldn't load subscription status.
- 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
base: master
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| 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 |
| 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']}!" | ||
| 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 | ||
| 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) | ||
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.