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 715425
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T05:11:08+00:00 2026-05-14T05:11:08+00:00

My code is based on the methods described here and here . def fraction?(number)

  • 0

My code is based on the methods described here and here.

def fraction?(number)
  number - number.truncate
end

def percentile(param_array, percentage)
  another_array = param_array.to_a.sort
  r = percentage.to_f * (param_array.size.to_f - 1) + 1
  if r <= 1 then return another_array[0]
  elsif r >= another_array.size then return another_array[another_array.size - 1]
  end
  ir = r.truncate
  another_array[ir] + fraction?((another_array[ir].to_f - another_array[ir - 1].to_f).abs)
end

Example usage:

test_array = [95.1772, 95.1567, 95.1937, 95.1959, 95.1442, 95.061, 95.1591, 95.1195,
95.1065, 95.0925, 95.199, 95.1682]
test_values = [0.0, 0.1, 0.2, 0.3, 0.4, 0.5, 0.6, 0.7, 0.8, 0.9, 1.0]

test_values.each do |value|
  puts value.to_s + ": " + percentile(test_array, value).to_s
end

Output:

0.0: 95.061
0.1: 95.1205
0.2: 95.1325
0.3: 95.1689
0.4: 95.1692
0.5: 95.1615
0.6: 95.1773
0.7: 95.1862
0.8: 95.2102
0.9: 95.1981
1.0: 95.199

The problem here is that the 80th percentile is higher than the 90th and the 100th. However, as far as I can tell my implementation is as described, and it returns the right answer for the example given (0.9).

Is there an error in my code I’m not seeing? Or is there a better way to do this?

  • 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-05-14T05:11:09+00:00Added an answer on May 14, 2026 at 5:11 am

    script

    This sounds like a homework problem. Anyway, it was kinda fun to do.

    # Score class
    class Score
      attr_accessor :value, :percentile
      def initialize(score)
        self.value = score.to_f
      end
      def <=>(foo)
        self.value <=> foo.value
      end
    end
    
    # load scores
    scores = []
    DATA.each do |line|
      scores << Score.new(line)
    end
    scores.sort!
    scores_count = scores.size
    
    # iterate through scores and calculate percentile
    scores.each_with_index do |s, i|
    
      # L/N(100) = P
      # L = number of scores beneath this score (score array index)
      # N = total number of scores
      # P = percentile
      s.percentile = (i.to_f/scores_count.to_f*100).ceil
    end
    
    # output
    puts "What is the precise percentile of each score"
    scores.each_with_index do |s,i|
      puts "#{s.value} is in the #{s.percentile} percentile"
    end
    
    # bonus: what score is in the Xth percentile?
    puts "\nWhat score is in the Xth percentile?"
    percentiles = [0, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
    percentiles.each do |p|
    
      # P/100(N) = L
      # P = percentile
      # N = total number of scores
      # L = score array index
      l = (p.to_f/100*scores_count).ceil
      puts "#{p} percentile? #{scores[l].value}"
    end
    
    
    __END__
    95.1772
    95.1567
    95.1937
    95.1959
    95.1442
    95.061
    95.1591
    95.1195
    95.1065
    95.0925
    95.199
    95.1682
    

    output

    What is the precise percentile of each score
    95.061 is in the 0 percentile
    95.0925 is in the 9 percentile
    95.1065 is in the 17 percentile
    95.1195 is in the 25 percentile
    95.1442 is in the 34 percentile
    95.1567 is in the 42 percentile
    95.1591 is in the 50 percentile
    95.1682 is in the 59 percentile
    95.1772 is in the 67 percentile
    95.1937 is in the 75 percentile
    95.1959 is in the 84 percentile
    95.199 is in the 92 percentile
    
    What score is in the Xth percentile?
    0 percentile? 95.061
    10 percentile? 95.1065
    20 percentile? 95.1195
    30 percentile? 95.1442
    40 percentile? 95.1567
    50 percentile? 95.1591
    60 percentile? 95.1772
    70 percentile? 95.1937
    80 percentile? 95.1959
    90 percentile? 95.199
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Ask A Question

Stats

  • Questions 437k
  • Answers 438k
  • Best Answers 0
  • User 1
  • Popular
  • Answers
  • Editorial Team

    How to approach applying for a job at a company ...

    • 7 Answers
  • Editorial Team

    What is a programmer’s life like?

    • 5 Answers
  • Editorial Team

    How to handle personal stress caused by utterly incompetent and ...

    • 5 Answers
  • Editorial Team
    Editorial Team added an answer Here is an article on how to create a UDF… May 15, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer For the case where I needed it I got it… May 15, 2026 at 4:26 pm
  • Editorial Team
    Editorial Team added an answer Can't be done in quartz. This entire framework is written… May 15, 2026 at 4:26 pm

Trending Tags

analytics british company computer developers django employee employer english facebook french google interview javascript language life php programmer programs salary

Top Members

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.