AI编程助手体验

一、背景

最近在接触各种AI插件,用copilot的话要10美元每月,太贵,就下载了清华的CodeGeeX

刚好,有个需求,需要将枚举的所有值,随机组合求和,并返回所有组合之和。

”假设你有一个名为 enum_values 的枚举,包含值为 1、2、3 和 4,用java代码来计算两个、三个或者四个选项的随机组合的和。“

二、CodeGeeX

首先用CodeGeeX提问,

我:枚举value有1,2,3,4多种选项,求出各种选项的排列组合之合

CodeGeeX: 假设你有一个名为 enum_values 的枚举,包含值为 1、2、3 和 4,你可以使用 Python 中的 itertools 模块来计算所有可能的排列和组合。

以下是计算所有可能的排列的示例代码:

from itertools import permutations

enum_values = [1, 2, 3, 4]

# 计算全排列  
permutations(enum_values)  

你可以使用 list() 函数将生成器转换为列表,以查看所有可能的组合:

list(combinations(enum_values, 2))  
list(combinations(enum_values, 3))  
list(combinations(enum_values, 4))  

输出结果:
[(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)]  
[(1, 2, 3), (1, 2, 4), (1, 3, 4), (2, 3, 4)]  
[(1, 2, 3, 4)]  

结果和我需要的一致,但是不是java代码,所以我继续问她

我:枚举value有1,2,3,4多种选项,求出两个到四个选项随机组合的和有哪些,用java代码实现

CodeGeeX: 以下是 Java 代码实现,用于枚举 value 有 1,2,3,4 多种选项,求出两个到四个选项随机组合的和有哪些:

import java.util.ArrayList;  
import java.util.List;  
import java.util.Random;

public class Main {  
    public static void main(String[] args) {  
        int numOptions = 4;  
        int minCombinations = 2;  
        int maxCombinations = 4;  
          
        // Create a list of all possible combinations  
        List<List<Integer>> combinations = new ArrayList<>();  
        for (int i = minCombinations; i <= maxCombinations; i++) {  
            combinations.add(new ArrayList<>());  
            for (int j = 0; j < numOptions; j++) {  
                combinations.get(i).add(j + 1);  
            }  
        }  
          
        // Print the sum of each combination  
        for (List<Integer> combination : combinations) {  
            int sum = 0;  
            for (Integer value : combination) {  
                sum += value;  
            }  
            System.out.println("Combination " + combination + " has sum " + sum);  
        }  
          
        // Shuffle the combinations and print them again  
        Random rand = new Random();  
        List<List<Integer>> shuffledCombinations = new ArrayList<>();  
        for (List<Integer> combination : combinations) {  
            shuffledCombinations.add(new ArrayList<>());  
            for (Integer value : combination) {  
                int index = rand.nextInt(combination.size());  
                shuffledCombinations.get(combination.size() - index).add(value);  
            }  
        }  
        for (List<Integer> combination : shuffledCombinations) {  
            int sum = 0;  
            for (Integer value : combination) {  
                sum += value;  
            }  
            System.out.println("Shuffled Combination " + combination + " has sum " + sum);  
        }  
    }  
}

跟我预料的不太一样,还报错,所以放弃了

二、Copilot

package com.fuchuang.mcenter.controller.web;

import java.util.Arrays;
import java.util.Random;

public class RandomCombinationSum {
    public static void main(String[] args) {
        int[] enum_values = {1, 2, 3, 4};
        for (int i = 2; i < enum_values.length; i++) {
            int[] combination = getRandomCombination(enum_values, i);
            int sum = calculateSum(combination);
            System.out.println(i+"random values: " + Arrays.toString(combination));
            System.out.println("Sum: " + sum);
        }

    }

    public static int[] getRandomCombination(int[] values, int count) {
        Random random = new Random();
        int[] combination = new int[count];
        for (int i = 0; i < count; i++) {
            combination[i] = values[random.nextInt(values.length)];
        }
        return combination;
    }

    public static int calculateSum(int[] values) {
        int sum = 0;
        for (int value : values) {
            sum += value;
        }
        return sum;
    }
}

试了下,返回的结果也跟我期望的不太一致,他真的是在随机找一个数字想加

三、chatGpt

由于第一次推荐的Python的方法是key实现的,所以想将Python方法翻译成java代码.

用chatGpt提问:itertools的combinations方法,转成java代码

先将python代码发给chatgpt
mport itertools

enum_values = [1, 2, 3, 4]
print(list(itertools.combinations(enum_values, 3)))

再问:将上面代码转成java代码

import java.util.ArrayList;
import java.util.List;

public class Main {
    public static void main(String[] args) {
        List<Integer> enumValues = new ArrayList<>();
        enumValues.add(1);
        enumValues.add(2);
        enumValues.add(3);
        enumValues.add(4);
       

        List<List<Integer>> combinations = new ArrayList<>();
        int n = enumValues.size();
        int k = 3;

        if (k <= n) {
            generateCombinations(enumValues, combinations, new ArrayList<>(), k, 0);
        }

        for (List<Integer> combination : combinations) {
            System.out.println(combination);
        }
    }

    private static void generateCombinations(List<Integer> enumValues, List<List<Integer>> combinations,
                                             List<Integer> currentCombination, int k, int startIndex) {
        if (currentCombination.size() == k) {
            combinations.add(new ArrayList<>(currentCombination));
            return;
        }

        for (int i = startIndex; i < enumValues.size(); i++) {
            currentCombination.add(enumValues.get(i));
            generateCombinations(enumValues, combinations, currentCombination, k, i + 1);
            currentCombination.remove(currentCombination.size() - 1);
        }
    }
}

自己再优化一下:

package com.fuchuang.mcenter.controller.web.vo;

import io.swagger.models.auth.In;

import java.util.ArrayList;
import java.util.List;
import java.util.function.Predicate;

public class Test {
    public static void main(String[] args) {
        List<Integer> enumValues = new ArrayList<>();
        enumValues.add(1);
        enumValues.add(2);
        enumValues.add(3);
        enumValues.add(4);

        List<List<Integer>> combinations = new ArrayList<>();
        for (int i = 2; i <= enumValues.size(); i++) {
            generateCombinations(enumValues, combinations, new ArrayList<>(), i, 0);
        }

        for (List<Integer> combination : combinations) {
            System.out.println(combination + ",和:" + combination.stream().mapToInt(i -> i.intValue()).sum());
        }

    }

    private static void generateCombinations(List<Integer> enumValues, List<List<Integer>> combinations,
                                             List<Integer> currentCombination, int k, int startIndex) {
        if (currentCombination.size() == k) {
            combinations.add(new ArrayList<>(currentCombination));
            return;
        }

        for (int i = startIndex; i < enumValues.size(); i++) {
            currentCombination.add(enumValues.get(i));
            generateCombinations(enumValues, combinations, currentCombination, k, i + 1);
            currentCombination.remove(currentCombination.size() - 1);
        }
    }

}

执行结果:

[1, 2],和:3
[1, 3],和:4
[1, 4],和:5
[2, 3],和:5
[2, 4],和:6
[3, 4],和:7
[1, 2, 3],和:6
[1, 2, 4],和:7
[1, 3, 4],和:8
[2, 3, 4],和:9
[1, 2, 3, 4],和:10

perfect,虽然是个简单的小需求,但是要自己写的这么简洁,还是很考验算法能力的,AI让你能快速解决复杂的东西。期待后面的应用

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值