Skip to content

Instantly share code, notes, and snippets.

@gameguy43
Last active October 23, 2016 20:48
Show Gist options
  • Save gameguy43/c4bb482652d62e31d1bee9d09e331999 to your computer and use it in GitHub Desktop.
Save gameguy43/c4bb482652d62e31d1bee9d09e331999 to your computer and use it in GitHub Desktop.
code to translation for interview cake code translation job application
Please translate this code.
It's available below in Python, Ruby, Java, and JavaScript.
You can consult whichever version you like.
Then save your answer as a new gist and send me a link.
Thanks!
--Parker
<language python>
from itertools import islice
def highest_product_of_3(list_of_ints):
if len(list_of_ints) &lt; 3:
raise Exception('Less than 3 items!')
# We're going to start at the 3rd item (at index 2)
# so pre-populate highests and lowests based on the first 2 items.
# we could also start these as None and check below if they're set
# but this is arguably cleaner
highest = max(list_of_ints[0], list_of_ints[1])
lowest = min(list_of_ints[0], list_of_ints[1])
highest_product_of_2 = list_of_ints[0] * list_of_ints[1]
lowest_product_of_2 = list_of_ints[0] * list_of_ints[1]
# except this one--we pre-populate it for the first /3/ items.
# this means in our first pass it'll check against itself, which is fine.
highest_product_of_three = list_of_ints[0] * list_of_ints[1] * list_of_ints[2]
# walk through items, starting at index 2
for current in islice(list_of_ints, 2, None):
# do we have a new highest product of 3?
# it's either the current highest,
# or the current times the highest product of two
# or the current times the lowest product of two
highest_product_of_three = max(
highest_product_of_three,
current * highest_product_of_2,
current * lowest_product_of_2)
# do we have a new highest product of two?
highest_product_of_2 = max(
highest_product_of_2,
current * highest,
current * lowest)
# do we have a new lowest product of two?
lowest_product_of_2 = min(
lowest_product_of_2,
current * highest,
current * lowest)
# do we have a new highest?
highest = max(highest, current)
# do we have a new lowest?
lowest = min(lowest, current)
return highest_product_of_three
</language>
<language ruby>
def highest_product_of_3(array_of_ints)
if array_of_ints.length &lt; 3
raise Exception, 'Less than 3 items!'
end
# We're going to start at the 3rd item (at index 2)
# so pre-populate highests and lowests based on the first 2 items.
# we could also start these as nil and check below if they're set
# but this is arguably cleaner
highest = [array_of_ints[0], array_of_ints[1]].max
lowest = [array_of_ints[0], array_of_ints[1]].min
highest_product_of_2 = array_of_ints[0] * array_of_ints[1]
lowest_product_of_2 = array_of_ints[0] * array_of_ints[1]
# except this one--we pre-populate it for the first /3/ items.
# this means in our first pass it'll check against itself, which is fine.
highest_product_of_three = array_of_ints[0] * array_of_ints[1] * array_of_ints[2]
# walk through items, starting at index 2
# (we could slice the array but that would use n space)
array_of_ints.each_with_index do |current, index|
next if index &lt; 2
# do we have a new highest product of 3?
# it's either the current highest,
# or the current times the highest product of two
# or the current times the lowest product of two
highest_product_of_three = [
highest_product_of_three,
current * highest_product_of_2,
current * lowest_product_of_2
].max
# do we have a new highest product of two?
highest_product_of_2 = [
highest_product_of_2,
current * highest,
current * lowest
].max
# do we have a new lowest product of two?
lowest_product_of_2 = [
lowest_product_of_2,
current * highest,
current * lowest
].min
# do we have a new highest?
highest = [highest, current].max
# do we have a new lowest?
lowest = [lowest, current].min
end
return highest_product_of_three
end
</language>
<language java>
public int highestProductOf3(int[] arrayOfInts) {
if (arrayOfInts.length &lt; 3) {
throw new IllegalArgumentException("Less than 3 items!");
}
// We're going to start at the 3rd item (at index 2)
// so pre-populate highests and lowests based on the first 2 items.
// we could also start these as null and check below if they're set
// but this is arguably cleaner
int highest = Math.max(arrayOfInts[0], arrayOfInts[1]);
int lowest = Math.min(arrayOfInts[0], arrayOfInts[1]);
int highestProductOf2 = arrayOfInts[0] * arrayOfInts[1];
int lowestProductOf2 = arrayOfInts[0] * arrayOfInts[1];
// except this one--we pre-populate it for the first /3/ items.
// this means in our first pass it'll check against itself, which is fine.
int highestProductOf3 = arrayOfInts[0] * arrayOfInts[1] * arrayOfInts[2];
// walk through items, starting at index 2
for (int i = 2; i &lt; arrayOfInts.length; i++) {
int current = arrayOfInts[i];
// do we have a new highest product of 3?
// it's either the current highest,
// or the current times the highest product of two
// or the current times the lowest product of two
highestProductOf3 = Math.max(Math.max(
highestProductOf3,
current * highestProductOf2),
current * lowestProductOf2);
// do we have a new highest product of two?
highestProductOf2 = Math.max(Math.max(
highestProductOf2,
current * highest),
current * lowest);
// do we have a new lowest product of two?
lowestProductOf2 = Math.min(Math.min(
lowestProductOf2,
current * highest),
current * lowest);
// do we have a new highest?
highest = Math.max(highest, current);
// do we have a new lowest?
lowest = Math.min(lowest, current);
}
return highestProductOf3;
}
</language>
<language javascript>
function highestProductOf3(arrayOfInts) {
if (arrayOfInts.length &lt; 3) {
throw new Error('Less than 3 items!');
}
// We're going to start at the 3rd item (at index 2)
// so pre-populate highests and lowests based on the first 2 items.
// we could also start these as null and check below if they're set
// but this is arguably cleaner
var highest = Math.max(arrayOfInts[0], arrayOfInts[1]);
var lowest = Math.min(arrayOfInts[0], arrayOfInts[1]);
var highestProductOf2 = arrayOfInts[0] * arrayOfInts[1];
var lowestProductOf2 = arrayOfInts[0] * arrayOfInts[1];
// except this one--we pre-populate it for the first /3/ items.
// this means in our first pass it'll check against itself, which is fine.
var highestProductOf3 = arrayOfInts[0] * arrayOfInts[1] * arrayOfInts[2];
// walk through items, starting at index 2
for (var i = 2; i &lt; arrayOfInts.length; i++) {
var current = arrayOfInts[i];
// do we have a new highest product of 3?
// it's either the current highest,
// or the current times the highest product of two
// or the current times the lowest product of two
highestProductOf3 = Math.max(
highestProductOf3,
current * highestProductOf2,
current * lowestProductOf2
);
// do we have a new highest product of two?
highestProductOf2 = Math.max(
highestProductOf2,
current * highest,
current * lowest
);
// do we have a new lowest product of two?
lowestProductOf2 = Math.min(
lowestProductOf2,
current * highest,
current * lowest
);
// do we have a new highest?
highest = Math.max(highest, current);
// do we have a new lowest?
lowest = Math.min(lowest, current);
}
return highestProductOf3;
}
</language>
@yulian-simeonov
Copy link

Very clean!!! (Y)

@Maheepk
Copy link

Maheepk commented Oct 22, 2016

func highestProductOf3(arrayOfInts: [Int]) -> Int {
        if arrayOfInts.count < 3 {
            print("Less than 3 items!")
            return 0
        }

        // We are going to start at the 3rd item(at index 2)
        // so pre-populate highests and lowests based on the first 2 items.
        // We could also start these as null and check bellow it they're set
        // but this is arguably cleaner

        var highest = max(arrayOfInts[0], arrayOfInts[1])
        var lowest = min(arrayOfInts[0], arrayOfInts[1])

        var highestProductOf2 = arrayOfInts[0] * arrayOfInts[1]
        var lowestProductOf2 = arrayOfInts[0] * arrayOfInts[1]

        // except this one--we pre-populate it for the first /3/ items.
        // this means in our first pass it'll check against itself, which is fine.

        var highestProductOf3 = arrayOfInts[0] * arrayOfInts[1] * arrayOfInts[2];

        // walk through items, starting at index 2
        for i in 2..<arrayOfInts.count {

            let current = arrayOfInts[i];

            // do we have a new highest product of 3?
            // it's either the current highest,
            // or the current times the highest product of two
            // or the current times the lowest product of two
            highestProductOf3 = max(
                highestProductOf3,
                current * highestProductOf2,
                current * lowestProductOf2
            );

            // do we have a new highest product of two?
            highestProductOf2 = max(
                highestProductOf2,
                current * highest,
                current * lowest
            );

            // do we have a new lowest product of two?
            lowestProductOf2 = min(
                lowestProductOf2,
                current * highest,
                current * lowest
            );

            // do we have a new highest?
            highest = max(highest, current);

            // do we have a new lowest?
            lowest = min(lowest, current);
        }

        return highestProductOf3;

}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment