![]() |
|
[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
|
RE: [EASY] Programming Challenge - 3SidedSquare - 09-15-2013 Python solution Spoiler:Code: import os
print("Enter file name")
while(True):
try:
name = input()
f = open(name, 'r')
break
except:
print("\nFile could not be found. Please try again.")
chars = 0
lines = 0
for line in f:
lines += 1
for char in line:
chars += 1
print("Chars: " + str(chars))
print("Lines: " + str(lines))RE: [EASY] Programming Challenge - meow - 07-25-2016 We should make this thread active again. C - Spoiler:Code: #include <stdio.h>
#include <sys/stat.h>
int main(int argc, char *argv[]) {
if(argc!=2) {
printf("Enter a filename as the first parameter.\n");
return 1;
} else {
struct stat st;
int r = stat(argv[1],&st);
if(r != 0) {
printf("%s does not exist.\n",argv[1]);
} else {
FILE *fp = fopen(argv[1],"r");
int lines = 0;
int ch;
while(ch != EOF) {
ch = fgetc(fp);
if(ch == '\n')
lines++;
}
printf("The number of lines in %s is %d.\n",argv[1],lines);
return 0;
}
}
}PHP - Spoiler:Code: <?php
if($argc != 2) {
print "Enter a filename as your second parameter.\n"; exit;
} else {
if(!file_exists($argv[1])) {
print "File " . $argv[1] . " does not exist.\n"; exit;
} else {
$lines = 0;
$fh = fopen($argv[1],"r");
while(!feof($fh)) {
$line = fgets($fh);
$lines++;
}
print "The number of lines in " . $argv[1] . " is " . $lines . "\n"; exit;
}
}
?>RE: [EASY] Programming Challenge - Wildfire - 07-26-2016 Size doesn't matter, it's how you use it. PHP: PHP Code: <?($a=file($argv[1]))?print("l:".count($a)." c:".strlen(implode("",$a))):print("err");
@meow you forgot to count chars by the way RE: [EASY] Programming Challenge - meow - 07-27-2016 (07-26-2016, 02:18 AM)Axari Wrote: Size doesn't matter, it's how you use it. Oh, whoops. All I remember reading was counting the lines. I'd fix it if I weren't lazy. RE: [EASY] Programming Challenge - Ex094 - 07-27-2016 Here's my solution in Java: Spoiler:Code: public static void main(String[] args) {
int lines = 0; //Number of lines in the text file
int chars = 0; //Number of characters in the text file
List<String> text; //Stores all the items from the text file
Scanner textFile = new Scanner(System.in);//Scanner Object
System.out.println("Path to To Text File: ");
String path = textFile.nextLine();//Read User input
File file = new File(path);
if(file.isFile()) { //Check if file exists
try(BufferedReader read = new BufferedReader(new FileReader(path))) {
text = read.lines().collect(Collectors.toList());//Convert Stream to List
chars = text.stream().map((items) -> (items.replaceAll("\\s+", "")).length()).reduce(chars, Integer::sum);//Reduce to Chars and count
//Print Results
System.out.println("Total Lines: " + text.size());
System.out.println("Total Characters: " + chars);
} catch (Exception e) {
System.out.println(e);
}
} else {
System.out.println("File not found!");
}
} |