Courses‎ > ‎AP Computer Science‎ > ‎Konstantinovich‎ > ‎

2018-11-19

posted Nov 18, 2018, 7:06 PM by Konstantinovich Samuel   [ updated Nov 20, 2018, 5:50 AM ]
Pclassic survey for people who went on the trip. Please fill it out later.


Optional Practice:
"How do you do better in CS when you are doing OK so far?"
-> answer: You learn by doing. So study and practice.
Cool Challenging problems (every year a new set is posted!)
https://adventofcode.com/


Sample Solution to AddWord:
private boolean addWord(String word,int row, int col, int dr, int dc){
    if(dr ==0 && dc == 0){
      return false;
    }
    //check if the word fits but don't modify board
    try{
      for(int i = 0; i < word.length(); i++){
        char c = data[row + i*dr][col + i*dc];
        if(c != '_' && c != word.charAt(i)){
          //System.out.println("mismatch "+word.charAt(i)+"!="+c);
          return false;
        }
      }
    }catch(ArrayIndexOutOfBoundsException e){
      return false;
    }
    //To get here the word must fit, so change the board now
    for(int i = 0; i < word.length(); i++){
      //System.out.println(word.charAt(i)+" "+(row + i*dr)+","+(col + i*dc));
      data[row + i*dr][col + i*dc] = word.charAt(i);
    }
    wordsToAdd.remove(word);
    wordsAdded.add(word);
    return true;
  }
Sample Solution to AddAllWords
public void addAllWords(){
    int fail = 0;
    //repeat up to 40 fail in a rows
    while(fail < 40 && wordsToAdd.size()>0){
      //pick a word and direction
      String word = wordsToAdd.get(randGen.nextInt(wordsToAdd.size()));
      int dr = randGen.nextInt()%2;
      int dc = randGen.nextInt()%2;

      //try to add the word 40 times
      int tries =40;
      int r,c;
      //DO WHILE LOOP IS OPTIONAL
      do{
        r = randGen.nextInt(data.length);
        c = randGen.nextInt(data[r].length);
      }while(!addWord(word,r,c,dr,dc) && tries-- > 0);
      //using x++ in an expression is optional

      if(tries < 1){//if the add succeeds, the tries does not reach 0.
        fail++;
      }
    }
  }



Homework:
An AP Practice Problem is attached to this post (PDF)
1.You must complete this by yourself, on paper by tomorrow. You will have time later this period to start reading/working on it! (This is instead of the problem being a quiz)


Ċ
Konstantinovich Samuel,
Nov 19, 2018, 6:09 AM
Comments