Last active
December 13, 2019 09:39
-
-
Save codeyu/cc033d680ca43df2b1bb8dfcf87804a2 to your computer and use it in GitHub Desktop.
put_apple_to_basket
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Linq; | |
namespace put_apple_to_basket | |
{ | |
class Program | |
{ | |
static void Main(string[] args) | |
{ | |
var apples = new int[] { 6, 16, 26, 36, 46, 56, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69 }; | |
var r = 6; | |
PutAppleToBasket(apples, apples.Length, r); | |
Console.ReadKey(); | |
} | |
private static void Put(int[] baskets, int[] apples, int index, int r, int start, int end) | |
{ | |
if(index == r) | |
{ | |
var ap = Enumerable.Range(0, r).Select(x => apples[baskets[x]]).ToArray(); | |
if (ap.Sum() == 100) | |
{ | |
Console.WriteLine(string.Join(",", ap)); | |
} | |
return; | |
} | |
//https://stackoverflow.com/questions/915745/thoughts-on-foreach-with-enumerable-range-vs-traditional-for-loop | |
//https://www.codeproject.com/articles/538561/ranges-in-csharp | |
foreach (var i in Enumerable.Range(start, end - start + 1)) | |
{ | |
baskets[index] = i; | |
Put(baskets, apples, index + 1, r, i, end); | |
} | |
} | |
private static void PutAppleToBasket(int[] apples, int n, int r) | |
{ | |
var baskets = new int[r]; | |
Put(baskets, apples, 0, r, 0, n - 1); | |
} | |
} | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package main | |
import "fmt" | |
func main() { | |
apples := []int{6,16,26,36,46,56,60,61,62,63,64,65,66,67,68,69} | |
n := len(apples) | |
r := 6 | |
put_apple_to_basket(apples, n, r) | |
} | |
func put(baskets []int, apples []int, index int, r int, start int, end int){ | |
if index==r{ | |
ap := make([]int, r) | |
for i := 0; i < r; i++ { | |
ap[i] = apples[baskets[i]] | |
} | |
if sum(ap) == 100{ | |
fmt.Println(ap) | |
} | |
return | |
} | |
for i := start; i <= end; i++ { | |
baskets[index] = i | |
put(baskets, apples, index + 1, r, i, end) | |
} | |
return | |
} | |
func put_apple_to_basket(apples []int, n int, r int){ | |
baskets := make([]int, r) | |
put(baskets, apples, 0, r, 0, n - 1) | |
} | |
func sum(array []int) int { | |
result := 0 | |
for _, v := range array { | |
result += v | |
} | |
return result | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Array.range = (start, end) => Array.from({length: (end - start)}, (v, k) => k + start); | |
const put = (baskets, apples, index, r, start, end) =>{ | |
if(index === r){ | |
let ap = Array.from(Array(r).keys()).map(i => apples[baskets[i]]); | |
let sum = ap.reduce((v, i) => (v + i)); | |
if(sum === 100){ | |
console.log(ap); | |
} | |
return; | |
} | |
Array.range(start, end).forEach(i=>{ | |
baskets[index] = i; | |
put(baskets, apples, index + 1, r, i, end); | |
}); | |
} | |
const put_apple_to_basket = (apples, n, r)=>{ | |
let baskets = new Array(r); | |
put(baskets, apples, 0, r, 0, n - 1); | |
} | |
let apples = [6,16,26,36,46,56,60,61,62,63,64,65,66,67,68,69]; | |
let n = apples.length; | |
let r = 6; | |
put_apple_to_basket(apples, n, r); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def put(baskets, apples, index, r, start, end): | |
if index == r: | |
ap = [apples[baskets[i]] for i in range(r)] | |
if sum(ap) == 100: | |
print(ap) | |
return | |
for i in range(start, end + 1): | |
baskets[index] = i | |
put(baskets, apples, index + 1, r, i, end) | |
return | |
def put_apple_to_basket(apples, n, r): | |
baskets = [0 for i in range(r + 1)] | |
put(baskets, apples, 0, r, 0, n - 1) | |
apples = [6,16,26,36,46,56,60,61,62,63,64,65,66,67,68,69] | |
n = len(apples) | |
r = 6 | |
put_apple_to_basket(apples, n, r) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def put(baskets, apples, index, r, s, e) | |
if index == r then | |
ap = (0..r-1).to_a.freeze.map do |i| | |
apples[baskets[i]] | |
end | |
if ap.inject(:+) == 100 then | |
p ap | |
end | |
return | |
end | |
for i in s .. e | |
baskets[index] = i | |
put(baskets, apples, index + 1, r, i, e) | |
end | |
return | |
end | |
def put_apple_to_basket(apples, n, r) | |
baskets = (0..r).to_a | |
put(baskets, apples, 0, r, 0, n - 1) | |
end | |
apples = [6,16,26,36,46,56,60,61,62,63,64,65,66,67,68,69] | |
n = apples.length | |
r = 6 | |
put_apple_to_basket(apples, n, r) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment