8 kyu

Compare within margin

6,160 of 15,889ITSOES

Description:

Create a function close_compare that accepts 3 parameters: a, b, and an optional margin. The function should return whether a is lower than, close to, or higher than b.

Please note the following:

  • When a is close to b, return 0.
    • For this challenge, a is considered "close to" b if margin is greater than or equal to the absolute distance between a and b.

Otherwise...

  • When a is less than b, return -1.

  • When a is greater than b, return 1.

If margin is not given, treat it as if it were zero.

Assume: margin >= 0

Tip: Some languages have a way to make parameters optional.


Example 1

If a = 3, b = 5, and margin = 3, then close_compare(a, b, margin) should return 0.

This is because a and b are no more than 3 numbers apart.

Example 2

If a = 3, b = 5, and margin = 0, then close_compare(a, b, margin) should return -1.

This is because the distance between a and b is greater than 0, and a is less than b.

Fundamentals
Logic

More By Author:

Check out these other kata created by ITSOES

Stats:

CreatedNov 13, 2015
PublishedAug 24, 2016
Warriors Trained28164
Total Skips1528
Total Code Submissions59295
Total Times Completed15889
Python Completions6160
JavaScript Completions4916
Ruby Completions428
C# Completions1145
C Completions1172
Java Completions1466
Julia Completions36
Rust Completions339
Go Completions812
CoffeeScript Completions8
Scala Completions13
Total Stars238
% of votes with a positive feedback rating85% of 1962
Total "Very Satisfied" Votes1492
Total "Somewhat Satisfied" Votes337
Total "Not Satisfied" Votes133
Total Rank Assessments9
Average Assessed Rank
7 kyu
Highest Assessed Rank
7 kyu
Lowest Assessed Rank
8 kyu
Ad
Contributors
  • ITSOES Avatar
  • ZozoFouchtra Avatar
  • smile67 Avatar
  • donaldsebleung Avatar
  • Chrono79 Avatar
  • user5036852 Avatar
  • B1ts Avatar
  • lilsweetcaligula Avatar
  • FArekkusu Avatar
  • hobovsky Avatar
  • stellartux Avatar
  • akar-0 Avatar
  • myplacedk Avatar
  • KayleighWasTaken Avatar
  • saudiGuy Avatar
Ad