这题好象是阿里的面试题,用Python模拟了一下。
做法可见:/blog.csdn.net/sinat_35562946/article/details/81275080,比较详细。

import random
lst = []
for i in range(1,100):
    lst.append(i)
lst.append(2015)
def select_n_numbers_from_list(lst,n):
    lst2 = lst[:]
    lst3 = []
    k = 0
    for i in range(n):
        lst3.append(lst2.pop(random.randint(0,99-k)))
        k+=1
    return lst3
times_to_run = int(input())
total = 0
for i in range(times_to_run): #Run i times
    result = 0
    n = random.randint(0,99) #Pick n numbers from list
    lst2 = select_n_numbers_from_list(lst, n)
    for num in lst2:
        result = result^num
    total += result
print(total/times_to_run)

第二版,之后计划完善:
1、代码风格和用户界面
使用方法:
将程序放在待测SSD下,运行main.py,输入1,1则进行写入1GB的测试,输入2无限循环测试
程序目的:测试SSD写入寿命
程序逻辑:产生一个随机文件,并通过不断复制,直到填满SSD。在填满后,对每个复制的文件和源文件MD5进行比对,如果出错则判断SSD写入寿命已到,反之,删除文件后从头开始测试。
本程序用了json实现了对已写入量的保存,使用时比较方便。

目前程序包含:
1、definations.py

LOGFILE = 'detDATA.json'

2、main.py

import readjson
import savejson
import rndwrite
import os
logdict = readjson.read()
user_select = int(input())

if user_select == 1: #For test purpose ,short run.
    user_select = int(input())
    execution_result = rndwrite.writedata(user_select)
    logdict.update({'data_written': logdict.get('data_written', 0)+execution_result[0]})
    print("{0} megabytes written".format(round(logdict.get('data_written', 0), 3)))
    rndwrite.validatedata(os.path.join(os.path.curdir, 'testfile'),execution_result[1])
    rndwrite.earsedata(os.path.join(os.path.curdir, 'testfile'))
    savejson.save(logdict)
    exit(0)
else:
    while(True):
        execution_result = rndwrite.writedata(32)
        logdict.update({'data_written': logdict.get('data_written', 0) + execution_result[0]})
        print("{0} megabytes written".format(round(logdict.get('data_written', 0),3)))
        rndwrite.validatedata(os.path.join(os.path.curdir, 'testfile'), execution_result[1])
        rndwrite.earsedata(os.path.join(os.path.curdir,'testfile'))
        savejson.save(logdict)

3、readjson.py

import os
import json
from definations import *
def read():
    if not os.path.exists('detDATA.json'):
        testdata = open(LOGFILE, 'w')
        logdict = dict()
    else:
        testdata = open(LOGFILE, 'r')
        logdict = json.load(testdata)
        testdata.close()
    return logdict

4、rndwrite.py

import numpy as np
import os
import hashlib
import shutil
# V2 : Return file MD5&file size.Improved precision.
# V1 : Return file size.
def writedata(totalgigs):
    os.mkdir('testfile')
    loop_to_run = int(totalgigs*1024/23.8)-1 # PARAMETER : 23.8
    os.chdir('testfile')
    np.savetxt('test1'+'.txt', np.random.rand(1000, 1000))
    md5file = open('test1.txt','rb')
    md5 = hashlib.md5(md5file.read()).hexdigest()
    md5file.close()
    print("Created file 'test1.txt' with MD5sum = "+md5)
    for i in range(2,loop_to_run):
        shutil.copy('test1.txt','test'+str(i)+'.txt')
        print("Copied test1.txt -> "+'test'+str(i)+'.txt')
    os.chdir('..')
    return (int(totalgigs*1024/23.8)*23.8,md5)
def earsedata(mountpoint):
    for file in os.listdir(mountpoint):
        os.remove(os.path.join(mountpoint,file))
    os.rmdir(mountpoint)

def validatedata(mountpoint,checksum):
    for file in os.listdir(mountpoint):
        print("Checking file "+file+": ")
        md5file = open(os.path.join(mountpoint,file),'rb')
        md5 = hashlib.md5(md5file.read()).hexdigest()
        md5file.close()
        if md5 == checksum:
            print("'"+md5+"'"+" matches original file md5 : "+"'"+checksum+"'!") #Too noisy.
            pass
        else:
            print("Found mismatch.Quitting.")
            print("At file "+file)
            print("Target MD5 = "+md5)
            exit(-1)

5、savejson.py

import json
from definations import *

def save(logdict):
    testdata = open(LOGFILE,'w')
    json.dump(logdict, testdata)

docker run --name mysqlA12 -e MYSQL_ROOT_PASSWORD=A12A12A12 -d -p 3307:3306 mysql

--name 后跟容器别名,如不指定,系统将自动生成一个ID
-e ... 为指定特定模板参数使用
-d 后台运行
-p 端口映射 3307:3306为容器的3306映射到本机的3307端口

水题,用栈做。

x = input()
s = ""
if x[0] == '-':
    n = x[1:]
    s = x[0]
else:
    n = x
b = []

for i in n:
    b.append(i)

a = ""
for i in n:
    a += b.pop()
print(int(s+a))

/*
 * GRAPH :
 * 3-2-0-1
 * DFS starting from point 3
 */
#include <stdio.h>
#define N 5
void dfs(int u);
int vis[N+1] = {0,};
int mat[5][5]={
        0,1,1,0,0,
        1,0,0,0,0,
        1,0,0,1,0,
        0,0,1,0,0,
        0,0,0,0,0
}; /1-2-3-4

int main() {
    printf("3");
    dfs(3);
    return 0;
}
void dfs(int u)
{
    vis[u]=1;
    for(int i=0;i<N;++i)
    {
        if (!vis[i] && mat[i][u])
        {
            printf("%d",i);
            dfs(i);
        }
    }
}