index | submit | rank | book

Computer Science by Example

Frequently Asked Questions

Here are some frequently asked questions (FAQ) about Computer Science by Example (CScx) and its exercises:


(solving the exercises)

How to read input until the end-of-file? (EOF)

How to read input until the end-of-file (EOF) is explained in detail in the input and output section of the 4. Programming chapter.

Whenever a program is reading from a file, either through accessing directly or through redirection using <in.txt on the terminal, we say a program has reached the EOF when there is no more input to be read. At this point we literally reach the end-of-file. When typing input from the keyboard, we can signal EOF to the program by holding “Ctrl” and pressing “D” at the beginning of a new line, or “Ctrl-D” for short.

EOF can be detected in a number of ways depending on the programming language you are using. Here is how to do detect it in Python, C and Haskell:

Remember that on the terminal, you can simulate the end-of-file (EOF) by holding “Ctrl” and pressing “D”.

If you want to exercise reading until EOF, try the repeat and hi exercises.

What are edge cases?

Informally, edge cases or corner cases are those in which “unusual-but-valid” values are given to an algorithm, function or program. They are an important thing to consider in software engineering.

In the case of the triple and inc exercises for example, inputs with leading zeroes are given (010, 011, 012, 013) and your program should produce the same results as if they didn’t have leading zeroes. (For C programmers, this means using %d instead of %i. Python and Haskell programmers shouldn’t usually worry about these specific edge cases as converting to an numeric type automatically gets rid of leading zeroes.)

Other examples of corner cases are exponentiation when the exponent is zero or one and testing with the maximum required length or the minimum required length. These apply to programmers of all languages. Edge cases often involve “zero” or “one”.

When you get a partial score, try to think of edge cases or corner cases for your submitted programs. Does it behave correctly for these cases?

Where can I find solutions to the exercises?

The book has solutions to some of the exercises, specially for exercises of chapter 3. Programming Basics and earlier exercises of chapter 4. Programming. Most solutions are first given in pseudo-code or flowchart first then in actual programming languages (Python, C and Haskell). So I recommend you:

  1. first try the exercise without looking at the solutions, perhaps with knowledge of the chapter up to that exercise;
  2. if you can’t solve it, then try the exercise while looking at pseudo-code or flowchart;
  3. if you still can’t solve it, finally look at the solution given in the book. Study it carefully to understand what you missed. If you end up doing this, I recommend you retry the exercise on the next couple days and try to complete it “from memory”.

(c.f. “Exercise solutions are in the book?! How can I prevent my students from “cheating” then?”)

Can you help me solve one of the exercises?

i.e.: Help! I can’t seem to solve one of the exercises.

If you are stuck on the very earlier exercises, I recommend you read the Computer Science by Example book. First setup your environment then learn the programming basics.

If you are stuck on later exercises, try looking and the hints and rereading the problem description carefully. Think of any edge cases that may cause your program to fail.

For later exercises, it may also help to pick another exercise to work on and come back later. Sometimes you will see the problem from a different angle when you decide to come back to it.

If you are still stuck after trying the above tricks, mail me (rudy@cscx.org). Be sure to mention your username, the id of the exercise you are trying, the timestamp of your submission and a brief description of your solution. Here is a good email subject when asking a question:

I cannot promise I’ll reply soon but I’ll try to get to it at some point.

Can I use CScx to train for job interview coding questions?

Yes.

Some CScx exercises are in the same style as some job interview coding questions.

Exercises here also may be useful for you to “warm up” in the week to an upcoming interview – even if you have already solved them in the past.

Can CScx automatically infer the language from the submitted file?

Yes. If the language is not set on the submit page, it is automatically inferred from the submitted file extension.

Files with a .py, .c and .hs extensions will automatically inferred to be Python, C and Haskell files respectively.

Can CScx automatically infer the exercise/problem id from the submitted file?

Yes. If the exercise/problem id is not set on the submit page, it is automatically inferred from the submitted file name. For example:

Can you implement remembering my username and password?

I recommend you use the “autofill login” feature of your web browser. This feature is supported by at least Firefox, Chrome/Chromium, Edge and Safari.

I probably will not be implementing saving logins in cookies, as I want to keep a simple “stateless”/“cookieless” implementation for the website.

Can you implement remembering my last submitted exercise and language?

I probably won’t implement that for the time being. However, the language and exercise are inferred from the filename, so you can use that instead.

(replies to constructive criticism)

These exercises are frustrating…

The exercises were designed not to be frustrating. Starting with the first exercise there are hints on where to start and what to do when you do not get a perfect score. Computer programming takes a little bit of patience and attention to detail. So if you do not get a full score at first, reread the exercise description and the hints attentively, think on what may have caused any issues and try again. Later on, if you are stuck in some exercise, there’s no shame in picking another exercise and coming back later. This may help you see the problem from a different angle when you decide to go back to it.

While every care has been taken to make so that exercise descriptions are unambiguous and accurate, they are not perfect: please email me (rudy@cscx.org) if you find something you don’t quite understand. I’ll be happy to amend exercise descriptions when they are unclear, ambiguous or inaccurate.

These exercises are boring…

If you are feeling bored by earlier exercises, try some later ones. If you’re up for a challenge, try triangle1, fibonacci, triangle, digit-palindrome, gcd, cash, function and rot13. If you are able to get these exercises right away, good job! Try to carry on solving exercises of later chapters. If you are not able to get these exercises at first, you can go back to earlier exercises while keeping in mind what you are working towards. You’ll appreciate more what earlier and easier exercises are trying to teach you, this will hopefully make them less boring.

If you are an experienced programmer and want to try the exercises to improve or refresh your CS knowledge, this is what I recommend:

  1. from chapters 3 and 4, complete just hello and add just to get used to how the scoring system expects you to write your programs; then
  2. jump straight into digit-sum or gcd from chapters 5 and 6 and carry on from there;
  3. if you get stuck, you can go back to chapters 3 and 4 and do some simpler exercises.

(comparison with other material)

Are there any sites similar to CScx?

i.e.: What are other sites similar to CScx?

Yes. CScx is not the first website with programming exercises and automated judging/grading. There are websites of this kind dating back to 1995.

See also “How does CScx compare to other similar websites?

How does CScx compare to other similar websites?

CScx is not better nor worse than others, but it may be more suited to your style of learning or may be easier to use in your teaching.

Most other websites with online programming exercises are focused on competitive programming problems. Here on CScx the focus is rather on introducing programming step by step with curated exercises of increasing difficulty. Later exercises of CScx are separated by computer science topics.

Here are some main “features” of CScx in contrast with most other websites of its kind:

Some of the websites listed above do “check” some of the above features.

(information for instructors, lecturers, professors or teachers)

Can I use CScx as part of my teaching module?

Yes, of course. The website is freely available online so you and your students are welcome to try it. See the next Q&A for how to use CScx as part of your teaching module.

If you use this in your teaching module and have feedback, let me know (rudy@cscx.org).

How can I use CScx as part of my teaching module?

CScx exercises can be used in teaching of programming in a number of courses. Here are some examples where CScx can be used:

Care has been taken so that the exercise sequence works for multiple paradigms, be it in high-level object-oriented programming (e.g.: Python, Java), low-level programming (e.g.: C) or functional programming (e.g.: Haskell).

You can use CScx as part of your programming teaching module either as additional teaching material which you point your students to or as the main teaching material on practical sessions with your students. Here are some suggestions for each of the two options:

To host your own set of exercises, you can use the Udge software to create and host your own exercises. This will give you the advantage of being able to look at your students solutions (to provide them help or etc).

Either way, I recommend instructors to complete the following short sequence of exercises: first hello, then triple1 and finally add. This should take around 15 minutes and will help you get a feel of how students are expected to solve and submit the exercises.

Can I share CScx with my colleagues?

Yes, of course. :-)

Feel free to point your colleagues to cscx.org

Exercise solutions are in the book?! How can I prevent my students from “cheating” then?

(cf. “Where can I find solutions to the exercises?”)

The book has solutions to some of the exercises but not to all of them.

For the exercises that the book does provide solutions, there is always a “twin/pair” exercise without a solution. For example:

  1. Both age1 and swap1 exercise the same concept of reading and writing numbers and strings. While age1 has a solution in the book, there is no solution to swap1. Even when adapting the age1 solution to solve swap1, students will learn about how to read and write numbers and strings.

  2. Both triple1 and inc1 exercise the same concept of declaring functions and performing arithmetic operations. While triple1 has a solution in the book, there is no solution to inc1. Even when adapting the triple1 solution to solve inc1, students will learn how to perform arithmetic operations and how to declare functions.

  3. Both add1 and mult1 exercise the same concept. The first has a solution in the book but the second has not.

  4. oddeven1 and order1

  5. good1 and signum1

  6. factorial1, power1 and fibonacci1

  7. … et cetera.

If you were ever wondering why some exercises in the index were paired, this is why.

How can I make exercises of the same type to my students?

i.e.: Can I prepare assignments to my students like the ones you have?

The actual backend of Computer Science by Example is open source. It’s called Udge and freely available for anyone to use. The documentation has details of how to prepare programming problems to be used with it, specifically on the “Creating a Problem” section. There’s details of how it works and how to install it there too. It is still experimental and may get some work to set up but there shouldn’t be any major problems.

In short, using Udge you can host your own set of exercises and assignments to your students. You’ll even be able to see the code they submitted.

How do you grade solutions automatically?

In summary the automated marking works as follows.

Programs are usually expected to read from standard input (keyboard) and print to standard output (screen) and to accept a sequence of test cases. We have an input file “in.txt” and a reference output file “out.txt”. Given that, this is how submitted programs are evaluated:

$ cc submitted-program.c -o exe
$ ./exe <in.txt >program-output.txt
$ diff -q program-output.txt out.txt && echo "OK" || echo "Failed test case"

The program is compiled; it is run with redirected input and output (keyboard and screen are replaced by input and output files); then we run a simple diff. Of course this will give you a simple “0 or 1” score. To actually have a grading 0/6, 1/6, 2/6, 3/6, 4/6, 5/6 or 6/6, the website uses multiple input and output pairs of files per exercise/problem then test the program until it fails arriving at the final score.

CScx includes “easier” test cases on the earlier files then “harder” test cases later and “edge” cases last. For example, for the add exercise, the first two test files include the examples given in the description, the next file has very small numbers, then bigger numbers, then negative numbers, etc, …, lastly we have test cases with leading zeroes (which may get C programmers who insist on using %i instead of %d, hint: %i treats leading zeroes as octals!). I have put quite some thought on how to structure the test cases for the exercises so that solutions that aren’t 100% correct are able to get some points.

Can I have the input and output test cases for the problems in the site?

Not at the moment. (I may potentially release the exercise test sets for educational use in the future. This way, teachers, lecturers and professors teaching programming and CS modules will be able to run their own exercise sets. But at the moment I’d like to keep them private.)

However, if you would like to create your own exercises and assignments, you can take a look and use the input and output test cases of the example exercises that come with Udge, the software behind this site.

(contributing)

How can I contribute to CScx?

Use the site and share it with your friends and colleagues. Let me know if you find any issues, I’ll be glad to fix them.

When the book is eventually out, buy it. Mail me showing interest in buying the book if you do: this will help me find an editor and I’ll notify you when the book is eventually published.

Feel free to use, fork or contribute to Udge too.

Thanks!

Thanks to codingarmadillo, fcmb57, jjlonz, ksteding, lupus, Marcus Lütolf (luemar), mivael, phil and veenhdek for helping to find issues in the website.

Do you have plans to translate CScx?

Yes, but not for now or anytime soon. I am currently working on expanding the topics covered, making CScx available in other languages is a lesser priority right now.

However, you can access a makeshift translation right now using Google Translate or a similar tool. Click on one of the links below:

The results are quite decent for a machine generated translation. If you do not understand English very well, you could use this as an opportunity to improve your English by switching back and forth between your language and English.

Another alternative is to use a browser extension to translate pages. There are extensions of this kind available for Firefox, for Chromium/Google Chrome and for Microsoft Edge.

Can I help you translate the website, exercises and book?

Thank you for the offer. Unfortunately for now, no, you can’t.

I eventually plan to release a book with the exercises and tutorial section of the CScx site so any translations will probably have to happen through my future editor for copyright reasons. Until my negotiations, terms and conditions with an eventual editor are settled, I am not able to accept any help with regards to translations. You can use the makeshift translation through google translate in the meanwhile.

(The above is the main reason. Another not-so-minor reason is that I currently do not have resources to maintain a translation. I am still in the middle of writing and refining the description of the exercises so any translation would become outdated quite quickly.)

You can still drop me a line of mail (rudy@cscx.org) and I’ll let you know if that changes.

Can you add support to my programming language of choice?

Can you add support to the XYZ programming language?

Perhaps. If there is enough people interested in using it I could. Mail me at rudy@cscx.org.

Can I help you add support to my programming language of choice?

Yes. The engine behind the site, Udge, is Free and Open Source Software. You can submit a pull request on GitHub with the compilation scripts of your language, example solutions and test files. Or simply mail me a patch (rudy@cscx.org). That is in itself no guarantee that I will add support here on the CScx site, but it is a good starting point.

Do you accept user submitted exercises?

Unfortunately, currently no. (This may change in the future, but not in the short term. My priority right now is expanding the exercise set with more topics and improving and completing the Computer Science by Example book to a publishable state.)

… but fortunately, you can host them yourself. See:

The engine behind this site, Udge, is open source. So, if you are a lecturer looking for hosting exercises or assignments like the ones available here, you can use Udge for that. By creating a local server for exercises/assignments at your institution, you’ll even be able to see the solutions submitted by your students. Check out Udge’s documentation for how to do this – there are some example exercises there to help you get started.

(I’m the author of both CScx and Udge, so if you have trouble using Udge, feel free to mail me (rudy@cscx.org) or create an issue ticket on GitHub.

Do you accept ideas for exercises?

Currently no.

I actually already have a quite extensive backlog of exercises I plan to add to the site, to list a few: exercises on basic cryptography, exercises on data structures, exercises on graph theory, exercises on computational geometry, etc. So, before I am able to accept requests or ideas I’d like to complete exercises on the above list. This will probably take a while…

Additionally, one of my objectives with this site is to release it as a book. For copyright reasons, I am not accepting major contributions to the exercise set here (at least for the time being).

(about the book)

Where can I buy the “Computer Science by Example” book?

The first 4 chapters are freely available online, you can access them by clicking on the “book” link in the top or bottom of this page.

The book is yet to be released in print or e-book format. If you are interested in buying it when it’s out, mail me (rudy@cscx.org) and I’ll let you know when it is.

(about the site)

Who created CScx?

Rudy Matela created and maintains the Computer Science by Example (CScx) website and online book.

Rudy has a PhD in Computer Science by the University of York, a Master’s degree in Computer Science by Universidade Estadual do Ceará (UECE) and a Bachelor’s degree in Computer Science by Universidade Estadual do Ceará (UECE). Rudy has past experience teaching programming and working in the software industry.

How accurate/reliable/scholarly is CScx?

(Rudy) – I try to be accurate as possible in the website, both in the exercises and in the book. However, in case you find any innacuracies, typos, incorrect exercise descriptions, please email me (rudy@cscx.org). – See “Who created CScx? for information about me.

index | submit | rank | book

Copyright © 2020-2021 Rudy Matela
All rights reserved