23S-CS106X-Practice-Key

23S-CS106X-Practice-Key - CS106X Autumn 2009 Handout 23S...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS106X Handout 23S Autumn 2009 October 28 th , 2009 CS106X Practice Solution Solution 1: Acronyms void ReadIntoMap(ifstream& in, Map<Vector<string> >& map) { Scanner s; s.setSpaceOption(Scanner::IgnoreSpaces); while (true) { string line; getline(in, line); if (in.fail()) break; s.setInput(line); string acronym = ""; while (s.hasMoreTokens()) acronym += s.nextToken()[0]; map[acronym].add(line); // [] creates empty vector if needed } } double PercentConfusing(Map<Vector<string> >& map) { Map<Vector<string> >::Iterator itr = map.iterator(); int numConfusing = 0; while (itr.hasNext()) { string key = itr.next(); if (map[key].size() > 1) numConfusing++; } return double(numConfusing)/map.size(); } Solution 2: Pascal’s Travels int NumPaths(Grid<int>& board) { return NumPaths(board, 0, 0); } int NumPaths(Grid<int>& board, int row, int col) { if ((row == board.numRows() – 1) && (col == board.numCols() – 1)) return 1; if (!OnBoard(board, row, col)) return 0; if (board[row][col] == 0) return 0;
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/07/2010 for the course CS 201 taught by Professor Selimaksoy during the Spring '09 term at Bilkent University.

Page1 / 2

23S-CS106X-Practice-Key - CS106X Autumn 2009 Handout 23S...

This preview shows document pages 1 - 2. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online