8/2 DS & 講座 === ###### tags: `Coding X` `DS` [TOC] ## AI and Big Data > [name=精誠科技 蔣居裕 (Fred Chiang)] > [time=Fri, Aug 2, 2019 2:00 PM] > [李飛飛 - AI界女神](https://www.ted.com/talks/fei_fei_li_how_we_re_teaching_computers_to_understand_pictures?language=zh-tw) [開源知識平台工具包](http://blog.jason.tools/2018/03/bookstackapp-useful.html) [OpenSource](https://blog.capterra.com/5-free-knowledge-management-software-solutions/) [開源知識平台](https://www.getapp.com/collaboration-software/knowledge-management/p/open-source/) [Analysis of Big data](https://www.youtube.com/playlist?list=PLj6E8qlqmkFv1rlU7BGntOGe_RFnrv9Ip) [大數據工程師三個月自學計畫](https://zhuanlan.zhihu.com/p/31651985) [我用OpenSource做多少事](https://fredwang.blogspot.com/2018/03/open-source-open-source.html) ## Lab Challenage 親友團 ![image alt](https://i.imgur.com/jXYJ1G8.png) ![image alt](https://imgur.com/MDqLJGO.png) ### Lab 3 Q_array ```Java= package q_array; import java.util.Scanner; /* Part I * 1 create a Queue class and methods * part II * 1 接收親友團數目 * 2 確認裡面人數 (array) * 3 create method: dequeue pop first element of the queue * 4 create method: enqueue push element from array * and check is there same group member in waiting queue * - if yes, enqueue the element before group member * - else put it in the end of waiting queue * 5 use while loop to clean waiting queue */ //Java program to implement a queue using an linear array class Queue { static int front, rear, capacity; static String queue[]; Queue(int c) { front = rear = 0; capacity = c; queue = new String[capacity]; } static int Q_len() { return rear - front; } static boolean isEmpty() { if (front == rear) return true; return false; } // function to insert an element at the rear of the queue static void Enqueue(String data) { // check queue is full or not if (capacity == rear) { System.out.println("Queue is full"); } // insert element at the rear else { queue[rear] = data; rear++; } } // function to delete an element from the front of the queue static String Dequeue() { String item = null; // if queue is empty if (isEmpty()) { System.out.printf("Queue is empty\n"); } // shift all the elements from index 2 till rear // to the right by one else { item = queue[0]; for (int i = 0; i < rear; i++) { queue[i] = queue[i + 1]; } // store null at rear indicating there's no element if (rear < capacity) { queue[rear] = null; } rear--; // update rear position } return item; } // print queue elements static void queueDisplay() { int i; if (isEmpty()) { System.out.println("Queue is Empty"); } // traverse front to rear and print elements for (i = front; i < rear; i++) { System.out.printf(" %s ", queue[i]); } } static String[] turntoArr() { String[] tmp = new String[Q_len()]; for (int i = 0; i < Q_len(); i++) { tmp[i] = queue[i]; } return tmp; } static void cut_in_line(String who, String data) { boolean flag = false; for (int i = front; i < rear; i++) { if (queue[i].equals(who)) { rear++; // update rear for (int j = rear; j > i; j--) { queue[j] = queue[j - 1]; } queue[i] = data; flag = true; } if (flag == true) { break; } } } } public class Q_array { public static void main(String[] args) { Scanner input = new Scanner(System.in); int GroupNum = input.nextInt(); // 1 <= GroupNum <= 1000 if (GroupNum > 1000 || GroupNum < 1) { System.out.println("Invalid number"); } else { // use jagged array : array of arrays such that member arrays can be of // different sizes String[][] jagArr = new String[GroupNum][]; for (int i = 0; i < jagArr.length; i++) { int ppl = input.nextInt(); if (ppl > 1000 || ppl < 0) { System.out.println("Invalid number"); } else { jagArr[i] = new String[ppl]; // input ppl in the group for (int j = 0; j < jagArr[i].length; j++) { jagArr[i][j] = input.next(); } } } Queue waiting_Q = new Queue(1000); // the upper bound is 1000 String[] res = new String[1000]; // array to store result boolean flag = true; // control while loop int res_index = 0; while (flag) { // System.out.println("Enter Command: ENQUEUE, DEQUEUE, STOP"); String command = input.next(); switch (command) { case "ENQUEUE": String s = input.next(); String who = checkGroup(jagArr, waiting_Q, s); // check element exists or not if (waiting_Q.isEmpty() || who == null) { waiting_Q.Enqueue(s); // insert element at rear } else { waiting_Q.cut_in_line(who, s); // 插隊 } break; case "DEQUEUE": res[res_index] = waiting_Q.Dequeue(); res_index++; break; case "STOP": flag = false; for (int i = 0; i < res.length; i++) { if (res[i] == null) { break; } else { System.out.println(res[i]); } } break; default: System.out.println("Invalid input"); } } } } public static String checkGroup(String[][] arr, Queue q, String data) { String who = null; int index = -1; boolean flag = false; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { if (arr[i][j].equals(data)) { index = i; // store data index break; } } } if (index == -1) { return null; } else { String[] tmpCheck = q.turntoArr(); for (int l = 0; l < tmpCheck.length; l++) { for (int k = 0; k < arr[index].length; k++) { if (tmpCheck[l].equals(arr[index][k])) { who = tmpCheck[l]; // 要排在誰前面 flag = true; } } if (flag == true) { break; } } return who; } } } ``` ### Lab 4 Q_list ```Java= package q_list; import java.util.ArrayList; import java.util.Scanner; public class Q_list { public static void main(String[] args) { Scanner input = new Scanner(System.in); // 處理親友團 int GroupNum = input.nextInt(); // 親友團數量 if (GroupNum < 1 || GroupNum > 1000) { System.out.println("親友團團數超出範圍"); } else { ArrayList<ArrayList<String>> Group = new ArrayList<ArrayList<String>>(); ArrayList<String> waiting_Queue = new ArrayList<String>(); ArrayList<String> output = new ArrayList<String>(); int indexofGroup = 0; while (GroupNum > 0) { int ppl = input.nextInt(); // 親友團人數 if (ppl < 1 || ppl > 1000) { System.out.println("親友人數超出範圍"); System.exit(0); } else { Group.add(new ArrayList<String>()); for (int i = 0; i < ppl; i++) { Group.get(indexofGroup).add(input.next()); indexofGroup++; GroupNum--; } } } // 開始排隊 String s; boolean flag = true, find; int teamNum; String command; while (flag) { // reset variable teamNum = -1; find = false; command = input.next(); switch (command) { case "ENQUEUE": s = input.next(); // 1. 先找自己的隊伍index // 2. 找找看排隊隊伍中有沒有隊友, 有回傳其index 再用add(index, usr_input) for (ArrayList<String> j : Group) { if (j.contains(s)) { teamNum = Group.indexOf(j); } } // input s 在親友團裡面,找排隊隊伍裡面是否有自己人 if (teamNum != -1 && waiting_Queue.size() != 0) { for (String tmp : waiting_Queue) { for (int i = 0; i < Group.get(teamNum).size(); i++) { if (tmp.equals(Group.get(teamNum).get(i))) { waiting_Queue.add(waiting_Queue.indexOf(tmp), s); find = true; break; } } if (find) { break; } } } if (!find) { waiting_Queue.add(s); // 沒有朋友,直接排在最後面 } break; case "DEQUEUE": output.add(waiting_Queue.get(0)); // 將結帳完的成員後加入output waiting_Queue.remove(0); // 在更新排隊成員 break; // 輸出所有結帳完的人 case "STOP": for (String i : output) { System.out.println(i); } flag = false; // 終止迴圈條件 break; default: System.out.println("Invalid input"); } } } } } ```