Write a function to check three given integers and return their sum. However, If one of the values is the same as another of the values, then both the values are not counted in the sum. In Ruby language.

Computer Networking: A Top-Down Approach (7th Edition)
7th Edition
ISBN:9780133594140
Author:James Kurose, Keith Ross
Publisher:James Kurose, Keith Ross
Chapter1: Computer Networks And The Internet
Section: Chapter Questions
Problem R1RQ: What is the difference between a host and an end system? List several different types of end...
icon
Related questions
Question
**Title:** Ruby Function for Integer Sum with Duplicate Check

**Task Description:**

Create a function in Ruby to process three given integers and return their sum. The function should check for any duplicate values among the three integers. If any two values are the same, neither of them should be included in the sum calculation.

**Instructions:**

- Write a function that accepts three integer arguments.
- Evaluate the integers to check if any are equal.
- Exclude any duplicates from the sum.
- Return the calculated sum.

**Example:**

Suppose the integers are 3, 3, and 5. Since the first two integers are duplicates, they are not counted, and only the third integer, 5, is summed, resulting in a total sum of 5. 

**Implementation Tips:**

Consider edge cases such as when all three integers are the same or when there are no duplicates. Efficiently handle the logic for checking duplicates and calculating the sum.
Transcribed Image Text:**Title:** Ruby Function for Integer Sum with Duplicate Check **Task Description:** Create a function in Ruby to process three given integers and return their sum. The function should check for any duplicate values among the three integers. If any two values are the same, neither of them should be included in the sum calculation. **Instructions:** - Write a function that accepts three integer arguments. - Evaluate the integers to check if any are equal. - Exclude any duplicates from the sum. - Return the calculated sum. **Example:** Suppose the integers are 3, 3, and 5. Since the first two integers are duplicates, they are not counted, and only the third integer, 5, is summed, resulting in a total sum of 5. **Implementation Tips:** Consider edge cases such as when all three integers are the same or when there are no duplicates. Efficiently handle the logic for checking duplicates and calculating the sum.
Expert Solution
Program

#function to check three integers and return their sum
def check_sum(num1, num2, num3)
    #Check numbers
    if num1==num2 && num2==num3
        #if all numbers are same return 0 as sum
        return 0
    elsif num1==num2
        #if num1 and num2 is same return num3 as sum
        return num3
    elsif num1==num3
        #if num1 and num3 is same return num2 as sum
        return num2
    elsif num2==num3
        #if num2 and num3 is same return num1 as sum
        return num1
    else
        #if all number is different return their sum
        return num1+num2+num3
    end
end

#test code
print "Sum(6,6,6): ",check_sum(6, 6, 6),"\n"
print "Sum(6,6,4): ",check_sum(6, 6, 4),"\n"
print "Sum(6,3,6): ",check_sum(6, 3, 6),"\n"
print "Sum(2,6,6): ",check_sum(2, 6, 6),"\n"
print "Sum(2,3,4): ",check_sum(2, 3, 4),"\n"

steps

Step by step

Solved in 2 steps

Blurred answer
Similar questions
Recommended textbooks for you
Computer Networking: A Top-Down Approach (7th Edi…
Computer Networking: A Top-Down Approach (7th Edi…
Computer Engineering
ISBN:
9780133594140
Author:
James Kurose, Keith Ross
Publisher:
PEARSON
Computer Organization and Design MIPS Edition, Fi…
Computer Organization and Design MIPS Edition, Fi…
Computer Engineering
ISBN:
9780124077263
Author:
David A. Patterson, John L. Hennessy
Publisher:
Elsevier Science
Network+ Guide to Networks (MindTap Course List)
Network+ Guide to Networks (MindTap Course List)
Computer Engineering
ISBN:
9781337569330
Author:
Jill West, Tamara Dean, Jean Andrews
Publisher:
Cengage Learning
Concepts of Database Management
Concepts of Database Management
Computer Engineering
ISBN:
9781337093422
Author:
Joy L. Starks, Philip J. Pratt, Mary Z. Last
Publisher:
Cengage Learning
Prelude to Programming
Prelude to Programming
Computer Engineering
ISBN:
9780133750423
Author:
VENIT, Stewart
Publisher:
Pearson Education
Sc Business Data Communications and Networking, T…
Sc Business Data Communications and Networking, T…
Computer Engineering
ISBN:
9781119368830
Author:
FITZGERALD
Publisher:
WILEY