From cbb6837ea6c119c8dd64bd941534677f8e14c625 Mon Sep 17 00:00:00 2001 From: Huseyin Ozdogan Date: Mon, 1 Nov 2021 18:32:24 +0300 Subject: [PATCH 1/2] 2nd --- full_name.rb | 28 ++++++++++++++++++++++++++++ sorted_words.rb | 34 ++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+) create mode 100644 full_name.rb create mode 100644 sorted_words.rb diff --git a/full_name.rb b/full_name.rb new file mode 100644 index 0000000..52371b3 --- /dev/null +++ b/full_name.rb @@ -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']}!" \ No newline at end of file diff --git a/sorted_words.rb b/sorted_words.rb new file mode 100644 index 0000000..118f0b5 --- /dev/null +++ b/sorted_words.rb @@ -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 + 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) \ No newline at end of file From b312b0a79c272e2e9a125042314478203b046aee Mon Sep 17 00:00:00 2001 From: Huseyin Ozdogan Date: Mon, 1 Nov 2021 19:11:17 +0300 Subject: [PATCH 2/2] leap_year added --- add_up.rb | 17 +++++++++++++++++ leap_year.rb | 15 +++++++++++++++ 2 files changed, 32 insertions(+) create mode 100644 add_up.rb create mode 100644 leap_year.rb diff --git a/add_up.rb b/add_up.rb new file mode 100644 index 0000000..889a33a --- /dev/null +++ b/add_up.rb @@ -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 diff --git a/leap_year.rb b/leap_year.rb new file mode 100644 index 0000000..ae89a78 --- /dev/null +++ b/leap_year.rb @@ -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 + puts i.to_s + ' is leap year' + end +end \ No newline at end of file