Sinisterly
[EASY] Programming Challenge - Printable Version

+- Sinisterly (https://sinister.ly)
+-- Forum: Coding (https://sinister.ly/Forum-Coding)
+--- Forum: Coding (https://sinister.ly/Forum-Coding--71)
+--- Thread: [EASY] Programming Challenge (/Thread-EASY-Programming-Challenge)

Pages: 1 2


[EASY] Programming Challenge - Dong - 09-14-2013

Hello members of Sinisterly. I've got an easy programming challenge for those of you who may be now learning to program. If experienced programmers wish to partake, they can as well.


Prerequisites
  • Basic programming knowledge (variables, control structures)
  • Understanding of basic file I/O in your desired programming language



Problem
Write a program that will accept a file path through user input (e.g. filename.txt), open that file and count the total number of lines and characters in that file. These values should be returned to the user. If the file does not exist, the user should be informed of this and given another opportunity to provide a valid file path.



Solution
If you can't figure out how to do this, you can refer to my solution. It may be a tad bit overcomplicated for this particular problem, but you'll get the idea.
Spoiler:

C++
Spoiler:
Code:
/* * AUTHOR: Aristotle * LANGUAGE: C++ * DESCRIPTION: This program will open a file, count the number of lines and characters, * and then return the result to the user. */ #include <iostream> #include <fstream> #include <string> int main(){ std::ifstream inputFile(NULL); std::string fileName,line; int lineCount=0,charCount=0; while (!inputFile){ std::cout << "Enter file name in the format 'filename.extension' (e.g. hello.txt).\n"; std::cout << "File name: "; std::cin >> fileName; inputFile.open(fileName.c_str()); if (!inputFile){ std::cout << "File does not exist.\n"; inputFile.close(); } } std::cout << "File located. \nReading number of lines and characters...\n"; while(std::getline(inputFile, line)){ lineCount++; charCount += line.length(); } std::cout << "Total line count is " << lineCount << ".\n"; std::cout << "Total character count is " << charCount << ".\n"; return 0; }
http://pastebin.com/yiwvZjyd





Please provide the code you used to solve the problem via PM or between spoiler tags in a reply to this thread. If you solved it in another language, I can add your solution to the list.


RE: [EASY] Programming Challenge - Cyanide and Cynicism - 09-14-2013

Code:
wc -m /file/directory/here && wc -l /file/directory/here



RE: [EASY] Programming Challenge - Oni - 09-14-2013

(09-14-2013, 07:11 PM)Cyanide and Cynicism Wrote:
Code:
wc -m /file/directory/here && wc -l /file/directory/here

Heh, in bash.


RE: [EASY] Programming Challenge - Cyanide and Cynicism - 09-14-2013

(09-14-2013, 07:15 PM)Oni Wrote: Heh, in bash.

Master Foo once said to a visiting programmer: “There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

The programmer, who was very proud of his mastery of C, said: “How can this be? C is the language in which the very kernel of Unix is implemented!”

Master Foo replied: “That is so. Nevertheless, there is more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

The programmer grew distressed. “But through the C language we experience the enlightenment of the Patriarch Ritchie! We become as one with the operating system and the machine, reaping matchless performance!”

Master Foo replied: “All that you say is true. But there is still more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

The programmer scoffed at Master Foo and rose to depart. But Master Foo nodded to his student Nubi, who wrote a line of shell script on a nearby whiteboard, and said: “Master programmer, consider this pipeline. Implemented in pure C, would it not span ten thousand lines?”

The programmer muttered through his beard, contemplating what Nubi had written. Finally he agreed that it was so.

“And how many hours would you require to implement and debug that C program?” asked Nubi.

“Many,” admitted the visiting programmer. “But only a fool would spend the time to do that when so many more worthy tasks await him.”

“And who better understands the Unix-nature?” Master Foo asked. “Is it he who writes the ten thousand lines, or he who, perceiving the emptiness of the task, gains merit by not coding?”

Upon hearing this, the programmer was enlightened.


RE: [EASY] Programming Challenge - Dong - 09-14-2013

(09-14-2013, 07:11 PM)Cyanide and Cynicism Wrote:
Code:
wc -m /file/directory/here && wc -l /file/directory/here

(09-14-2013, 07:16 PM)Cyanide and Cynicism Wrote: “And who better understands the Unix-nature?” Master Foo asked. “Is it he who writes the ten thousand lines, or he who, perceiving the emptiness of the task, gains merit by not coding?”

Upon hearing this, the programmer was enlightened.

Notamused Sad

By the way, I'm not sure whether this is true or not: If a new line has not been established at the end of file, will your command, "wc -l ..." count the very last line? Haven't tested it.


RE: [EASY] Programming Challenge - Oni - 09-14-2013

(09-14-2013, 07:16 PM)Cyanide and Cynicism Wrote: Master Foo once said to a visiting programmer: “There is more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

The programmer, who was very proud of his mastery of C, said: “How can this be? C is the language in which the very kernel of Unix is implemented!”

Master Foo replied: “That is so. Nevertheless, there is more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

The programmer grew distressed. “But through the C language we experience the enlightenment of the Patriarch Ritchie! We become as one with the operating system and the machine, reaping matchless performance!”

Master Foo replied: “All that you say is true. But there is still more Unix-nature in one line of shell script than there is in ten thousand lines of C.”

The programmer scoffed at Master Foo and rose to depart. But Master Foo nodded to his student Nubi, who wrote a line of shell script on a nearby whiteboard, and said: “Master programmer, consider this pipeline. Implemented in pure C, would it not span ten thousand lines?”

The programmer muttered through his beard, contemplating what Nubi had written. Finally he agreed that it was so.

“And how many hours would you require to implement and debug that C program?” asked Nubi.

“Many,” admitted the visiting programmer. “But only a fool would spend the time to do that when so many more worthy tasks await him.”

“And who better understands the Unix-nature?” Master Foo asked. “Is it he who writes the ten thousand lines, or he who, perceiving the emptiness of the task, gains merit by not coding?”

Upon hearing this, the programmer was enlightened.

You should really stop quoting those blogs. Tongue

(09-14-2013, 07:21 PM)Aristotle Wrote: Notamused Sad

By the way, I'm not sure whether this is true or not: If a new line has not been established at the end of file, will your command, "wc -l ..." count the very last line? Haven't tested it.

Relatively sure it would.


RE: [EASY] Programming Challenge - Cyanide and Cynicism - 09-14-2013

(09-14-2013, 07:21 PM)Aristotle Wrote: By the way, I'm not sure whether this is true or not: If a new line has not been established at the end of file, will your command, "wc -l ..." count the very last line? Haven't tested it.

I believe so.

(09-14-2013, 07:21 PM)Oni Wrote: You should really stop quoting those blogs. Tongue

http://catb.org/esr/writings/unix-koans/


RE: [EASY] Programming Challenge - Oni - 09-14-2013

(09-14-2013, 07:23 PM)Cyanide and Cynicism Wrote: I believe so.


http://catb.org/esr/writings/unix-koans/

I've already seen that one. The Codeless Code is good too. Tongue


RE: [EASY] Programming Challenge - w00t - 09-14-2013

His answer is actually more correct than yours, as you count newline characters in your character count.


RE: [EASY] Programming Challenge - Dong - 09-14-2013

(09-14-2013, 07:26 PM)w00t Wrote: His answer is actually more correct than yours, as you count newline characters in your character count.

My program does not include the '\n' characters in the character count. Rather it includes the whitespaces in each line.

Example 1:
Quote:TEXT FILE CONTAINS:
Code:
LOL LOL

My program returns a character count of: 7

Example 2:
Quote:TEXT FILE CONTAINS:
Code:
LOL LOL LOL

My program returns a character count of: 10


I wrote this up quickly and at the time I didn't consider the counting of whitespaces, unfortunately. I only tested without whitespaces and that was a mistake on my part. However, technically it does count all of the characters with the exception of '\n'.