我应该创建一个函数,该输入是一个列表和两个数字,该函数反转子列表,其位置由两个数字表示.例如,这是它应该做的:
>>> lst = [1, 2, 3, 4, 5]
>>> reverse_sublist (lst,0,4)
>>> lst [4, 3, 2, 1, 5]
Run Code Online (Sandbox Code Playgroud)
我创建了一个函数并且它可以工作,但我不确定它是否已经到位.这是我的代码:
def reverse_sublist(lst,start,end):
sublist=lst[start:end]
sublist.reverse()
lst[start:end]=sublist
print(lst)
Run Code Online (Sandbox Code Playgroud) 我正在将客户端编写为TCP客户端服务器程序的一部分。
我的代码到达连接部分并引发了无效的参数错误,我已经遍历了几次代码,但找不到问题。
该代码接收3个参数,第一个是IP地址或主机名,第二个是端口,第三个是要发送的消息的最大长度。
我的代码使用getaddrinfo来转换ip地址或主机名,创建所需的变量,启动连接,从文件读取,发送数据和接收数据。
我使用以下代码运行代码:
gcc -std=gnu99 -O3 -Wall -o pcc_client pcc_client.c
./pcc_client 127.0.0.1 2233 4
Run Code Online (Sandbox Code Playgroud)
输出为:
sockaddr_in初始化
错误,启动连接:无效的参数
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
#define FILE_ADDR "/dev/urandom"
int main(int argc, char *argv[]) {
if (argc != 4) {
printf("should receive 3 arguments Received %d args\n", argc);
exit(1);
}
//Get command line arguments
unsigned int port …Run Code Online (Sandbox Code Playgroud) 我正在尝试构建一个函数,它将字符串和数字k作为输入,并返回一个字典,其字符串是来自原始字符串的长度为k的字符串,每个键的值是一个索引序列,字符串出现了.
运行示例:
<<< hash_sequence("byebyeboy",3 (
{ 'yeb': [1, 4], 'boy': [6], 'bye': [0, 3], 'ebo': [5], 'eby':
[2] }
Run Code Online (Sandbox Code Playgroud)
这是我的代码:
def hash_sequence(string, k):
dictionary={}
for i in range(len(string)):
dictionary[i:i+3]=[i]
if dictionary[i:i+3] in dictionary:
dictionary[i:i+3]+=[1]
return dictionary
Run Code Online (Sandbox Code Playgroud) 我正在尝试编写一个代码,它获取一个int数组并检查数字的平方根是否在数组中,如果是,则会打印是,否则它将打印否.我无法弄清楚为什么我的代码不会一直返回.
例如,如果我的输入是:1 4 0 2 16 3
我的输出将是:是是是否否是否
这是我的代码:
import java.util.Arrays;
public class Assignment02Q04 {
public static void main(String[] args) {
int[] intlist = new int[args.length];
for (int i=0; i < args.length; i++) {
intlist[i] = Integer.parseInt(args[i]);
}
for (int number : intlist) {
int sqrt = (int)(Math.sqrt(number));
System.out.println(sqrt);
if (Arrays.asList(intlist).contains(sqrt)) {
System.out.println("yes");
} else {
System.out.println("no");
}
}
}
}
Run Code Online (Sandbox Code Playgroud) 我正在开发一个链表程序,它允许我只在列表上循环一次,而且我不能将列表的元素复制到另一个数据结构.
假设列表不为空(至少有一个节点),并且最后一个节点的下一个节点为空.
以下方法在(2n/3)长度列表的索引处返回元素n.
例如if n=1或n=2它返回第一个元素
if n=3或n=4它返回第二个元素.
我想保留一个临时节点,如果数字(2n/3)是整数,则获取下一个节点.有一个更好的方法吗?
public class LinkedListNode {
private int value;
private LinkedListNode next;
public LinkedListNode(int value) {
this.value = value;
}
public int getValue() {
return value;
}
public void setValue(int value) {
this.value = value;
}
public LinkedListNode getNext() {
return next;
}
public void setNext(LinkedListNode next) {
this.next = next;
}
}
public class ListUtils {
public static LinkedListNode findTwoThirdsNode(LinkedListNode head){ …Run Code Online (Sandbox Code Playgroud) 我应该写一个函数,它增加了两个整数,函数长caculates与n位质数的密度,通过获取随机的(次)与n位长整数,并发现其中有多少是素数,则输出是(cntprimes/times).我编写了我的代码,但随机导入错误.NameError:未定义全局名称"random".请告诉我在代码中应该修复的内容.
def is_prime(m):
""" probabilistic test for compositeness of m>30
adds a trivial sieve to quickly eliminate divisibility
by small primes. """
if m in [2,3,5,7,11,13,17,19,23,29]:
return True # treats small primes separately
for prime in [2,3,5,7,11,13,17,19,23,29]:
if m % prime == 0:
return False
for i in range(0,100):
a = random.randint(1,m-1) # a is a random integer in [1..m-1]
if pow(a,m-1,m) != 1:
return False
return True
def density_primes(n, times=10000):
from random import getrandbits
cntprimes=0
for i in range(times): …Run Code Online (Sandbox Code Playgroud) 我正在开发一个 TCP 客户端服务器程序,它应该支持使用线程的多个客户端。
套接字创建、连接、绑定和接受按预期工作,因为我在运行代码时没有收到任何错误。但是,每当我尝试从服务器读取()时,代码都会进入无限循环,并且没有任何反应。
我先尝试从服务器写入,然后将写入结果写入服务器的终端。
客户端代码:
#include <stdlib.h>
#include <stdio.h>
#include <stdio.h>
#include <unistd.h>
#include <assert.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <dirent.h>
#define FILE_ADDR "/dev/urandom"
int main(int argc, char *argv[]) {
//Get command line arguments
unsigned int port = atoi(argv[2]);
int length = atoi(argv[3]); //Number of bytes to read
char* buffer = malloc(length * sizeof(char)); //Buffer to hold data read from file
char* recvBuf = malloc(10 …Run Code Online (Sandbox Code Playgroud)