Initial Commit
This commit is contained in:
44
3.a. fuel-injection-perfection/broken-solution.py
Normal file
44
3.a. fuel-injection-perfection/broken-solution.py
Normal file
@@ -0,0 +1,44 @@
|
||||
def solution(n):
|
||||
opCounter = 0
|
||||
pellets = int(n)
|
||||
|
||||
if pellets == 0:
|
||||
return 1
|
||||
|
||||
while pellets > 1:
|
||||
print "Iteration Started w/ " + str(pellets) + " pellets left."
|
||||
if pellets == 3:
|
||||
print "Special 3"
|
||||
pellets -= 1
|
||||
opCounter += 1
|
||||
|
||||
if pellets % 2 == 0:
|
||||
pellets /= 2
|
||||
opCounter += 1
|
||||
print "Divided Pellets by two - operations: " + str(opCounter)
|
||||
continue
|
||||
if bitwiseIsPowerTwo(pellets+1) and pellets >= 15:
|
||||
pellets = (pellets+1)/2
|
||||
opCounter += 2
|
||||
print "Power of two fast path - operations: " + str(opCounter)
|
||||
continue
|
||||
|
||||
pellets += 1
|
||||
opCounter += 1
|
||||
print "Base case, subtract one - operations: " + str(opCounter)
|
||||
|
||||
print "One pellet left - total ops: " + str(opCounter)
|
||||
return opCounter
|
||||
|
||||
|
||||
def bitwiseIsPowerTwo(n):
|
||||
return (n != 0) and (n & (n-1) == 0)
|
||||
|
||||
|
||||
case1 = int(solution('372')) # Return 12 # 372 -> 186 -> 93 -> 92 -> 46 -> 23 -> 22 -> 11 -> 10 -> 5 -> 4 -> 2 -> 1
|
||||
# Return 11 # 372 -> 186 -> 93 -> 94 -> 47 -> 48 -> 24 -> 12 -> 6 -> 3 -> 2 -> 1
|
||||
if case1 == 11:
|
||||
print "Test 1: Passed"
|
||||
else:
|
||||
print "Test 1: Failed"
|
||||
print "Recieved " + str(case1)
|
||||
81
3.a. fuel-injection-perfection/patterns.py
Normal file
81
3.a. fuel-injection-perfection/patterns.py
Normal file
@@ -0,0 +1,81 @@
|
||||
# Return true or false on if the number is a power of two
|
||||
def bitwiseIsPowerTwo(n):
|
||||
return (n != 0) and (n & (n-1) == 0)
|
||||
|
||||
# solution(1) returns 0: 1
|
||||
# solution(2) returns 1: 2 -> 1
|
||||
# solution(3) returns 2: 3 -> 2 -> 1
|
||||
# solution(4) returns 2: 4 -> 2 -> 1
|
||||
# solution(5) returns 3: 5 -> 4 -> 2 -> 1
|
||||
# solution(6) returns 3: 6 -> 3 -> 2 -> 1
|
||||
# solution(7) returns 4: 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(7) returns 4: 7 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(8) returns 3: 8 -> 4 -> 2 -> 1
|
||||
# solution(9) returns 4: 9 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(9) returns 5: 9 -> 10 -> 5 -> 4 -> 2 -> 1
|
||||
# solution(9) returns 6: 9 -> 10 -> 5 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(10) returns 4: 10 -> 5 -> 4 -> 2 -> 1
|
||||
# solution(10) returns 5: 10 -> 5 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(10) returns 5: 10 -> 9 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(11) returns 5: 11 -> 12 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(11) returns 5: 11 -> 10 -> 5 -> 4 -> 2 -> 1
|
||||
# solution(12) returns 4: 12 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(13) returns 5: 13 -> 12 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(13) returns 6: 13 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(13) returns 6: 13 -> 14 -> 7 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(14) returns 5: 14 -> 7 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(14) returns 5: 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(15) returns 6: 15 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(16) returns 4: 16 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(17) returns 5: 17 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(17) returns 6: 17 -> 18 -> 9 -> 5 -> 4 -> 2 -> 1
|
||||
# solution(17) returns 7: 17 -> 18 -> 9 -> 5 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(18) returns 5: 18 -> 9 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(18) returns 6: 18 -> 17 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(19) returns 6: 19 -> 18 -> 9 -> 8 -> 4 -> 2 -> 1
|
||||
|
||||
|
||||
# solution(1) returns 0: 1 .
|
||||
# solution(2) returns 1: 2 -> 1 / #
|
||||
# solution(3) returns 2: 3 -> 2 -> 1 - /
|
||||
# solution(4) returns 2: 4 -> 2 -> 1 / / ###
|
||||
# solution(5) returns 3: 5 -> 4 -> 2 -> 1 - / /
|
||||
# solution(6) returns 3: 6 -> 3 -> 2 -> 1 / - /
|
||||
# solution(7) returns 4: 7 -> 6 -> 3 -> 2 -> 1 - / - /
|
||||
# solution(7) returns 4: 7 -> 8 -> 4 -> 2 -> 1 + / / / -
|
||||
# solution(8) returns 3: 8 -> 4 -> 2 -> 1 / / / ###
|
||||
# solution(9) returns 4: 9 -> 8 -> 4 -> 2 -> 1 - / / /
|
||||
# solution(10) returns 4: 10 -> 5 -> 4 -> 2 -> 1 / - / /
|
||||
# solution(11) returns 5: 11 -> 10 -> 5 -> 4 -> 2 -> 1 - / - / /
|
||||
# solution(12) returns 4: 12 -> 6 -> 3 -> 2 -> 1 / / - /
|
||||
# solution(13) returns 5: 13 -> 12 -> 6 -> 3 -> 2 -> 1 - / / - /
|
||||
# solution(14) returns 5: 14 -> 7 -> 6 -> 3 -> 2 -> 1 / - / - / -
|
||||
# solution(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1 + / / / / #
|
||||
# solution(16) returns 4: 16 -> 8 -> 4 -> 2 -> 1 / / / / ###
|
||||
# solution(17) returns 5: 17 -> 16 -> 8 -> 4 -> 2 -> 1 - / / / /
|
||||
# solution(18) returns 5: 18 -> 9 -> 8 -> 4 -> 2 -> 1 / - / / /
|
||||
# solution(19) returns 6: 19 -> 18 -> 9 -> 8 -> 4 -> 2 -> 1 - / - / / /
|
||||
# solution(20) returns 5: 20 -> 10 -> 5 -> 4 -> 2 -> 1 / / - / /
|
||||
# solution(21) returns 6: 21 -> 20 -> 10 -> 5 -> 4 -> 2 -> 1 - / / - / /
|
||||
# solution(21) returns 6: 22 -> 11 -> 10 -> 5 -> 4 -> 2 -> 1 - / / - / /
|
||||
|
||||
|
||||
|
||||
# solution(30) returns _: 30 -> 15 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(30) returns _: 30 -> 31 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 -
|
||||
# solution(31) returns _: 31 -> 32 -> 15 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(32) returns 5: 32 -> 16 -> 8 -> 4 -> 2 -> 1 / / / / / ###
|
||||
|
||||
# solution(61) returns _: 61 -> 60 -> 30 -> 15 -> 14 -> 7 -> 6 -> 3 -> 2 -
|
||||
# solution(62) returns 8: 62 -> 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(63) returns 7: 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(64) returns 6: 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1/ / / / / / ###
|
||||
|
||||
#
|
||||
# solution(125) returns 11: 125 -> 124 -> 62 -> 31 -> 30 -> 15 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(125) returns 10: 125 -> 124 -> 62 -> 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(125) returns 10: 125 -> 126 -> 127 -> 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 -
|
||||
# solution(126) returns 9: #
|
||||
# solution(127) returns 8: #
|
||||
# solution(128) returns 7: ###
|
||||
53
3.a. fuel-injection-perfection/patterns2.py
Normal file
53
3.a. fuel-injection-perfection/patterns2.py
Normal file
@@ -0,0 +1,53 @@
|
||||
# Step 1. if even, divide by two
|
||||
|
||||
# Step 2. if odd, add one and test if power of two - if true include as op
|
||||
# Step 3. if odd, remove one
|
||||
|
||||
# test for 1 return op counter if true, else repeat
|
||||
|
||||
|
||||
# solution(1) returns 0: 1 .
|
||||
# solution(2) returns 1: 2 -> 1 / #
|
||||
# solution(3) returns 2: 3 -> 2 -> 1 - /
|
||||
# solution(4) returns 2: 4 -> 2 -> 1 / / ###
|
||||
# solution(5) returns 3: 5 -> 4 -> 2 -> 1 - / /
|
||||
# solution(6) returns 3: 6 -> 3 -> 2 -> 1 / - /
|
||||
# solution(7) returns 4: 7 -> 6 -> 3 -> 2 -> 1 - / - /
|
||||
# solution(7) returns 4: 7 -> 8 -> 4 -> 2 -> 1 + / / / -
|
||||
# solution(8) returns 3: 8 -> 4 -> 2 -> 1 / / / ###
|
||||
# solution(9) returns 4: 9 -> 8 -> 4 -> 2 -> 1 - / / /
|
||||
# solution(10) returns 4: 10 -> 5 -> 4 -> 2 -> 1 / - / /
|
||||
# solution(11) returns 5: 11 -> 10 -> 5 -> 4 -> 2 -> 1 - / - / /
|
||||
# solution(12) returns 4: 12 -> 6 -> 3 -> 2 -> 1 / / - /
|
||||
# solution(13) returns 5: 13 -> 12 -> 6 -> 3 -> 2 -> 1 - / / - /
|
||||
# solution(14) returns 5: 14 -> 7 -> 6 -> 3 -> 2 -> 1 / - / - / -
|
||||
# solution(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1 + / / / / #
|
||||
# solution(16) returns 4: 16 -> 8 -> 4 -> 2 -> 1 / / / / ###
|
||||
# solution(17) returns 5: 17 -> 16 -> 8 -> 4 -> 2 -> 1 - / / / /
|
||||
# solution(18) returns 5: 18 -> 9 -> 8 -> 4 -> 2 -> 1 / - / / /
|
||||
# solution(19) returns 6: 19 -> 18 -> 9 -> 8 -> 4 -> 2 -> 1 - / - / / /
|
||||
# solution(20) returns 5: 20 -> 10 -> 5 -> 4 -> 2 -> 1 / / - / /
|
||||
# solution(21) returns 6: 21 -> 20 -> 10 -> 5 -> 4 -> 2 -> 1 - / / - / /
|
||||
# solution(21) returns 6: 22 -> 11 -> 10 -> 5 -> 4 -> 2 -> 1 - / / - / /
|
||||
|
||||
# solution(30) returns _: 30 -> 15 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(30) returns _: 30 -> 31 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 -
|
||||
# solution(31) returns _: 31 -> 32 -> 15 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(32) returns 5: 32 -> 16 -> 8 -> 4 -> 2 -> 1 / / / / / ###
|
||||
|
||||
# solution(61) returns _: 61 -> 60 -> 30 -> 15 -> 14 -> 7 -> 6 -> 3 -> 2 -
|
||||
# solution(62) returns 8: 62 -> 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(63) returns 7: 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(64) returns 6: 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1/ / / / / / ###
|
||||
|
||||
#
|
||||
# solution(125) returns 11: 125 -> 124 -> 62 -> 31 -> 30 -> 15 -> 14 -> 7 -> 6 -> 3 -> 2 -> 1
|
||||
# solution(125) returns 10: 125 -> 124 -> 62 -> 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
# solution(125) returns 10: 125 -> 126 -> 127 -> 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 -
|
||||
# solution(125) returns 9 : 125 -> 124 -> 62 -> 31 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
|
||||
# solution(126) returns 9: 126 -> 127 -> 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(126) returns 9: 126 -> 63 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
|
||||
# solution(127) returns 8: 127 -> 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 #
|
||||
# solution(128) returns 7: 128 -> 64 -> 32 -> 16 -> 8 -> 4 -> 2 -> 1 ###
|
||||
53
3.a. fuel-injection-perfection/readme.txt
Normal file
53
3.a. fuel-injection-perfection/readme.txt
Normal file
@@ -0,0 +1,53 @@
|
||||
Fuel Injection Perfection
|
||||
=========================
|
||||
|
||||
Commander Lambda has asked for your help to refine the automatic quantum antimatter fuel injection system for her LAMBCHOP doomsday device. It's a great chance for you to get a closer look at the LAMBCHOP - and maybe sneak in a bit of sabotage while you're at it - so you took the job gladly.
|
||||
|
||||
Quantum antimatter fuel comes in small pellets, which is convenient since the many moving parts of the LAMBCHOP each need to be fed fuel one pellet at a time. However, minions dump pellets in bulk into the fuel intake. You need to figure out the most efficient way to sort and shift the pellets down to a single pellet at a time.
|
||||
|
||||
The fuel control mechanisms have three operations:
|
||||
|
||||
1) Add one fuel pellet
|
||||
2) Remove one fuel pellet
|
||||
3) Divide the entire group of fuel pellets by 2 (due to the destructive energy released when a quantum antimatter pellet is cut in half, the safety controls will only allow this to happen if there is an even number of pellets)
|
||||
|
||||
Write a function called solution(n) which takes a positive integer as a string and returns the minimum number of operations needed to transform the number of pellets to 1. The fuel intake control panel can only display a number up to 309 digits long, so there won't ever be more pellets than you can express in that many digits.
|
||||
|
||||
For example:
|
||||
solution(4) returns 2: 4 -> 2 -> 1
|
||||
solution(15) returns 5: 15 -> 16 -> 8 -> 4 -> 2 -> 1
|
||||
|
||||
Languages
|
||||
=========
|
||||
|
||||
To provide a Python solution, edit solution.py
|
||||
To provide a Java solution, edit Solution.java
|
||||
|
||||
Test cases
|
||||
==========
|
||||
Your code should pass the following test cases.
|
||||
Note that it may also be run against hidden test cases not shown here.
|
||||
|
||||
-- Python cases --
|
||||
Input:
|
||||
solution.solution('15')
|
||||
Output:
|
||||
5
|
||||
|
||||
Input:
|
||||
solution.solution('4')
|
||||
Output:
|
||||
2
|
||||
|
||||
-- Java cases --
|
||||
Input:
|
||||
Solution.solution('4')
|
||||
Output:
|
||||
2
|
||||
|
||||
Input:
|
||||
Solution.solution('15')
|
||||
Output:
|
||||
5
|
||||
|
||||
Use verify [file] to test your solution and see how it does. When you are finished editing your code, use submit [file] to submit your answer. If your solution passes the test cases, it will be removed from your home folder.
|
||||
16
3.a. fuel-injection-perfection/sandbox.py
Normal file
16
3.a. fuel-injection-perfection/sandbox.py
Normal file
@@ -0,0 +1,16 @@
|
||||
# i = 20
|
||||
# while i > 0:
|
||||
# print i
|
||||
# if ((i >> 1) & 1) == 1:
|
||||
# print "true: " + str(bin(i))
|
||||
# else:
|
||||
# print "false: " + str(bin(i))
|
||||
# i = i - 1
|
||||
|
||||
print str(170) + " " + str(bin(170))
|
||||
print str(170+1) + " " + str(bin(170+1))
|
||||
print str(170+2) + " " + str(bin(170+2))
|
||||
print str(170+3) + " " + str(bin(170+3))
|
||||
print str(170+4) + " " + str(bin(170+4))
|
||||
print str(170-1) + " " + str(bin(170-1))
|
||||
|
||||
62
3.a. fuel-injection-perfection/solution.py
Normal file
62
3.a. fuel-injection-perfection/solution.py
Normal file
@@ -0,0 +1,62 @@
|
||||
def solution(n):
|
||||
opCounter = 0
|
||||
pellets = int(n)
|
||||
|
||||
while pellets > 1:
|
||||
if pellets > 100:
|
||||
print "Iteration Started w/ " + str(pellets) + " pellets left. - " + str(bin(pellets))
|
||||
elif pellets < 100 and pellets > 9:
|
||||
print "Iteration Started w/ " + str(pellets) + " pellets left. - " + str(bin(pellets))
|
||||
else:
|
||||
print "Iteration Started w/ " + str(pellets) + " pellets left. - " + str(bin(pellets))
|
||||
|
||||
if pellets % 2 == 0:
|
||||
pellets /= 2
|
||||
elif (pellets > 3 and (pellets & 1) == 1 and (pellets >> 1) & 1) == 1:
|
||||
pellets += 1
|
||||
else:
|
||||
pellets -= 1
|
||||
|
||||
opCounter += 1
|
||||
return opCounter
|
||||
|
||||
|
||||
|
||||
|
||||
case1 = int(solution('372'))
|
||||
if case1 == 11:
|
||||
print "Test 1: Passed"
|
||||
else:
|
||||
print "Test 1: Failed"
|
||||
print "Recieved " + str(case1)
|
||||
|
||||
|
||||
case2 = int(solution('15'))
|
||||
if case2 == 5:
|
||||
print "Test 2: Passed"
|
||||
else:
|
||||
print "Test 2: Failed"
|
||||
print "Recieved " + str(case2)
|
||||
|
||||
|
||||
case3 = int(solution('5'))
|
||||
if case3 == 3:
|
||||
print "Test 3: Passed"
|
||||
else:
|
||||
print "Test 3: Failed"
|
||||
print "Recieved " + str(case3)
|
||||
|
||||
|
||||
case4 = int(solution('4'))
|
||||
if case4 == 2:
|
||||
print "Test 4: Passed"
|
||||
else:
|
||||
print "Test 4: Failed"
|
||||
print "Recieved " + str(case4)
|
||||
|
||||
case5 = int(solution('170'))
|
||||
if case5 == 10:
|
||||
print "Test 5: Passed"
|
||||
else:
|
||||
print "Test 5: Failed"
|
||||
print "Recieved " + str(case5)
|
||||
Reference in New Issue
Block a user