Skip to content

Instantly share code, notes, and snippets.

@mdecourse
Last active October 17, 2019 02:45
Show Gist options
  • Save mdecourse/9f69e77cb456b97cccc8eb5a9dbd991c to your computer and use it in GitHub Desktop.
Save mdecourse/9f69e77cb456b97cccc8eb5a9dbd991c to your computer and use it in GitHub Desktop.
random grouping from reading URL student number data
import 'dart:html';
void main() {
// 每一組 10 人
int num = 10;
// 組序由 1 開始
int gth = 1;
// 迴圈序號變數
int i;
// 每組學員暫存數列
var gp_list = [];
// 全班分組數列
var cp2019 = [];
HttpRequest.getString(
'https://mde.tw/cp2019/downloads/2019fall_cp_1a_list.txt')
.then((String resp) {
// 利用 trim() 去除字串最後的跳行符號, 之後再利用 split() 根據 \n 轉為數列
var stud_list = resp.trim().split("\n");
// 數列利用 shuffle() 方法以隨機方法弄亂順序
stud_list.shuffle();
// 逐一讀取已經弄亂的學生學號數列, 利用模數運算每幾人分成一組
for (i = 0; i < stud_list.length; i++) {
// 0, 1~(num-1), num
if (i % num == 0) {
gp_list = [];
// 列印區隔符號
print('=' * 20);
print("group $gth :");
print(stud_list[i]);
// 在各分組數列中加入將對應的學員學號
gp_list.add(stud_list[i]);
gth = gth + 1;
} else {
print(stud_list[i]);
// 在各分組數列中加入將對應的學員學號
gp_list.add(stud_list[i]);
}
if (i % num == 0) {
// 逐步將各組暫存的分組數列加入全班分組數列中
cp2019.add(gp_list);
}
}
// 列出全班分組數列
print(cp2019);
});
}
@mdecourse
Copy link
Author

假如將透過 HttpRequest.getString 所取回的字串, 導向外部函式呼叫執行, 可以改寫為:
import 'dart:html';

// 每一組 10 人
int num = 10;
// 組序由 1 開始
int gth = 1;
// 迴圈序號變數
int i;
// 每組學員暫存數列
var gp_list = [];
// 全班分組數列
var cp2019 = [];

void main() {
  HttpRequest.getString(
          'https://mde.tw/cp2019/downloads/2019fall_cp_1a_list.txt')
      .then(grouping);
}

void grouping(String resp) {
  // 利用 trim() 去除字串最後的跳行符號, 之後再利用 split() 根據 \n 轉為數列
  var stud_list = resp.trim().split("\n");
  // 數列利用 shuffle() 方法以隨機方法弄亂順序
  stud_list.shuffle();
  // 逐一讀取已經弄亂的學生學號數列, 利用模數運算每幾人分成一組
  for (i = 0; i < stud_list.length; i++) {
    // 0, 1~(num-1), num
    if (i % num == 0) {
      gp_list = [];
      // 列印區隔符號
      print('=' * 20);
      print("group $gth :");
      print(stud_list[i]);
      // 在各分組數列中加入將對應的學員學號
      gp_list.add(stud_list[i]);
      gth = gth + 1;
    } else {
      print(stud_list[i]);
      // 在各分組數列中加入將對應的學員學號
      gp_list.add(stud_list[i]);
    }
    if (i % num == 0) {
      // 逐步將各組暫存的分組數列加入全班分組數列中
      cp2019.add(gp_list);
    }
  }
  // 列出全班分組數列
  print(cp2019);
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment