Using Python 3.6.1 and recursion Given an array of ints, is it possible to choos
ID: 3817227 • Letter: U
Question
Using Python 3.6.1 and recursion
Given an array of ints, is it possible to choose group of some of the ints, such that the group sums to the given target, with this additional constraint: if there are numbers in the array that are adjacent and the identical value, they must either all be chosen, or none of them chosen. For example, with the array [1, 2, 2, 2, 5, 2], either all three 2's in the middle must be chosen or not, all as a group, (one loop can be used to find the extent of the identical values). def groupSumClump(start, nums, target): Given an array of ints, is it possible to divide the ints into two groups, so that the sums of the two groups are the same. Every int must be in one group or the other. Write a recursive helper method that takes whatever arguments you like, and make the initial call to your recursive helper from splitArray(). (No loops needed.) def splitArray(nums): def splitArrayHelper(): Given an array of ints, is it possible to divide the ints into two groups, so that the sum of one group is a multiple of 10, and the sum of the other group is odd. Every int must be in one group or the other. Write a recursive helper method that takes whatever arguments you like, and make the initial call to your recursive helper from splitOdd10(). (No loops needed.) def splitOdd10(nums): def splitOdd10Helper(): Given an array of ints, is it possible to divide the ints into two groups, so that the sum of the two groups is the same, with these constraints: all the values that are multiple of 5 must be in one group, and all the values that are a multiple of 3 (and not a multiple of 5) must be in the other. (No loops needed.) def split53(nums): def split53Helper():Explanation / Answer
public boolean groupSumClump(int start, int[] nums, int target) {
if(start>=nums.length){
return target==0;}
if(start!=0 ){ //removed uneeded and statement
int count=0;//I chanced these two values
int i=start;
while(i<nums.length && nums[start]==nums[i]){
count++;
i++;
}
//choose all adjacent identical--i took off the (-1) from count
if(groupSumClump(i, nums, target-((count)*nums[start]))) return true;
//choose none
if(groupSumClump(i, nums, target)) return true;
return false;}
else {
//choose
if(groupSumClump(start+1, nums, target-nums[start])) return true;
//dont choose
if(groupSumClump(start+1, nums, target)) return true;
return false;}
}
*************************************************
public boolean splitArray(int[] nums) {
if(nums.length == 1)
return false;
if(nums.length == 0)
return true;
int total = totalSum(nums , nums.length);
if(total % 2 == 0)
{
if(groupSum(0, nums.length, nums, total/2))
return true;
}
return false;
}
public int totalSum(int [] nums, int n)
{
if (n == 1)
return nums[0];
else
return totalSum(nums, n-1) + nums[n-1];
}
public boolean groupSum(int start, int end, int[] nums, int target) {
if (start >= end)
if(target == 0)
return true;
else return false;
if (groupSum(start + 1, end, nums, target - nums[start])) return true;
if (groupSum(start + 1, end, nums, target)) return true;
return false;}
***************************************************
*****************************************************
public boolean splitOdd10(int[] nums) {
if(nums.length == 1)
if(nums[0] % 2 != 0 || nums[0] % 10 == 0)
return true;
else return false;
if(nums.length == 0)
return false;
int total = totalSum(nums, nums.length);
int tens = total / 10;
if(determineValidity1(total, tens, nums))
return true;
return false;
}
public int totalSum(int [] nums, int n)
{
if (n == 1)
return nums[0];
else
return totalSum(nums, n-1) + nums[n-1];
}
public boolean determineValidity1(int total, int tens, int [] nums)
{
if(tens <= 0)
return false;
if(groupSum(0, nums.length, nums, tens * 10) && (total - tens*10) % 2 != 0 )
return true;
else tens--;
return determineValidity1(total, tens, nums);
}
public boolean groupSum(int start, int end, int[] nums, int target) {
if (start >= end)
if(target == 0)
return true;
else return false;
if (groupSum(start + 1, end, nums, target - nums[start])) return true;
if (groupSum(start + 1, end, nums, target)) return true;
return false;
}
*******************************************************
public boolean split53(int[] nums) {
if(nums.length == 0)
return false;
else
return groupSum53(0, nums, 0, 0);
}
public boolean groupSum53(int i, int[] nums, int sum5, int sum3)
{
if(i == nums.length)
return (sum5 == sum3);
if(nums[i]%5==0){
return groupSum53(i+1, nums, sum5 + nums[i], sum3);
}
else if(nums[i]%3==0){
return groupSum53(i+1, nums, sum5, sum3 + nums[i]);
}
else
return groupSum53( i+1, nums, sum5 + nums[i], sum3) || groupSum53(i+1, nums, sum5, sum3 + nums[i]);
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.