Write a program in your favourite language that meets the following criteria. (No googling)

Write a program in your favourite language that meets the following criteria. (No googling)

He isn't fucking around this time:
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.


make_chocolate(4, 1, 9) → 4
make_chocolate(4, 1, 10) → -1
make_chocolate(4, 1, 7) → 2

Other urls found in this thread:

play.nim-lang.org/?gist=835989d809280ddcdde869db14cd13cb
twitter.com/SFWRedditGifs

Code tags were a bad idea:
We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.


make_chocolate(4, 1, 9) → 4
make_chocolate(4, 1, 10) → -1
make_chocolate(4, 1, 7) → 2

He'll still fucking stab you

...

Here's my solution anyway
def make_chocolate(small, big, goal):
# small, big, goal
# We need to know if big goes into goal, return 0
# we check if we have enough small to make the difference
# Fail and we return -1
quot, remains = divmod(goal, 5)

if small + (big * 5) == goal: return small
if not remains and big >= quot: return 0
quot = (quot if big >= quot else big) * 5

return goal - quot if small + quot >= goal else -1

wew
make_chocolate(int a, int b, int c)
{
return (a + 5 * b < c) ? -1 : c % 5;
}

Bow - bow

i thought that would happen when i was re-reading after posting, brb fixing

this one doesn't quite work. if you want to reach 11, but have no small and 3 large ones, it will return true.
int make_chocolate(int s, int l, int g) {
int m = g / 5 < l ? g / 5 : l;
return g - m * 5

...

public static int make_chocolate(int numSmall, int numBig, int weight){
int count = 0;
while (weight > 5 && numBig > 0){
weight -= 5;
numBig--;
}

if(numSmall < weight){
return -1;
}
return weight;
}

forgot to remove count, planned to use it ^^

ah, and it should be while (weight >= 5

you might as well give him a medal for understanding the question because it doesn't make sense

Maybe explain what the fuck this function accepts?

int make_chocolate(int a, int b, int c)
{
return ((a + 5 * b < c) || (a < c % 5)) ? -1 : c % 5 + (5 * (c / 5 - b));
}

not OP, but: you have a desired weight (the last parameter) and a number of big bars (weight = 5) and small bars (weight = 1)

you want to get the number of small bars you need to use to get the desired weight after the big bars are already used

person who solved it here.
x - # small weights of value 1
y - # large weights of value 5
g - goal value
find a combination of small and large weights (using large weights first), to reach your goal value, returning the number of small weights used if it was possible to reach the goal value or -1 otherwise.

golden rule: get it going before you try and one-line it

>making people do your homework
>they fell for it

I already posted my solution here nigger:

I am no nigger nigger

watch yourself anyway nigger, nigger nigger

fn make_chocolate(values: Vec) -> i32
{
let result = values.iter().fold(0, |acc, i| acc + i) % 5;
return if result > 1 { result } else { -1 };
}

wait

What are the arguments in your function?

one lining it makes it more fun, it would be too easy without such a restriction
and if you have a testing program it would be a shame not to use it and doing every test manually
int make_chocolate(int a, int b, int c)
{
return ((a + 5 * b < c) || (a < c % 5)) ? -1 : c % 5 + (5 * ((c / 5 - b) < 0) * (c / 5 - b));
}

I don't get your question, OP. You seem to accept 3 arguments arbitrarily. Hence you want how many small bars (1 kg) needed. So, your answer will range from 0 to 4, when can it not be done?

Here, I see 0 is listed as an expected value too.
4, 1, 10 should return 0 not -1.
Hence 6, 2, 7 should return 0, not 2. 3 x 5 kg bars = 15 kgs = 6 + 2 + 7

6 is the number of small bars you have
2 is the number of big bars you have
7 is your kg goal

Then fucking say it in the question, motherfucker.

i'm not OP, you can guess from the examples

just use miniKanren or Prolog

def make_chocolate(sB : Int, lB : Int, g : Int) : Int = {
val sB_orig = sB
def i_make_chocolate(sB : Int, lB : Int, g : Int) : Int = if(g==0) sB_orig - sB else if(g>4 && lB > 0) i_make_chocolate(sB,lB-1,g-5) else if(g>0 && sB > 0) i_make_chocolate(sB-1,lB,g-1) else -1
i_make_chocolate(sB, lB, g)
}


Not meant to be efficient - I could use modulo and division, but I wanted to explicitly take them one by one.

Still ashamed of how ugly this is, one of these days I'll hopefully learn all the FP tricks that make it pretty.

tfw op ignores your soultion :(

Which was yours? I'll run it now

with this fix

Run mine too
fn make_chocolate(small: i32, big: i32, target: i32) -> i32
{
if target - 5 * big > small {
-1
} else {
(target % (5 * big)) % 5
}

}

fn main()
{
println!("{}", make_chocolate(1, 2, 5));
}

Didn't even run m8

well, then your tests cant run java. tested it now for the first 5 and no problems (and correct solution)

Can your thing run Scala from ?

mind testing
int make_chocolate(int a, int b, int c)
{
return ((a + 5 * b < c) || (a < c % 5)) ? -1 : c % 5 + (5 * ((c / 5 - b) < 0) * (c / 5 - b));
}
this one should work

Got it.

Don't have a rust compiler installed

Nope and tant mieux ~: 3

Nope and tant mieux ~: 3

return goal%bigBars;

>ok class and your homework for next week will be {op post}
>rajeet makes thread on Sup Forums, collect working code on different languages
>sell it to retards
>???
>profit

>return ((a + 5 * b < c) || (a < c % 5)) ? -1 : c % 5 + (5 * ((c / 5 - b) < 0) * (c / 5 - b));

I think there is some fuckery going on, can you try it with 60, 100, 550 and 1,2,5 and see what you actually get locally?

actually that's (bigBars*5) and it works.

Well, I'm just going with "it's correct".

> tant mieux
More like the test runner is shit. But eh, I'm reasonably confident it works.

you didn't apply my fix i posted some seconds after my answer while (weight >= 5 && numBig > 0)

My mistake. Wish you fags would just use the vars and functions names provided though so I didn't have to change every submission by hand

my function was named "make_chocolate", don't know why you changed it ...

i think i found the problem, try
int make_chocolate(int a, int b, int c)
{
return ((a + 5 * b < c) || (a < c % 5)) ? -1 : c % 5 + (5 * ((c / 5 - b) > 0) * (c / 5 - b));
}
please

Winrar

thanks, I feel retarded because the problem was one < was meant to be a > and i didn't see the typo

>you fags
Ungrateful fucktard ape

def make_chocolate(small, big, goal)
sr = 5*[0, (goal/5)-big].max+(goal%5)
small < sr ? -1 : sr
end
don't stab plz

Ported it to python though

def make_chocolate(small, big, goal):
sr = max(5*[0, (goal/5)-big]) + (goal%5)
return -1 if small < sr else sr


Forgot code

Not the person who posted that code, but try moving the times 5 outside the max. Multiplying a list by a number in python just duplicates the elements.

The python max doesn't max around 0 and calculation, it just takes calculation every time.

Fixed.

No wait, python does something else there. I think. Not a python expert. Never mind about comment, but code is still wrong though.

Can you post the tests in a more C&Pable way?

Ruby solution:

def make_chocolate(small, big, weight)
weight -= 5*(big

I live to see another day.

>Can you post the tests in a more C&Pable way?
assert make_chocolate(4, 1, 9) == 4
assert make_chocolate(4, 1, 10) == -1
assert make_chocolate(4, 1, 7) == 2
assert make_chocolate(6, 2, 7) == 2
assert make_chocolate(4, 1, 5) == 0
assert make_chocolate(4, 1, 4) == 4
assert make_chocolate(5, 4, 9) == 4
assert make_chocolate(9, 3, 18) == 3
assert make_chocolate(3, 1, 9) == -1
assert make_chocolate(1, 2, 7) == -1
assert make_chocolate(1, 2, 6) == 1
assert make_chocolate(1, 2, 5) == 0
assert make_chocolate(6, 1, 10) == 5
assert make_chocolate(6, 1, 11) == 6
assert make_chocolate(6, 1, 12) == -1
assert make_chocolate(6, 1, 13) == -1
assert make_chocolate(6, 2, 10) == 0
assert make_chocolate(6, 2, 11) == 1
assert make_chocolate(6, 2, 12) == 2
assert make_chocolate(60, 100, 550) == 50
assert make_chocolate(1000, 1000000, 5000006) == 6
assert make_chocolate(7, 1, 12) == 7
assert make_chocolate(7, 1, 13) == -1
assert make_chocolate(7, 2, 13) == 3

...

chocolate :: (RealFloat a) => a -> a -> a
chocolate kilos
| kilos mod 5 == 0 = -1
| kilos mod 5 !=0 = kilos mod 5

Hahaha, nice.
What testing framework did oyu use?

Anyway I'll put this in my c.v.
See you next time, birdmin!

proc make(s, b, t: int): int =
if s + b * 5 < t or t mod 5 > s: -1 elif b < t div 5: t - 5 * b else: t mod 5
play.nim-lang.org/?gist=835989d809280ddcdde869db14cd13cb

the way this question is worded the answer is just desired number % 5

It's shitty wording but if you look at the example function you'll probably get that you're working with a specific amount of large and small bars each.

Python here:

1 #We want make a package of goal kilos of chocolate. We have small bars (1 kilo each) and big bars (5 kilos each). Return the number of small bars to use, assuming we always use big bars before small bars. Return -1 if it can't be done.
2
3 def make_chocolate(s, b, g):
4 bmol = b*5
5
6 while (bmol > g): bmol-=5
7
8 return -1 if (g - bmol > s) else g - bmol

the object oriented way

class PackageChocolate{
protected $small = 1;
protected $big = 5;

public function __construct($limitSmall, $limitLarge, $goal){

$this->limitSmall = $limitSmall;
$this->limitLarge = $limitLarge;
$this->goal = $goal;

$this->smallBars = $this->getBars();
}
public function getBars(){
$weightSmall = $this->limitSmall * $this->small;
$weightLarge = $this->limitLarge * $this->big;

if($weightSmall + $weightLarge < $this->goal){
return -1;
}

if($this->goal < $weightLarge){
return $goal / $this->small;
}
else{
$remainder = $this->goal % $this->big;
$residual = $remainder / $this->small;
return $residual;
}

}
public function __destruct(){
return $this->smallBars;
}
}

nevermind fuck this doesn't work :(