Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7900637
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 3, 20262026-06-03T09:01:04+00:00 2026-06-03T09:01:04+00:00

Given a non-negative integer n and an arbitrary set of inequalities that are user-defined

  • 0

Given a non-negative integer n and an arbitrary set of inequalities that are user-defined (in say an external text file), I want to determine whether n satisfies any inequality, and if so, which one(s).


Here is a points list.

n = 0: 1
n < 5: 5
n = 5: 10

If you draw a number n that’s equal to 5, you get 10 points.
If n less than 5, you get 5 points.
If n is 0, you get 1 point.


The stuff left of the colon is the “condition”, while the stuff on the right is the “value”.
All entries will be of the form:

n1 op n2: val

In this system, equality takes precedence over inequality, so the order that they appear in will not matter in the end. The inputs are non-negative integers, though intermediary and results may not be non-negative. The results may not even be numbers (eg: could be strings). I have designed it so that will only accept the most basic inequalities, to make it easier for writing a parser (and to see whether this idea is feasible)

My program has two components:

  1. a parser that will read structured input and build a data structure to store the conditions and their associated results.

  2. a function that will take an argument (a non-negative integer) and return the result (or, as in the example, the number of points I receive)

If the list was hardcoded, that is an easy task: just use a case-when or if-else block and I’m done. But the problem isn’t as easy as that.

Recall the list at the top. It can contain an arbitrary number of (in)equalities. Perhaps there’s only 3 like above. Maybe there are none, or maybe there are 10, 20, 50, or even 1000000. Essentially, you can have m inequalities, for m >= 0

Given a number n and a data structure containing an arbitrary number of conditions and results, I want to be able to determine whether it satisfies any of the conditions and return the associated value. So as with the example above, if I pass in 5, the function will return 10.

They condition/value pairs are not unique in their raw form. You may have multiple instances of the same (in)equality but with different values. eg:

n = 0: 10
n = 0: 1000
n > 0: n

Notice the last entry: if n is greater than 0, then it is just whatever you got.

If multiple inequalities are satisfied (eg: n > 5, n > 6, n > 7), all of them should be returned. If that is not possible to do efficiently, I can return just the first one that satisfied it and ignore the rest. But I would like to be able to retrieve the entire list.


I’ve been thinking about this for a while and I’m thinking I should use two hash tables: the first one will store the equalities, while the second will store the inequalities.

Equality is easy enough to handle: Just grab the condition as a key and have a list of values. Then I can quickly check whether n is in the hash and grab the appropriate value.

However, for inequality, I am not sure how it will work. Does anyone have any ideas how I can solve this problem in as little computational steps as possible? It’s clear that I can easily accomplish this in O(n) time: just run it through each (in)equality one by one. But what happens if this checking is done in real-time? (eg: updated constantly)

For example, it is pretty clear that if I have 100 inequalities and 99 of them check for values > 100 while the other one checks for value <= 100, I shouldn’t have to bother checking those 99 inequalities when I pass in 47.

You may use any data structure to store the data. The parser itself is not included in the calculation because that will be pre-processed and only needs to be done once, but if it may be problematic if it takes too long to parse the data.

Since I am using Ruby, I likely have more flexible options when it comes to “messing around” with the data and how it will be interpreted.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-03T09:01:05+00:00Added an answer on June 3, 2026 at 9:01 am
    class RuleSet
      Rule = Struct.new(:op1,:op,:op2,:result) do
        def <=>(r2)
          # Op of "=" sorts before others
          [op=="=" ? 0 : 1, op2.to_i] <=> [r2.op=="=" ? 0 : 1, r2.op2.to_i]
        end
        def matches(n)
          @op2i ||= op2.to_i
          case op
            when "=" then n == @op2i
            when "<" then n  < @op2i
            when ">" then n  > @op2i
          end
        end
      end
    
      def initialize(text)
        @rules = text.each_line.map do |line|
          Rule.new *line.split(/[\s:]+/)
        end.sort
      end
    
      def value_for( n )
        if rule = @rules.find{ |r| r.matches(n) }
          rule.result=="n" ? n : rule.result.to_i
        end
      end
    end
    
    set = RuleSet.new( DATA.read )
    -1.upto(8) do |n|
      puts "%2i => %s" % [ n, set.value_for(n).inspect ]
    end
    
    #=> -1 => 5
    #=>  0 => 1
    #=>  1 => 5
    #=>  2 => 5
    #=>  3 => 5
    #=>  4 => 5
    #=>  5 => 10
    #=>  6 => nil
    #=>  7 => 7
    #=>  8 => nil
    
    __END__
    n = 0: 1
    n < 5: 5
    n = 5: 10
    n = 7: n
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Given an arbitrary file descriptor, can I make it blocking if it is non-blocking?
Given long long int x, y; , I want a function that can compare
Given the following recursive function: // Pre-condition: y is non-negative. int mysterious(int x, int
Im stuck with the below problem. Problem Statement: Given a non-negative int n, return
Given a list of integers, how can I best find an integer that is
Given n non-negative integers a1, a2, ..., an, where each represents a point at
Given: I added a non-nullable foreign key to a table. I settled on a
Given a crash report (non-symbolicated) on iOS, is there a way to determine which
Given k positive integer numbers a 1 < a 2 < a 3 <
For a given integer, n , I need to print all the lists of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.