Sritharan Mahendra Babu
BAN USER- 0of 0 votes
Answers2. CURIOUS ANT (easier)
- Sritharan Mahendra Babu in United States
Assume you have a typical tree-like structure. Each branch can have more children branches but it can also have some number of fruits (this is just a number associated with each branch).
Imagine an ant that wants to start at the root or the tree and go to the very end of some branch path. The ant is asking you: is it possible for me to encounter at least N fruits on my way?
Write a program that answers Yes or No to this question.
Write the code in Python| Report Duplicate | Flag | PURGE
Pixability Software Engineer Algorithm - -1of 1 vote
Answers1. DR. ROPES (harder)
- Sritharan Mahendra Babu in United States
Assume you have some number of ropes of varying length.
For example: 2 ft, 5 ft, 6 ft, 6 ft (possible to have two or more ropes of the same length).
Write a program that determines if it is possible to string together the ropes to produce length of exactly N.
For the example set above, your program would answer:
N of 7 : yes
N of 11 : yes
N of 3 : no
N of 12 : yes
N of 18 : no
Note: Please write your solution without using the itertools package.
Write the code in Python| Report Duplicate | Flag | PURGE
Pixability Software Engineer Algorithm - 0of 0 votes
Answers2. CURIOUS ANT (easier)
- Sritharan Mahendra Babu in United States
Assume you have a typical tree-like structure. Each branch can have more children branches but it can also have some number of fruits (this is just a number associated with each branch).
Imagine an ant that wants to start at the root or the tree and go to the very end of some branch path. The ant is asking you: is it possible for me to encounter at least N fruits on my way?
Write a program that answers Yes or No to this question.
Please write the code in Python| Report Duplicate | Flag | PURGE
- 0of 0 votes
Answers1. DR. ROPES (harder)
- Sritharan Mahendra Babu in United States
Assume you have some number of ropes of varying length.
For example: 2 ft, 5 ft, 6 ft, 6 ft (possible to have two or more ropes of the same length).
Write a program that determines if it is possible to string together the ropes to produce length of exactly N.
For the example set above, your program would answer:
N of 7 : yes
N of 11 : yes
N of 3 : no
N of 12 : yes
N of 18 : no
Note: Please write your solution without using the itertools package.
Please write the code in Python| Report Duplicate | Flag | PURGE
Algorithm
def drroper(self, array_ropes, desired_rope_length):
# initializing local variables for the final sum result
final_sum = array_ropes[0]
# initializing the pointer index to monitor the status of the final sum
start = 0
# getting the length of the array for iteration
length_array_ropes = len(array_ropes)
for i in range(1, length_array_ropes + 1):
# this seems to be like a O(n2) problem but the while loop is run for the length of i
# and not for the entire length of the array hence by means of arbitrary computation
# the total time complexity would be O(n)
# The while loop is run to maintain the final result < or = to the desired rope length
while (final_sum > desired_rope_length) and (start < i - 1):
final_sum = final_sum - array_ropes[start]
start += 1
# Terminating statement if the desired length is obtained
if final_sum == desired_rope_length:
return "It is possible to tie the ropes together"
# If the final sum is less than the required length the additional ropes are added
# to the final sum
if i < length_array_ropes:
final_sum = final_sum + array_ropes[i]
# This is the worst case scenario when the ropes cannot be tied together to obtained
# the desired length
return "The ropes cannot be tied together"
Can you check the code which I worte at the time of coding challenge !!!!
- Sritharan Mahendra Babu February 22, 2015
I think u can look at the answer given by Nelson. This not exactly a finding total sum. You have to check whether sum of a particular set of elements is equal to the given value and as said by you not to find the maximum sum of elements present in the set
- Sritharan Mahendra Babu February 22, 2015