7 kyu

Looking for a benefactor

311 of 9,357g964

Description:

The accounts of the "Fat to Fit Club (FFC)" association are supervised by John as a volunteered accountant. The association is funded through financial donations from generous benefactors. John has a list of the first n donations: [14, 30, 5, 7, 9, 11, 15] He wants to know how much the next benefactor should give to the association so that the average of the first n + 1 donations should reach an average of 30. After doing the math he found 149. He thinks that he could have made a mistake.

if dons = [14, 30, 5, 7, 9, 11, 15] then new_avg(dons, 30) --> 149

Could you help him?

Task

The function new_avg(arr, navg) should return the expected donation (rounded up to the next integer) that will permit to reach the average navg.

Should the last donation be a non positive number (<= 0) John wants us:

  • to return:

    • Nothing in Haskell, Elm
    • None in F#, Ocaml, Rust, Scala
    • -1 in C, D, Fortran, Nim, PowerShell, Go, Pascal, Prolog, Lua, Perl, Erlang
  • or to throw an error (some examples for such a case):

    • IllegalArgumentException() in Clojure, Java, Kotlin
    • ArgumentException() in C#
    • echo ERROR in Shell
    • argument-error in Racket
    • std::invalid_argument in C++
    • ValueError in Python

So, he will clearly see that his expectations are not great enough. In "Sample Tests" you can see what to return.

Notes:

  • all donations and navg are numbers (integers or floats), arr can be empty.
  • See examples below and "Sample Tests" to see which return is to be done.
new_avg([14, 30, 5, 7, 9, 11, 15], 92) should return 645
new_avg([14, 30, 5, 7, 9, 11, 15], 2) 
should raise an error (ValueError or invalid_argument or argument-error or DomainError or ... ) 
or return `-1` or ERROR or Nothing or None depending on the language.
Fundamentals
Arrays

Stats:

CreatedJan 17, 2016
PublishedJan 17, 2016
Warriors Trained22102
Total Skips2870
Total Code Submissions86287
Total Times Completed9357
Ruby Completions311
Python Completions2188
JavaScript Completions2076
CoffeeScript Completions18
Java Completions1082
C# Completions403
Clojure Completions102
Haskell Completions175
Elixir Completions188
TypeScript Completions180
C++ Completions611
PHP Completions322
Crystal Completions13
F# Completions71
C Completions361
OCaml Completions57
Rust Completions417
R Completions123
Nim Completions28
Shell Completions87
Kotlin Completions348
Fortran Completions35
Julia Completions38
PowerShell Completions76
Go Completions389
Reason Completions11
Racket Completions29
Scala Completions103
Prolog Completions28
Lua Completions74
Pascal Completions13
Perl Completions38
Elm Completions9
D Completions10
Erlang Completions11
Total Stars194
% of votes with a positive feedback rating79% of 1360
Total "Very Satisfied" Votes915
Total "Somewhat Satisfied" Votes321
Total "Not Satisfied" Votes124
Ad
Contributors
  • g964 Avatar
  • GiacomoSorbi Avatar
  • kazk Avatar
  • Blind4Basics Avatar
  • Voile Avatar
  • monadius Avatar
  • hobovsky Avatar
  • trashy_incel Avatar
  • user8436785 Avatar
  • KayleighWasTaken Avatar
Ad