Jie's blog

混就完事了


  • 首页

  • 关于

  • 分类

  • 归档

  • 日程表

JPA的一些用法

发表于 2020-01-18
1
2
3
4
5
6
findFirstByOrderByIdDesc() // 1.查询ID最大的一条数据,注意first后面要跟by()。
findTop5ByOrderByIdAsc() // 2. 查询top5。id从小到大排序,return list
findFirstByValidAtBeforeAndInvalidAtAfterOrderByCreatedAtDesc(Instant time1,Instant time2) //3. 查询数据在某一时间段内。
findByNameLike(String name) //4. like查询,return list
findByIdIsBetween(Integer low, Integer high) //5. 查询数据在某一范围,与3相似。return list
findByMoneyOrderByIdDesc(int money) //6.相同钱数的人,根据id排。 return list

20191218

发表于 2019-12-28

KMP algorithm

发表于 2019-11-26

很久以前看到的KMP算法,重新回顾了一下。主要就是如何寻找next数组。
对于类似字符串A,aaaaaaab,与字符串B,aaab。寻找A中包含字符串B的位置。
如果使用暴力搜索,每次失配都需要从A的下一位开始,但其实并不用。对于这个例子来说,
前面的两位是不需要再次判断的,因为有公共的前后缀,因此构建一个next数组如下。
B: a a a b
index: 0 1 2 3
next: 0 1 2 0

假设我们已经知道了aa这个字符串的值为1,那么如果新加入另一个字母a,则判断index为1的值和该值是否相同。
相同,则在之前的前缀表长度加一,如果不同,则找到前一个字符的前缀表,进行如上的判断。

最后进行匹配的时候,则在失配时候可以直接跳过已经匹配过的前缀。

阅读全文 »

hadoop ArrayWritable实现自定义类

发表于 2019-10-31

感觉网上的博客对于如何重写arraywritable并不是很详细,自己重新整理了一下,如何使用arraywritable存放复杂类。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
public class MyArrayWritable extends ArrayWritable { 
public MyArrayWritable() {
super(SomeComplexClass.class); //首先构造函数中写需要存放的对象的类
}
}

public MyArrayWritable(String[] strings) {

super(SomeComplexClass.class);
Text[] texts = new Text[strings.length];
for (int i = 0; i < strings.length; i++) {
texts[i] = new Text(strings[i]);
}
set(texts); //这个是set父类的value
}


//由于是自定义的对象,因此需要写toString函数。
//否则在存入的时候会存入对象的地址,而不是想要的信息。

public String toString() {
StringBuilder sb = new StringBuilder();
//使用this.get()获取strings的长度
for (int i = 0; i < this.get().length; i++) {
//some code
}
return sb.toString();
}

//最后在reduce的时候,就可以存入一个array的value了。

//例如这样,在reduce时,有一个复杂对象的values。可以用arraylist将其加入,最后变成string数组放到MyArrayWritable中。
ArrayList<String> al = new ArrayList<String>();
for (SomeComplexClass val : values) {
al.add(val.toString());
}

context.write(key, new MyArrayWritable((String[]) al.toArray(new String[al.size()])));

完美世界2020Java开发

发表于 2019-09-28

完美世界2020Java游戏开发笔试

做了一下完美的Java笔试。发现关于类的东西忘光了,好久不写。编程题的一个题大概是自己创建一个栈,实现一些功能,返回最大最小元素,min,max,以及pop和push四个功能。
确实很简单,但是基础的东西有点忘了,第一题没写出来就直接退了。
虽然事后看到有人直接在Main函数里建一个自带的Stack就可以了。不过僵硬在创建class上了。
发现主要原因好像是忘了在Stack类中加入static修饰,没用本地IDE,半天没有运行成功。
下面重新把两道题都写一下。

阅读全文 »

puzzle9-problem

发表于 2019-09-14 | 分类于 python , C

1.The 9 puzzle描述
2.代码分析部分
3.延伸N宫格

The 9 puzzle描述

Dispatch the integers from 0 to 8, with 0 possibly changed to None, as a list of 3 lists of size 3, to represent a 9 puzzle.

For instance, let [[4, 0, 8], [1, 3, 7], [5, 2, 6]] or [[4, None ,8], [1, 3, 7], [5, 2, 6]] represent the 9 puzzle

阅读全文 »

SelectTermAndCourse

发表于 2019-06-30 | 分类于 Shell

学了Shell之后,感觉学校的HandBook太蠢了,没有选项根据学期来分离课程,毕竟选课的时候是根据学期来选的,你只在页面上看到了这门课,但是点进去才发现不是这学期的。就很浪费时间。
所以用shell写了一个脚本可以直接将课程代码与他开设的学期对应起来,下次再找课的时候就会比较的方便。

1
2
3
4
5
6
7
8
URL=https://www.handbook.unsw.edu.au/postgraduate/courses/2019/
course=`wget -q -O- https://www.handbook.unsw.edu.au/postgraduate/specialisations/2019/COMPAS |egrep "<a href=\"/postgraduate/courses/2019/"|sed -r 's?<a href="/postgraduate/courses/2019/([A-Z]{4}[0-9]{4})/?\1?'|awk '{print $1}' |sort|uniq`

for class in $course
do
echo $class `wget -q -O- https://www.handbook.unsw.edu.au/postgraduate/courses/2019/$class|egrep "Term [0-9]"|sed -r 's/<.*>([Summer Term|Term][^<]*).*/\1/'|head -n1` >> courseAndterm.txt

done

代码也是非常的简单,基本就是使用了一些基本的命令,egrep,sed,head等等之类。不过还是省了不少事,爽

prolog学习

发表于 2019-05-18 | 分类于 prolog
  • Write a prolog predicate insert(Num, List, NewList) that takes a number Num along with a list of numbers List which is already sorted in increasing order, and binds NewList to the list obtained by inserting Num into List so that the resulting list is still sorted in increasing order.

    阅读全文 »

QuickSort

发表于 2019-04-27 | 分类于 Java

一般的快排,效率是稍微低一点的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
public void quickSort(int[] arr, int begin, int end) {
if (begin < end) {
int partition = partition(arr, begin, end);
quickSort(arr, begin, partition - 1);
quickSort(arr, partition + 1, end);
}

}

public int partition(int[] arr, int begin, int end) {
int pivot = arr[end];
int start = begin;

for (int i = begin; i < end; i++) {
if (arr[i] < pivot) {
swap(arr, i, start);
start++;
}
}
swap(arr, start, end);
return start;
}

所以可以有两个标记i,j,从两边靠拢。初始的i = begin,j = end,用k来扫描整个序列。
始终保持序列中[start,i] < pivot; [i,k] = pivot; [j ,end] > pivot的。
因此当遇到小于pivot的元素,swap(arr,i,k),i++
当遇到大于pivot的元素,swap(arr,j,k),j–

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
public int partition(int[] arr, int begin, int end) {
int pivot = arr[begin];
int i = begin;
int j = end - 1;
int k = begin + 1;
while(k <= j){
if(arr[k] < pivot){
swap(arr, i, k);
i++;
k++;
} else if (arr[k] > pivot) {
swap(items, j, k);
j--;
} else {
k++;
}

}
swap(arr, start, end);
return start;
}

还有另一种实现方式,也就是双轴快排(DualPivotQuickSort),也是JDK1.8对基本数据类型排序的实现。
看一下源码是怎么实现的。可以看出是分成了三段,取两个中心点。pivot1,pivot2,且pivot <= pivot2,可将序列分成三段:x < pivot1、pivot1 <= x <= pivot2,x > pivot2

1
2
3
4
5
6
7
8
9
/*
* left part center part right part
* +--------------------------------------------------------------+
* | < pivot1 | pivot1 <= && <= pivot2 | ? | > pivot2 |
* +--------------------------------------------------------------+
* ^ ^ ^
* | | |
* less k great
*/

根据这个,大概实现了一下。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

public void quickSort(int[] arr) {
partition(arr,0,arr.length-1);
}
public void partition(int[] arr, int begin, int end) {

if (begin < end) {

int pivot1 = arr[begin];
int pivot2 = arr[end];
if (pivot1 > pivot2) {
swap(arr, begin, end);
int temp = pivot1;
pivot1 = pivot2;
pivot2 = temp;
}
int i = begin, j = end, k = begin + 1;

outer:
while (k < j) {
if (arr[k] < pivot1) {
i++;
swap(arr, i, k);
k++;
} else if (arr[k] <= pivot2) {
k++;
} else { // arr[k] > pivot2

while (arr[--j] > pivot2) {
if (j == k) {
break outer;
}
}
if (arr[j] < pivot1) {
swap(arr, j, k);
i++;
swap(arr, i, k);
} else { // pivot1 <= x <= pivot2
swap(arr, j, k);
}
k++;
}
}
swap(arr, begin, i);
swap(arr, end, j);

partition(arr, begin, i - 1);
partition(arr, i + 1, j - 1);
partition(arr, j + 1, end);
}

}

COMP9021tangram

发表于 2019-04-23 | 分类于 python

2. Background


The game of tangram consists in creating shapes out of pieces. We assume that each piece has its own colour,
different to the colour of any other piece in the set we are working with. Just for reference, here is the list of
colours that are available to us (you will not make use of this list):

阅读全文 »
12
Jie

Jie

咸鱼就是我
15 日志
10 分类
10 标签
© 2020 Jie
|