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
icon
Related questions
Question
100%
CSC108H Loops and Strings Lab
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 to 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 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!
Transcribed Image Text:CSC108H Loops and Strings Lab 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 to 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 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!
3. Next, work on the code itself. How many accumulators are you going to need? How are you going to
count the number of t and s characters on each line? (There's a useful string method to help with
that!)
When you're ready to test your work: you'll be in good shape because you already have test cases
you developed that can help you! Run your program with all the inputs you came up with, and see
whether it passes your test cases. If it does, great! If it doesn't, also great: you've caught a bug! It
means that you have found a defect before you or others actually start using and trusting the code.
3
TO SUBMIT: Solving Another Problem
Now, work on solving Fergusonball Ratings.
https://dmoj.ca/problem/ccc22j2
Follow the same steps from the previous problem: first try to identify similar problems that you already
know how to solve, then design a suite of test cases, and finally write the code. Write your program in a file
named ferguson.py.
4 An Extra Problem
If you're up for another challenge, how about Elder?
https://dmoj.ca/problem/coci18c4p1
Use the same steps that you used to solve the previous problems.
For this bugger, you're going to need a way to keep track of the unique wizards that the wand has obeyed.
For example, if the wand obeys wizard A, then B, then A again, that's only 2 wizards that the wand has
obeyed, not 3. I recommend using a string to hold the unique wizards that the wand has obeyed; only add
a wizard to the end of that string if that wizard isn't already in there.
Submit your ferguson.py file to MarkUs. MarkUs lets you run some minimal tests on your code, but you
should always do more testing on your own as well!
See you next week!
Transcribed Image Text:3. Next, work on the code itself. How many accumulators are you going to need? How are you going to count the number of t and s characters on each line? (There's a useful string method to help with that!) When you're ready to test your work: you'll be in good shape because you already have test cases you developed that can help you! Run your program with all the inputs you came up with, and see whether it passes your test cases. If it does, great! If it doesn't, also great: you've caught a bug! It means that you have found a defect before you or others actually start using and trusting the code. 3 TO SUBMIT: Solving Another Problem Now, work on solving Fergusonball Ratings. https://dmoj.ca/problem/ccc22j2 Follow the same steps from the previous problem: first try to identify similar problems that you already know how to solve, then design a suite of test cases, and finally write the code. Write your program in a file named ferguson.py. 4 An Extra Problem If you're up for another challenge, how about Elder? https://dmoj.ca/problem/coci18c4p1 Use the same steps that you used to solve the previous problems. For this bugger, you're going to need a way to keep track of the unique wizards that the wand has obeyed. For example, if the wand obeys wizard A, then B, then A again, that's only 2 wizards that the wand has obeyed, not 3. I recommend using a string to hold the unique wizards that the wand has obeyed; only add a wizard to the end of that string if that wizard isn't already in there. Submit your ferguson.py file to MarkUs. MarkUs lets you run some minimal tests on your code, but you should always do more testing on your own as well! See you next week!
Expert Solution
steps

Step by step

Solved in 4 steps with 9 images

Blurred answer
Knowledge Booster
File Input and Output Operations
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.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
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)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education