【人工智能 Open AI 】我们程序员真的要下岗了- 全能写Go / C / Java / C++ / Python / JS 人工智能机器人

news2024/9/20 9:26:27

文章目录

    • @[toc]
  • 人工智能 AI Code 写代码测试
    • 用golang实现冒泡排序
    • 用golang实现计算环比函数
    • goroutine and channel
    • 用golang实现二叉树遍历代码
    • 用golang实现线程安全的HashMap操作代码
    • using C programming language write a tiny Operation System
    • use C language write a tiny compiler
    • 用golang实现B+树代码
    • 使用 golang 写一个开启10个协程请求rpc接口的代码
    • 用 C 语言实现一个HashMap 代码
    • use C language write a Collection Operation API

人工智能 AI Code 写代码测试

用golang实现冒泡排序

package main

import "fmt"
 
func main() {
    arr := []int{3,2,1,3,2,4,8,7,9,2,3}
    bubbleSort(arr) //冒泡排序
    fmt.Println("排序后:",arr)
}
 
func bubbleSort(arr []int){
    for i:= 0; i < len(arr)-1; i++{
        for j:=0;j < len(arr)-1-i ;j++{
            if arr[j] > arr[j+1]{
                arr[j],arr[j+1] = arr[j+1],arr[j]
            }
        }
    }
}

用golang实现计算环比函数

package main
 
import "fmt"
 
func main() {
	fmt.Println(calcLinkRatio(5,3))
}
 
//计算环比值(升、降)
func calcLinkRatio(a, b float64) float64 {
    //环比
    linkRatio := (b - a) / a * 100
    return linkRatio
}

在这里插入图片描述

goroutine and channel


package main

import "fmt"

// create a channel
var ch = make(chan int)

// send data to channel
func sendData(nums ...int) {
	for _, n := range nums {
		ch <- n
	}
}

// receive data from channel
func receiveData() {
	for {
		num := <-ch
		fmt.Println("receive data from channel:", num)
	}
}

// main function
func main() {
	// create go routine
	go sendData(1, 2, 3, 4, 5, 6)
	go receiveData()

	// wait for all routines to finish
	var input string
	fmt.Scanln(&input)
	fmt.Println("Done")
}

用golang实现二叉树遍历代码

type TreeNode struct
{
    Val int
    Left *TreeNode
    Right *TreeNode
}

//先序遍历
func PreOrder(node *TreeNode) {
    if node == nil {
        return
    }
    //访问根节点
    fmt.Printf("%d ", node.Val)
    PreOrder(node.Left)
    PreOrder(node.Right)
}

//中序遍历
func InOrder(node *TreeNode) {
    if node == nil {
        return
    }
    InOrder(node.Left)
    fmt.Printf("%d ", node.Val)
    InOrder(node.Right)
}

//后序遍历
func PostOrder(node *TreeNode) {
    if node == nil {
        return
    }
    PostOrder(node.Left)
    PostOrder(node.Right)
    fmt.Printf("%d ", node.Val)
}

用golang实现线程安全的HashMap操作代码

go
package main
 
import (
    "fmt"
    "sync"
)
 
type SafeHashMap struct {
    lock *sync.Mutex
    m    map[int]int
}

// 创建一个新的 HashMap,并初始化 Mutex 锁
func NewSafeHashMap() *SafeHashMap {
    return &SafeHashMap{ lock: new(sync.Mutex), m : make(map[int]int)}
}
 
// 根据键值对 kv 向 HashMap 中插入一项数据
// 返回 bool 值,若插入成功则返回 true,否则返回 false
func (shm *SafeHashMap) Put(k int, v int) bool {
    // 上锁
    shm.lock.Lock()
    defer shm.lock.Unlock()
 
    // 若 key 已经存在,则返回 false
    _, ok := shm.m[k]
    if ok {
        return false
    }
 
    // 插入 key-value 键值对
    shm.m[k] = v
 
    return true
}
 
// 根据 key 从 HashMap 中取出一项 key-value 键值对,
// 若 key 存在则返回对应的 value 以及 true 值,
// 否则返回零值以及 false 值
func (shm *SafeHashMap) Get(k int) (int, bool) {
    // 上锁
    shm.lock.Lock()
    defer shm.lock.Unlock()
 
    // 根据 key 取出 value
    v, ok := shm.m[k]
    return v, ok
}
 
// 从 HashMap 中移除一项 key-value 键值对,
// 若 key 存在则移除该项并返回 true 值,
// 否则返回 false 值
func (shm *SafeHashMap) Remove(k int) bool {
    // 上锁
    shm.lock.Lock()
    defer shm.lock.Unlock()
 
    _, ok := shm.m[k]
    if ok {
        delete(shm.m, k)
    }
    return ok
}
 
// 返回 HashMap 中 key-value 键值对的项数
func (shm *SafeHashMap) Size() int {
    return len(shm.m)
}
 
func main() {
    // 创建一个空的安全的 HashMap
    shm := NewSafeHashMap()
 
    // 插入 key-value 键值对
    shm.Put(1, 10)
    v, ok := shm.Get(1)
    fmt.Println(v, ok)
 
    // 移除 key
    ok = shm.Remove(1)
    fmt.Println(ok)
 
    // 获取 key 的数量
    fmt.Println(shm.Size())
}

using C programming language write a tiny Operation System

哈哈,让 AI 写一个操作系统,估计有点难为它了。目前来看,还达不到MOSS的水平,自动生成操作系统。

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/******************** Function declarations ********************/
void clear(void); // clears the console
void print_welcome_message (void);  // prints an introductory message
void command_input(void); // get an input from the user
int run_command (char *inp); // runs the command which is given by the user

/******************** Main code ********************/
int main(void)
{
    clear();
    print_welcome_message();
    command_input();

    return 0;
}

/******************** Function definitions ********************/
void clear()
{
    #ifdef WINDOWS
    system("cls");
    #else
    system("clear");
    #endif
}

void print_welcome_message()
{
    printf("Welcome to our Tiny OS!\n");
    printf("Start typing the commands:\n\n");
}

void command_input()
{
    char input_str[20];

    while(1)
    {
        printf("> ");
        scanf("%s", input_str);
        if(run_command(input_str) == -1)
        {
            break;
        }
    }
}

int run_command(char *inp)
{
    if(strcmp(inp, "exit") == 0)
    {
        printf("\nbye!\n\n");
        return -1;
    }
    else
    {
        printf("Invalid Command.\nType \"exit\" to Exit.\n\n");
        return 0;
    }
    
}

用 C 语言实现操作系统的自举启动的代码。

#include "boot.h"

// 加载操作系统
void boot_load_os()
{    
    // 这里是一大堆代码
    // ...

    // 执行操作系统
    execute_os();
}

use C language write a tiny compiler

写个编译器看看。

#include <stdio.h>
#include <string.h>

const int MAX_SIZE = 1000;

// creating a structure to store the tokens.
struct tokens {
    char* type;
    char* value;
};

//function to lex the program
struct tokens lex(char* input)
{
    struct tokens token;
    int i = 0;
    int startIndex = 0;
    while(input[i] != '\0')
    {
        char currentChar = input[i];
        if (currentChar == ' ')
        {
            char* str = malloc(i - startIndex);
            memcpy(str, &input[startIndex], i - startIndex);
            token.type = str;
            token.value = NULL;
            break;
        }
        else if (currentChar == '+' || currentChar == '-')
        {
            token.type = "op";
            token.value = malloc(1);
            token.value[1] = '\0';
            token.value = currentChar;
            break;
        }
        i++;
    }

    return token;
}

int main() {
    char input[MAX_SIZE];
    printf("Enter a simple expression:\t");
    scanf("%s", input);
    struct tokens token = lex(input);
    printf("Type: %s, Value: %s\n", token.type, token.value);
    return 0;
}

用golang实现B+树代码

package bptree

// B+树定义
type BPTree struct {
	Order    int                    // B+树的阶数
	Root     *Node                  // 根节点
	Height   int                    // 树的高度
	Size     int                    // 树的大小
	Sequence func(interface{}, int) // B+树键比较函数
}

// B+树中的节点
type Node struct {
	IsLeaf   bool              // 标识该节点是否为叶子节点
	Entries  []*Entry          // 节点上的数据条目
	Next     *Node             // 指向右兄弟节点
	Children []*Node           // 不是叶子节点时有效,存放子节点指针
	Parent   *Node             // 指向父节点
	Items    map[interface{}]*Entry // 键值对
}

// 数据条目
type Entry struct {
	Key   interface{} // B+树索引键
	Value interface{} // 数据值
}

// 构造函数,构建一棵B+树
func NewTree(order int, sequence func(interface{}, int)) *BPTree {
	tree := &BPTree{
		Order:    order,
		Root:     nil,
		Height:   0,
		Size:     0,
		Sequence: sequence,
	}

	return tree
}

// 搜索数据,返回搜索键的数据记录
func (tree *BPTree) Search(key interface{}) interface{} {
	node := tree._search(tree.Root, key)
	if node == nil {
		return nil
	}

	entry := node.Items[key]
	if entry == nil {
		return nil
	}

	return entry.Value
}

// 内部搜索函数,返回搜索键所在节点
func (tree *BPTree) _search(node *Node, key interface{}) *Node {
	if node == nil {
		return nil
	}

	// 在节点上搜索键
	i, _ := tree._binarySearch(node, key)

	// 找到键
	if i >= 0 && node.Entries[i].Key == key {
		return node
	}

	// 不是叶子节点,从子节点继续搜索
	if !node.IsLeaf {
		return tree._search(node.Children[i], key)
	}

	// 未找到键
	return nil
}

// 二分搜索函数,在节点上搜索键
func (tree *BPTree) _binarySearch(node *Node, key interface{}) (int, bool) {
	low := 0
	high := len(node.Entries) - 1
	for low <= high {
		mid := (low + high) / 2
		result := tree.cmpFunc(key, node.Entries[mid])
		if result == 0 {
			return mid, true // 搜索成功
		} else if result < 0 {
			high = mid - 1
		} else {
			low = mid + 1
		}
	}
	return low, false // 搜索失败
}

			

使用 golang 写一个开启10个协程请求rpc接口的代码

// 请求rpc
func RpcRequest(aid int, ch chan int) {
    resp := sendRpc(aid) // 发送rpc
    ch <- resp
}

func main() {
	ch := make(chan int, 10)
    for i := 0; i < 10; i++ {
    	go RpcRequest(i, ch)
    }
    for i := 0; i < 10; i++ {
    	resp := <- ch
    	fmt.Println("resp:", resp)
    }
}

用 C 语言实现一个HashMap 代码

/* 定义key-value字段 */
typedef struct
{
    void *key; //key 
    void *value; //value
    struct ht_node *next; //单链表
} ht_node;

/* 定义hashmap */
typedef struct
{
    int size; //字典的大小 
    ht_node **head;  //字典的头
    int count;   //元素的个数
}ht_hash_map;

/*hash函数,计算键值*/
int hash_int_func(int key)
{
    key += ~(key << 15);
    key ^= (key >> 10);
    key += (key << 3);
    key ^= (key >> 6);
    key += ~(key << 11);
    key ^= (key >> 16);
    return (key & 0x7FFFFFFF);
}

/* 创建hashmap */
ht_hash_map *ht_create(int size)
{
    ht_hash_map *ht = (ht_hash_map*)malloc(sizeof(ht_hash_map));
    ht->count = 0;
    ht->size = size;
    ht->head = (ht_node**)malloc(sizeof(ht_node*)*size);
    int i;
    for(i = 0; i < ht->size; i++){
        ht->head[i] = NULL;
    }
    return ht;
}

/* 设置hashmap */
int ht_set(ht_hash_map *ht,void *key, void *value)
{
    int index = hash_int_func(*(int*)key) % ht->size;
    ht_node *tmp = ht->head[index];
    ht_node *last = NULL;
    while(tmp){
        if(*(int*)tmp->key == *(int*)key){
            tmp->value = value;
            return 0;
        }
        last = tmp;
        tmp = tmp->next;
    }

    // 没有重复映射
    ht_node *node = (ht_node*)malloc(sizeof(ht_node));
    node->key = key;
    node->value = value;
    node->next = NULL;

    // head为空
    if(ht->head[index] == NULL){
        ht->head[index] = node;
    }
    else{
        last->next = node;
    }
    ht->count++;

    return 1;
}

/* 获取hashmap */
void *ht_get(ht_hash_map *ht, void *key)
{
    int index = hash_int_func(*(int*)key) % ht->size;
    ht_node *tmp = ht->head[index];
    while(tmp){
        if(*(int*)tmp->key == *(int*)key)
            return tmp->value;
        tmp = tmp->next;
    }
    return NULL;
}

/* 删除hashmap */
int ht_remove(ht_hash_map *ht, void *key)
{
    int index = hash_int_func(*(int*)key) % ht->size;
    ht_node *tmp = ht->head[index];
    ht_node *last = NULL;
    while(tmp){
        if(*(int*)tmp->key == *(int*)key){
            if(last == NULL){//头节点
                ht->head[index] = tmp->next;
            }
            else{
                last->next = tmp->next;
            }
            free(tmp);
            return 0;
        }
        last = tmp;
        tmp = tmp->next;
    }
    return 1;
}

/* 销毁hashmap */
void ht_destory(ht_hash_map *ht) {
    int i;
    for (i = 0; i < ht.size; i++) {
        item *cur_item, *cur_item_tmp;
        cur_item_tmp = ht->items[i];
        while (cur_item_tmp) {
            cur_item = cur_item_tmp;
            cur_item_tmp = cur_item_tmp->next;
            free(cur_item);
        }
    }
    free(ht);
}

/* 查找hashmap中的key-value */
ht_item *ht_search(ht_hash_map *ht, const char *key) {
    int index;
    index = hash(key, ht->size);
    item *cur_item;
    cur_item = ht->items[index];
    while (cur_item) {
        if (strcmp(cur_item->key, key) == 0) { return cur_item; }
        cur_item = cur_item->next;
    }
    return NULL;
}

use C language write a Collection Operation API

#include <stdlib.h>

typedef struct item {
    void *data;
} Item;

typedef struct Collection{
    void* data;
    int size;
} Collection;

// Create a new Collection
Collection* Collection_create() {
    Collection *collection = (Collection*)malloc(sizeof(Collection));
    if (collection == NULL) {
        return NULL;
    }
    collection->data = NULL;
    collection->size = 0;

    return collection;
}

// Add an item to the Collection
int Collection_add(Collection *collection, void* item) {
    if (collection == NULL) return 0;

    // Increase the size of the collection
    int newSize = collection->size + 1;
    Item *items = (Item*)realloc(collection->data, newSize*sizeof(Item));
    if (items == NULL) {
        return 0;
    }
    collection->data = items;

    // Add the item
    Item *item_ptr = &items[collection->size];
    item_ptr->data = item;
    collection->size = newSize;

    return 1;
}

// Remove an item from the Collection
int Collection_remove(Collection *collection, void* item) {
    if (collection == NULL || collection->size == 0) return 0;

    // Find the item to remove
    Item *items = (Item*)collection->data;
    int removeIndex = -1;
    int i;
    for (i = 0; i < collection->size; i++) {
        Item *item_ptr = &items[i];
        if (item_ptr->data == item) {
            removeIndex = i;
            break;
        }
    }
    if (removeIndex == -1) {
        return 0;
    }

    // Remove the item
    int newSize = collection->size - 1;
    Item *newItems = (Item*)malloc(newSize*sizeof(Item));
    if (newItems == NULL) return 0;
    // Copy the elements before the removeIndex
    for (i = 0; i < removeIndex; i++) {
        newItems[i] = items[i];
    }
    // Copy the elements after the removeIndex
    for (i = removeIndex; i < newSize; i++) {
        newItems[i] = items[i+1];
    }
    free(collection->data);
    collection->data = newItems;
    collection->size = newSize;

    return 1;
}

// Clear all items from the Collection
int Collection_clear(Collection *collection) {
    if (collection == NULL) return 0;

    free(collection->data);
    collection->data = NULL;
    collection->size = 0;

    return 1;
}

// Delete the Collection
void Collection_delete(Collection *collection) {
    if (collection == NULL) return;

    free(collection->data);
    free(collection);
}

估计 csdn 封掉链接,不让贴,请私信我!

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/384216.html

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!

相关文章

【9】SCI易中期刊推荐——工程技术-计算机:软件工程(中科院4区)

🚀🚀🚀NEW!!!SCI易中期刊推荐栏目来啦 ~ 📚🍀 SCI即《科学引文索引》(Science Citation Index, SCI),是1961年由美国科学信息研究所(Institute for Scientific Information, ISI)创办的文献检索工具,创始人是美国著名情报专家尤金加菲尔德(Eugene Garfield…

电脑C盘满了,怎么清理c盘空间?

电脑的C盘是系统盘&#xff0c;存储着操作系统和软件等关键文件&#xff0c;因此当C盘空间不足时&#xff0c;电脑的性能和稳定性都会受到影响。 真实案例&#xff1a;c盘空间莫名其妙变小&#xff1f; “C盘快满了不敢乱删&#xff0c;请问该如何清理&#xff1f;” “求大佬…

八股文(一)

一、箭头函数和普通函数有什么区别 (1)箭头函数比普通函数更加简洁 (2)箭头函数没有自己的this 箭头函数不同于传统JavaScript中的函数&#xff0c;箭头函数并没有属于⾃⼰的this&#xff0c;它所谓的this是捕获其所在上下⽂的 this 值&#xff0c;作为⾃⼰的 this 值&#…

希赛PMP模拟题2022(第9套)

希赛PMP模拟题2022&#xff08;第9套&#xff09; 22 需求和范围 区别 21 什么阶段&#xff1f; 在开发过程中不断衡量 价值 和优先级关系排序20 产品路线图 回顾 团队沟通方式 无法面对面沟通时候 其他人参与 了解项目 需求发布计划 风险 排序发表计划 vs 评审会议 老母鸡 是…

ESP-C3入门14. 实现基本的web server

ESP-C3入门14. 实现基本的web server一、ESP32 IDF创建WEB SERVER的流程1. 配置web服务器2. 注册 URI处理器3. 实现 URI处理器函数4. 处理HTTP请求5. 处理web socket连接6. 注册 URI 处理函数7. 启动HTTP服务器8. 发送响应9. 关闭 http 服务二、本要主要使用API的说明1. httpd_…

编码器的作用和功能有哪些

编码器的作用和功能有哪些 最近很多咨询编码器的作用的&#xff0c;小编结合一下线上和线下的资料&#xff0c;总结了一下&#xff0c;供大家参考。 要说编码器&#xff0c;它的作用很多&#xff0c;主要是用来测量机械运动的速度、位置、角度、距离或计数的&#xff0c;编码器…

SkyWalking使用案例-2

文章目录SkyWalking实现基于容器环境Dubbo微服务链路跟踪部署Dubbo Provider构建Dubbo Provider镜像运行dubbo-provider部署Dubbo Consumer构建Dubbo Consumer镜像运行dubbo-consumer验证SkyWalking收集python项目数据Skywalking告警Skywalking指标Skywalking告警规则SkyWalkin…

62. 不同路径

62. 不同路径 一个机器人位于一个 m∗nm * nm∗n 网格的左上角 &#xff08;起始点在下图中标记为 “Start” &#xff09;。 机器人每次只能向下或者向右移动一步。机器人试图达到网格的右下角&#xff08;在下图中标记为 “Finish” &#xff09;。 问总共有多少条不同的路…

virtualbox7虚拟机中安装苹果macOS big sur系统详细教程

第1步&#xff0c;在 Windows 10/11 PC 上启用虚拟化。 现在的电脑一般都默认开启虚拟化技术了。 如果你遇到一些报错&#xff0c;比如收到错误消息“无法在虚拟机上打开会话”&#xff0c;可以查看 如果没有遇到问题&#xff0c;可以直接进入到第二步。 第2步&#xff0c;在…

【Spark分布式内存计算框架——Spark Streaming】12. 偏移量管理(上)代码重构与Checkpoint 恢复

6. 偏移量管理 针对前面实现【百度热搜排行榜Top10】实时状态统计应用来说&#xff0c;当应用关闭以后&#xff0c;再次启动&#xff08;Restart&#xff09;执行&#xff0c;并没有继续从上次消费偏移量读取数据和获取以前状态信息&#xff0c;而是从最新偏移量&#xff08;L…

JVM 不同垃圾回收器的日志格式分析

1、GC日志采集 在服务器上我们需要配置一些参数才能采集到历史的GC日志信息&#xff0c;这些参数通常在项目启动的时候就需要指定&#xff0c; 如果你项目是jar包&#xff0c;可以按照下面方式指定这些GC参数即可。 下面这些参数意思是把GC日志记录到/opt/app/abc-user/ard-…

蓝桥杯备赛——Echarts学习

文章目录前言学习 ECharts 的方法快速上手基础知识option 配置选项可选配置title 标题组件tooltip 提示框组件axisPointer 坐标轴指示器legend 图例组件toolbox 工具栏坐标轴xAxis和yAxisseries &#xff08;[ ]用数组表示,数组里是一个个数据对象&#xff09;饼状图散点图交互…

盘点代码情诗集合❤,程序员表白的巅峰之作,特此奉献

程序员怎么表白&#xff1f;写代码啊&#xff01;每到情人节&#xff0c;程序员们就纷纷出动&#xff0c;各种别出心裁的表白代码倾囊相送。我曾被大批表白代码砸晕&#xff0c;沉浸在“虚拟的”幸福感中不能自拔。我在众多代码中精选了以下几十条&#xff0c;每一条都是文学素…

Python中的遍历字典的键和值

一、Python的字典在项目的开发过程中&#xff0c;如果遇到有映射关系的内容可以考虑使用Python中的字典进行存储数据&#xff0c;字典中冒号前的数据称为【键】、冒号后的数据称为【值】。二、Python字典的用法2.1、Python的定义#Python字典的定义 字典名称{键1:值1,键2:值2,键…

JavaScript Date 日期对象

文章目录JavaScript Date 日期对象Date 对象Date 对象属性Date 对象方法创建日期设置日期两个日期比较JavaScript Date 日期对象 日期对象用于处理日期和时间。 Date 对象 Date 对象用于处理日期与实际。 创建 Date 对象&#xff1a; new Date(). 以上四种方法同样可以创建…

Validate端口使用手册

知行之桥EDI系统从2020版本开始引入了Validate端口&#xff0c;用来实现对XML数据文件进行一些规则的验证&#xff0c;保证XML数据文件的有效性。本文将介绍如何使用Validate端口。 端口创建 同其他功能性端口一样&#xff0c;只需要将Validata端口从左侧的端口清单拖拽到右侧…

子数组达到规定累加和的最大长度系列问题

文章目录1、题目一&#xff1a;正整数数组中子数组累加和 KKK 最大长度1.1 题目描述1.2 思路分析1.3 代码实现2、题目二&#xff1a;整数数组中子数组累加和为 KKK 的最大长度2.1 题目描述2.2 思路分析2.3 代码实现2.4 引申变形2.5 技巧应用题2.5.1 剑指 Offer II 010. 和为 k …

关于HTTP/3的小知识点

客户端用 TCP 发送了三个包&#xff0c;但服务器所在的操作系统只收到了后两个包&#xff0c;第一个包丢了。那么内核里的 TCP 协议栈就只能把已经收到的包暂存起来&#xff0c;“停下”等着客户端重传那个丢失的包&#xff0c;这样就又出现了“队头阻塞”。由于这种“队头阻塞…

Kubernetes之存储管理(中)

NFS网络存储 emptyDir和hostPath存储&#xff0c;都仅仅是把数据存储在pod所在的节点上&#xff0c;并没有同步到其他节点&#xff0c;如果pod出现问题&#xff0c;通过deployment会产生一个新的pod&#xff0c;如果新的pod不在之前的节点&#xff0c;则会出现问题&#xff0c…

CV——day81(1) 读论文: 基于自监督一致性学习的驾驶场景交通事故检测(有源码)

Traffic Accident Detection via Self-Supervised Consistency Learning in Driving Scenarios 基于自监督一致性学习的驾驶场景交通事故检测I. INTRODUCTIONIII. OUR APPROACHA. 帧预测B. 物体位置预测C. 驾驶场景上下文表示(DSCR)D. 协作多任务一致性学习E.交通事故判定IV. E…