CS1020E Sitin 1 Discussion -- Counting Palindromes.

Slides:



Advertisements
Similar presentations
This Time Whitespace and Input/Output revisited The Programming cycle Boolean Operators The “if” control structure LAB –Write a program that takes an integer.
Advertisements

Liang, Introduction to Java Programming, Ninth Edition, (c) 2013 Pearson Education, Inc. All rights reserved. 1 Chapter 9 Strings.
Chapter 7 Strings F To process strings using the String class, the StringBuffer class, and the StringTokenizer class. F To use the String class to process.
1 Strings and Text I/O. 2 Motivations Often you encounter the problems that involve string processing and file input and output. Suppose you need to write.
Java Programming Strings Chapter 7.
Java Strings in 10 minutes
Refining Edits and Alignments Υλικό βασισμένο στο κεφάλαιο 12 του βιβλίου: Dan Gusfield, Algorithms on Strings, Trees and Sequences, Cambridge University.
1 Module 7 Halting Problem –Fundamental program behavior problem –A specific unsolvable problem –Diagonalization technique revisited Proof more complex.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
1 ICS102: Introduction To Computing King Fahd University of Petroleum & Minerals College of Computer Science & Engineering Information & Computer Science.
Computer Programming and Basic Software Engineering 4. Basic Software Engineering 1 Writing a Good Program 4. Basic Software Engineering 3 October 2007.
Computer programming 1 Multidimensional Arrays, and STL containers: vectors and maps.
Flowcharts Remember that a solution to a problem is called an algorithm. Algorithms are often a series of steps required to solve the problem. A flowchart.
Dynamic Programming Reading Material: Chapter 7 Sections and 6.
1 Lab Session-VI CS121 Fall 2000 l HW#2 Assigned l Multiple Choice Selectors l The While Loop l Switch-Case-Break Exercise l The Counter Controlled Loops.
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
1 Applications of Recursion (Walls & Mirrors - Chapter 5)
CIS Computer Programming Logic
Chapter 2 Recursion: The Mirrors. © 2005 Pearson Addison-Wesley. All rights reserved2-2 Recursive Solutions Recursion is an extremely powerful problem-
Lesson 6C – Loops 3 – Advanced File Processing By John B. Owen All rights reserved ©2011.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
© Copyright 2012 by Pearson Education, Inc. All Rights Reserved. Chapter 8 More on Strings and Special Methods 1.
Recursion A method is recursive if it makes a call to itself. A method is recursive if it makes a call to itself. For example: For example: public void.
A.Abhari CPS1251 Multidimensional Arrays Multidimensional array is the array with two or more dimensions. For example: char box [3] [3] defines a two-dimensional.
Lecture 4. RAM Model, Space and Time Complexity
Introduction to Programming (in C++) Algorithms on sequences. Reasoning about loops: Invariants. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept.
Chapter 11 Arrays Continued
Arrays An array is a data structure that consists of an ordered collection of similar items (where “similar items” means items of the same type.) An array.
Strings CS303E: Elements of Computers and Programming.
CS1020 Lab 1 Discussion.
CSE1222: Lecture 7The Ohio State University1. logExample.cpp // example of log(k) for k = 1,2,..,8... int main() { cout
What does C store? >>A = [1 2 3] >>B = [1 1] >>[C,D]=meshgrid(A,B) c) a) d) b)
C++ Basics. Compilation What does compilation do? g++ hello.cpp g++ -o hello.cpp hello.
Strings Mr. Smith AP Computer Science A. What are Strings? Name some of the characteristics of strings: A string is a sequence of characters, such as.
Strings Basic data type in computational biology A string is an ordered succession of characters or symbols from a finite set called an alphabet Sequence.
String operations. About strings To handle strings more easily, we need to include a library> #include To see what the library allows us to do, look here:
1. Comparing Strings 2. Converting strings/numbers 3. Additional String Functions Strings Built-In Functions 1.
Strings Characters and Sentences 1.Overview 2.Creating Strings 3.Slicing Strings 4.Searching for substrings 1.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
Recursion. Objectives At the conclusion of this lesson, students should be able to Explain what recursion is Design and write functions that use recursion.
Introduction to Programming Python Lab 8: Loops 26 February PythonLab8 lecture slides.ppt Ping Brennan
Winter 2016CISC101 - Prof. McLeod1 CISC101 Reminders Quiz 3 this week – last section on Friday. Assignment 4 is posted. Data mining: –Designing functions.
Module 7 Halting Problem –Fundamental program behavior problem –A specific unsolvable problem –Diagonalization technique revisited Proof more complex 1.
 Type Called bool  Bool has only two possible values: True and False.
C++ Memory Management – Homework Exercises
Recursion Version 1.0.
Repetitive Structures
SQL – Python and Databases
CS1020 – Data Structures And Algorithms 1 AY Semester 2
CMPT 120 Topic:  Case Study.
Introduction to Programming
Sit-In Lab 1 Ob-CHESS-ion
CS 106A, Lecture 9 Problem-Solving with Strings
Chapter 7: Strings and Characters
نوع داده هاي انتزاعي Abstract Data Types
Chapter 8 More on Strings and Special Methods
String Manipulation Chapter 7 Attaway MATLAB 4E.
Introduction to Programming
What's wrong with Easter jokes? They crack you up
CS Software Studio Assignment 1
Introduction to javadoc
Functions continued.
Topics Basic String Operations String Slicing
Suggested self-checks: Section 7.11 #1-11
Introduction to Computer Science
Topics Basic String Operations String Slicing
Introduction to Programming
Topics Basic String Operations String Slicing
Chapter 13 Control Structures
Presentation transcript:

CS1020E Sitin 1 Discussion -- Counting Palindromes

2  given a R*C matrix with each cell consist of a single lowercase alphabet.  to count how many palindromes in the matrix, in horizontal, vertical and diagonal direction.  palindrome: a string which reads the same backward or forward  input: The first line will consist of two integers separated by single space representing R and C respectively. The next R lines consist of C characters representing the matrix  output: the number of palindromes in the matrix. Problem Description

3 assemble every substring in every row, column and diagonal, and send it to a common function to check whether it is a palindrome. If yes, add one to the total number of palindromes How to solve this problem

4 int main() { read input; solve(R,C,matrix); output; } int solve(int R, int C, string matrix[100]) { assemble substring in each row; for each substring, check palindrome; assemble substring in each column; for each substring, check palindrome; assemble substring in each diagonal; for each substring, check palindrome; } bool checkPalindrome(string str) { string reverse_str=str; std::reverse(reverse_str.begin(), reverse_str.end()); if(reverse_str==str) return true; return false; } Framework

5 for ( int i=0; i<R; i++ ) { //search-by-row iterator for ( int j=0; j<C; j++ ) { //starting column of search iterator for ( int k=1; k<=C-j; k++ ) { //length of search iterator test=matrix[i].substr( j, k ); if(checkPalindrome(test)) pal_num++;// if palindrome, pal_num++} } Details assemble substring in each row; for each substring, check palindrome: assemble substring in each column; for each substring, check palindrome: for ( int i=0; i<C; i++ ) { //search-by-column iterator for ( int j=0; j<R; j++ ) { //starting row of search iterator test=matrix[j][i];//load first character of column; check only begins with second character as single character are counted as horizontal for ( int k=j+1; k<R; k++ ) { //position of search test.push_back( matrix[k][i] ); if(checkPalindrome(test)) pal_num++;// if palindrome, pal_num++ }

6 for ( int i=0; i<R; i++ ) { //search-by-row iterator for ( int j=0; j<C; j++ ) { //starting column of search test=matrix[i][j]; for ( int k=1; k<R-i && k<C-j; k++ ) { //position of search test.push_back( matrix[i+k][j+k] ); if(checkPalindrome(test)) pal_num++;// if palindrome, pal_num++ } Details assemble substring in each diagonal; for each substring, check palindrome:

7 about algorithm: 1. cannot correctly loop for the starting/ending/center point of the text that they want to check 2. double counting. 3. cannot correctly construct the word 4. cannot correctly check whether the word is palindrome or not. 5. used low-level functions in C to implement rather than string class Common problems/errors:

8 about programming style: 1. did not write pre and post conditions 2. few comments for the tricky code snippets Common problems/errors:

9 1. checking on each row, column and diagonal instead of assembling a string and pass it to a common function to do the checking. -- can be correct, but not a good programming habit. Remarks:

END OF FILE