After a summer of slinging some C# code in a Microsoft shop, Vector Bill is ready to level up and get a better, err more interesting, job. To prepare for his fall job hunting campaign, he is doing some coding exercises on HackerRank and LeetCode in hopes of acing the impending technical programming interviews.
Unfortunately, while he has a pretty good handle on data structures (ie. just use a vector for all the things), he is struggling with reading input from standard input.
For this challenge, help Vector Bill out by writing a program that reads input in the following format:
First you will get a format specifier in the form: N
, L
, or S
.
Next, you will get a stream of integers.
If the format is N
, you will get an integer n
which specifies the
number of integers followed by the n
integers.
If the format is L
, you will get a single line with all the
integers.
If the format is S
, you will get an integer s
which specifies the
sentinel to read until. That is you should read all the integers
until you reach this sentinel.
All integers will be separated by whitespace.
Once you read in the integers, you should compute the sum of all the integers and then display the total.
An example of the expected input and output is described below.
Input will be provided via standard input in the form described above. Here is an example input:
N 5 1 2 3 4 5 N 5 1 2 3 4 5 L 1 2 3 4 5 S -1 1 2 3 4 5 -1
You are to process each format block until you reach the end of the input stream.
Output must be printed to standard output in the following format:
The sum of $INTEGERS is $RESULT
Where $INTEGERS
is the list of integers provided in the input and
$RESULT
is the sum of all those integers.
Here is the output given the example input above:
The sum of 1 2 3 4 5 is 15 The sum of 1 2 3 4 5 is 15 The sum of 1 2 3 4 5 is 15 The sum of 1 2 3 4 5 is 15
If you are given an invalid format, you should display the following message:
Unknown format: $FORMAT
Where $FORMAT
is the invalid format read from the input stream.
If the list of integers is empty, then you should not display anything.
To submit your work, follow the same procedure you used for Reading 00:
$ cd path/to/cse-30872-fa17-assignments # Go to assignments repository $ git checkout master # Make sure we are on master $ git pull --rebase # Pull any changes from GitLab $ git checkout -b challenge00 # Create and checkout challenge00 branch $ $EDITOR challenge00/program.cpp # Edit your code $ git add challenge00/program.cpp # Stage your changes $ git commit -m "challenge00: done" # Commit your changes $ git push -u origin challenge00 # Send changes to GitLab
Note, dredd supports C, C++, Python, Java, Perl, and Ruby.
You may use any of those languages, just be sure to name your source file
program.$EXTENSION
where $EXTENSION
is the appropriate file extension
for your chosen language (e.g. py
for Python).
If you wish to try out another language, please let the instructor know and support can be added to dredd.
To check your code, you can use the .scripts/submit.py
script or curl:
$ .scripts/submit.py Submitting challenge00 assignment ... Submitting challenge00 code ... Result Success Score 6.00 $ curl -F source=@challenge00/program.cpp https://dredd.h4x0r.space/code/cse-30872-fa17/challenge00 {"score": 6, "result": "Success"}
Note, dredd contains additional test cases beyond that which is show in the examples above. This means that even if you pass on the input above, you are not guaranteed to pass the dredd test if you do not handle additional situations.
This means you should consider what the possible edge cases are and how to handle them. Feel free to create your own input and output test files and use them to verify your program:
$ ./program < input.txt | diff -u - output.txt
Once you have commited your work and pushed it to GitLab, member to create a merge request. Refer to the Reading 00 TA List to determine your corresponding TA for the merge request.