1 For-Loops and Strings It's common to write loops that process characters of strings. Remember that you can use a regular for-loop if all you need are the characters, and a range for-loop if you need the indices. 1. Assign the string "superconductivity" to a variable s. 2. Write a loop that prints each character in s on a separate line. 3. Write a loop that prints each character in s on the same line, with a comma and a space after each character: s, u, p, e, r, c, etc. Notice that there is a comma after the last character. We won't worry about this. To do this, build a new string that includes the appropriate characters, then print that new string. 2 Solving a Problem Now let's solve a problem from the Chapter 3 exercises. The problem is called English or French: https://dmoj.ca/problem/ccc11s1 Let's do some analysis on this problem to give us a plan that we can follow. 3 1. Notice that the English/French data is spread over N lines. Which of the three problems in Chapter Three Cups, Occupied Spaces, or Data Plan have a similar input format? Answering this question is important as we may be able to use a similar pattern from before to solve this new problem (much as we used the "count vowels" pattern to solve the "exclude spaces" problem in class). It may also help you zone in on the most appropriate type of loop to use to read the data. 2. Let's now think about some input/output pairs for this problem. We want to be sure that we fully understand what the problem is asking, and also have some test cases ready to test our code once we write it. On the one hand, you can't test every possible input value and outcome. There are way too many! On the other hand, doing one or two tests isn't enough to be sure that your program works for all possible input values. You have to strike a balance and limit your testing to a relatively small set of values, so that you can do your testing in a reasonable amount of time. Look at the bounds on valid inputs from the problem description. Here, we know that N is at least 1 and at most 10000. It would be a good idea o test an input with one line (a boundary case), two lines, and maybe even four or five lines. If it works for these cases, it probably works for larger numbers of lines, as well. And what about each line? Each line has between 1 and 100 characters. So we could test a line with 1 character, a line with 2 characters, and maybe lines with 10 or 20 characters. Importantly, do not test with an empty line: that is not a valid input for this problem! We should also vary the number of s and t characters on each line and in total. Sometimes we want English to win and other times we want French to win. And a tie don't forget about a tie. And lowercase and uppercase versions of the t and s characters. Yeah, there's a lot to capture here! When we pick a representative test case, we do so believing something very strong: that if the code works for that one case, we can be confident it will work for all other cases in the category it represents. Obviously, we must choose carefully, or this may not be true! Write a test suite of inputs and outputs that you could use to test your code once you write it. For each case, provide the input, expected output, and a description of the purpose or significance of the test case (that is, what situation the case represents). Do this before you write the code!
1 For-Loops and Strings It's common to write loops that process characters of strings. Remember that you can use a regular for-loop if all you need are the characters, and a range for-loop if you need the indices. 1. Assign the string "superconductivity" to a variable s. 2. Write a loop that prints each character in s on a separate line. 3. Write a loop that prints each character in s on the same line, with a comma and a space after each character: s, u, p, e, r, c, etc. Notice that there is a comma after the last character. We won't worry about this. To do this, build a new string that includes the appropriate characters, then print that new string. 2 Solving a Problem Now let's solve a problem from the Chapter 3 exercises. The problem is called English or French: https://dmoj.ca/problem/ccc11s1 Let's do some analysis on this problem to give us a plan that we can follow. 3 1. Notice that the English/French data is spread over N lines. Which of the three problems in Chapter Three Cups, Occupied Spaces, or Data Plan have a similar input format? Answering this question is important as we may be able to use a similar pattern from before to solve this new problem (much as we used the "count vowels" pattern to solve the "exclude spaces" problem in class). It may also help you zone in on the most appropriate type of loop to use to read the data. 2. Let's now think about some input/output pairs for this problem. We want to be sure that we fully understand what the problem is asking, and also have some test cases ready to test our code once we write it. On the one hand, you can't test every possible input value and outcome. There are way too many! On the other hand, doing one or two tests isn't enough to be sure that your program works for all possible input values. You have to strike a balance and limit your testing to a relatively small set of values, so that you can do your testing in a reasonable amount of time. Look at the bounds on valid inputs from the problem description. Here, we know that N is at least 1 and at most 10000. It would be a good idea o test an input with one line (a boundary case), two lines, and maybe even four or five lines. If it works for these cases, it probably works for larger numbers of lines, as well. And what about each line? Each line has between 1 and 100 characters. So we could test a line with 1 character, a line with 2 characters, and maybe lines with 10 or 20 characters. Importantly, do not test with an empty line: that is not a valid input for this problem! We should also vary the number of s and t characters on each line and in total. Sometimes we want English to win and other times we want French to win. And a tie don't forget about a tie. And lowercase and uppercase versions of the t and s characters. Yeah, there's a lot to capture here! When we pick a representative test case, we do so believing something very strong: that if the code works for that one case, we can be confident it will work for all other cases in the category it represents. Obviously, we must choose carefully, or this may not be true! Write a test suite of inputs and outputs that you could use to test your code once you write it. For each case, provide the input, expected output, and a description of the purpose or significance of the test case (that is, what situation the case represents). Do this before you write the code!
Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
Related questions
Question
100%
Expert Solution
This question has been solved!
Explore an expertly crafted, step-by-step solution for a thorough understanding of key concepts.
Step by step
Solved in 4 steps with 9 images
Knowledge Booster
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.Recommended textbooks for you
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education